diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-04-19 19:32:08 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-04-19 19:32:08 +0000 |
commit | f3745ea489c5ef454e2ce68926c5f39f5b30f240 (patch) | |
tree | c21d461878d61bdb01bca9c8cbb01e095cd48734 | |
parent | d9c51e9fa78f7ee6adb37543f77b4f85e57cccc4 (diff) | |
download | busybox-w32-f3745ea489c5ef454e2ce68926c5f39f5b30f240.tar.gz busybox-w32-f3745ea489c5ef454e2ce68926c5f39f5b30f240.tar.bz2 busybox-w32-f3745ea489c5ef454e2ce68926c5f39f5b30f240.zip |
libbb: introduce xmalloc_xopen_read_close and use where appropriate
instead of xmalloc_open_read_close.
function old new delta
xmalloc_xopen_read_close - 34 +34
xmalloc_open_read_close 163 171 +8
passwd_main 1070 1074 +4
rexecve 254 257 +3
handle_incoming_and_exit 2657 2659 +2
parse_command 1509 1510 +1
buffer_fill_and_print 76 73 -3
evaltreenr 599 589 -10
evaltree 599 589 -10
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 5/3 up/down: 52/-23) Total: 29 bytes
-rw-r--r-- | include/libbb.h | 7 | ||||
-rw-r--r-- | libbb/read.c | 12 | ||||
-rw-r--r-- | miscutils/chat.c | 2 | ||||
-rw-r--r-- | printutils/lpd.c | 2 | ||||
-rw-r--r-- | util-linux/readprofile.c | 4 |
5 files changed, 20 insertions, 7 deletions
diff --git a/include/libbb.h b/include/libbb.h index 1c54a32d5..f9f28f983 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -556,7 +556,10 @@ extern char *reads(int fd, char *buf, size_t count); | |||
556 | extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p); | 556 | extern char *xmalloc_reads(int fd, char *pfx, size_t *maxsz_p); |
557 | extern ssize_t read_close(int fd, void *buf, size_t maxsz); | 557 | extern ssize_t read_close(int fd, void *buf, size_t maxsz); |
558 | extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz); | 558 | extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz); |
559 | /* Returns NULL if file can't be opened */ | ||
559 | extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p); | 560 | extern void *xmalloc_open_read_close(const char *filename, size_t *maxsz_p); |
561 | /* Never returns NULL */ | ||
562 | extern void *xmalloc_xopen_read_close(const char *filename, size_t *maxsz_p); | ||
560 | 563 | ||
561 | extern ssize_t safe_write(int fd, const void *buf, size_t count); | 564 | extern ssize_t safe_write(int fd, const void *buf, size_t count); |
562 | // NB: will return short write on error, not -1, | 565 | // NB: will return short write on error, not -1, |
@@ -568,9 +571,9 @@ extern void xwrite(int fd, const void *buf, size_t count); | |||
568 | extern void xprint_and_close_file(FILE *file); | 571 | extern void xprint_and_close_file(FILE *file); |
569 | /* Reads up to (and including) TERMINATING_STRING: */ | 572 | /* Reads up to (and including) TERMINATING_STRING: */ |
570 | extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string); | 573 | extern char *xmalloc_fgets_str(FILE *file, const char *terminating_string); |
571 | /* Chops off TERMINATING_STRING: from the end: */ | 574 | /* Chops off TERMINATING_STRING from the end: */ |
572 | extern char *xmalloc_fgetline_str(FILE *file, const char *terminating_string); | 575 | extern char *xmalloc_fgetline_str(FILE *file, const char *terminating_string); |
573 | /* Reads up to (and including) "\n" or NUL byte */ | 576 | /* Reads up to (and including) "\n" or NUL byte: */ |
574 | extern char *xmalloc_fgets(FILE *file); | 577 | extern char *xmalloc_fgets(FILE *file); |
575 | /* Chops off '\n' from the end, unlike fgets: */ | 578 | /* Chops off '\n' from the end, unlike fgets: */ |
576 | extern char *xmalloc_fgetline(FILE *file); | 579 | extern char *xmalloc_fgetline(FILE *file); |
diff --git a/libbb/read.c b/libbb/read.c index 1d31fb076..ba366cb97 100644 --- a/libbb/read.c +++ b/libbb/read.c | |||
@@ -212,7 +212,9 @@ void *xmalloc_open_read_close(const char *filename, size_t *sizep) | |||
212 | int fd; | 212 | int fd; |
213 | off_t len; | 213 | off_t len; |
214 | 214 | ||
215 | fd = xopen(filename, O_RDONLY); | 215 | fd = open(filename, O_RDONLY); |
216 | if (fd < 0) | ||
217 | return NULL; | ||
216 | /* /proc/N/stat files report len 0 here */ | 218 | /* /proc/N/stat files report len 0 here */ |
217 | /* In order to make such files readable, we add small const */ | 219 | /* In order to make such files readable, we add small const */ |
218 | len = xlseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ | 220 | len = xlseek(fd, 0, SEEK_END) | 0x3ff; /* + up to 1k */ |
@@ -229,3 +231,11 @@ void *xmalloc_open_read_close(const char *filename, size_t *sizep) | |||
229 | *sizep = size; | 231 | *sizep = size; |
230 | return buf; | 232 | return buf; |
231 | } | 233 | } |
234 | |||
235 | void *xmalloc_xopen_read_close(const char *filename, size_t *sizep) | ||
236 | { | ||
237 | void *buf = xmalloc_open_read_close(filename, sizep); | ||
238 | if (!buf) | ||
239 | bb_perror_msg_and_die("can't read '%s'", filename); | ||
240 | return buf; | ||
241 | } | ||
diff --git a/miscutils/chat.c b/miscutils/chat.c index 64d4ba4fd..5bbbb688f 100644 --- a/miscutils/chat.c +++ b/miscutils/chat.c | |||
@@ -363,7 +363,7 @@ int chat_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
363 | if ('@' == *buf) { | 363 | if ('@' == *buf) { |
364 | // skip the @ and any following white-space | 364 | // skip the @ and any following white-space |
365 | trim(++buf); | 365 | trim(++buf); |
366 | buf = loaded = xmalloc_open_read_close(buf, NULL); | 366 | buf = loaded = xmalloc_xopen_read_close(buf, NULL); |
367 | } | 367 | } |
368 | 368 | ||
369 | // expand escape sequences in command | 369 | // expand escape sequences in command |
diff --git a/printutils/lpd.c b/printutils/lpd.c index 953324452..11920d211 100644 --- a/printutils/lpd.c +++ b/printutils/lpd.c | |||
@@ -160,7 +160,7 @@ int lpd_main(int argc ATTRIBUTE_UNUSED, char *argv[]) | |||
160 | // (we exit 127 if helper cannot be executed) | 160 | // (we exit 127 if helper cannot be executed) |
161 | var[1] = '\0'; | 161 | var[1] = '\0'; |
162 | // read and delete ctrlfile | 162 | // read and delete ctrlfile |
163 | q = xmalloc_open_read_close(filenames[0], NULL); | 163 | q = xmalloc_xopen_read_close(filenames[0], NULL); |
164 | unlink(filenames[0]); | 164 | unlink(filenames[0]); |
165 | // provide datafile name | 165 | // provide datafile name |
166 | // we can use leaky setenv since we are about to exec or exit | 166 | // we can use leaky setenv since we are about to exec or exit |
diff --git a/util-linux/readprofile.c b/util-linux/readprofile.c index e25d07d2b..cac5fa4a1 100644 --- a/util-linux/readprofile.c +++ b/util-linux/readprofile.c | |||
@@ -109,9 +109,9 @@ int readprofile_main(int argc ATTRIBUTE_UNUSED, char **argv) | |||
109 | * Use an fd for the profiling buffer, to skip stdio overhead | 109 | * Use an fd for the profiling buffer, to skip stdio overhead |
110 | */ | 110 | */ |
111 | len = MAXINT(ssize_t); | 111 | len = MAXINT(ssize_t); |
112 | buf = xmalloc_open_read_close(proFile, &len); | 112 | buf = xmalloc_xopen_read_close(proFile, &len); |
113 | if (!optNative) { | 113 | if (!optNative) { |
114 | int entries = len/sizeof(*buf); | 114 | int entries = len / sizeof(*buf); |
115 | int big = 0, small = 0, i; | 115 | int big = 0, small = 0, i; |
116 | unsigned *p; | 116 | unsigned *p; |
117 | 117 | ||