diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-11-26 15:45:17 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2006-11-26 15:45:17 +0000 |
commit | dd16e0325070225462e98088574fd17defad0ae0 (patch) | |
tree | af6c1681e91a3e5c13bf3dce8aa798c07462468a /libbb | |
parent | 48a92503e79c2345601821c4599ee25d1db35d66 (diff) | |
download | busybox-w32-dd16e0325070225462e98088574fd17defad0ae0.tar.gz busybox-w32-dd16e0325070225462e98088574fd17defad0ae0.tar.bz2 busybox-w32-dd16e0325070225462e98088574fd17defad0ae0.zip |
small fixes:
fix xstrdup to not grossly overallocate memory
use xopen instean of xopen3 in several places
etc.
git-svn-id: svn://busybox.net/trunk/busybox@16673 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/xfuncs.c | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 773e718b8..ade639516 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -57,7 +57,7 @@ char * xstrdup(const char *s) | |||
57 | if (s == NULL) | 57 | if (s == NULL) |
58 | return NULL; | 58 | return NULL; |
59 | 59 | ||
60 | t = strdup (s); | 60 | t = strdup(s); |
61 | 61 | ||
62 | if (t == NULL) | 62 | if (t == NULL) |
63 | bb_error_msg_and_die(bb_msg_memory_exhausted); | 63 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
@@ -69,23 +69,33 @@ char * xstrdup(const char *s) | |||
69 | // the (possibly truncated to length n) string into it. | 69 | // the (possibly truncated to length n) string into it. |
70 | char * xstrndup(const char *s, int n) | 70 | char * xstrndup(const char *s, int n) |
71 | { | 71 | { |
72 | int m; | ||
72 | char *t; | 73 | char *t; |
73 | 74 | ||
74 | if (ENABLE_DEBUG && s == NULL) | 75 | if (ENABLE_DEBUG && s == NULL) |
75 | bb_error_msg_and_die("xstrndup bug"); | 76 | bb_error_msg_and_die("xstrndup bug"); |
76 | 77 | ||
77 | /* TODO: think about xstrndup("abc", 10000)!!! */ | 78 | /* We can just xmalloc(n+1) and strncpy into it, */ |
78 | t = xmalloc(++n); | 79 | /* but think about xstrndup("abc", 10000) wastage! */ |
80 | m = n; | ||
81 | t = (char*) s; | ||
82 | while (m) { | ||
83 | if (!*t) break; | ||
84 | m--; t++; | ||
85 | } | ||
86 | n = n - m; | ||
87 | t = xmalloc(n + 1); | ||
88 | t[n] = '\0'; | ||
79 | 89 | ||
80 | return safe_strncpy(t,s,n); | 90 | return memcpy(t,s,n); |
81 | } | 91 | } |
82 | 92 | ||
83 | // Die if we can't open a file and return a FILE * to it. | 93 | // Die if we can't open a file and return a FILE * to it. |
84 | // Notice we haven't got xfread(), This is for use with fscanf() and friends. | 94 | // Notice we haven't got xfread(), This is for use with fscanf() and friends. |
85 | FILE *xfopen(const char *path, const char *mode) | 95 | FILE *xfopen(const char *path, const char *mode) |
86 | { | 96 | { |
87 | FILE *fp; | 97 | FILE *fp = fopen(path, mode); |
88 | if ((fp = fopen(path, mode)) == NULL) | 98 | if (fp == NULL) |
89 | bb_perror_msg_and_die("%s", path); | 99 | bb_perror_msg_and_die("%s", path); |
90 | return fp; | 100 | return fp; |
91 | } | 101 | } |
@@ -93,8 +103,8 @@ FILE *xfopen(const char *path, const char *mode) | |||
93 | // Die if we can't open an existing file and return an fd. | 103 | // Die if we can't open an existing file and return an fd. |
94 | int xopen(const char *pathname, int flags) | 104 | int xopen(const char *pathname, int flags) |
95 | { | 105 | { |
96 | if (ENABLE_DEBUG && (flags & O_CREAT)) | 106 | //if (ENABLE_DEBUG && (flags & O_CREAT)) |
97 | bb_error_msg_and_die("xopen() with O_CREAT"); | 107 | // bb_error_msg_and_die("xopen() with O_CREAT"); |
98 | 108 | ||
99 | return xopen3(pathname, flags, 0666); | 109 | return xopen3(pathname, flags, 0666); |
100 | } | 110 | } |
@@ -142,7 +152,7 @@ off_t xlseek(int fd, off_t offset, int whence) | |||
142 | return off; | 152 | return off; |
143 | } | 153 | } |
144 | 154 | ||
145 | // Die with supplied error message if this FILE * has ferror set. | 155 | // Die with supplied filename if this FILE * has ferror set. |
146 | void die_if_ferror(FILE *fp, const char *fn) | 156 | void die_if_ferror(FILE *fp, const char *fn) |
147 | { | 157 | { |
148 | if (ferror(fp)) { | 158 | if (ferror(fp)) { |
@@ -214,7 +224,6 @@ void xsetenv(const char *key, const char *value) | |||
214 | bb_error_msg_and_die(bb_msg_memory_exhausted); | 224 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
215 | } | 225 | } |
216 | 226 | ||
217 | |||
218 | // Converts unsigned long long value into compact 4-char | 227 | // Converts unsigned long long value into compact 4-char |
219 | // representation. Examples: "1234", "1.2k", " 27M", "123T" | 228 | // representation. Examples: "1234", "1.2k", " 27M", "123T" |
220 | // Fifth char is always '\0' | 229 | // Fifth char is always '\0' |
@@ -257,7 +266,6 @@ void smart_ulltoa5(unsigned long long ul, char buf[5]) | |||
257 | buf[4] = '\0'; | 266 | buf[4] = '\0'; |
258 | } | 267 | } |
259 | 268 | ||
260 | |||
261 | // Convert unsigned integer to ascii, writing into supplied buffer. A | 269 | // Convert unsigned integer to ascii, writing into supplied buffer. A |
262 | // truncated result is always null terminated (unless buflen is 0), and | 270 | // truncated result is always null terminated (unless buflen is 0), and |
263 | // contains the first few digits of the result ala strncpy. | 271 | // contains the first few digits of the result ala strncpy. |