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
This commit is contained in:
Denis Vlasenko 2008-08-04 21:16:46 +00:00
parent 5db861a9eb
commit 855ff6f503
3 changed files with 20 additions and 47 deletions

View File

@ -586,9 +586,6 @@ extern ssize_t nonblock_safe_read(int fd, void *buf, size_t count) FAST_FUNC;
extern ssize_t full_read(int fd, void *buf, size_t count) FAST_FUNC; extern ssize_t full_read(int fd, void *buf, size_t count) FAST_FUNC;
extern void xread(int fd, void *buf, size_t count) FAST_FUNC; extern void xread(int fd, void *buf, size_t count) FAST_FUNC;
extern unsigned char xread_char(int fd) FAST_FUNC; extern unsigned char xread_char(int fd) FAST_FUNC;
// Reads one line a-la fgets (but doesn't save terminating '\n').
// Uses single full_read() call, works only on seekable streams.
extern char *reads(int fd, char *buf, size_t count) FAST_FUNC;
extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC; extern ssize_t read_close(int fd, void *buf, size_t maxsz) FAST_FUNC;
extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC; extern ssize_t open_read_close(const char *filename, void *buf, size_t maxsz) FAST_FUNC;
// Reads one line a-la fgets (but doesn't save terminating '\n'). // Reads one line a-la fgets (but doesn't save terminating '\n').

View File

@ -127,31 +127,6 @@ unsigned char FAST_FUNC xread_char(int fd)
return tmp; return tmp;
} }
/* Read one line a-la fgets. Works only on seekable streams */
char* FAST_FUNC reads(int fd, char *buffer, size_t size)
{
char *p;
if (size < 2)
return NULL;
size = full_read(fd, buffer, size-1);
if ((ssize_t)size <= 0)
return NULL;
buffer[size] = '\0';
p = strchr(buffer, '\n');
if (p) {
off_t offset;
*p++ = '\0';
/* avoid incorrect (unsigned) widening */
offset = (off_t)(p - buffer) - (off_t)size;
/* set fd position right after '\n' */
if (offset && lseek(fd, offset, SEEK_CUR) == (off_t)-1)
return NULL;
}
return buffer;
}
// Reads one line a-la fgets (but doesn't save terminating '\n'). // Reads one line a-la fgets (but doesn't save terminating '\n').
// Reads byte-by-byte. Useful when it is important to not read ahead. // Reads byte-by-byte. Useful when it is important to not read ahead.
// Bytes are appended to pfx (which must be malloced, or NULL). // Bytes are appended to pfx (which must be malloced, or NULL).

View File

@ -280,18 +280,18 @@ static int FAST_FUNC include_conf_file_act(const char *filename,
struct dep_t **first = &conf->first; struct dep_t **first = &conf->first;
struct dep_t **current = &conf->current; struct dep_t **current = &conf->current;
int continuation_line = 0; int continuation_line = 0;
int fd; FILE *f;
if (bb_basename(filename)[0] == '.') if (bb_basename(filename)[0] == '.')
return TRUE; return TRUE;
fd = open(filename, O_RDONLY); f = fopen_for_read(filename);
if (fd < 0) if (f == NULL)
return FALSE; return FALSE;
// alias parsing is not 100% correct (no correct handling of continuation lines within an alias)! // alias parsing is not 100% correct (no correct handling of continuation lines within an alias)!
while (reads(fd, line_buffer, sizeof(line_buffer))) { while (fgets(line_buffer, sizeof(line_buffer), f)) {
int l; int l;
*strchrnul(line_buffer, '#') = '\0'; *strchrnul(line_buffer, '#') = '\0';
@ -376,9 +376,9 @@ static int FAST_FUNC include_conf_file_act(const char *filename,
if (dt) if (dt)
dt->m_isblacklisted = 1; dt->m_isblacklisted = 1;
} }
} /* while (reads(...)) */ } /* while (fgets(...)) */
close(fd); fclose(f);
return TRUE; return TRUE;
} }
@ -403,7 +403,7 @@ static int include_conf_file2(struct include_conf_t *conf,
*/ */
static struct dep_t *build_dep(void) static struct dep_t *build_dep(void)
{ {
int fd; FILE *f;
struct utsname un; struct utsname un;
struct include_conf_t conf = { NULL, NULL }; struct include_conf_t conf = { NULL, NULL };
char *filename; char *filename;
@ -419,18 +419,18 @@ static struct dep_t *build_dep(void)
} }
filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/"CONFIG_DEFAULT_DEPMOD_FILE, un.release); filename = xasprintf(CONFIG_DEFAULT_MODULES_DIR"/%s/"CONFIG_DEFAULT_DEPMOD_FILE, un.release);
fd = open(filename, O_RDONLY); f = fopen_for_read(filename);
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
free(filename); free(filename);
if (fd < 0) { if (f == NULL) {
/* Ok, that didn't work. Fall back to looking in /lib/modules */ /* Ok, that didn't work. Fall back to looking in /lib/modules */
fd = open(CONFIG_DEFAULT_MODULES_DIR"/"CONFIG_DEFAULT_DEPMOD_FILE, O_RDONLY); f = fopen_for_read(CONFIG_DEFAULT_MODULES_DIR"/"CONFIG_DEFAULT_DEPMOD_FILE);
if (fd < 0) { if (f == NULL) {
bb_error_msg_and_die("cannot parse " CONFIG_DEFAULT_DEPMOD_FILE); bb_error_msg_and_die("cannot parse " CONFIG_DEFAULT_DEPMOD_FILE);
} }
} }
while (reads(fd, line_buffer, sizeof(line_buffer))) { while (fgets(line_buffer, sizeof(line_buffer), f)) {
int l = strlen(line_buffer); int l = strlen(line_buffer);
char *p = 0; char *p = 0;
@ -545,8 +545,8 @@ static struct dep_t *build_dep(void)
/* is there other dependable module(s) ? */ /* is there other dependable module(s) ? */
continuation_line = (line_buffer[l-1] == '\\'); continuation_line = (line_buffer[l-1] == '\\');
} /* while (reads(...)) */ } /* while (fgets(...)) */
close(fd); fclose(f);
/* /*
* First parse system-specific options and aliases * First parse system-specific options and aliases
@ -594,13 +594,14 @@ static struct dep_t *build_dep(void)
/* return 1 = loaded, 0 = not loaded, -1 = can't tell */ /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
static int already_loaded(const char *name) static int already_loaded(const char *name)
{ {
int fd, ret = 0; FILE *f;
int ret = 0;
fd = open("/proc/modules", O_RDONLY); f = fopen_for_read("/proc/modules");
if (fd < 0) if (f == NULL)
return -1; return -1;
while (reads(fd, line_buffer, sizeof(line_buffer))) { while (fgets(line_buffer, sizeof(line_buffer), f)) {
char *p; char *p;
p = strchr(line_buffer, ' '); p = strchr(line_buffer, ' ');
@ -627,7 +628,7 @@ static int already_loaded(const char *name)
} }
} }
done: done:
close(fd); fclose(f);
return ret; return ret;
} }