libbb: [x]fopen_for_{read,write} introduced and used.

(by Valdimir)

function                                             old     new   delta
config_open2                                           -      41     +41
config_read                                          507     542     +35
find_pair                                            169     187     +18
fopen_for_write                                        -      14     +14
fopen_for_read                                         -      14     +14
find_main                                            406     418     +12
xfopen_for_write                                       -      10     +10
xfopen_for_read                                        -      10     +10
popstring                                            134     140      +6
parse_inittab                                        396     401      +5
next_token                                           923     928      +5
pack_gzip                                           1659    1661      +2
bb__parsespent                                       117     119      +2
fallbackSort                                        1719    1717      -2
evalvar                                             1376    1374      -2
qrealloc                                              36      33      -3
...
...
...
...
singlemount                                         4579    4569     -10
process_stdin                                        443     433     -10
patch_main                                          1111    1101     -10
ifupdown_main                                       2175    2165     -10
file_action_grep                                      90      80     -10
uuidcache_init                                       649     637     -12
hush_main                                            797     785     -12
read_config                                          230     217     -13
dpkg_main                                           3835    3820     -15
read_line_input                                     3134    3110     -24
sysctl_main                                          232     203     -29
config_open                                           40      10     -30
WARN_BAD_LINE                                         44       -     -44
login_main                                          1714    1575    -139
------------------------------------------------------------------------------
(add/remove: 5/1 grow/shrink: 8/74 up/down: 174/-737)        Total: -563 bytes
This commit is contained in:
Denis Vlasenko
2008-07-21 23:05:26 +00:00
parent b74a2dba57
commit 5415c856ea
71 changed files with 173 additions and 168 deletions

View File

@@ -382,7 +382,7 @@ static int arp_show(char *name)
}
host = xstrdup(ap->sprint(&sa, 1));
}
fp = xfopen("/proc/net/arp", "r");
fp = xfopen_for_read("/proc/net/arp");
/* Bypass header -- read one line */
fgets(line, sizeof(line), fp);

View File

