aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-07-21 23:05:26 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-07-21 23:05:26 +0000
commit5415c856eaccd1bc5d064022770a288f43b2e94f (patch)
tree5973db4e6f81b5e311e8944700ded5db1083d440
parentb74a2dba571d1c5a6127c683fb50923336d9c59f (diff)
downloadbusybox-w32-5415c856eaccd1bc5d064022770a288f43b2e94f.tar.gz
busybox-w32-5415c856eaccd1bc5d064022770a288f43b2e94f.tar.bz2
busybox-w32-5415c856eaccd1bc5d064022770a288f43b2e94f.zip
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
-rw-r--r--archival/cpio.c2
-rw-r--r--archival/dpkg.c10
-rw-r--r--archival/tar.c2
-rw-r--r--coreutils/dos2unix.c2
-rw-r--r--coreutils/sort.c2
-rw-r--r--coreutils/uudecode.c2
-rw-r--r--debianutils/start_stop_daemon.c2
-rw-r--r--e2fsprogs/old_e2fsprogs/blkid/devname.c6
-rw-r--r--e2fsprogs/old_e2fsprogs/blkid/save.c2
-rw-r--r--e2fsprogs/old_e2fsprogs/e2fsck.c4
-rw-r--r--e2fsprogs/old_e2fsprogs/ext2fs/ismounted.c2
-rw-r--r--e2fsprogs/old_e2fsprogs/ext2fs/test_io.c2
-rw-r--r--e2fsprogs/old_e2fsprogs/mke2fs.c2
-rw-r--r--editors/awk.c2
-rw-r--r--editors/patch.c4
-rw-r--r--editors/sed.c6
-rw-r--r--findutils/grep.c4
-rw-r--r--findutils/xargs.c2
-rw-r--r--include/libbb.h7
-rw-r--r--init/init.c2
-rw-r--r--libbb/appletlib.c2
-rw-r--r--libbb/lineedit.c4
-rw-r--r--libbb/login.c2
-rw-r--r--libbb/parse_config.c20
-rw-r--r--libbb/procps.c2
-rw-r--r--libbb/rtc.c2
-rw-r--r--libbb/wfopen.c20
-rw-r--r--libpwdgrp/pwd_grp.c8
-rw-r--r--libpwdgrp/pwd_grp_internal.c2
-rw-r--r--loginutils/getty.c2
-rw-r--r--loginutils/login.c34
-rw-r--r--miscutils/crond.c2
-rw-r--r--miscutils/devfsd.c2
-rw-r--r--miscutils/fbsplash.c2
-rw-r--r--miscutils/less.c2
-rw-r--r--miscutils/makedevs.c2
-rw-r--r--miscutils/man.c4
-rw-r--r--modutils/depmod.c2
-rw-r--r--modutils/insmod.c6
-rw-r--r--modutils/lsmod.c4
-rw-r--r--modutils/modprobe-small.c6
-rw-r--r--networking/arp.c2
-rw-r--r--networking/dnsd.c5
-rw-r--r--networking/hostname.c29
-rw-r--r--networking/httpd.c2
-rw-r--r--networking/ifupdown.c6
-rw-r--r--networking/interface.c2
-rw-r--r--networking/libiproute/iproute.c2
-rw-r--r--networking/libiproute/rt_names.c2
-rw-r--r--networking/nameif.c2
-rw-r--r--networking/netstat.c2
-rw-r--r--networking/route.c4
-rw-r--r--networking/traceroute.c2
-rw-r--r--networking/udhcp/files.c4
-rw-r--r--procps/fuser.c4
-rw-r--r--procps/sysctl.c11
-rw-r--r--procps/top.c6
-rw-r--r--scripts/basic/split-include.c2
-rw-r--r--selinux/setfiles.c6
-rw-r--r--shell/bbsh.c2
-rw-r--r--shell/hush.c6
-rw-r--r--shell/lash_unused.c4
-rw-r--r--util-linux/fbset.c2
-rw-r--r--util-linux/fdisk.c2
-rw-r--r--util-linux/fdisk_sun.c2
-rw-r--r--util-linux/hexdump.c21
-rw-r--r--util-linux/mkfs_minix.c2
-rw-r--r--util-linux/more.c2
-rw-r--r--util-linux/mount.c2
-rw-r--r--util-linux/readprofile.c2
-rw-r--r--util-linux/volume_id/get_devname.c4
71 files changed, 173 insertions, 168 deletions
diff --git a/archival/cpio.c b/archival/cpio.c
index ebe911a96..37b6b5cce 100644
--- a/archival/cpio.c
+++ b/archival/cpio.c
@@ -287,7 +287,7 @@ int cpio_main(int argc UNUSED_PARAM, char **argv)
287 bb_show_usage(); 287 bb_show_usage();
288 if (opt & CPIO_OPT_FILE) { 288 if (opt & CPIO_OPT_FILE) {
289 fclose(stdout); 289 fclose(stdout);
290 stdout = fopen(cpio_filename, "w"); 290 stdout = fopen_for_write(cpio_filename);
291 /* Paranoia: I don't trust libc that much */ 291 /* Paranoia: I don't trust libc that much */
292 xdup2(fileno(stdout), STDOUT_FILENO); 292 xdup2(fileno(stdout), STDOUT_FILENO);
293 } 293 }
diff --git a/archival/dpkg.c b/archival/dpkg.c
index 671aae7cc..9ea308703 100644
--- a/archival/dpkg.c
+++ b/archival/dpkg.c
@@ -748,7 +748,7 @@ static void index_status_file(const char *filename)
748 status_node_t *status_node = NULL; 748 status_node_t *status_node = NULL;
749 unsigned status_num; 749 unsigned status_num;
750 750
751 status_file = xfopen(filename, "r"); 751 status_file = xfopen_for_read(filename);
752 while ((control_buffer = xmalloc_fgetline_str(status_file, "\n\n")) != NULL) { 752 while ((control_buffer = xmalloc_fgetline_str(status_file, "\n\n")) != NULL) {
753 const unsigned package_num = fill_package_struct(control_buffer); 753 const unsigned package_num = fill_package_struct(control_buffer);
754 if (package_num != -1) { 754 if (package_num != -1) {
@@ -790,8 +790,8 @@ static void write_buffer_no_status(FILE *new_status_file, const char *control_bu
790/* This could do with a cleanup */ 790/* This could do with a cleanup */
791static void write_status_file(deb_file_t **deb_file) 791static void write_status_file(deb_file_t **deb_file)
792{ 792{
793 FILE *old_status_file = xfopen("/var/lib/dpkg/status", "r"); 793 FILE *old_status_file = xfopen_for_read("/var/lib/dpkg/status");
794 FILE *new_status_file = xfopen("/var/lib/dpkg/status.udeb", "w"); 794 FILE *new_status_file = xfopen_for_write("/var/lib/dpkg/status.udeb");
795 char *package_name; 795 char *package_name;
796 char *status_from_file; 796 char *status_from_file;
797 char *control_buffer = NULL; 797 char *control_buffer = NULL;
@@ -1161,7 +1161,7 @@ static char **create_list(const char *filename)
1161 int count; 1161 int count;
1162 1162
1163 /* don't use [xw]fopen here, handle error ourself */ 1163 /* don't use [xw]fopen here, handle error ourself */
1164 list_stream = fopen(filename, "r"); 1164 list_stream = fopen_for_read(filename);
1165 if (list_stream == NULL) { 1165 if (list_stream == NULL) {
1166 return NULL; 1166 return NULL;
1167 } 1167 }
@@ -1548,7 +1548,7 @@ static void unpack_package(deb_file_t *deb_file)
1548 1548
1549 /* Create the list file */ 1549 /* Create the list file */
1550 list_filename = xasprintf("/var/lib/dpkg/info/%s.%s", package_name, "list"); 1550 list_filename = xasprintf("/var/lib/dpkg/info/%s.%s", package_name, "list");
1551 out_stream = xfopen(list_filename, "w"); 1551 out_stream = xfopen_for_write(list_filename);
1552 while (archive_handle->sub_archive->passed) { 1552 while (archive_handle->sub_archive->passed) {
1553 /* the leading . has been stripped by data_extract_all_prefix already */ 1553 /* the leading . has been stripped by data_extract_all_prefix already */
1554 fputs(archive_handle->sub_archive->passed->data, out_stream); 1554 fputs(archive_handle->sub_archive->passed->data, out_stream);
diff --git a/archival/tar.c b/archival/tar.c
index a46442f8e..b2481b843 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -663,7 +663,7 @@ static llist_t *append_file_list_to_list(llist_t *list)
663 llist_t *newlist = NULL; 663 llist_t *newlist = NULL;
664 664
665 while (list) { 665 while (list) {
666 src_stream = xfopen(llist_pop(&list), "r"); 666 src_stream = xfopen_for_read(llist_pop(&list));
667 while ((line = xmalloc_fgetline(src_stream)) != NULL) { 667 while ((line = xmalloc_fgetline(src_stream)) != NULL) {
668 /* kill trailing '/' unless the string is just "/" */ 668 /* kill trailing '/' unless the string is just "/" */
669 char *cp = last_char_is(line, '/'); 669 char *cp = last_char_is(line, '/');
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c
index 311dc1380..309cbc3b8 100644
--- a/coreutils/dos2unix.c
+++ b/coreutils/dos2unix.c
@@ -35,7 +35,7 @@ static void convert(char *fn, int conv_type)
35 resolved_fn = xmalloc_follow_symlinks(fn); 35 resolved_fn = xmalloc_follow_symlinks(fn);
36 if (resolved_fn == NULL) 36 if (resolved_fn == NULL)
37 bb_simple_perror_msg_and_die(fn); 37 bb_simple_perror_msg_and_die(fn);
38 in = xfopen(resolved_fn, "r"); 38 in = xfopen_for_read(resolved_fn);
39 fstat(fileno(in), &st); 39 fstat(fileno(in), &st);
40 40
41 temp_fn = xasprintf("%sXXXXXX", resolved_fn); 41 temp_fn = xasprintf("%sXXXXXX", resolved_fn);
diff --git a/coreutils/sort.c b/coreutils/sort.c
index ac8fc9b2e..fad6d1244 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -293,7 +293,7 @@ int sort_main(int argc UNUSED_PARAM, char **argv)
293 "k::"; /* -k takes list */ 293 "k::"; /* -k takes list */
294 getopt32(argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t); 294 getopt32(argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t);
295#if ENABLE_FEATURE_SORT_BIG 295#if ENABLE_FEATURE_SORT_BIG
296 if (option_mask32 & FLAG_o) outfile = xfopen(str_o, "w"); 296 if (option_mask32 & FLAG_o) outfile = xfopen_for_write(str_o);
297 if (option_mask32 & FLAG_t) { 297 if (option_mask32 & FLAG_t) {
298 if (!str_t[0] || str_t[1]) 298 if (!str_t[0] || str_t[1])
299 bb_error_msg_and_die("bad -t parameter"); 299 bb_error_msg_and_die("bad -t parameter");
diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c
index 81a86cb15..0298a4bdb 100644
--- a/coreutils/uudecode.c
+++ b/coreutils/uudecode.c
@@ -179,7 +179,7 @@ int uudecode_main(int argc UNUSED_PARAM, char **argv)
179 } 179 }
180 dst_stream = stdout; 180 dst_stream = stdout;
181 if (NOT_LONE_DASH(outname)) { 181 if (NOT_LONE_DASH(outname)) {
182 dst_stream = xfopen(outname, "w"); 182 dst_stream = xfopen_for_write(outname);
183 fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO)); 183 fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO));
184 } 184 }
185 free(line); 185 free(line);
diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c
index 91f51a3c6..875eca511 100644
--- a/debianutils/start_stop_daemon.c
+++ b/debianutils/start_stop_daemon.c
@@ -204,7 +204,7 @@ static void do_pidfile(void)
204 FILE *f; 204 FILE *f;
205 unsigned pid; 205 unsigned pid;
206 206
207 f = fopen(pidfile, "r"); 207 f = fopen_for_read(pidfile);
208 if (f) { 208 if (f) {
209 if (fscanf(f, "%u", &pid) == 1) 209 if (fscanf(f, "%u", &pid) == 1)
210 check(pid); 210 check(pid);
diff --git a/e2fsprogs/old_e2fsprogs/blkid/devname.c b/e2fsprogs/old_e2fsprogs/blkid/devname.c
index 97d1c3548..5b9e48f13 100644
--- a/e2fsprogs/old_e2fsprogs/blkid/devname.c
+++ b/e2fsprogs/old_e2fsprogs/blkid/devname.c
@@ -152,7 +152,7 @@ static dev_t lvm_get_devno(const char *lvm_device)
152 dev_t ret = 0; 152 dev_t ret = 0;
153 153
154 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device)); 154 DBG(DEBUG_DEVNAME, printf("opening %s\n", lvm_device));
155 if ((lvf = fopen(lvm_device, "r")) == NULL) { 155 if ((lvf = fopen_for_read(lvm_device)) == NULL) {
156 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno, 156 DBG(DEBUG_DEVNAME, printf("%s: (%d) %s\n", lvm_device, errno,
157 strerror(errno))); 157 strerror(errno)));
158 return 0; 158 return 0;
@@ -233,7 +233,7 @@ evms_probe_all(blkid_cache cache)
233 FILE *procpt; 233 FILE *procpt;
234 char device[110]; 234 char device[110];
235 235
236 procpt = fopen(PROC_EVMS_VOLUMES, "r"); 236 procpt = fopen_for_read(PROC_EVMS_VOLUMES);
237 if (!procpt) 237 if (!procpt)
238 return 0; 238 return 0;
239 while (fgets(line, sizeof(line), procpt)) { 239 while (fgets(line, sizeof(line), procpt)) {
@@ -282,7 +282,7 @@ int blkid_probe_all(blkid_cache cache)
282 lvm_probe_all(cache); 282 lvm_probe_all(cache);
283#endif 283#endif
284 284
285 proc = fopen(PROC_PARTITIONS, "r"); 285 proc = fopen_for_read(PROC_PARTITIONS);
286 if (!proc) 286 if (!proc)
287 return -BLKID_ERR_PROC; 287 return -BLKID_ERR_PROC;
288 288
diff --git a/e2fsprogs/old_e2fsprogs/blkid/save.c b/e2fsprogs/old_e2fsprogs/blkid/save.c
index cdbaabcb1..3600260e2 100644
--- a/e2fsprogs/old_e2fsprogs/blkid/save.c
+++ b/e2fsprogs/old_e2fsprogs/blkid/save.c
@@ -102,7 +102,7 @@ int blkid_flush_cache(blkid_cache cache)
102 } 102 }
103 103
104 if (!file) { 104 if (!file) {
105 file = fopen(filename, "w"); 105 file = fopen_for_write(filename);
106 opened = filename; 106 opened = filename;
107 } 107 }
108 108
diff --git a/e2fsprogs/old_e2fsprogs/e2fsck.c b/e2fsprogs/old_e2fsprogs/e2fsck.c
index 4887a57d7..7cb1227db 100644
--- a/e2fsprogs/old_e2fsprogs/e2fsck.c
+++ b/e2fsprogs/old_e2fsprogs/e2fsck.c
@@ -12672,7 +12672,7 @@ static int is_on_batt(void)
12672 unsigned int acflag; 12672 unsigned int acflag;
12673 struct dirent* de; 12673 struct dirent* de;
12674 12674
12675 f = fopen("/proc/apm", "r"); 12675 f = fopen_for_read("/proc/apm");
12676 if (f) { 12676 if (f) {
12677 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4) 12677 if (fscanf(f, "%s %s %s %x", tmp, tmp, tmp, &acflag) != 4)
12678 acflag = 1; 12678 acflag = 1;
@@ -12686,7 +12686,7 @@ static int is_on_batt(void)
12686 continue; 12686 continue;
12687 snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state", 12687 snprintf(fname, 80, "/proc/acpi/ac_adapter/%s/state",
12688 de->d_name); 12688 de->d_name);
12689 f = fopen(fname, "r"); 12689 f = fopen_for_read(fname);
12690 if (!f) 12690 if (!f)
12691 continue; 12691 continue;
12692 if (fscanf(f, "%s %s", tmp2, tmp) != 2) 12692 if (fscanf(f, "%s %s", tmp2, tmp) != 2)
diff --git a/e2fsprogs/old_e2fsprogs/ext2fs/ismounted.c b/e2fsprogs/old_e2fsprogs/ext2fs/ismounted.c
index d943f1185..7f24f9ba8 100644
--- a/e2fsprogs/old_e2fsprogs/ext2fs/ismounted.c
+++ b/e2fsprogs/old_e2fsprogs/ext2fs/ismounted.c
@@ -249,7 +249,7 @@ static int is_swap_device(const char *file)
249 file_dev = st_buf.st_rdev; 249 file_dev = st_buf.st_rdev;
250#endif /* __GNU__ */ 250#endif /* __GNU__ */
251 251
252 if (!(f = fopen("/proc/swaps", "r"))) 252 if (!(f = fopen_for_read("/proc/swaps")))
253 return 0; 253 return 0;
254 /* Skip the first line */ 254 /* Skip the first line */
255 fgets(buf, sizeof(buf), f); 255 fgets(buf, sizeof(buf), f);
diff --git a/e2fsprogs/old_e2fsprogs/ext2fs/test_io.c b/e2fsprogs/old_e2fsprogs/ext2fs/test_io.c
index bd74225c0..3d40d9a97 100644
--- a/e2fsprogs/old_e2fsprogs/ext2fs/test_io.c
+++ b/e2fsprogs/old_e2fsprogs/ext2fs/test_io.c
@@ -180,7 +180,7 @@ static errcode_t test_open(const char *name, int flags, io_channel *channel)
180 180
181 data->outfile = NULL; 181 data->outfile = NULL;
182 if ((value = getenv("TEST_IO_LOGFILE")) != NULL) 182 if ((value = getenv("TEST_IO_LOGFILE")) != NULL)
183 data->outfile = fopen(value, "w"); 183 data->outfile = fopen_for_write(value);
184 if (!data->outfile) 184 if (!data->outfile)
185 data->outfile = stderr; 185 data->outfile = stderr;
186 186
diff --git a/e2fsprogs/old_e2fsprogs/mke2fs.c b/e2fsprogs/old_e2fsprogs/mke2fs.c
index e16fe93e8..852c249b1 100644
--- a/e2fsprogs/old_e2fsprogs/mke2fs.c
+++ b/e2fsprogs/old_e2fsprogs/mke2fs.c
@@ -225,7 +225,7 @@ static void read_bb_file(ext2_filsys fs, badblocks_list *bb_list,
225 FILE *f; 225 FILE *f;
226 errcode_t retval; 226 errcode_t retval;
227 227
228 f = xfopen(bad_blocks_file, "r"); 228 f = xfopen_for_read(bad_blocks_file);
229 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block); 229 retval = ext2fs_read_bb_FILE(fs, f, bb_list, invalid_block);
230 fclose (f); 230 fclose (f);
231 mke2fs_error_msg_and_die(retval, "read bad blocks from list"); 231 mke2fs_error_msg_and_die(retval, "read bad blocks from list");
diff --git a/editors/awk.c b/editors/awk.c
index 7af9e1eeb..571d68193 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -2400,7 +2400,7 @@ static var *evaluate(node *op, var *res)
2400 X.rsm->F = popen(L.s, "r"); 2400 X.rsm->F = popen(L.s, "r");
2401 X.rsm->is_pipe = TRUE; 2401 X.rsm->is_pipe = TRUE;
2402 } else { 2402 } else {
2403 X.rsm->F = fopen(L.s, "r"); /* not xfopen! */ 2403 X.rsm->F = fopen_for_read(L.s); /* not xfopen! */
2404 } 2404 }
2405 } 2405 }
2406 } else { 2406 } else {
diff --git a/editors/patch.c b/editors/patch.c
index d5f6df275..e8482a7a9 100644
--- a/editors/patch.c
+++ b/editors/patch.c
@@ -131,9 +131,9 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
131 } else { 131 } else {
132 backup_filename = xasprintf("%s.orig", new_filename); 132 backup_filename = xasprintf("%s.orig", new_filename);
133 xrename(new_filename, backup_filename); 133 xrename(new_filename, backup_filename);
134 src_stream = xfopen(backup_filename, "r"); 134 src_stream = xfopen_for_read(backup_filename);
135 } 135 }
136 dst_stream = xfopen(new_filename, "w"); 136 dst_stream = xfopen_for_write(new_filename);
137 fchmod(fileno(dst_stream), saved_stat.st_mode); 137 fchmod(fileno(dst_stream), saved_stat.st_mode);
138 138
139 printf("patching file %s\n", new_filename); 139 printf("patching file %s\n", new_filename);
diff --git a/editors/sed.c b/editors/sed.c
index 67e88418a..496d3d22e 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -438,7 +438,7 @@ static const char *parse_cmd_args(sed_cmd_t *sed_cmd, const char *cmdstr)
438 bb_error_msg_and_die("command only uses one address"); 438 bb_error_msg_and_die("command only uses one address");
439 cmdstr += parse_file_cmd(/*sed_cmd,*/ cmdstr, &sed_cmd->string); 439 cmdstr += parse_file_cmd(/*sed_cmd,*/ cmdstr, &sed_cmd->string);
440 if (sed_cmd->cmd == 'w') { 440 if (sed_cmd->cmd == 'w') {
441 sed_cmd->sw_file = xfopen(sed_cmd->string, "w"); 441 sed_cmd->sw_file = xfopen_for_write(sed_cmd->string);
442 sed_cmd->sw_last_char = '\n'; 442 sed_cmd->sw_last_char = '\n';
443 } 443 }
444 /* handle branch commands */ 444 /* handle branch commands */
@@ -1025,7 +1025,7 @@ static void process_files(void)
1025 { 1025 {
1026 FILE *rfile; 1026 FILE *rfile;
1027 1027
1028 rfile = fopen(sed_cmd->string, "r"); 1028 rfile = fopen_for_read(sed_cmd->string);
1029 if (rfile) { 1029 if (rfile) {
1030 char *line; 1030 char *line;
1031 1031
@@ -1271,7 +1271,7 @@ int sed_main(int argc UNUSED_PARAM, char **argv)
1271 while (opt_f) { // -f 1271 while (opt_f) { // -f
1272 char *line; 1272 char *line;
1273 FILE *cmdfile; 1273 FILE *cmdfile;
1274 cmdfile = xfopen(llist_pop(&opt_f), "r"); 1274 cmdfile = xfopen_for_read(llist_pop(&opt_f));
1275 while ((line = xmalloc_fgetline(cmdfile)) != NULL) { 1275 while ((line = xmalloc_fgetline(cmdfile)) != NULL) {
1276 add_cmd(line); 1276 add_cmd(line);
1277 free(line); 1277 free(line);
diff --git a/findutils/grep.c b/findutils/grep.c
index 5cfd7721f..030e62461 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -375,7 +375,7 @@ static int FAST_FUNC file_action_grep(const char *filename,
375 void* matched, 375 void* matched,
376 int depth UNUSED_PARAM) 376 int depth UNUSED_PARAM)
377{ 377{
378 FILE *file = fopen(filename, "r"); 378 FILE *file = fopen_for_read(filename);
379 if (file == NULL) { 379 if (file == NULL) {
380 if (!SUPPRESS_ERR_MSGS) 380 if (!SUPPRESS_ERR_MSGS)
381 bb_simple_perror_msg(filename); 381 bb_simple_perror_msg(filename);
@@ -514,7 +514,7 @@ int grep_main(int argc, char **argv)
514 } 514 }
515 } 515 }
516 /* else: fopen(dir) will succeed, but reading won't */ 516 /* else: fopen(dir) will succeed, but reading won't */
517 file = fopen(cur_file, "r"); 517 file = fopen_for_read(cur_file);
518 if (file == NULL) { 518 if (file == NULL) {
519 if (!SUPPRESS_ERR_MSGS) 519 if (!SUPPRESS_ERR_MSGS)
520 bb_simple_perror_msg(cur_file); 520 bb_simple_perror_msg(cur_file);
diff --git a/findutils/xargs.c b/findutils/xargs.c
index 7a9865b5e..8f0a3d4ad 100644
--- a/findutils/xargs.c
+++ b/findutils/xargs.c
@@ -277,7 +277,7 @@ static int xargs_ask_confirmation(void)
277 FILE *tty_stream; 277 FILE *tty_stream;
278 int c, savec; 278 int c, savec;
279 279
280 tty_stream = xfopen(CURRENT_TTY, "r"); 280 tty_stream = xfopen_for_read(CURRENT_TTY);
281 fputs(" ?...", stderr); 281 fputs(" ?...", stderr);
282 fflush(stderr); 282 fflush(stderr);
283 c = savec = getc(tty_stream); 283 c = savec = getc(tty_stream);
diff --git a/include/libbb.h b/include/libbb.h
index 4b2a839a5..684e130c9 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -631,6 +631,10 @@ extern FILE *fopen_or_warn(const char *filename, const char *mode) FAST_FUNC;
631/* "Opens" stdin if filename is special, else just opens file: */ 631/* "Opens" stdin if filename is special, else just opens file: */
632extern FILE *xfopen_stdin(const char *filename) FAST_FUNC; 632extern FILE *xfopen_stdin(const char *filename) FAST_FUNC;
633extern FILE *fopen_or_warn_stdin(const char *filename) FAST_FUNC; 633extern FILE *fopen_or_warn_stdin(const char *filename) FAST_FUNC;
634extern FILE* fopen_for_read(const char *path) FAST_FUNC;
635extern FILE* xfopen_for_read(const char *path) FAST_FUNC;
636extern FILE* fopen_for_write(const char *path) FAST_FUNC;
637extern FILE* xfopen_for_write(const char *path) FAST_FUNC;
634 638
635int bb_pstrcmp(const void *a, const void *b) /* not FAST_FUNC! */; 639int bb_pstrcmp(const void *a, const void *b) /* not FAST_FUNC! */;
636void qsort_string_vector(char **sv, unsigned count) FAST_FUNC; 640void qsort_string_vector(char **sv, unsigned count) FAST_FUNC;
@@ -993,10 +997,12 @@ int bb_parse_mode(const char* s, mode_t* theMode) FAST_FUNC;
993 * Config file parser 997 * Config file parser
994 */ 998 */
995enum { 999enum {
1000 PARSE_VANILLA = 0x00000000, // trim line, collapse delimiters, warn and continue if less than mintokens
996 PARSE_DONT_REDUCE = 0x00010000, // do not treat consecutive delimiters as one 1001 PARSE_DONT_REDUCE = 0x00010000, // do not treat consecutive delimiters as one
997 PARSE_DONT_TRIM = 0x00020000, // do not trim line of leading and trailing delimiters 1002 PARSE_DONT_TRIM = 0x00020000, // do not trim line of leading and trailing delimiters
998 PARSE_LAST_IS_GREEDY = 0x00040000, // last token takes whole remainder of the line 1003 PARSE_LAST_IS_GREEDY = 0x00040000, // last token takes whole remainder of the line
999// PARSE_DONT_NULL = 0x00080000, // do not set tokens[] to NULL 1004// PARSE_DONT_NULL = 0x00080000, // do not set tokens[] to NULL
1005 PARSE_MIN_DIE = 0x00100000, // die if less tokens found
1000 // keep a copy of current line 1006 // keep a copy of current line
1001 PARSE_KEEP_COPY = 0x00200000 * ENABLE_DEBUG_CROND_OPTION, 1007 PARSE_KEEP_COPY = 0x00200000 * ENABLE_DEBUG_CROND_OPTION,
1002}; 1008};
@@ -1007,6 +1013,7 @@ typedef struct parser_t {
1007 int lineno; 1013 int lineno;
1008} parser_t; 1014} parser_t;
1009parser_t* config_open(const char *filename) FAST_FUNC; 1015parser_t* config_open(const char *filename) FAST_FUNC;
1016parser_t* config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path)) FAST_FUNC;
1010int config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims) FAST_FUNC; 1017int config_read(parser_t *parser, char **tokens, unsigned flags, const char *delims) FAST_FUNC;
1011#define config_read(parser, tokens, max, min, str, flags) \ 1018#define config_read(parser, tokens, max, min, str, flags) \
1012 config_read(parser, tokens, ((flags) | (((min) & 0xFF) << 8) | ((max) & 0xFF)), str) 1019 config_read(parser, tokens, ((flags) | (((min) & 0xFF) << 8) | ((max) & 0xFF)), str)
diff --git a/init/init.c b/init/init.c
index d11d425ba..884603af3 100644
--- a/init/init.c
+++ b/init/init.c
@@ -681,7 +681,7 @@ static void parse_inittab(void)
681 "sysinit\0""respawn\0""askfirst\0""wait\0""once\0" 681 "sysinit\0""respawn\0""askfirst\0""wait\0""once\0"
682 "ctrlaltdel\0""shutdown\0""restart\0"; 682 "ctrlaltdel\0""shutdown\0""restart\0";
683 683
684 parser_t *parser = config_open(INITTAB); 684 parser_t *parser = config_open2(INITTAB, fopen_for_read);
685 /* No inittab file -- set up some default behavior */ 685 /* No inittab file -- set up some default behavior */
686 if (parser == NULL) { 686 if (parser == NULL) {
687 /* Reboot on Ctrl-Alt-Del */ 687 /* Reboot on Ctrl-Alt-Del */
diff --git a/libbb/appletlib.c b/libbb/appletlib.c
index 8c35450f8..beb1d6fcb 100644
--- a/libbb/appletlib.c
+++ b/libbb/appletlib.c
@@ -316,7 +316,7 @@ static void parse_config_file(void)
316 || !S_ISREG(st.st_mode) /* Not a regular file? */ 316 || !S_ISREG(st.st_mode) /* Not a regular file? */
317 || (st.st_uid != 0) /* Not owned by root? */ 317 || (st.st_uid != 0) /* Not owned by root? */
318 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */ 318 || (st.st_mode & (S_IWGRP | S_IWOTH)) /* Writable by non-root? */
319 || !(f = fopen(config_file, "r")) /* Cannot open? */ 319 || !(f = fopen_for_read(config_file)) /* Cannot open? */
320 ) { 320 ) {
321 return; 321 return;
322 } 322 }
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index a46b5d2c4..2e16e6a0a 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -989,7 +989,7 @@ static void load_history(const char *fromfile)
989 989
990 /* NB: do not trash old history if file can't be opened */ 990 /* NB: do not trash old history if file can't be opened */
991 991
992 fp = fopen(fromfile, "r"); 992 fp = fopen_for_read(fromfile);
993 if (fp) { 993 if (fp) {
994 /* clean up old history */ 994 /* clean up old history */
995 for (hi = state->cnt_history; hi > 0;) { 995 for (hi = state->cnt_history; hi > 0;) {
@@ -1022,7 +1022,7 @@ static void save_history(const char *tofile)
1022{ 1022{
1023 FILE *fp; 1023 FILE *fp;
1024 1024
1025 fp = fopen(tofile, "w"); 1025 fp = fopen_for_write(tofile);
1026 if (fp) { 1026 if (fp) {
1027 int i; 1027 int i;
1028 1028
diff --git a/libbb/login.c b/libbb/login.c
index 1703a2eb7..b3e199ce4 100644
--- a/libbb/login.c
+++ b/libbb/login.c
@@ -32,7 +32,7 @@ void FAST_FUNC print_login_issue(const char *issue_file, const char *tty)
32 32
33 puts("\r"); /* start a new line */ 33 puts("\r"); /* start a new line */
34 34
35 fp = fopen(issue_file, "r"); 35 fp = fopen_for_read(issue_file);
36 if (!fp) 36 if (!fp)
37 return; 37 return;
38 while ((c = fgetc(fp)) != EOF) { 38 while ((c = fgetc(fp)) != EOF) {
diff --git a/libbb/parse_config.c b/libbb/parse_config.c
index 3174a649e..5109066d8 100644
--- a/libbb/parse_config.c
+++ b/libbb/parse_config.c
@@ -59,11 +59,11 @@ Typical usage:
59 59
60*/ 60*/
61 61
62parser_t* FAST_FUNC config_open(const char *filename) 62parser_t* FAST_FUNC config_open2(const char *filename, FILE* FAST_FUNC (*fopen_func)(const char *path))
63{ 63{
64 parser_t *parser = xzalloc(sizeof(parser_t)); 64 parser_t *parser = xzalloc(sizeof(parser_t));
65 /* empty file configures nothing */ 65 /* empty file configures nothing */
66 parser->fp = fopen_or_warn_stdin(filename); 66 parser->fp = fopen_func(filename);
67 if (parser->fp) 67 if (parser->fp)
68 return parser; 68 return parser;
69 if (ENABLE_FEATURE_CLEAN_UP) 69 if (ENABLE_FEATURE_CLEAN_UP)
@@ -71,6 +71,11 @@ parser_t* FAST_FUNC config_open(const char *filename)
71 return NULL; 71 return NULL;
72} 72}
73 73
74parser_t* FAST_FUNC config_open(const char *filename)
75{
76 return config_open2(filename, fopen_or_warn_stdin);
77}
78
74static void config_free_data(parser_t *const parser) 79static void config_free_data(parser_t *const parser)
75{ 80{
76 free(parser->line); 81 free(parser->line);
@@ -114,6 +119,7 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const
114 int ntokens = flags & 0xFF; 119 int ntokens = flags & 0xFF;
115 int mintokens = (flags & 0xFF00) >> 8; 120 int mintokens = (flags & 0xFF00) >> 8;
116 121
122 again:
117 // N.B. this could only be used in read-in-one-go version, or when tokens use xstrdup(). TODO 123 // N.B. this could only be used in read-in-one-go version, or when tokens use xstrdup(). TODO
118 //if (!parser->lineno || !(flags & PARSE_DONT_NULL)) 124 //if (!parser->lineno || !(flags & PARSE_DONT_NULL))
119 memset(tokens, 0, sizeof(tokens[0]) * ntokens); 125 memset(tokens, 0, sizeof(tokens[0]) * ntokens);
@@ -211,9 +217,13 @@ int FAST_FUNC config_read(parser_t *parser, char **tokens, unsigned flags, const
211 //bb_info_msg("A[%s]", line); 217 //bb_info_msg("A[%s]", line);
212 } 218 }
213 219
214 if (ii < mintokens) 220 if (ii < mintokens) {
215 bb_error_msg_and_die("bad line %u: %d tokens found, %d needed", 221 bb_error_msg("bad line %u: %d tokens found, %d needed",
216 parser->lineno, ii, mintokens); 222 parser->lineno, ii, mintokens);
223 if (flags & PARSE_MIN_DIE)
224 xfunc_die();
225 goto again;
226 }
217 227
218 return ii; 228 return ii;
219} 229}
diff --git a/libbb/procps.c b/libbb/procps.c
index f799099fd..ba3d25050 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -308,7 +308,7 @@ procps_status_t* FAST_FUNC procps_scan(procps_status_t* sp, int flags)
308 FILE *file; 308 FILE *file;
309 309
310 strcpy(filename_tail, "/smaps"); 310 strcpy(filename_tail, "/smaps");
311 file = fopen(filename, "r"); 311 file = fopen_for_read(filename);
312 if (!file) 312 if (!file)
313 break; 313 break;
314 while (fgets(buf, sizeof(buf), file)) { 314 while (fgets(buf, sizeof(buf), file)) {
diff --git a/libbb/rtc.c b/libbb/rtc.c
index 1fdeee80f..222d977ca 100644
--- a/libbb/rtc.c
+++ b/libbb/rtc.c
@@ -14,7 +14,7 @@
14int FAST_FUNC rtc_adjtime_is_utc(void) 14int FAST_FUNC rtc_adjtime_is_utc(void)
15{ 15{
16 int utc = 0; 16 int utc = 0;
17 FILE *f = fopen(ADJTIME_PATH, "r"); 17 FILE *f = fopen_for_read(ADJTIME_PATH);
18 18
19 if (f) { 19 if (f) {
20 RESERVE_CONFIG_BUFFER(buffer, 128); 20 RESERVE_CONFIG_BUFFER(buffer, 128);
diff --git a/libbb/wfopen.c b/libbb/wfopen.c
index ac365c2a0..4c84b3ba9 100644
--- a/libbb/wfopen.c
+++ b/libbb/wfopen.c
@@ -18,3 +18,23 @@ FILE* FAST_FUNC fopen_or_warn(const char *path, const char *mode)
18 } 18 }
19 return fp; 19 return fp;
20} 20}
21
22FILE* FAST_FUNC fopen_for_read(const char *path)
23{
24 return fopen(path, "r");
25}
26
27FILE* FAST_FUNC xfopen_for_read(const char *path)
28{
29 return xfopen(path, "r");
30}
31
32FILE* FAST_FUNC fopen_for_write(const char *path)
33{
34 return fopen(path, "w");
35}
36
37FILE* FAST_FUNC xfopen_for_write(const char *path)
38{
39 return xfopen(path, "w");
40}
diff --git a/libpwdgrp/pwd_grp.c b/libpwdgrp/pwd_grp.c
index 867caf096..206519084 100644
--- a/libpwdgrp/pwd_grp.c
+++ b/libpwdgrp/pwd_grp.c
@@ -464,7 +464,7 @@ int getpwent_r(struct passwd *__restrict resultbuf,
464 *result = NULL; /* In case of error... */ 464 *result = NULL; /* In case of error... */
465 465
466 if (!pwf) { 466 if (!pwf) {
467 pwf = fopen(_PATH_PASSWD, "r"); 467 pwf = fopen_for_read(_PATH_PASSWD);
468 if (!pwf) { 468 if (!pwf) {
469 rv = errno; 469 rv = errno;
470 goto ERR; 470 goto ERR;
@@ -511,7 +511,7 @@ int getgrent_r(struct group *__restrict resultbuf,
511 *result = NULL; /* In case of error... */ 511 *result = NULL; /* In case of error... */
512 512
513 if (!grf) { 513 if (!grf) {
514 grf = fopen(_PATH_GROUP, "r"); 514 grf = fopen_for_read(_PATH_GROUP);
515 if (!grf) { 515 if (!grf) {
516 rv = errno; 516 rv = errno;
517 goto ERR; 517 goto ERR;
@@ -558,7 +558,7 @@ int getspent_r(struct spwd *resultbuf, char *buffer,
558 *result = NULL; /* In case of error... */ 558 *result = NULL; /* In case of error... */
559 559
560 if (!spf) { 560 if (!spf) {
561 spf = fopen(_PATH_SHADOW, "r"); 561 spf = fopen_for_read(_PATH_SHADOW);
562 if (!spf) { 562 if (!spf) {
563 rv = errno; 563 rv = errno;
564 goto ERR; 564 goto ERR;
@@ -630,7 +630,7 @@ int initgroups(const char *user, gid_t gid)
630 char buff[PWD_BUFFER_SIZE]; 630 char buff[PWD_BUFFER_SIZE];
631 631
632 rv = -1; 632 rv = -1;
633 grfile = fopen(_PATH_GROUP, "r"); 633 grfile = fopen_for_read(_PATH_GROUP);
634 if (grfile != NULL) { 634 if (grfile != NULL) {
635 635
636 /* We alloc space for 8 gids at a time. */ 636 /* We alloc space for 8 gids at a time. */
diff --git a/libpwdgrp/pwd_grp_internal.c b/libpwdgrp/pwd_grp_internal.c
index d55edc349..ffdc85e4e 100644
--- a/libpwdgrp/pwd_grp_internal.c
+++ b/libpwdgrp/pwd_grp_internal.c
@@ -32,7 +32,7 @@ int GETXXKEY_R_FUNC(GETXXKEY_R_KEYTYPE key,
32 32
33 *result = NULL; 33 *result = NULL;
34 34
35 stream = fopen(GETXXKEY_R_PATHNAME, "r"); 35 stream = fopen_for_read(GETXXKEY_R_PATHNAME);
36 if (!stream) 36 if (!stream)
37 return errno; 37 return errno;
38 while (1) { 38 while (1) {
diff --git a/loginutils/getty.c b/loginutils/getty.c
index 7d4fe4df9..358a45c7c 100644
--- a/loginutils/getty.c
+++ b/loginutils/getty.c
@@ -668,7 +668,7 @@ int getty_main(int argc UNUSED_PARAM, char **argv)
668 logmode = LOGMODE_BOTH; 668 logmode = LOGMODE_BOTH;
669 669
670#ifdef DEBUGGING 670#ifdef DEBUGGING
671 dbf = xfopen(DEBUGTERM, "w"); 671 dbf = xfopen_for_write(DEBUGTERM);
672 for (n = 1; argv[n]; n++) { 672 for (n = 1; argv[n]; n++) {
673 debug(argv[n]); 673 debug(argv[n]);
674 debug("\n"); 674 debug("\n");
diff --git a/loginutils/login.c b/loginutils/login.c
index 9c7941e0d..5a75ed218 100644
--- a/loginutils/login.c
+++ b/loginutils/login.c
@@ -122,7 +122,7 @@ static void die_if_nologin(void)
122 if (access("/etc/nologin", F_OK)) 122 if (access("/etc/nologin", F_OK))
123 return; 123 return;
124 124
125 fp = fopen("/etc/nologin", "r"); 125 fp = fopen_for_read("/etc/nologin");
126 if (fp) { 126 if (fp) {
127 while ((c = getc(fp)) != EOF) 127 while ((c = getc(fp)) != EOF)
128 bb_putchar((c=='\n') ? '\r' : c); 128 bb_putchar((c=='\n') ? '\r' : c);
@@ -139,30 +139,20 @@ static ALWAYS_INLINE void die_if_nologin(void) {}
139#if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM 139#if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM
140static int check_securetty(void) 140static int check_securetty(void)
141{ 141{
142 FILE *fp; 142 char *buf;
143 int i; 143 int ret = 1;
144 char buf[256]; 144 parser_t *parser = config_open2("/etc/securetty", fopen_for_read);
145 145 /* N.B. A missing securetty file is not an error. */
146 fp = fopen("/etc/securetty", "r"); 146 if (parser) {
147 if (!fp) { 147 while (config_read(parser, &buf, 1, 1, "# \t", 0)) {
148 /* A missing securetty file is not an error. */ 148 if (strcmp(buf, short_tty) == 0)
149 return 1;
150 }
151 while (fgets(buf, sizeof(buf)-1, fp)) {
152 for (i = strlen(buf)-1; i >= 0; --i) {
153 if (!isspace(buf[i]))
154 break; 149 break;
155 } 150 }
156 buf[++i] = '\0'; 151 config_close(parser);
157 if (!buf[0] || (buf[0] == '#')) 152 // buf != NULL here iff config file was empty (OK) or buf equals short_tty (OK)
158 continue; 153 ret = buf != NULL;
159 if (strcmp(buf, short_tty) == 0) {
160 fclose(fp);
161 return 1;
162 }
163 } 154 }
164 fclose(fp); 155 return ret;
165 return 0;
166} 156}
167#else 157#else
168static ALWAYS_INLINE int check_securetty(void) { return 1; } 158static ALWAYS_INLINE int check_securetty(void) { return 1; }
diff --git a/miscutils/crond.c b/miscutils/crond.c
index c7ee793a5..b3a06a376 100644
--- a/miscutils/crond.c
+++ b/miscutils/crond.c
@@ -529,7 +529,7 @@ static void CheckUpdates(void)
529 FILE *fi; 529 FILE *fi;
530 char buf[256]; 530 char buf[256];
531 531
532 fi = fopen(CRONUPDATE, "r"); 532 fi = fopen_for_read(CRONUPDATE);
533 if (fi != NULL) { 533 if (fi != NULL) {
534 unlink(CRONUPDATE); 534 unlink(CRONUPDATE);
535 while (fgets(buf, sizeof(buf), fi) != NULL) { 535 while (fgets(buf, sizeof(buf), fi) != NULL) {
diff --git a/miscutils/devfsd.c b/miscutils/devfsd.c
index c85ff08f3..61b97dce4 100644
--- a/miscutils/devfsd.c
+++ b/miscutils/devfsd.c
@@ -459,7 +459,7 @@ static void read_config_file(char *path, int optional, unsigned long *event_mask
459 free(p); 459 free(p);
460 return; 460 return;
461 } 461 }
462 fp = fopen(path, "r"); 462 fp = fopen_for_read(path);
463 if (fp != NULL) { 463 if (fp != NULL) {
464 while (fgets(buf, STRING_LENGTH, fp) != NULL) { 464 while (fgets(buf, STRING_LENGTH, fp) != NULL) {
465 /* Skip whitespace */ 465 /* Skip whitespace */
diff --git a/miscutils/fbsplash.c b/miscutils/fbsplash.c
index 67847c1eb..380f09bea 100644
--- a/miscutils/fbsplash.c
+++ b/miscutils/fbsplash.c
@@ -341,7 +341,7 @@ static void init(const char *cfg_filename)
341 case 7: 341 case 7:
342 G.bdebug_messages = val; 342 G.bdebug_messages = val;
343 if (G.bdebug_messages) 343 if (G.bdebug_messages)
344 G.logfile_fd = xfopen("/tmp/fbsplash.log", "w"); 344 G.logfile_fd = xfopen_for_write("/tmp/fbsplash.log");
345 break; 345 break;
346#endif 346#endif
347 err: 347 err:
diff --git a/miscutils/less.c b/miscutils/less.c
index 1e22d333d..530a40a8c 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -1129,7 +1129,7 @@ static void save_input_to_file(void)
1129 print_statusline("Log file: "); 1129 print_statusline("Log file: ");
1130 current_line = less_gets(sizeof("Log file: ")-1); 1130 current_line = less_gets(sizeof("Log file: ")-1);
1131 if (current_line[0]) { 1131 if (current_line[0]) {
1132 fp = fopen(current_line, "w"); 1132 fp = fopen_for_write(current_line);
1133 if (!fp) { 1133 if (!fp) {
1134 msg = "Error opening log file"; 1134 msg = "Error opening log file";
1135 goto ret; 1135 goto ret;
diff --git a/miscutils/makedevs.c b/miscutils/makedevs.c
index 3b45d70b4..ed08f7ece 100644
--- a/miscutils/makedevs.c
+++ b/miscutils/makedevs.c
@@ -80,7 +80,7 @@ int makedevs_main(int argc, char **argv)
80 80
81 getopt32(argv, "d:", &line); 81 getopt32(argv, "d:", &line);
82 if (line) 82 if (line)
83 table = xfopen(line, "r"); 83 table = xfopen_for_read(line);
84 84
85 if (optind >= argc || (rootdir=argv[optind])==NULL) { 85 if (optind >= argc || (rootdir=argv[optind])==NULL) {
86 bb_error_msg_and_die("root directory not specified"); 86 bb_error_msg_and_die("root directory not specified");
diff --git a/miscutils/man.c b/miscutils/man.c
index 7ef5941a1..df00c3ee7 100644
--- a/miscutils/man.c
+++ b/miscutils/man.c
@@ -106,9 +106,7 @@ int man_main(int argc UNUSED_PARAM, char **argv)
106 if (parser) { 106 if (parser) {
107 /* go through man configuration file and search relevant paths, sections */ 107 /* go through man configuration file and search relevant paths, sections */
108 char *token[2]; 108 char *token[2];
109 while (config_read(parser, token, 2, 0, "# \t", PARSE_LAST_IS_GREEDY)) { 109 while (config_read(parser, token, 2, 2, "# \t", PARSE_LAST_IS_GREEDY)) {
110 if (!token[1])
111 continue;
112 if (strcmp("MANPATH", token[0]) == 0) { 110 if (strcmp("MANPATH", token[0]) == 0) {
113 man_path_list[count_mp] = xstrdup(token[1]); 111 man_path_list[count_mp] = xstrdup(token[1]);
114 count_mp++; 112 count_mp++;
diff --git a/modutils/depmod.c b/modutils/depmod.c
index d128ac0ff..b6a914eb0 100644
--- a/modutils/depmod.c
+++ b/modutils/depmod.c
@@ -150,7 +150,7 @@ int depmod_main(int argc UNUSED_PARAM, char **argv)
150 150
151 if (!(option_mask32 & ARG_n)) { /* --dry-run */ 151 if (!(option_mask32 & ARG_n)) { /* --dry-run */
152 chp = concat_path_file(moddir, CONFIG_DEFAULT_DEPMOD_FILE); 152 chp = concat_path_file(moddir, CONFIG_DEFAULT_DEPMOD_FILE);
153 filedes = xfopen(chp, "w"); 153 filedes = xfopen_for_write(chp);
154 if (ENABLE_FEATURE_CLEAN_UP) 154 if (ENABLE_FEATURE_CLEAN_UP)
155 free(chp); 155 free(chp);
156 } 156 }
diff --git a/modutils/insmod.c b/modutils/insmod.c
index 97f4a87e2..9dcc5b02d 100644
--- a/modutils/insmod.c
+++ b/modutils/insmod.c
@@ -3992,7 +3992,7 @@ int insmod_main(int argc, char **argv)
3992 3992
3993 /* Get a filedesc for the module. Check that we have a complete path */ 3993 /* Get a filedesc for the module. Check that we have a complete path */
3994 if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode) 3994 if (stat(arg1, &st) < 0 || !S_ISREG(st.st_mode)
3995 || (fp = fopen(arg1, "r")) == NULL 3995 || (fp = fopen_for_read(arg1)) == NULL
3996 ) { 3996 ) {
3997 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`, 3997 /* Hmm. Could not open it. First search under /lib/modules/`uname -r`,
3998 * but do not error out yet if we fail to find it... */ 3998 * but do not error out yet if we fail to find it... */
@@ -4016,7 +4016,7 @@ int insmod_main(int argc, char **argv)
4016 } 4016 }
4017 4017
4018 /* Check if we have found anything yet */ 4018 /* Check if we have found anything yet */
4019 if (!m_filename || ((fp = fopen(m_filename, "r")) == NULL)) { 4019 if (!m_filename || ((fp = fopen_for_read(m_filename)) == NULL)) {
4020 int r; 4020 int r;
4021 char *module_dir; 4021 char *module_dir;
4022 4022
@@ -4033,7 +4033,7 @@ int insmod_main(int argc, char **argv)
4033 bb_error_msg_and_die("%s: module not found", m_fullName); 4033 bb_error_msg_and_die("%s: module not found", m_fullName);
4034 free(module_dir); 4034 free(module_dir);
4035 if (m_filename == NULL 4035 if (m_filename == NULL
4036 || ((fp = fopen(m_filename, "r")) == NULL) 4036 || ((fp = fopen_for_read(m_filename)) == NULL)
4037 ) { 4037 ) {
4038 bb_error_msg_and_die("%s: module not found", m_fullName); 4038 bb_error_msg_and_die("%s: module not found", m_fullName);
4039 } 4039 }
diff --git a/modutils/lsmod.c b/modutils/lsmod.c
index 5e37923a9..3f237039e 100644
--- a/modutils/lsmod.c
+++ b/modutils/lsmod.c
@@ -28,7 +28,7 @@ static void check_tainted(void)
28 FILE *f; 28 FILE *f;
29 29
30 tainted = 0; 30 tainted = 0;
31 f = fopen(TAINT_FILENAME, "r"); 31 f = fopen_for_read(TAINT_FILENAME);
32 if (f) { 32 if (f) {
33 fscanf(f, "%d", &tainted); 33 fscanf(f, "%d", &tainted);
34 fclose(f); 34 fclose(f);
@@ -145,7 +145,7 @@ int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
145int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 145int lsmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
146int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) 146int lsmod_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
147{ 147{
148 FILE *file = xfopen("/proc/modules", "r"); 148 FILE *file = xfopen_for_read("/proc/modules");
149 149
150 printf("Module Size Used by"); 150 printf("Module Size Used by");
151 check_tainted(); 151 check_tainted();
diff --git a/modutils/modprobe-small.c b/modutils/modprobe-small.c
index ae7c29cd1..f75dae8a3 100644
--- a/modutils/modprobe-small.c
+++ b/modutils/modprobe-small.c
@@ -299,7 +299,7 @@ static FAST_FUNC int fileAction(const char *pathname,
299static int load_dep_bb(void) 299static int load_dep_bb(void)
300{ 300{
301 char *line; 301 char *line;
302 FILE *fp = fopen(DEPFILE_BB, "r"); 302 FILE *fp = fopen_for_read(DEPFILE_BB);
303 303
304 if (!fp) 304 if (!fp)
305 return 0; 305 return 0;
@@ -492,7 +492,7 @@ static int already_loaded(const char *name)
492 char *line; 492 char *line;
493 FILE* modules; 493 FILE* modules;
494 494
495 modules = xfopen("/proc/modules", "r"); 495 modules = xfopen_for_read("/proc/modules");
496 while ((line = xmalloc_fgets(modules)) != NULL) { 496 while ((line = xmalloc_fgets(modules)) != NULL) {
497 if (strncmp(line, name, len) == 0 && line[len] == ' ') { 497 if (strncmp(line, name, len) == 0 && line[len] == ' ') {
498 free(line); 498 free(line);
@@ -688,7 +688,7 @@ int modprobe_main(int argc UNUSED_PARAM, char **argv)
688 688
689 /* are we lsmod? -> just dump /proc/modules */ 689 /* are we lsmod? -> just dump /proc/modules */
690 if ('l' == applet0) { 690 if ('l' == applet0) {
691 xprint_and_close_file(xfopen("/proc/modules", "r")); 691 xprint_and_close_file(xfopen_for_read("/proc/modules"));
692 return EXIT_SUCCESS; 692 return EXIT_SUCCESS;
693 } 693 }
694 694
diff --git a/networking/arp.c b/networking/arp.c
index ac8c870f8..620f7c00f 100644
--- a/networking/arp.c
+++ b/networking/arp.c
@@ -382,7 +382,7 @@ static int arp_show(char *name)
382 } 382 }
383 host = xstrdup(ap->sprint(&sa, 1)); 383 host = xstrdup(ap->sprint(&sa, 1));
384 } 384 }
385 fp = xfopen("/proc/net/arp", "r"); 385 fp = xfopen_for_read("/proc/net/arp");
386 /* Bypass header -- read one line */ 386 /* Bypass header -- read one line */
387 fgets(line, sizeof(line), fp); 387 fgets(line, sizeof(line), fp);
388 388
diff --git a/networking/dnsd.c b/networking/dnsd.c
index 0047a8445..efb5cfba7 100644
--- a/networking/dnsd.c
+++ b/networking/dnsd.c
@@ -113,7 +113,7 @@ static void dnsentryinit(void)
113 parser = config_open(fileconf); 113 parser = config_open(fileconf);
114 if (parser) { 114 if (parser) {
115 char *token[2]; 115 char *token[2];
116 while (config_read(parser, token, 2, 0, "# \t", 0)) { 116 while (config_read(parser, token, 2, 2, "# \t", 0)) {
117 unsigned int a,b,c,d; 117 unsigned int a,b,c,d;
118 /* 118 /*
119 * Assumes all host names are lower case only 119 * Assumes all host names are lower case only
@@ -121,7 +121,8 @@ static void dnsentryinit(void)
121 * Presently the dot is copied into name without 121 * Presently the dot is copied into name without
122 * converting to a length/string substring for that label. 122 * converting to a length/string substring for that label.
123 */ 123 */
124 if (!token[1] || sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4) 124// if (!token[1] || sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
125 if (sscanf(token[1], ".%u.%u.%u.%u"+1, &a, &b, &c, &d) != 4)
125 continue; 126 continue;
126 127
127 m = xzalloc(sizeof(*m)); 128 m = xzalloc(sizeof(*m));
diff --git a/networking/hostname.c b/networking/hostname.c
index 93cbc961f..dd2a20689 100644
--- a/networking/hostname.c
+++ b/networking/hostname.c
@@ -16,28 +16,19 @@
16 16
17static void do_sethostname(char *s, int isfile) 17static void do_sethostname(char *s, int isfile)
18{ 18{
19 FILE *f;
20
21 if (!s) 19 if (!s)
22 return; 20 return;
23 if (!isfile) { 21 if (isfile) {
24 if (sethostname(s, strlen(s)) < 0) { 22 parser_t *parser = config_open2(s, xfopen_for_read);
25 if (errno == EPERM) 23 while (config_read(parser, &s, 1, 1, "# \t", 0)) {
26 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); 24 do_sethostname(s, 0);
27 bb_perror_msg_and_die("sethostname");
28 }
29 } else {
30 f = xfopen(s, "r");
31#define strbuf bb_common_bufsiz1
32 while (fgets(strbuf, sizeof(strbuf), f) != NULL) {
33 if (strbuf[0] == '#') {
34 continue;
35 }
36 chomp(strbuf);
37 do_sethostname(strbuf, 0);
38 } 25 }
39 if (ENABLE_FEATURE_CLEAN_UP) 26 if (ENABLE_FEATURE_CLEAN_UP)
40 fclose(f); 27 config_close(parser);
28 } else if (sethostname(s, strlen(s)) < 0) {
29 if (errno == EPERM)
30 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
31 bb_perror_msg_and_die("sethostname");
41 } 32 }
42} 33}
43 34
@@ -98,5 +89,5 @@ int hostname_main(int argc, char **argv)
98 } 89 }
99 if (ENABLE_FEATURE_CLEAN_UP) 90 if (ENABLE_FEATURE_CLEAN_UP)
100 free(buf); 91 free(buf);
101 return 0; 92 return EXIT_SUCCESS;
102} 93}
diff --git a/networking/httpd.c b/networking/httpd.c
index 8c4242e44..82891f121 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -518,7 +518,7 @@ static void parse_conf(const char *path, int flag)
518 sprintf((char *)filename, "%s/%s", path, httpd_conf); 518 sprintf((char *)filename, "%s/%s", path, httpd_conf);
519 } 519 }
520 520
521 while ((f = fopen(filename, "r")) == NULL) { 521 while ((f = fopen_for_read(filename)) == NULL) {
522 if (flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) { 522 if (flag == SUBDIR_PARSE || flag == FIND_FROM_HTTPD_ROOT) {
523 /* config file not found, no changes to config */ 523 /* config file not found, no changes to config */
524 return; 524 return;
diff --git a/networking/ifupdown.c b/networking/ifupdown.c
index cb937cac4..c0e9e812e 100644
--- a/networking/ifupdown.c
+++ b/networking/ifupdown.c
@@ -692,7 +692,7 @@ static struct interfaces_file_t *read_interfaces(const char *filename)
692 enum { NONE, IFACE, MAPPING } currently_processing = NONE; 692 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
693 693
694 defn = xzalloc(sizeof(*defn)); 694 defn = xzalloc(sizeof(*defn));
695 f = xfopen(filename, "r"); 695 f = xfopen_for_read(filename);
696 696
697 while ((buf = xmalloc_fgetline(f)) != NULL) { 697 while ((buf = xmalloc_fgetline(f)) != NULL) {
698#if ENABLE_DESKTOP 698#if ENABLE_DESKTOP
@@ -1090,7 +1090,7 @@ static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1090static llist_t *read_iface_state(void) 1090static llist_t *read_iface_state(void)
1091{ 1091{
1092 llist_t *state_list = NULL; 1092 llist_t *state_list = NULL;
1093 FILE *state_fp = fopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "r"); 1093 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
1094 1094
1095 if (state_fp) { 1095 if (state_fp) {
1096 char *start, *end_ptr; 1096 char *start, *end_ptr;
@@ -1256,7 +1256,7 @@ int ifupdown_main(int argc, char **argv)
1256 } 1256 }
1257 1257
1258 /* Actually write the new state */ 1258 /* Actually write the new state */
1259 state_fp = xfopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "w"); 1259 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
1260 state = state_list; 1260 state = state_list;
1261 while (state) { 1261 while (state) {
1262 if (state->data) { 1262 if (state->data) {
diff --git a/networking/interface.c b/networking/interface.c
index 83b24f1d6..065b4baeb 100644
--- a/networking/interface.c
+++ b/networking/interface.c
@@ -990,7 +990,7 @@ static void ife_print(struct interface *ptr)
990#define IPV6_ADDR_MAPPED 0x1000U 990#define IPV6_ADDR_MAPPED 0x1000U
991#define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */ 991#define IPV6_ADDR_RESERVED 0x2000U /* reserved address space */
992 992
993 f = fopen(_PATH_PROCNET_IFINET6, "r"); 993 f = fopen_for_read(_PATH_PROCNET_IFINET6);
994 if (f != NULL) { 994 if (f != NULL) {
995 while (fscanf 995 while (fscanf
996 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n", 996 (f, "%4s%4s%4s%4s%4s%4s%4s%4s %08x %02x %02x %02x %20s\n",
diff --git a/networking/libiproute/iproute.c b/networking/libiproute/iproute.c
index 17af41f9c..bdccad69d 100644
--- a/networking/libiproute/iproute.c
+++ b/networking/libiproute/iproute.c
@@ -64,7 +64,7 @@ static unsigned get_hz(void)
64 if (hz_internal) 64 if (hz_internal)
65 return hz_internal; 65 return hz_internal;
66 66
67 fp = fopen("/proc/net/psched", "r"); 67 fp = fopen_for_read("/proc/net/psched");
68 if (fp) { 68 if (fp) {
69 unsigned nom, denom; 69 unsigned nom, denom;
70 70
diff --git a/networking/libiproute/rt_names.c b/networking/libiproute/rt_names.c
index 797c83b4e..b22df9cb7 100644
--- a/networking/libiproute/rt_names.c
+++ b/networking/libiproute/rt_names.c
@@ -18,7 +18,7 @@ static void rtnl_tab_initialize(const char *file, const char **tab, int size)
18 char buf[512]; 18 char buf[512];
19 FILE *fp; 19 FILE *fp;
20 20
21 fp = fopen(file, "r"); 21 fp = fopen_for_read(file);
22 if (!fp) 22 if (!fp)
23 return; 23 return;
24 while (fgets(buf, sizeof(buf), fp)) { 24 while (fgets(buf, sizeof(buf), fp)) {
diff --git a/networking/nameif.c b/networking/nameif.c
index 76a8cb7df..5a3bd606f 100644
--- a/networking/nameif.c
+++ b/networking/nameif.c
@@ -170,7 +170,7 @@ int nameif_main(int argc, char **argv)
170 } 170 }
171 171
172 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0); 172 ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
173 ifh = xfopen("/proc/net/dev", "r"); 173 ifh = xfopen_for_read("/proc/net/dev");
174 174
175 linenum = 0; 175 linenum = 0;
176 while (clist) { 176 while (clist) {
diff --git a/networking/netstat.c b/networking/netstat.c
index 24b26545e..46510acc1 100644
--- a/networking/netstat.c
+++ b/networking/netstat.c
@@ -466,7 +466,7 @@ static void do_info(const char *file, const char *name, int (*proc)(int, char *)
466 FILE *procinfo; 466 FILE *procinfo;
467 char *buffer; 467 char *buffer;
468 468
469 procinfo = fopen(file, "r"); 469 procinfo = fopen_for_read(file);
470 if (procinfo == NULL) { 470 if (procinfo == NULL) {
471 if (errno != ENOENT) { 471 if (errno != ENOENT) {
472 bb_simple_perror_msg(file); 472 bb_simple_perror_msg(file);
diff --git a/networking/route.c b/networking/route.c
index 7b6d4f45f..2bc2f9210 100644
--- a/networking/route.c
+++ b/networking/route.c
@@ -484,7 +484,7 @@ void FAST_FUNC bb_displayroutes(int noresolve, int netstatfmt)
484 struct sockaddr_in s_addr; 484 struct sockaddr_in s_addr;
485 struct in_addr mask; 485 struct in_addr mask;
486 486
487 FILE *fp = xfopen("/proc/net/route", "r"); 487 FILE *fp = xfopen_for_read("/proc/net/route");
488 488
489 printf("Kernel IP routing table\n" 489 printf("Kernel IP routing table\n"
490 "Destination Gateway Genmask Flags %s Iface\n", 490 "Destination Gateway Genmask Flags %s Iface\n",
@@ -552,7 +552,7 @@ static void INET6_displayroutes(void)
552 int iflags, metric, refcnt, use, prefix_len, slen; 552 int iflags, metric, refcnt, use, prefix_len, slen;
553 struct sockaddr_in6 snaddr6; 553 struct sockaddr_in6 snaddr6;
554 554
555 FILE *fp = xfopen("/proc/net/ipv6_route", "r"); 555 FILE *fp = xfopen_for_read("/proc/net/ipv6_route");
556 556
557 printf("Kernel IPv6 routing table\n%-44s%-40s" 557 printf("Kernel IPv6 routing table\n%-44s%-40s"
558 "Flags Metric Ref Use Iface\n", 558 "Flags Metric Ref Use Iface\n",
diff --git a/networking/traceroute.c b/networking/traceroute.c
index f16fc792f..4e6ca2d9b 100644
--- a/networking/traceroute.c
+++ b/networking/traceroute.c
@@ -508,7 +508,7 @@ findsaddr(const struct sockaddr_in *to, struct sockaddr_in *from)
508 struct IFADDRLIST *al; 508 struct IFADDRLIST *al;
509 char buf[256], tdevice[256], device[256]; 509 char buf[256], tdevice[256], device[256];
510 510
511 f = xfopen("/proc/net/route", "r"); 511 f = xfopen_for_read("/proc/net/route");
512 512
513 /* Find the appropriate interface */ 513 /* Find the appropriate interface */
514 n = 0; 514 n = 0;
diff --git a/networking/udhcp/files.c b/networking/udhcp/files.c
index fe6bff4ba..264a98899 100644
--- a/networking/udhcp/files.c
+++ b/networking/udhcp/files.c
@@ -321,9 +321,7 @@ void read_config(const char *file)
321 if (!parser) 321 if (!parser)
322 return; 322 return;
323 323
324 while (config_read(parser, token, 2, 0, "# \t", PARSE_LAST_IS_GREEDY)) { 324 while (config_read(parser, token, 2, 2, "# \t", PARSE_LAST_IS_GREEDY)) {
325 if (!token[1])
326 continue;
327 for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) { 325 for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
328 if (!strcasecmp(token[0], k->keyword)) { 326 if (!strcasecmp(token[0], k->keyword)) {
329 if (!k->handler(token[1], k->var)) { 327 if (!k->handler(token[1], k->var)) {
diff --git a/procps/fuser.c b/procps/fuser.c
index ca7c7e267..d2ac9ebfc 100644
--- a/procps/fuser.c
+++ b/procps/fuser.c
@@ -109,7 +109,7 @@ static inode_list *scan_proc_net(const char *proto,
109 tmp_dev = find_socket_dev(); 109 tmp_dev = find_socket_dev();
110 110
111 sprintf(path, "/proc/net/%s", proto); 111 sprintf(path, "/proc/net/%s", proto);
112 f = fopen(path, "r"); 112 f = fopen_for_read(path);
113 if (!f) 113 if (!f)
114 return ilist; 114 return ilist;
115 115
@@ -158,7 +158,7 @@ static pid_list *scan_pid_maps(const char *fname, pid_t pid,
158 long long uint64_inode; 158 long long uint64_inode;
159 dev_t dev; 159 dev_t dev;
160 160
161 file = fopen(fname, "r"); 161 file = fopen_for_read(fname);
162 if (!file) 162 if (!file)
163 return plist; 163 return plist;
164 while (fgets(line, MAX_LINE, file)) { 164 while (fgets(line, MAX_LINE, file)) {
diff --git a/procps/sysctl.c b/procps/sysctl.c
index 6e582b0f9..3fe6f6f3e 100644
--- a/procps/sysctl.c
+++ b/procps/sysctl.c
@@ -98,10 +98,11 @@ static int sysctl_preload_file_and_exit(const char *filename)
98 if (!parser) 98 if (!parser)
99 return 1; 99 return 1;
100 100
101 while (config_read(parser, token, 2, 0, "# \t=", PARSE_LAST_IS_GREEDY)) { // TODO: ';' is comment char too 101 while (config_read(parser, token, 2, 2, "# \t=", PARSE_LAST_IS_GREEDY)) { // TODO: ';' is comment char too
102 if (!token[1]) { 102// if (!token[1]) {
103 bb_error_msg(WARN_BAD_LINE, filename, parser->lineno); 103// bb_error_msg(WARN_BAD_LINE, filename, parser->lineno);
104 } else { 104// } else {
105 {
105#if 0 106#if 0
106 char *s = xasprintf("%s=%s", token[0], token[1]); 107 char *s = xasprintf("%s=%s", token[0], token[1]);
107 sysctl_write_setting(s); 108 sysctl_write_setting(s);
@@ -205,7 +206,7 @@ static int sysctl_read_setting(const char *name)
205 while ((cptr = strchr(outname, '/')) != NULL) 206 while ((cptr = strchr(outname, '/')) != NULL)
206 *cptr = '.'; 207 *cptr = '.';
207 208
208 fp = fopen(tmpname, "r"); 209 fp = fopen_for_read(tmpname);
209 if (fp == NULL) { 210 if (fp == NULL) {
210 switch (errno) { 211 switch (errno) {
211 case ENOENT: 212 case ENOENT:
diff --git a/procps/top.c b/procps/top.c
index 392a3c82b..1a6b8abb2 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -164,7 +164,7 @@ static int mult_lvl_cmp(void* a, void* b)
164 164
165static void get_jiffy_counts(void) 165static void get_jiffy_counts(void)
166{ 166{
167 FILE* fp = xfopen("stat", "r"); 167 FILE* fp = xfopen_for_read("stat");
168 prev_jif = jif; 168 prev_jif = jif;
169 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld", 169 if (fscanf(fp, "cpu %lld %lld %lld %lld %lld %lld %lld %lld",
170 &jif.usr,&jif.nic,&jif.sys,&jif.idle, 170 &jif.usr,&jif.nic,&jif.sys,&jif.idle,
@@ -268,7 +268,7 @@ static unsigned long display_header(int scr_width)
268#endif 268#endif
269 269
270 /* read memory info */ 270 /* read memory info */
271 fp = xfopen("meminfo", "r"); 271 fp = xfopen_for_read("meminfo");
272 272
273 /* 273 /*
274 * Old kernels (such as 2.4.x) had a nice summary of memory info that 274 * Old kernels (such as 2.4.x) had a nice summary of memory info that
@@ -617,7 +617,7 @@ static void display_topmem_header(int scr_width)
617 memset(&Z, 0, sizeof(Z)); 617 memset(&Z, 0, sizeof(Z));
618 618
619 /* read memory info */ 619 /* read memory info */
620 fp = xfopen("meminfo", "r"); 620 fp = xfopen_for_read("meminfo");
621 while (fgets(linebuf, sizeof(linebuf), fp)) { 621 while (fgets(linebuf, sizeof(linebuf), fp)) {
622 char *p; 622 char *p;
623 623
diff --git a/scripts/basic/split-include.c b/scripts/basic/split-include.c
index 459c45276..60934b5b1 100644
--- a/scripts/basic/split-include.c
+++ b/scripts/basic/split-include.c
@@ -154,7 +154,7 @@ int main(int argc, const char * argv [])
154 } 154 }
155 155
156 /* Write the file. */ 156 /* Write the file. */
157 if ((fp_target = fopen(ptarget, "w" )) == NULL) 157 if ((fp_target = fopen(ptarget, "w")) == NULL)
158 ERROR_EXIT(ptarget); 158 ERROR_EXIT(ptarget);
159 fputs(line, fp_target); 159 fputs(line, fp_target);
160 if (ferror(fp_target) || fclose(fp_target) != 0) 160 if (ferror(fp_target) || fclose(fp_target) != 0)
diff --git a/selinux/setfiles.c b/selinux/setfiles.c
index d567f83aa..8eb04e635 100644
--- a/selinux/setfiles.c
+++ b/selinux/setfiles.c
@@ -554,7 +554,7 @@ int setfiles_main(int argc, char **argv)
554 if ((applet_name[0] == 's') && (flags & OPT_c)) { 554 if ((applet_name[0] == 's') && (flags & OPT_c)) {
555 FILE *policystream; 555 FILE *policystream;
556 556
557 policystream = xfopen(policyfile, "r"); 557 policystream = xfopen_for_read(policyfile);
558 if (sepol_set_policydb_from_file(policystream) < 0) { 558 if (sepol_set_policydb_from_file(policystream) < 0) {
559 bb_error_msg_and_die("sepol_set_policydb_from_file on %s", policyfile); 559 bb_error_msg_and_die("sepol_set_policydb_from_file on %s", policyfile);
560 } 560 }
@@ -575,7 +575,7 @@ int setfiles_main(int argc, char **argv)
575 if (flags & OPT_o) { 575 if (flags & OPT_o) {
576 outfile = stdout; 576 outfile = stdout;
577 if (NOT_LONE_CHAR(out_filename, '-')) { 577 if (NOT_LONE_CHAR(out_filename, '-')) {
578 outfile = xfopen(out_filename, "w"); 578 outfile = xfopen_for_write(out_filename);
579 } 579 }
580 } 580 }
581 if (applet_name[0] == 'r') { /* restorecon */ 581 if (applet_name[0] == 'r') { /* restorecon */
@@ -620,7 +620,7 @@ int setfiles_main(int argc, char **argv)
620 FILE *f = stdin; 620 FILE *f = stdin;
621 621
622 if (NOT_LONE_CHAR(input_filename, '-')) 622 if (NOT_LONE_CHAR(input_filename, '-'))
623 f = xfopen(input_filename, "r"); 623 f = xfopen_for_read(input_filename);
624 while ((len = getline(&buf, &buf_len, f)) > 0) { 624 while ((len = getline(&buf, &buf_len, f)) > 0) {
625 buf[len - 1] = '\0'; 625 buf[len - 1] = '\0';
626 errors |= process_one(buf); 626 errors |= process_one(buf);
diff --git a/shell/bbsh.c b/shell/bbsh.c
index 3be64eec7..897c0227c 100644
--- a/shell/bbsh.c
+++ b/shell/bbsh.c
@@ -206,7 +206,7 @@ int bbsh_main(int argc, char **argv)
206 206
207 getopt32(argv, "c:", &command); 207 getopt32(argv, "c:", &command);
208 208
209 f = argv[optind] ? xfopen(argv[optind],"r") : NULL; 209 f = argv[optind] ? xfopen_for_read(argv[optind]) : NULL;
210 if (command) handle(command); 210 if (command) handle(command);
211 else { 211 else {
212 unsigned cmdlen=0; 212 unsigned cmdlen=0;
diff --git a/shell/hush.c b/shell/hush.c
index 3dc27d9c2..cf6a18f86 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -4012,7 +4012,7 @@ int hush_main(int argc, char **argv)
4012 4012
4013 if (argv[0] && argv[0][0] == '-') { 4013 if (argv[0] && argv[0][0] == '-') {
4014 debug_printf("sourcing /etc/profile\n"); 4014 debug_printf("sourcing /etc/profile\n");
4015 input = fopen("/etc/profile", "r"); 4015 input = fopen_for_read("/etc/profile");
4016 if (input != NULL) { 4016 if (input != NULL) {
4017 close_on_exec_on(fileno(input)); 4017 close_on_exec_on(fileno(input));
4018 parse_and_run_file(input); 4018 parse_and_run_file(input);
@@ -4116,7 +4116,7 @@ int hush_main(int argc, char **argv)
4116 debug_printf("\nrunning script '%s'\n", argv[optind]); 4116 debug_printf("\nrunning script '%s'\n", argv[optind]);
4117 global_argv = argv + optind; 4117 global_argv = argv + optind;
4118 global_argc = argc - optind; 4118 global_argc = argc - optind;
4119 input = xfopen(argv[optind], "r"); 4119 input = xfopen_for_read(argv[optind]);
4120 fcntl(fileno(input), F_SETFD, FD_CLOEXEC); 4120 fcntl(fileno(input), F_SETFD, FD_CLOEXEC);
4121 opt = parse_and_run_file(input); 4121 opt = parse_and_run_file(input);
4122 } 4122 }
@@ -4423,7 +4423,7 @@ static int builtin_source(char **argv)
4423 return EXIT_FAILURE; 4423 return EXIT_FAILURE;
4424 4424
4425 /* XXX search through $PATH is missing */ 4425 /* XXX search through $PATH is missing */
4426 input = fopen(argv[1], "r"); 4426 input = fopen_for_read(argv[1]);
4427 if (!input) { 4427 if (!input) {
4428 bb_error_msg("can't open '%s'", argv[1]); 4428 bb_error_msg("can't open '%s'", argv[1]);
4429 return EXIT_FAILURE; 4429 return EXIT_FAILURE;
diff --git a/shell/lash_unused.c b/shell/lash_unused.c
index 708408954..90b1f56cf 100644
--- a/shell/lash_unused.c
+++ b/shell/lash_unused.c
@@ -1513,7 +1513,7 @@ int lash_main(int argc, char **argv)
1513 1513
1514 if (global_argv[0] && global_argv[0][0] == '-') { 1514 if (global_argv[0] && global_argv[0][0] == '-') {
1515 FILE *prof_input; 1515 FILE *prof_input;
1516 prof_input = fopen("/etc/profile", "r"); 1516 prof_input = fopen_for_read("/etc/profile");
1517 if (prof_input) { 1517 if (prof_input) {
1518 llist_add_to(&close_me_list, (void *)(long)fileno(prof_input)); 1518 llist_add_to(&close_me_list, (void *)(long)fileno(prof_input));
1519 /* Now run the file */ 1519 /* Now run the file */
@@ -1553,7 +1553,7 @@ int lash_main(int argc, char **argv)
1553 } 1553 }
1554 } else if (!local_pending_command && global_argv[optind]) { 1554 } else if (!local_pending_command && global_argv[optind]) {
1555 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]); 1555 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
1556 input = xfopen(global_argv[optind], "r"); 1556 input = xfopen_for_read(global_argv[optind]);
1557 /* be lazy, never mark this closed */ 1557 /* be lazy, never mark this closed */
1558 llist_add_to(&close_me_list, (void *)(long)fileno(input)); 1558 llist_add_to(&close_me_list, (void *)(long)fileno(input));
1559 } 1559 }
diff --git a/util-linux/fbset.c b/util-linux/fbset.c
index ab7770d4f..ac45fe830 100644
--- a/util-linux/fbset.c
+++ b/util-linux/fbset.c
@@ -178,7 +178,7 @@ static int readmode(struct fb_var_screeninfo *base, const char *fn,
178 char buf[256]; 178 char buf[256];
179 char *p = buf; 179 char *p = buf;
180 180
181 f = xfopen(fn, "r"); 181 f = xfopen_for_read(fn);
182 while (!feof(f)) { 182 while (!feof(f)) {
183 fgets(buf, sizeof(buf), f); 183 fgets(buf, sizeof(buf), f);
184 p = strstr(buf, "mode "); 184 p = strstr(buf, "mode ");
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index 702567a71..b1f0b65c6 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -2684,7 +2684,7 @@ is_ide_cdrom_or_tape(const char *device)
2684 return 0; 2684 return 0;
2685 2685
2686 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5); 2686 snprintf(buf, sizeof(buf), "/proc/ide/%s/media", device+5);
2687 procf = fopen(buf, "r"); 2687 procf = fopen_for_read(buf);
2688 if (procf != NULL && fgets(buf, sizeof(buf), procf)) 2688 if (procf != NULL && fgets(buf, sizeof(buf), procf))
2689 is_ide = (!strncmp(buf, "cdrom", 5) || 2689 is_ide = (!strncmp(buf, "cdrom", 5) ||
2690 !strncmp(buf, "tape", 4)); 2690 !strncmp(buf, "tape", 4));
diff --git a/util-linux/fdisk_sun.c b/util-linux/fdisk_sun.c
index 427efbee3..d1a436b4a 100644
--- a/util-linux/fdisk_sun.c
+++ b/util-linux/fdisk_sun.c
@@ -181,7 +181,7 @@ sun_autoconfigure_scsi(void)
181 id[0] & 0xff, 181 id[0] & 0xff,
182 (id[0]>>8) & 0xff 182 (id[0]>>8) & 0xff
183 ); 183 );
184 pfd = fopen("/proc/scsi/scsi", "r"); 184 pfd = fopen_for_read("/proc/scsi/scsi");
185 if (!pfd) { 185 if (!pfd) {
186 return NULL; 186 return NULL;
187 } 187 }
diff --git a/util-linux/hexdump.c b/util-linux/hexdump.c
index 4d2b0592b..8ac12f082 100644
--- a/util-linux/hexdump.c
+++ b/util-linux/hexdump.c
@@ -14,24 +14,13 @@
14 14
15/* This is a NOEXEC applet. Be very careful! */ 15/* This is a NOEXEC applet. Be very careful! */
16 16
17
18static void bb_dump_addfile(dumper_t *dumper, char *name) 17static void bb_dump_addfile(dumper_t *dumper, char *name)
19{ 18{
20 char *p; 19 parser_t *parser = config_open2(name, xfopen_for_read);
21 FILE *fp; 20 while (config_read(parser, &name, 1, 1, "# \t", 0)) {
22 char *buf; 21 bb_dump_add(dumper, name);
23
24 fp = xfopen(name, "r");
25
26 while ((buf = xmalloc_fgetline(fp)) != NULL) {
27 p = skip_whitespace(buf);
28
29 if (*p && (*p != '#')) {
30 bb_dump_add(dumper, p);
31 }
32 free(buf);
33 } 22 }
34 fclose(fp); 23 config_close(parser);
35} 24}
36 25
37static const char *const add_strings[] = { 26static const char *const add_strings[] = {
@@ -131,7 +120,7 @@ int hexdump_main(int argc, char **argv)
131 120
132 do { 121 do {
133 char *buf; 122 char *buf;
134 fp = xfopen(*argv, "r"); 123 fp = xfopen_for_read(*argv);
135 jump_in: 124 jump_in:
136 while ((buf = xmalloc_fgetline(fp)) != NULL) { 125 while ((buf = xmalloc_fgetline(fp)) != NULL) {
137 p = buf; 126 p = buf;
diff --git a/util-linux/mkfs_minix.c b/util-linux/mkfs_minix.c
index 8c3991a99..b29bf5a48 100644
--- a/util-linux/mkfs_minix.c
+++ b/util-linux/mkfs_minix.c
@@ -540,7 +540,7 @@ static void get_list_blocks(char *filename)
540 FILE *listfile; 540 FILE *listfile;
541 unsigned long blockno; 541 unsigned long blockno;
542 542
543 listfile = xfopen(filename, "r"); 543 listfile = xfopen_for_read(filename);
544 while (!feof(listfile)) { 544 while (!feof(listfile)) {
545 fscanf(listfile, "%ld\n", &blockno); 545 fscanf(listfile, "%ld\n", &blockno);
546 mark_zone(blockno); 546 mark_zone(blockno);
diff --git a/util-linux/more.c b/util-linux/more.c
index 93954660e..cf8e137eb 100644
--- a/util-linux/more.c
+++ b/util-linux/more.c
@@ -72,7 +72,7 @@ int more_main(int argc UNUSED_PARAM, char **argv)
72 * is not a tty and turns into cat. This makes sense. */ 72 * is not a tty and turns into cat. This makes sense. */
73 if (!isatty(STDOUT_FILENO)) 73 if (!isatty(STDOUT_FILENO))
74 return bb_cat(argv); 74 return bb_cat(argv);
75 cin = fopen(CURRENT_TTY, "r"); 75 cin = fopen_for_read(CURRENT_TTY);
76 if (!cin) 76 if (!cin)
77 return bb_cat(argv); 77 return bb_cat(argv);
78 78
diff --git a/util-linux/mount.c b/util-linux/mount.c
index 31f433b3c..2288e2c7f 100644
--- a/util-linux/mount.c
+++ b/util-linux/mount.c
@@ -363,7 +363,7 @@ static llist_t *get_block_backed_filesystems(void)
363 FILE *f; 363 FILE *f;
364 364
365 for (i = 0; i < 2; i++) { 365 for (i = 0; i < 2; i++) {
366 f = fopen(filesystems[i], "r"); 366 f = fopen_for_read(filesystems[i]);
367 if (!f) continue; 367 if (!f) continue;
368 368
369 while ((buf = xmalloc_fgetline(f)) != NULL) { 369 while ((buf = xmalloc_fgetline(f)) != NULL) {
diff --git a/util-linux/readprofile.c b/util-linux/readprofile.c
index 1c157120d..1f5ba2ecc 100644
--- a/util-linux/readprofile.c
+++ b/util-linux/readprofile.c
@@ -144,7 +144,7 @@ int readprofile_main(int argc UNUSED_PARAM, char **argv)
144 144
145 total = 0; 145 total = 0;
146 146
147 map = xfopen(mapFile, "r"); 147 map = xfopen_for_read(mapFile);
148 148
149 while (fgets(mapline, S_LEN, map)) { 149 while (fgets(mapline, S_LEN, map)) {
150 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3) 150 if (sscanf(mapline, "%llx %s %s", &fn_add, mode, fn_name) != 3)
diff --git a/util-linux/volume_id/get_devname.c b/util-linux/volume_id/get_devname.c
index d1968b744..f9a2c905f 100644
--- a/util-linux/volume_id/get_devname.c
+++ b/util-linux/volume_id/get_devname.c
@@ -165,7 +165,7 @@ uuidcache_init_partitions(void)
165 int handleOnFirst; 165 int handleOnFirst;
166 char *chptr; 166 char *chptr;
167 167
168 procpt = xfopen("/proc/partitions", "r"); 168 procpt = xfopen_for_read("/proc/partitions");
169/* 169/*
170# cat /proc/partitions 170# cat /proc/partitions
171major minor #blocks name 171major minor #blocks name
@@ -259,7 +259,7 @@ uuidcache_init_cdroms(void)
259 int ma, mi; 259 int ma, mi;
260 FILE *proccd; 260 FILE *proccd;
261 261
262 proccd = fopen(PROC_CDROMS, "r"); 262 proccd = fopen_for_read(PROC_CDROMS);
263 if (!proccd) { 263 if (!proccd) {
264// static smallint warn = 0; 264// static smallint warn = 0;
265// if (!warn) { 265// if (!warn) {