aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-08-04 21:16:46 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-08-04 21:16:46 +0000
commit855ff6f503ee50fad3eb6fa30e2b02f53f32d63d (patch)
tree7cc63f646f0c9c5bcacefe2a8e453a3796902af0
parent5db861a9eb93c4562798654f53022088784f35eb (diff)
downloadbusybox-w32-855ff6f503ee50fad3eb6fa30e2b02f53f32d63d.tar.gz
busybox-w32-855ff6f503ee50fad3eb6fa30e2b02f53f32d63d.tar.bz2
busybox-w32-855ff6f503ee50fad3eb6fa30e2b02f53f32d63d.zip
modprobe: use buffering line reads (fgets) instead of reads().
libbb: remove reads() function old new delta include_conf_file_act 961 980 +19 localcmd 282 284 +2 already_loaded 155 151 -4 in_cksum 58 53 -5 modprobe_main 1630 1624 -6 reads 129 - -129 ------------------------------------------------------------------------------ (add/remove: 0/1 grow/shrink: 2/3 up/down: 21/-144) Total: -123 bytes
-rw-r--r--include/libbb.h3
-rw-r--r--libbb/read.c25
-rw-r--r--modutils/modprobe.c39
3 files changed, 20 insertions, 47 deletions
diff --git a/include/libbb.h b/include/libbb.h
index f7a68492c..9cbab4f1d 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -586,9 +586,6 @@ extern ssize_t nonblock_safe_read(int fd, void *buf, size_t count) FAST_FUNC;
586extern ssize_t full_read(int fd, void *buf, size_t count) FAST_FUNC; 586extern ssize_t full_read(int fd, void *buf, size_t count) FAST_FUNC;
587extern void xread(int fd, void *buf, size_t count) FAST_FUNC; 587extern void xread(int fd, void *buf, size_t count) FAST_FUNC;
588extern unsigned char xread_char(int fd) FAST_FUNC; 588extern unsigned char xread_char(int fd) FAST_FUNC;
589// Reads one line a-la fgets (but doesn't save terminating '\n').
590// Uses single full_read() call, works only on seekable streams.
591extern char *reads(int fd, char *buf, size_t count) FAST_FUNC;
592extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC; 589extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC;
593extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC; 590extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC;
594// Reads one line a-la fgets (but doesn't save terminating '\n'). 591// Reads one line a-la fgets (but doesn't save terminating '\n').
diff --git a/libbb/read.c b/libbb/read.c
index 7af895207..18f62838e 100644
--- a/libbb/read.c
+++ b/libbb/read.c
@@ -127,31 +127,6 @@ unsigned char FAST_FUNC xread_char(int fd)
127 return tmp; 127 return tmp;
128} 128}
129 129
130/* Read one line a-la fgets. Works only on seekable streams */
131char* FAST_FUNC reads(int fd, char *buffer, size_t size)
132{
133 char *p;
134
135 if (size < 2)
136 return NULL;
137 size = full_read(fd, buffer, size-1);
138 if ((ssize_t)size <= 0)
139 return NULL;
140
141 buffer[size] = '\0';
142 p = strchr(buffer, '\n');
143 if (p) {
144 off_t offset;
145 *p++ = '\0';
146 /* avoid incorrect (unsigned) widening */
147 offset = (off_t)(p - buffer) - (off_t)size;
148 /* set fd position right after '\n' */
149 if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1)
150 return NULL;
151 }
152 return buffer;
153}
154
155// Reads one line a-la fgets (but doesn't save terminating '\n'). 130// Reads one line a-la fgets (but doesn't save terminating '\n').
156// Reads byte-by-byte. Useful when it is important to not read ahead. 131// Reads byte-by-byte. Useful when it is important to not read ahead.
157// Bytes are appended to pfx (which must be malloced, or NULL). 132// Bytes are appended to pfx (which must be malloced, or NULL).
diff --git a/modutils/modprobe.c b/modutils/modprobe.c
index 3a2d893ee..01f8bb89b 100644
--- a/modutils/modprobe.c
+++ b/modutils/modprobe.c
@@ -280,18 +280,18 @@ static int FAST_FUNC include_conf_file_act(const char *filename,
280 struct dep_t **first = &conf->first; 280 struct dep_t **first = &conf->first;
281 struct dep_t **current = &conf->current; 281 struct dep_t **current = &conf->current;
282 int continuation_line = 0; 282 int continuation_line = 0;
283 int fd; 283 FILE *f;
284 284
285 if (bb_basename(filename)[0] == '.') 285 if (bb_basename(filename)[0] == '.')
286 return TRUE; 286 return TRUE;
287 287
288 fd = open(filename, O_RDONLY); 288 f = fopen_for_read(filename);
289 if (fd < 0) 289 if (f == NULL)
290 return FALSE; 290 return FALSE;
291 291
292 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias)! 292 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias)!
293 293
294 while (reads(fd, line_buffer, sizeof(line_buffer))) { 294 while (fgets(line_buffer, sizeof(line_buffer), f)) {
295 int l; 295 int l;
296 296
297 *strchrnul(line_buffer, '#') = '\0'; 297 *strchrnul(line_buffer, '#') = '\0';
@@ -376,9 +376,9 @@ static int FAST_FUNC include_conf_file_act(const char *filename,
376 if (dt) 376 if (dt)
377 dt->m_isblacklisted = 1; 377 dt->m_isblacklisted = 1;
378 } 378 }
379 } /* while (reads(...)) */ 379 } /* while (fgets(...)) */
380 380
381 close(fd); 381 fclose(f);
382 return TRUE; 382 return TRUE;
383} 383}
384 384
@@ -403,7 +403,7 @@ static int include_conf_file2(struct include_conf_t *conf,
403 */ 403 */
404static struct dep_t *build_dep(void) 404static struct dep_t *build_dep(void)
405{ 405{
406 int fd; 406 FILE *f;
407 struct utsname un; 407 struct utsname un;
408 struct include_conf_t conf = { NULL, NULL }; 408 struct include_conf_t conf = { NULL, NULL };
409 char *filename; 409 char *filename;
@@ -419,18 +419,18 @@ static struct dep_t *build_dep(void)
419 } 419 }
420 420
421 filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/"CONFIG_DEFAULT_DEPMOD_FILE, un.release); 421 filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/"CONFIG_DEFAULT_DEPMOD_FILE, un.release);
422 fd = open(filename, O_RDONLY); 422 f = fopen_for_read(filename);
423 if (ENABLE_FEATURE_CLEAN_UP) 423 if (ENABLE_FEATURE_CLEAN_UP)
424 free(filename); 424 free(filename);
425 if (fd < 0) { 425 if (f == NULL) {
426 /* Ok, that didn't work. Fall back to looking in /lib/modules */ 426 /* Ok, that didn't work. Fall back to looking in /lib/modules */
427 fd = open(CONFIG_DEFAULT_MODULES_DIR"/"CONFIG_DEFAULT_DEPMOD_FILE, O_RDONLY); 427 f = fopen_for_read(CONFIG_DEFAULT_MODULES_DIR"/"CONFIG_DEFAULT_DEPMOD_FILE);
428 if (fd < 0) { 428 if (f == NULL) {
429 bb_error_msg_and_die("cannot parse " CONFIG_DEFAULT_DEPMOD_FILE); 429 bb_error_msg_and_die("cannot parse " CONFIG_DEFAULT_DEPMOD_FILE);
430 } 430 }
431 } 431 }
432 432
433 while (reads(fd, line_buffer, sizeof(line_buffer))) { 433 while (fgets(line_buffer, sizeof(line_buffer), f)) {
434 int l = strlen(line_buffer); 434 int l = strlen(line_buffer);
435 char *p = 0; 435 char *p = 0;
436 436
@@ -545,8 +545,8 @@ static struct dep_t *build_dep(void)
545 545
546 /* is there other dependable module(s) ? */ 546 /* is there other dependable module(s) ? */
547 continuation_line = (line_buffer[l-1] == '\\'); 547 continuation_line = (line_buffer[l-1] == '\\');
548 } /* while (reads(...)) */ 548 } /* while (fgets(...)) */
549 close(fd); 549 fclose(f);
550 550
551 /* 551 /*
552 * First parse system-specific options and aliases 552 * First parse system-specific options and aliases
@@ -594,13 +594,14 @@ static struct dep_t *build_dep(void)
594/* return 1 = loaded, 0 = not loaded, -1 = can't tell */ 594/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
595static int already_loaded(const char *name) 595static int already_loaded(const char *name)
596{ 596{
597 int fd, ret = 0; 597 FILE *f;
598 int ret = 0;
598 599
599 fd = open("/proc/modules", O_RDONLY); 600 f = fopen_for_read("/proc/modules");
600 if (fd < 0) 601 if (f == NULL)
601 return -1; 602 return -1;
602 603
603 while (reads(fd, line_buffer, sizeof(line_buffer))) { 604 while (fgets(line_buffer, sizeof(line_buffer), f)) {
604 char *p; 605 char *p;
605 606
606 p = strchr(line_buffer, ' '); 607 p = strchr(line_buffer, ' ');
@@ -627,7 +628,7 @@ static int already_loaded(const char *name)
627 } 628 }
628 } 629 }
629 done: 630 done:
630 close(fd); 631 fclose(f);
631 return ret; 632 return ret;
632} 633}
633 634