@@ -113,7 +113,7 @@ static void dnsentryinit(void)
parser = config_open(fileconf);
if (parser) {
char *token[2];
while (config_read(parser, token, 2, 0, "# \t", 0)) {
while (config_read(parser, token, 2, 2, "# \t", 0)) {
unsigned int a,b,c,d;
/*
* Assumes all host names are lower case only
@@ -121,7 +121,8 @@ static void dnsentryinit(void)
* Presently the dot is copied into name without
* converting to a length/string substring for that label.
*/
if (!token[1] || sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
// if (!token[1] || sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
if (sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
continue;
m = xzalloc(sizeof(*m));

View File

@@ -16,28 +16,19 @@
static void do_sethostname(char *s, int isfile)
{
FILE *f;
if (!s)
return;
if (!isfile) {
if (sethostname(s, strlen(s)) < 0) {
if (errno == EPERM)
bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
bb_perror_msg_and_die("sethostname");
}
} else {
f = xfopen(s, "r");
#define strbuf bb_common_bufsiz1
while (fgets(strbuf, sizeof(strbuf), f) != NULL) {
if (strbuf[0] == '#') {
continue;
}
chomp(strbuf);
do_sethostname(strbuf, 0);
if (isfile) {
parser_t *parser = config_open2(s, xfopen_for_read);
while (config_read(parser, &s, 1, 1, "# \t", 0)) {
do_sethostname(s, 0);
}
if (ENABLE_FEATURE_CLEAN_UP)
fclose(f);
config_close(parser);
} else if (sethostname(s, strlen(s)) < 0) {
if (errno == EPERM)
bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
bb_perror_msg_and_die("sethostname");
}
}
@@ -98,5 +89,5 @@ int hostname_main(int argc, char **argv)
}
if (ENABLE_FEATURE_CLEAN_UP)
free(buf);
return 0;
return EXIT_SUCCESS;
}

View File

@@ -518,7 +518,7 @@ static void parse_conf(const char *path, int flag)
sprintf((char *)filename, "%s/%s", path, httpd_conf);
}
while ((f = fopen(filename, "r")) == NULL) {
while ((f = fopen_for_read(filename)) == NULL) {
if (flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
/* config file not found, no changes to config */
return;

View File

@@ -692,7 +692,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
enum { NONE, IFACE, MAPPING } currently_processing = NONE;
defn = xzalloc(sizeof(*defn));
f = xfopen(filename, "r");
f = xfopen_for_read(filename);
while ((buf = xmalloc_fgetline(f)) != NULL) {
#if ENABLE_DESKTOP
@@ -1090,7 +1090,7 @@ static llist_t *find_iface_state(llist_t *state_list, const char *iface)
static llist_t *read_iface_state(void)
{
llist_t *state_list = NULL;
FILE *state_fp = fopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "r");
FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
if (state_fp) {
char *start, *end_ptr;
@@ -1256,7 +1256,7 @@ int ifupdown_main(int argc, char **argv)
}
/* Actually write the new state */
state_fp = xfopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "w");
state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
state = state_list;
while (state) {
if (state->data) {

View File

@@ -990,7 +990,7 @@ static void ife_print(struct interface *ptr)
#define IPV6_ADDR_MAPPED 0x1000U
#define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
f = fopen(_PATH_PROCNET_IFINET6, "r");
f = fopen_for_read(_PATH_PROCNET_IFINET6);
if (f != NULL) {
while (fscanf
(f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",

View File

@@ -64,7 +64,7 @@ static unsigned get_hz(void)
if (hz_internal)
return hz_internal;
fp = fopen("/proc/net/psched", "r");
fp = fopen_for_read("/proc/net/psched");
if (fp) {
unsigned nom, denom;

View File

@@ -18,7 +18,7 @@ static void rtnl_tab_initialize(const char *file, const char **tab, int size)
char buf[512];
FILE *fp;
fp = fopen(file, "r");
fp = fopen_for_read(file);
if (!fp)
return;
while (fgets(buf, sizeof(buf), fp)) {

View File

@@ -170,7 +170,7 @@ int nameif_main(int argc, char **argv)
}
ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
ifh = xfopen("/proc/net/dev", "r");
ifh = xfopen_for_read("/proc/net/dev");
linenum = 0;
while (clist) {

View File

@@ -466,7 +466,7 @@ static void do_info(const char *file, const char *name, int (*proc)(int, char *)
FILE *procinfo;
char *buffer;
procinfo = fopen(file, "r");
procinfo = fopen_for_read(file);
if (procinfo == NULL) {
if (errno != ENOENT) {
bb_simple_perror_msg(file);

View File

@@ -484,7 +484,7 @@ void FAST_FUNC bb_displayroutes(int noresolve, int netstatfmt)
struct sockaddr_in s_addr;
struct in_addr mask;
FILE *fp = xfopen("/proc/net/route", "r");
FILE *fp = xfopen_for_read("/proc/net/route");
printf("Kernel IP routing table\n"
"Destination Gateway Genmask Flags %s Iface\n",
@@ -552,7 +552,7 @@ static void INET6_displayroutes(void)
int iflags, metric, refcnt, use, prefix_len, slen;
struct sockaddr_in6 snaddr6;
FILE *fp = xfopen("/proc/net/ipv6_route", "r");
FILE *fp = xfopen_for_read("/proc/net/ipv6_route");
printf("Kernel IPv6 routing table\n%-44s%-40s"
"Flags Metric Ref Use Iface\n",

View File

@@ -508,7 +508,7 @@ findsaddr(const struct sockaddr_in *to, struct sockaddr_in *from)
struct IFADDRLIST *al;
char buf[256], tdevice[256], device[256];
f = xfopen("/proc/net/route", "r");
f = xfopen_for_read("/proc/net/route");
/* Find the appropriate interface */
n = 0;

View File

@@ -321,9 +321,7 @@ void read_config(const char *file)
if (!parser)
return;
while (config_read(parser, token, 2, 0, "# \t", PARSE_LAST_IS_GREEDY)) {
if (!token[1])
continue;
while (config_read(parser, token, 2, 2, "# \t", PARSE_LAST_IS_GREEDY)) {
for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
if (!strcasecmp(token[0], k->keyword)) {
if (!k->handler(token[1], k->var)) {