aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManuel Novoa III <mjn3@codepoet.org>2003-03-19 09:13:01 +0000
committerManuel Novoa III <mjn3@codepoet.org>2003-03-19 09:13:01 +0000
commitcad5364599eb5062d59e0c397ed638ddd61a8d5d (patch)
treea318d0f03aa076c74b576ea45dc543a5669e8e91
parente01f9662a5bd5d91be4f6b3941b57fff73cd5af1 (diff)
downloadbusybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.gz
busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.bz2
busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.zip
Major coreutils update.
-rw-r--r--Makefile2
-rw-r--r--applets/applets.c18
-rw-r--r--applets/busybox.c20
-rw-r--r--archival/ar.c12
-rw-r--r--archival/bunzip2.c16
-rw-r--r--archival/cpio.c4
-rw-r--r--archival/dpkg.c91
-rw-r--r--archival/dpkg_deb.c6
-rw-r--r--archival/gunzip.c30
-rw-r--r--archival/gzip.c32
-rw-r--r--archival/libunarchive/archive_copy_file.c2
-rw-r--r--archival/libunarchive/archive_xread.c2
-rw-r--r--archival/libunarchive/archive_xread_all.c2
-rw-r--r--archival/libunarchive/archive_xread_all_eof.c2
-rw-r--r--archival/libunarchive/check_header_gzip.c16
-rw-r--r--archival/libunarchive/data_extract_all.c16
-rw-r--r--archival/libunarchive/decompress_bunzip2.c8
-rw-r--r--archival/libunarchive/decompress_uncompress.c8
-rw-r--r--archival/libunarchive/decompress_unzip.c32
-rw-r--r--archival/libunarchive/get_header_ar.c16
-rw-r--r--archival/libunarchive/get_header_cpio.c16
-rw-r--r--archival/libunarchive/get_header_tar.c10
-rw-r--r--archival/libunarchive/get_header_tar_gz.c2
-rw-r--r--archival/libunarchive/header_verbose_list.c2
-rw-r--r--archival/libunarchive/seek_by_jump.c2
-rw-r--r--archival/libunarchive/uncompress.c8
-rw-r--r--archival/libunarchive/unpack_ar_archive.c2
-rw-r--r--archival/libunarchive/unzip.c32
-rw-r--r--archival/rpm.c14
-rw-r--r--archival/rpm2cpio.c18
-rw-r--r--archival/tar.c55
-rw-r--r--archival/uncompress.c20
-rw-r--r--archival/unzip.c18
-rw-r--r--console-tools/chvt.c6
-rw-r--r--console-tools/deallocvt.c10
-rw-r--r--console-tools/dumpkmap.c6
-rw-r--r--console-tools/loadacm.c36
-rw-r--r--console-tools/loadfont.c30
-rw-r--r--console-tools/loadkmap.c10
-rw-r--r--console-tools/openvt.c10
-rw-r--r--console-tools/setkeycodes.c8
-rw-r--r--coreutils/Config.in41
-rw-r--r--coreutils/basename.c35
-rw-r--r--coreutils/cal.c484
-rw-r--r--coreutils/cat.c60
-rw-r--r--coreutils/chgrp.c60
-rw-r--r--coreutils/chmod.c85
-rw-r--r--coreutils/chown.c104
-rw-r--r--coreutils/chroot.c50
-rw-r--r--coreutils/cmp.c156
-rw-r--r--coreutils/cp.c130
-rw-r--r--coreutils/cut.c23
-rw-r--r--coreutils/date.c22
-rw-r--r--coreutils/dd.c47
-rw-r--r--coreutils/df.c185
-rw-r--r--coreutils/dirname.c14
-rw-r--r--coreutils/dos2unix.c20
-rw-r--r--coreutils/du.c227
-rw-r--r--coreutils/echo.c133
-rw-r--r--coreutils/env.c52
-rw-r--r--coreutils/expr.c24
-rw-r--r--coreutils/false.c3
-rw-r--r--coreutils/head.c154
-rw-r--r--coreutils/hostid.c12
-rw-r--r--coreutils/id.c76
-rw-r--r--coreutils/length.c14
-rw-r--r--coreutils/libcoreutils/Makefile30
-rw-r--r--coreutils/libcoreutils/Makefile.in32
-rw-r--r--coreutils/libcoreutils/coreutils.h12
-rw-r--r--coreutils/libcoreutils/cp_mv_stat.c45
-rw-r--r--coreutils/libcoreutils/getopt_mk_fifo_nod.c45
-rw-r--r--coreutils/libcoreutils/xgetoptfile_sort_uniq.c38
-rw-r--r--coreutils/ln.c163
-rw-r--r--coreutils/logname.c29
-rw-r--r--coreutils/ls.c557
-rw-r--r--coreutils/md5sum.c38
-rw-r--r--coreutils/mkdir.c58
-rw-r--r--coreutils/mkfifo.c53
-rw-r--r--coreutils/mknod.c94
-rw-r--r--coreutils/mv.c203
-rw-r--r--coreutils/od.c203
-rw-r--r--coreutils/printf.c2
-rw-r--r--coreutils/pwd.c16
-rw-r--r--coreutils/realpath.c20
-rw-r--r--coreutils/rm.c67
-rw-r--r--coreutils/rmdir.c105
-rw-r--r--coreutils/sha1sum.c4
-rw-r--r--coreutils/sleep.c65
-rw-r--r--coreutils/sort.c98
-rw-r--r--coreutils/stty.c493
-rw-r--r--coreutils/sync.c8
-rw-r--r--coreutils/tail.c396
-rw-r--r--coreutils/tee.c115
-rw-r--r--coreutils/test.c12
-rw-r--r--coreutils/touch.c67
-rw-r--r--coreutils/tr.c10
-rw-r--r--coreutils/true.c3
-rw-r--r--coreutils/tty.c44
-rw-r--r--coreutils/uname.c38
-rw-r--r--coreutils/uniq.c126
-rw-r--r--coreutils/usleep.c16
-rw-r--r--coreutils/uudecode.c26
-rw-r--r--coreutils/uuencode.c38
-rw-r--r--coreutils/watch.c91
-rw-r--r--coreutils/wc.c325
-rw-r--r--coreutils/who.c6
-rw-r--r--coreutils/whoami.c11
-rw-r--r--coreutils/yes.c45
-rw-r--r--debianutils/mktemp.c2
-rw-r--r--debianutils/readlink.c2
-rw-r--r--debianutils/run_parts.c6
-rw-r--r--debianutils/start_stop_daemon.c22
-rw-r--r--debianutils/which.c2
-rw-r--r--editors/awk.c66
-rw-r--r--editors/sed.c62
-rw-r--r--editors/vi.c16
-rw-r--r--findutils/find.c28
-rw-r--r--findutils/grep.c24
-rw-r--r--findutils/xargs.c12
-rw-r--r--include/dump.h11
-rw-r--r--include/libbb.h216
-rw-r--r--include/usage.h97
-rw-r--r--init/halt.c2
-rw-r--r--init/init.c26
-rw-r--r--init/mesg.c4
-rw-r--r--init/poweroff.c2
-rw-r--r--init/reboot.c4
-rw-r--r--init/start_stop_daemon.c22
-rw-r--r--libbb/Makefile.in59
-rw-r--r--libbb/ask_confirmation.c50
-rw-r--r--libbb/bb_asprintf.c19
-rw-r--r--libbb/change_identity.c6
-rw-r--r--libbb/compare_string_array.c3
-rw-r--r--libbb/concat_path_file.c2
-rw-r--r--libbb/copy_file.c54
-rw-r--r--libbb/copy_file_chunk.c70
-rw-r--r--libbb/copyfd.c76
-rw-r--r--libbb/correct_password.c4
-rw-r--r--libbb/create_icmp6_socket.c4
-rw-r--r--libbb/create_icmp_socket.c4
-rw-r--r--libbb/default_error_retval.c32
-rw-r--r--libbb/dirname.c60
-rw-r--r--libbb/dump.c517
-rw-r--r--libbb/error_msg.c4
-rw-r--r--libbb/error_msg_and_die.c6
-rw-r--r--libbb/fclose_nonstdin.c37
-rw-r--r--libbb/fflush_stdout_and_exit.c37
-rw-r--r--libbb/find_root_device.c6
-rw-r--r--libbb/full_read.c11
-rw-r--r--libbb/full_write.c10
-rw-r--r--libbb/get_console.c2
-rw-r--r--libbb/get_last_path_component.c6
-rw-r--r--libbb/get_line_from_file.c48
-rw-r--r--libbb/getopt_ulflags.c (renamed from libbb/time_string.c)51
-rw-r--r--libbb/herror_msg.c4
-rw-r--r--libbb/herror_msg_and_die.c6
-rw-r--r--libbb/inet_common.c26
-rw-r--r--libbb/interface.c30
-rw-r--r--libbb/kernel_version.c2
-rw-r--r--libbb/loop.c12
-rw-r--r--libbb/make_directory.c118
-rw-r--r--libbb/messages.c59
-rw-r--r--libbb/mode_string.c150
-rw-r--r--libbb/module_syscalls.c2
-rw-r--r--libbb/mtab.c13
-rw-r--r--libbb/mtab_file.c4
-rw-r--r--libbb/my_getgrnam.c2
-rw-r--r--libbb/my_getpwnam.c2
-rw-r--r--libbb/my_getpwnamegid.c4
-rw-r--r--libbb/obscure.c6
-rw-r--r--libbb/parse_mode.c215
-rw-r--r--libbb/parse_number.c84
-rw-r--r--libbb/perror_msg.c4
-rw-r--r--libbb/perror_msg_and_die.c6
-rw-r--r--libbb/perror_nomsg.c30
-rw-r--r--libbb/perror_nomsg_and_die.c30
-rw-r--r--libbb/print_file.c49
-rw-r--r--libbb/printf.c177
-rw-r--r--libbb/process_escape_sequence.c39
-rw-r--r--libbb/procps.c2
-rw-r--r--libbb/read_package_field.c4
-rw-r--r--libbb/recursive_action.c10
-rw-r--r--libbb/remove_file.c26
-rw-r--r--libbb/run_parts.c14
-rw-r--r--libbb/run_shell.c6
-rw-r--r--libbb/setup_environment.c4
-rw-r--r--libbb/simplify_path.c9
-rw-r--r--libbb/skip_whitespace.c33
-rw-r--r--libbb/speed_table.c130
-rw-r--r--libbb/syscalls.c4
-rw-r--r--libbb/syslog_msg_with_name.c2
-rw-r--r--libbb/verror_msg.c4
-rw-r--r--libbb/vherror_msg.c4
-rw-r--r--libbb/vperror_msg.c4
-rw-r--r--libbb/warn_ignoring_args.c30
-rw-r--r--libbb/wfopen.c4
-rw-r--r--libbb/wfopen_input.c54
-rw-r--r--libbb/xconnect.c6
-rw-r--r--libbb/xfuncs.c89
-rw-r--r--libbb/xgetcwd.c2
-rw-r--r--libbb/xgethostbyname.c2
-rw-r--r--libbb/xgethostbyname2.c2
-rw-r--r--libbb/xgetlarg.c11
-rw-r--r--libbb/xgetularg.c160
-rw-r--r--libbb/xreadlink.c2
-rw-r--r--libbb/xregcomp.c2
-rw-r--r--libpwdgrp/shadow.c2
-rw-r--r--loginutils/addgroup.c23
-rw-r--r--loginutils/adduser.c38
-rw-r--r--loginutils/deluser.c38
-rw-r--r--loginutils/getty.c23
-rw-r--r--loginutils/login.c16
-rw-r--r--loginutils/passwd.c26
-rw-r--r--loginutils/su.c14
-rw-r--r--loginutils/sulogin.c6
-rw-r--r--loginutils/vlock.c15
-rw-r--r--miscutils/adjtimex.c6
-rw-r--r--miscutils/crond.c10
-rw-r--r--miscutils/crontab.c28
-rw-r--r--miscutils/dc.c10
-rw-r--r--miscutils/dutmp.c95
-rw-r--r--miscutils/makedevs.c8
-rw-r--r--miscutils/mt.c12
-rw-r--r--miscutils/strings.c4
-rw-r--r--miscutils/time.c16
-rw-r--r--miscutils/update.c4
-rw-r--r--miscutils/watchdog.c4
-rw-r--r--modutils/insmod.c126
-rw-r--r--modutils/lsmod.c11
-rw-r--r--modutils/modprobe.c36
-rw-r--r--modutils/rmmod.c10
-rw-r--r--networking/arping.c42
-rw-r--r--networking/ftpgetput.c50
-rw-r--r--networking/hostname.c12
-rw-r--r--networking/httpd.c58
-rw-r--r--networking/ifconfig.c16
-rw-r--r--networking/ifupdown.c84
-rw-r--r--networking/inetd.c6
-rw-r--r--networking/ip.c4
-rw-r--r--networking/ipcalc.c12
-rw-r--r--networking/libiproute/ip_parse_common_args.c4
-rw-r--r--networking/libiproute/ipaddress.c24
-rw-r--r--networking/libiproute/iplink.c8
-rw-r--r--networking/libiproute/iproute.c34
-rw-r--r--networking/libiproute/iptunnel.c40
-rw-r--r--networking/libiproute/libnetlink.c70
-rw-r--r--networking/libiproute/ll_addr.c6
-rw-r--r--networking/libiproute/utils.c18
-rw-r--r--networking/nameif.c16
-rw-r--r--networking/nc.c24
-rw-r--r--networking/netstat.c14
-rw-r--r--networking/nslookup.c4
-rw-r--r--networking/ping.c26
-rw-r--r--networking/ping6.c32
-rw-r--r--networking/route.c76
-rw-r--r--networking/telnet.c2
-rw-r--r--networking/telnetd.c14
-rw-r--r--networking/tftp.c42
-rw-r--r--networking/traceroute.c26
-rw-r--r--networking/udhcp/dhcpc.c6
-rw-r--r--networking/vconfig.c62
-rw-r--r--networking/wget.c46
-rw-r--r--procps/free.c2
-rw-r--r--procps/kill.c16
-rw-r--r--procps/pidof.c2
-rw-r--r--procps/renice.c4
-rw-r--r--procps/top.c20
-rw-r--r--shell/ash.c32
-rw-r--r--shell/cmdedit.c25
-rw-r--r--shell/hush.c66
-rw-r--r--shell/lash.c70
-rw-r--r--shell/msh.c6
-rw-r--r--sysklogd/klogd.c10
-rw-r--r--sysklogd/logger.c6
-rw-r--r--sysklogd/logread.c2
-rw-r--r--sysklogd/syslogd.c34
-rw-r--r--util-linux/dmesg.c8
-rw-r--r--util-linux/fbset.c16
-rw-r--r--util-linux/fdflush.c12
-rw-r--r--util-linux/fdisk.c10
-rw-r--r--util-linux/freeramdisk.c6
-rw-r--r--util-linux/fsck_minix.c10
-rw-r--r--util-linux/getopt.c22
-rw-r--r--util-linux/hexdump.c149
-rw-r--r--util-linux/hwclock.c18
-rw-r--r--util-linux/losetup.c6
-rw-r--r--util-linux/mkfs_minix.c62
-rw-r--r--util-linux/mkswap.c46
-rw-r--r--util-linux/more.c4
-rw-r--r--util-linux/mount.c34
-rw-r--r--util-linux/nfsmount.c30
-rw-r--r--util-linux/pivot_root.c4
-rw-r--r--util-linux/rdate.c8
-rw-r--r--util-linux/swaponoff.c14
-rw-r--r--util-linux/umount.c21
295 files changed, 6907 insertions, 5533 deletions
diff --git a/Makefile b/Makefile
index 48911f77e..6be57b554 100644
--- a/Makefile
+++ b/Makefile
@@ -29,7 +29,7 @@ include Rules.mak
29DIRS:=applets archival archival/libunarchive coreutils console-tools \ 29DIRS:=applets archival archival/libunarchive coreutils console-tools \
30 debianutils editors findutils init miscutils modutils networking \ 30 debianutils editors findutils init miscutils modutils networking \
31 networking/libiproute networking/udhcp procps loginutils shell \ 31 networking/libiproute networking/udhcp procps loginutils shell \
32 sysklogd util-linux libbb libpwdgrp 32 sysklogd util-linux libbb libpwdgrp coreutils/libcoreutils
33 33
34ifeq ($(strip $(HAVE_DOT_CONFIG)),y) 34ifeq ($(strip $(HAVE_DOT_CONFIG)),y)
35 35
diff --git a/applets/applets.c b/applets/applets.c
index 9f69da185..1b69ade92 100644
--- a/applets/applets.c
+++ b/applets/applets.c
@@ -78,7 +78,7 @@ static struct BB_suid_config *suid_config;
78 78
79 79
80 80
81extern void show_usage(void) 81extern void bb_show_usage(void)
82{ 82{
83 const char *format_string; 83 const char *format_string;
84 const char *usage_string = usage_messages; 84 const char *usage_string = usage_messages;
@@ -93,7 +93,7 @@ extern void show_usage(void)
93 format_string = "%s\n\nUsage: %s %s\n\n"; 93 format_string = "%s\n\nUsage: %s %s\n\n";
94 if(*usage_string == '\b') 94 if(*usage_string == '\b')
95 format_string = "%s\n\nNo help available.\n\n"; 95 format_string = "%s\n\nNo help available.\n\n";
96 fprintf(stderr, format_string, full_version, applet_using->name, usage_string); 96 fprintf(stderr, format_string, bb_msg_full_version, applet_using->name, usage_string);
97 97
98 exit(EXIT_FAILURE); 98 exit(EXIT_FAILURE);
99} 99}
@@ -127,7 +127,7 @@ void run_applet_by_name(const char *name, int argc, char **argv)
127 recurse_level++; 127 recurse_level++;
128 /* Do a binary search to find the applet entry given the name. */ 128 /* Do a binary search to find the applet entry given the name. */
129 if ((applet_using = find_applet_by_name(name)) != NULL) { 129 if ((applet_using = find_applet_by_name(name)) != NULL) {
130 applet_name = applet_using->name; 130 bb_applet_name = applet_using->name;
131 if (argv[1] && strcmp(argv[1], "--help") == 0) { 131 if (argv[1] && strcmp(argv[1], "--help") == 0) {
132 if (strcmp(applet_using->name, "busybox")==0) { 132 if (strcmp(applet_using->name, "busybox")==0) {
133 if(argv[2]) 133 if(argv[2])
@@ -136,7 +136,7 @@ void run_applet_by_name(const char *name, int argc, char **argv)
136 applet_using = NULL; 136 applet_using = NULL;
137 } 137 }
138 if(applet_using) 138 if(applet_using)
139 show_usage(); 139 bb_show_usage();
140 been_there_done_that=1; 140 been_there_done_that=1;
141 busybox_main(0, NULL); 141 busybox_main(0, NULL);
142 } 142 }
@@ -201,18 +201,18 @@ void check_suid ( struct BB_applet *applet )
201 m >>= 3; 201 m >>= 3;
202 202
203 if (!( m & S_IXOTH )) /* is x bit not set ? */ 203 if (!( m & S_IXOTH )) /* is x bit not set ? */
204 error_msg_and_die ( "You have no permission to run this applet!" ); 204 bb_error_msg_and_die ( "You have no permission to run this applet!" );
205 205
206 if (( sct-> m_mode & ( S_ISGID | S_IXGRP )) == ( S_ISGID | S_IXGRP )) { /* *both* have to be set for sgid */ 206 if (( sct-> m_mode & ( S_ISGID | S_IXGRP )) == ( S_ISGID | S_IXGRP )) { /* *both* have to be set for sgid */
207 if ( setegid ( sct-> m_gid )) 207 if ( setegid ( sct-> m_gid ))
208 error_msg_and_die ( "BusyBox binary has insufficient rights to set proper GID for applet!" ); 208 bb_error_msg_and_die ( "BusyBox binary has insufficient rights to set proper GID for applet!" );
209 } 209 }
210 else 210 else
211 setgid ( rgid ); /* no sgid -> drop */ 211 setgid ( rgid ); /* no sgid -> drop */
212 212
213 if ( sct-> m_mode & S_ISUID ) { 213 if ( sct-> m_mode & S_ISUID ) {
214 if ( seteuid ( sct-> m_uid )) 214 if ( seteuid ( sct-> m_uid ))
215 error_msg_and_die ( "BusyBox binary has insufficient rights to set proper UID for applet!" ); 215 bb_error_msg_and_die ( "BusyBox binary has insufficient rights to set proper UID for applet!" );
216 } 216 }
217 else 217 else
218 setuid ( ruid ); /* no suid -> drop */ 218 setuid ( ruid ); /* no suid -> drop */
@@ -237,7 +237,7 @@ void check_suid ( struct BB_applet *applet )
237 237
238 if ( applet-> need_suid == _BB_SUID_ALWAYS ) { 238 if ( applet-> need_suid == _BB_SUID_ALWAYS ) {
239 if ( geteuid ( ) != 0 ) 239 if ( geteuid ( ) != 0 )
240 error_msg_and_die ( "This applet requires root priviledges!" ); 240 bb_error_msg_and_die ( "This applet requires root priviledges!" );
241 } 241 }
242 else if ( applet-> need_suid == _BB_SUID_NEVER ) { 242 else if ( applet-> need_suid == _BB_SUID_NEVER ) {
243 setgid ( rgid ); /* drop all priviledges */ 243 setgid ( rgid ); /* drop all priviledges */
@@ -280,7 +280,7 @@ int parse_config_file ( void )
280 p = strchr ( buffer, '#' ); 280 p = strchr ( buffer, '#' );
281 if ( p ) 281 if ( p )
282 *p = 0; 282 *p = 0;
283 p = buffer + xstrlen ( buffer ); 283 p = buffer + bb_strlen ( buffer );
284 while (( p > buffer ) && isspace ( *--p )) 284 while (( p > buffer ) && isspace ( *--p ))
285 *p = 0; 285 *p = 0;
286 286
diff --git a/applets/busybox.c b/applets/busybox.c
index 4f1ef2661..457a85a68 100644
--- a/applets/busybox.c
+++ b/applets/busybox.c
@@ -10,7 +10,7 @@
10#endif 10#endif
11 11
12int been_there_done_that = 0; /* Also used in applets.c */ 12int been_there_done_that = 0; /* Also used in applets.c */
13const char *applet_name; 13const char *bb_applet_name;
14 14
15#ifdef CONFIG_FEATURE_INSTALLER 15#ifdef CONFIG_FEATURE_INSTALLER
16/* 16/*
@@ -60,7 +60,7 @@ static void install_links(const char *busybox, int use_symbolic_links)
60 install_dir[applets[i].location], applets[i].name); 60 install_dir[applets[i].location], applets[i].name);
61 rc = Link(busybox, fpc); 61 rc = Link(busybox, fpc);
62 if (rc!=0 && errno!=EEXIST) { 62 if (rc!=0 && errno!=EEXIST) {
63 perror_msg("%s", fpc); 63 bb_perror_msg("%s", fpc);
64 } 64 }
65 free(fpc); 65 free(fpc);
66 } 66 }
@@ -72,14 +72,14 @@ int main(int argc, char **argv)
72{ 72{
73 const char *s; 73 const char *s;
74 74
75 applet_name = argv[0]; 75 bb_applet_name = argv[0];
76 76
77 if (applet_name[0] == '-') 77 if (bb_applet_name[0] == '-')
78 applet_name++; 78 bb_applet_name++;
79 79
80 for (s = applet_name; *s != '\0';) { 80 for (s = bb_applet_name; *s != '\0';) {
81 if (*s++ == '/') 81 if (*s++ == '/')
82 applet_name = s; 82 bb_applet_name = s;
83 } 83 }
84 84
85#ifdef CONFIG_LOCALE_SUPPORT 85#ifdef CONFIG_LOCALE_SUPPORT
@@ -91,8 +91,8 @@ int main(int argc, char **argv)
91 } 91 }
92#endif 92#endif
93 93
94 run_applet_by_name(applet_name, argc, argv); 94 run_applet_by_name(bb_applet_name, argc, argv);
95 error_msg_and_die("applet not found"); 95 bb_error_msg_and_die("applet not found");
96} 96}
97 97
98 98
@@ -143,7 +143,7 @@ int busybox_main(int argc, char **argv)
143 "\tutilities into a single executable. Most people will create a\n" 143 "\tutilities into a single executable. Most people will create a\n"
144 "\tlink to busybox for each function they wish to use, and BusyBox\n" 144 "\tlink to busybox for each function they wish to use, and BusyBox\n"
145 "\twill act like whatever it was invoked as.\n" 145 "\twill act like whatever it was invoked as.\n"
146 "\nCurrently defined functions:\n", full_version); 146 "\nCurrently defined functions:\n", bb_msg_full_version);
147 147
148 while (a->name != 0) { 148 while (a->name != 0) {
149 col += 149 col +=
diff --git a/archival/ar.c b/archival/ar.c
index 87968f7be..57ec92719 100644
--- a/archival/ar.c
+++ b/archival/ar.c
@@ -40,7 +40,7 @@
40 40
41static void header_verbose_list_ar(const file_header_t *file_header) 41static void header_verbose_list_ar(const file_header_t *file_header)
42{ 42{
43 const char *mode = mode_string(file_header->mode); 43 const char *mode = bb_mode_string(file_header->mode);
44 char *mtime; 44 char *mtime;
45 45
46 mtime = ctime(&file_header->mtime); 46 mtime = ctime(&file_header->mtime);
@@ -58,7 +58,7 @@ static void data_extract_regular_file(archive_handle_t *archive_handle)
58 int dst_fd; 58 int dst_fd;
59 59
60 file_header = archive_handle->file_header; 60 file_header = archive_handle->file_header;
61 dst_fd = xopen(file_header->name, O_WRONLY | O_CREAT); 61 dst_fd = bb_xopen(file_header->name, O_WRONLY | O_CREAT);
62 archive_copy_file(archive_handle, dst_fd); 62 archive_copy_file(archive_handle, dst_fd);
63 close(dst_fd); 63 close(dst_fd);
64 64
@@ -110,16 +110,16 @@ extern int ar_main(int argc, char **argv)
110 archive_handle->action_header = header_verbose_list_ar; 110 archive_handle->action_header = header_verbose_list_ar;
111 break; 111 break;
112 default: 112 default:
113 show_usage(); 113 bb_show_usage();
114 } 114 }
115 } 115 }
116 116
117 /* check the src filename was specified */ 117 /* check the src filename was specified */
118 if (optind == argc) { 118 if (optind == argc) {
119 show_usage(); 119 bb_show_usage();
120 } 120 }
121 121
122 archive_handle->src_fd = xopen(argv[optind++], O_RDONLY); 122 archive_handle->src_fd = bb_xopen(argv[optind++], O_RDONLY);
123 123
124 /* TODO: This is the same as in tar, seperate function ? */ 124 /* TODO: This is the same as in tar, seperate function ? */
125 while (optind < argc) { 125 while (optind < argc) {
@@ -133,7 +133,7 @@ extern int ar_main(int argc, char **argv)
133#else 133#else
134 archive_xread_all(archive_handle, magic, 7); 134 archive_xread_all(archive_handle, magic, 7);
135 if (strncmp(magic, "!<arch>", 7) != 0) { 135 if (strncmp(magic, "!<arch>", 7) != 0) {
136 error_msg_and_die("Invalid ar magic"); 136 bb_error_msg_and_die("Invalid ar magic");
137 } 137 }
138 archive_handle->offset += 7; 138 archive_handle->offset += 7;
139 139
diff --git a/archival/bunzip2.c b/archival/bunzip2.c
index d5c06f4fd..eb5238cd4 100644
--- a/archival/bunzip2.c
+++ b/archival/bunzip2.c
@@ -41,7 +41,7 @@ int bunzip2_main(int argc, char **argv)
41 char *delete_name = NULL; 41 char *delete_name = NULL;
42 42
43 /* if called as bzcat */ 43 /* if called as bzcat */
44 if (strcmp(applet_name, "bzcat") == 0) 44 if (strcmp(bb_applet_name, "bzcat") == 0)
45 flags |= bunzip_to_stdout; 45 flags |= bunzip_to_stdout;
46 46
47 while ((opt = getopt(argc, argv, "cfh")) != -1) { 47 while ((opt = getopt(argc, argv, "cfh")) != -1) {
@@ -54,7 +54,7 @@ int bunzip2_main(int argc, char **argv)
54 break; 54 break;
55 case 'h': 55 case 'h':
56 default: 56 default:
57 show_usage(); /* exit's inside usage */ 57 bb_show_usage(); /* exit's inside usage */
58 } 58 }
59 } 59 }
60 60
@@ -64,23 +64,23 @@ int bunzip2_main(int argc, char **argv)
64 src_fd = fileno(stdin); 64 src_fd = fileno(stdin);
65 } else { 65 } else {
66 /* Open input file */ 66 /* Open input file */
67 src_fd = xopen(argv[optind], O_RDONLY); 67 src_fd = bb_xopen(argv[optind], O_RDONLY);
68 68
69 save_name = xstrdup(argv[optind]); 69 save_name = bb_xstrdup(argv[optind]);
70 if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0) 70 if (strcmp(save_name + strlen(save_name) - 4, ".bz2") != 0)
71 error_msg_and_die("Invalid extension"); 71 bb_error_msg_and_die("Invalid extension");
72 save_name[strlen(save_name) - 4] = '\0'; 72 save_name[strlen(save_name) - 4] = '\0';
73 } 73 }
74 74
75 /* Check that the input is sane. */ 75 /* Check that the input is sane. */
76 if (isatty(src_fd) && (flags & bunzip_force) == 0) { 76 if (isatty(src_fd) && (flags & bunzip_force) == 0) {
77 error_msg_and_die("compressed data not read from terminal. Use -f to force it."); 77 bb_error_msg_and_die("compressed data not read from terminal. Use -f to force it.");
78 } 78 }
79 79
80 if (flags & bunzip_to_stdout) { 80 if (flags & bunzip_to_stdout) {
81 dst_fd = fileno(stdout); 81 dst_fd = fileno(stdout);
82 } else { 82 } else {
83 dst_fd = xopen(save_name, O_WRONLY | O_CREAT); 83 dst_fd = bb_xopen(save_name, O_WRONLY | O_CREAT);
84 } 84 }
85 85
86 if (uncompressStream(src_fd, dst_fd)) { 86 if (uncompressStream(src_fd, dst_fd)) {
@@ -96,7 +96,7 @@ int bunzip2_main(int argc, char **argv)
96 } 96 }
97 97
98 if ((delete_name) && (unlink(delete_name) < 0)) { 98 if ((delete_name) && (unlink(delete_name) < 0)) {
99 error_msg_and_die("Couldn't remove %s", delete_name); 99 bb_error_msg_and_die("Couldn't remove %s", delete_name);
100 } 100 }
101 101
102 return status; 102 return status;
diff --git a/archival/cpio.c b/archival/cpio.c
index 49f3f88e4..111807c43 100644
--- a/archival/cpio.c
+++ b/archival/cpio.c
@@ -63,11 +63,11 @@ extern int cpio_main(int argc, char **argv)
63 archive_handle->action_header = header_list; 63 archive_handle->action_header = header_list;
64 break; 64 break;
65 case 'F': 65 case 'F':
66 archive_handle->src_fd = xopen(optarg, O_RDONLY); 66 archive_handle->src_fd = bb_xopen(optarg, O_RDONLY);
67 archive_handle->seek = seek_by_jump; 67 archive_handle->seek = seek_by_jump;
68 break; 68 break;
69 default: 69 default:
70 show_usage(); 70 bb_show_usage();
71 } 71 }
72 } 72 }
73 73
diff --git a/archival/dpkg.c b/archival/dpkg.c
index 692592268..3288f7e63 100644
--- a/archival/dpkg.c
+++ b/archival/dpkg.c
@@ -177,7 +177,7 @@ int search_name_hashtable(const char *key)
177 } 177 }
178 } 178 }
179 } 179 }
180 name_hashtable[probe_address] = xstrdup(key); 180 name_hashtable[probe_address] = bb_xstrdup(key);
181 return(probe_address); 181 return(probe_address);
182} 182}
183 183
@@ -218,10 +218,10 @@ int version_compare_part(const char *version1, const char *version2)
218 int ret; 218 int ret;
219 219
220 if (version1 == NULL) { 220 if (version1 == NULL) {
221 version1 = xstrdup(""); 221 version1 = bb_xstrdup("");
222 } 222 }
223 if (version2 == NULL) { 223 if (version2 == NULL) {
224 version2 = xstrdup(""); 224 version2 = bb_xstrdup("");
225 } 225 }
226 upstream_len1 = strlen(version1); 226 upstream_len1 = strlen(version1);
227 upstream_len2 = strlen(version2); 227 upstream_len2 = strlen(version2);
@@ -229,10 +229,10 @@ int version_compare_part(const char *version1, const char *version2)
229 while ((len1 < upstream_len1) || (len2 < upstream_len2)) { 229 while ((len1 < upstream_len1) || (len2 < upstream_len2)) {
230 /* Compare non-digit section */ 230 /* Compare non-digit section */
231 tmp_int = strcspn(&version1[len1], "0123456789"); 231 tmp_int = strcspn(&version1[len1], "0123456789");
232 name1_char = xstrndup(&version1[len1], tmp_int); 232 name1_char = bb_xstrndup(&version1[len1], tmp_int);
233 len1 += tmp_int; 233 len1 += tmp_int;
234 tmp_int = strcspn(&version2[len2], "0123456789"); 234 tmp_int = strcspn(&version2[len2], "0123456789");
235 name2_char = xstrndup(&version2[len2], tmp_int); 235 name2_char = bb_xstrndup(&version2[len2], tmp_int);
236 len2 += tmp_int; 236 len2 += tmp_int;
237 tmp_int = strcmp(name1_char, name2_char); 237 tmp_int = strcmp(name1_char, name2_char);
238 free(name1_char); 238 free(name1_char);
@@ -244,10 +244,10 @@ int version_compare_part(const char *version1, const char *version2)
244 244
245 /* Compare digits */ 245 /* Compare digits */
246 tmp_int = strspn(&version1[len1], "0123456789"); 246 tmp_int = strspn(&version1[len1], "0123456789");
247 name1_char = xstrndup(&version1[len1], tmp_int); 247 name1_char = bb_xstrndup(&version1[len1], tmp_int);
248 len1 += tmp_int; 248 len1 += tmp_int;
249 tmp_int = strspn(&version2[len2], "0123456789"); 249 tmp_int = strspn(&version2[len2], "0123456789");
250 name2_char = xstrndup(&version2[len2], tmp_int); 250 name2_char = bb_xstrndup(&version2[len2], tmp_int);
251 len2 += tmp_int; 251 len2 += tmp_int;
252 ver_num1 = atoi(name1_char); 252 ver_num1 = atoi(name1_char);
253 ver_num2 = atoi(name2_char); 253 ver_num2 = atoi(name2_char);
@@ -306,8 +306,8 @@ int version_compare(const unsigned int ver1, const unsigned int ver2)
306 } 306 }
307 307
308 /* Compare upstream version */ 308 /* Compare upstream version */
309 upstream_ver1 = xstrdup(ver1_ptr); 309 upstream_ver1 = bb_xstrdup(ver1_ptr);
310 upstream_ver2 = xstrdup(ver2_ptr); 310 upstream_ver2 = bb_xstrdup(ver2_ptr);
311 311
312 /* Chop off debian version, and store for later use */ 312 /* Chop off debian version, and store for later use */
313 deb_ver1 = strrchr(upstream_ver1, '-'); 313 deb_ver1 = strrchr(upstream_ver1, '-');
@@ -397,7 +397,7 @@ int search_package_hashtable(const unsigned int name, const unsigned int version
397 */ 397 */
398void add_split_dependencies(common_node_t *parent_node, const char *whole_line, unsigned int edge_type) 398void add_split_dependencies(common_node_t *parent_node, const char *whole_line, unsigned int edge_type)
399{ 399{
400 char *line = xstrdup(whole_line); 400 char *line = bb_xstrdup(whole_line);
401 char *line2; 401 char *line2;
402 char *line_ptr1 = NULL; 402 char *line_ptr1 = NULL;
403 char *line_ptr2 = NULL; 403 char *line_ptr2 = NULL;
@@ -410,7 +410,7 @@ void add_split_dependencies(common_node_t *parent_node, const char *whole_line,
410 410
411 field = strtok_r(line, ",", &line_ptr1); 411 field = strtok_r(line, ",", &line_ptr1);
412 do { 412 do {
413 line2 = xstrdup(field); 413 line2 = bb_xstrdup(field);
414 field2 = strtok_r(line2, "|", &line_ptr2); 414 field2 = strtok_r(line2, "|", &line_ptr2);
415 if ((edge_type == EDGE_DEPENDS) && (strcmp(field, field2) != 0)) { 415 if ((edge_type == EDGE_DEPENDS) && (strcmp(field, field2) != 0)) {
416 type = EDGE_OR_DEPENDS; 416 type = EDGE_OR_DEPENDS;
@@ -457,7 +457,7 @@ void add_split_dependencies(common_node_t *parent_node, const char *whole_line,
457 else if (strncmp(version, ">=", offset_ch) == 0) { 457 else if (strncmp(version, ">=", offset_ch) == 0) {
458 edge->operator = VER_MORE_EQUAL; 458 edge->operator = VER_MORE_EQUAL;
459 } else { 459 } else {
460 error_msg_and_die("Illegal operator\n"); 460 bb_error_msg_and_die("Illegal operator\n");
461 } 461 }
462 } 462 }
463 /* skip to start of version numbers */ 463 /* skip to start of version numbers */
@@ -588,7 +588,7 @@ unsigned int get_status(const unsigned int status_node, const int num)
588 status_string += strspn(status_string, " "); 588 status_string += strspn(status_string, " ");
589 } 589 }
590 len = strcspn(status_string, " \n\0"); 590 len = strcspn(status_string, " \n\0");
591 state_sub_string = xstrndup(status_string, len); 591 state_sub_string = bb_xstrndup(status_string, len);
592 state_sub_num = search_name_hashtable(state_sub_string); 592 state_sub_num = search_name_hashtable(state_sub_string);
593 free(state_sub_string); 593 free(state_sub_string);
594 return(state_sub_num); 594 return(state_sub_num);
@@ -620,7 +620,7 @@ void set_status(const unsigned int status_node_num, const char *new_value, const
620 status_len = new_value_len; 620 status_len = new_value_len;
621 break; 621 break;
622 default: 622 default:
623 error_msg_and_die("DEBUG ONLY: this shouldnt happen"); 623 bb_error_msg_and_die("DEBUG ONLY: this shouldnt happen");
624 } 624 }
625 625
626 new_status = (char *) xmalloc(want_len + flag_len + status_len + 3); 626 new_status = (char *) xmalloc(want_len + flag_len + status_len + 3);
@@ -638,7 +638,7 @@ void index_status_file(const char *filename)
638 status_node_t *status_node = NULL; 638 status_node_t *status_node = NULL;
639 unsigned int status_num; 639 unsigned int status_num;
640 640
641 status_file = xfopen(filename, "r"); 641 status_file = bb_xfopen(filename, "r");
642 while ((control_buffer = fgets_str(status_file, "\n\n")) != NULL) { 642 while ((control_buffer = fgets_str(status_file, "\n\n")) != NULL) {
643 const unsigned int package_num = fill_package_struct(control_buffer); 643 const unsigned int package_num = fill_package_struct(control_buffer);
644 if (package_num != -1) { 644 if (package_num != -1) {
@@ -648,7 +648,7 @@ void index_status_file(const char *filename)
648 if (status_line != NULL) { 648 if (status_line != NULL) {
649 status_line += 7; 649 status_line += 7;
650 status_line += strspn(status_line, " \n\t"); 650 status_line += strspn(status_line, " \n\t");
651 status_line = xstrndup(status_line, strcspn(status_line, "\n\0")); 651 status_line = bb_xstrndup(status_line, strcspn(status_line, "\n\0"));
652 status_node->status = search_name_hashtable(status_line); 652 status_node->status = search_name_hashtable(status_line);
653 free(status_line); 653 free(status_line);
654 } 654 }
@@ -754,8 +754,8 @@ void write_buffer_no_status(FILE *new_status_file, const char *control_buffer)
754/* This could do with a cleanup */ 754/* This could do with a cleanup */
755void write_status_file(deb_file_t **deb_file) 755void write_status_file(deb_file_t **deb_file)
756{ 756{
757 FILE *old_status_file = xfopen("/var/lib/dpkg/status", "r"); 757 FILE *old_status_file = bb_xfopen("/var/lib/dpkg/status", "r");
758 FILE *new_status_file = xfopen("/var/lib/dpkg/status.udeb", "w"); 758 FILE *new_status_file = bb_xfopen("/var/lib/dpkg/status.udeb", "w");
759 char *package_name; 759 char *package_name;
760 char *status_from_file; 760 char *status_from_file;
761 char *control_buffer = NULL; 761 char *control_buffer = NULL;
@@ -773,14 +773,14 @@ void write_status_file(deb_file_t **deb_file)
773 773
774 tmp_string += 8; 774 tmp_string += 8;
775 tmp_string += strspn(tmp_string, " \n\t"); 775 tmp_string += strspn(tmp_string, " \n\t");
776 package_name = xstrndup(tmp_string, strcspn(tmp_string, "\n\0")); 776 package_name = bb_xstrndup(tmp_string, strcspn(tmp_string, "\n\0"));
777 write_flag = FALSE; 777 write_flag = FALSE;
778 tmp_string = strstr(control_buffer, "Status:"); 778 tmp_string = strstr(control_buffer, "Status:");
779 if (tmp_string != NULL) { 779 if (tmp_string != NULL) {
780 /* Seperate the status value from the control buffer */ 780 /* Seperate the status value from the control buffer */
781 tmp_string += 7; 781 tmp_string += 7;
782 tmp_string += strspn(tmp_string, " \n\t"); 782 tmp_string += strspn(tmp_string, " \n\t");
783 status_from_file = xstrndup(tmp_string, strcspn(tmp_string, "\n")); 783 status_from_file = bb_xstrndup(tmp_string, strcspn(tmp_string, "\n"));
784 } else { 784 } else {
785 status_from_file = NULL; 785 status_from_file = NULL;
786 } 786 }
@@ -810,7 +810,7 @@ void write_status_file(deb_file_t **deb_file)
810 } 810 }
811 /* This is temperary, debugging only */ 811 /* This is temperary, debugging only */
812 if (deb_file[i] == NULL) { 812 if (deb_file[i] == NULL) {
813 error_msg_and_die("ALERT: Couldnt find a control file, your status file may be broken, status may be incorrect for %s", package_name); 813 bb_error_msg_and_die("ALERT: Couldnt find a control file, your status file may be broken, status may be incorrect for %s", package_name);
814 } 814 }
815 } 815 }
816 else if (strcmp("not-installed", name_hashtable[state_status]) == 0) { 816 else if (strcmp("not-installed", name_hashtable[state_status]) == 0) {
@@ -881,15 +881,15 @@ void write_status_file(deb_file_t **deb_file)
881 if (rename("/var/lib/dpkg/status", "/var/lib/dpkg/status.udeb.bak") == -1) { 881 if (rename("/var/lib/dpkg/status", "/var/lib/dpkg/status.udeb.bak") == -1) {
882 struct stat stat_buf; 882 struct stat stat_buf;
883 if (stat("/var/lib/dpkg/status", &stat_buf) == 0) { 883 if (stat("/var/lib/dpkg/status", &stat_buf) == 0) {
884 error_msg_and_die("Couldnt create backup status file"); 884 bb_error_msg_and_die("Couldnt create backup status file");
885 } 885 }
886 /* Its ok if renaming the status file fails becasue status 886 /* Its ok if renaming the status file fails becasue status
887 * file doesnt exist, maybe we are starting from scratch */ 887 * file doesnt exist, maybe we are starting from scratch */
888 error_msg("No status file found, creating new one"); 888 bb_error_msg("No status file found, creating new one");
889 } 889 }
890 890
891 if (rename("/var/lib/dpkg/status.udeb", "/var/lib/dpkg/status") == -1) { 891 if (rename("/var/lib/dpkg/status.udeb", "/var/lib/dpkg/status") == -1) {
892 error_msg_and_die("DANGER: Couldnt create status file, you need to manually repair your status file"); 892 bb_error_msg_and_die("DANGER: Couldnt create status file, you need to manually repair your status file");
893 } 893 }
894} 894}
895 895
@@ -976,7 +976,7 @@ int check_deps(deb_file_t **deb_file, int deb_start, int dep_max_count)
976 } 976 }
977 977
978 if (result) { 978 if (result) {
979 error_msg_and_die("Package %s conflicts with %s", 979 bb_error_msg_and_die("Package %s conflicts with %s",
980 name_hashtable[package_node->name], 980 name_hashtable[package_node->name],
981 name_hashtable[package_edge->name]); 981 name_hashtable[package_edge->name]);
982 } 982 }
@@ -1021,7 +1021,7 @@ int check_deps(deb_file_t **deb_file, int deb_start, int dep_max_count)
1021 } 1021 }
1022 1022
1023 if (result) { 1023 if (result) {
1024 error_msg_and_die("Package %s pre-depends on %s, but it is not installed", 1024 bb_error_msg_and_die("Package %s pre-depends on %s, but it is not installed",
1025 name_hashtable[package_node->name], 1025 name_hashtable[package_node->name],
1026 name_hashtable[package_edge->name]); 1026 name_hashtable[package_edge->name]);
1027 } 1027 }
@@ -1038,7 +1038,7 @@ int check_deps(deb_file_t **deb_file, int deb_start, int dep_max_count)
1038 } 1038 }
1039 /* It must be already installed, or to be installed */ 1039 /* It must be already installed, or to be installed */
1040 if (result) { 1040 if (result) {
1041 error_msg_and_die("Package %s depends on %s, but it is not installed, or flaged to be installed", 1041 bb_error_msg_and_die("Package %s depends on %s, but it is not installed, or flaged to be installed",
1042 name_hashtable[package_node->name], 1042 name_hashtable[package_node->name],
1043 name_hashtable[package_edge->name]); 1043 name_hashtable[package_edge->name]);
1044 } 1044 }
@@ -1065,9 +1065,8 @@ char **create_list(const char *filename)
1065 return(NULL); 1065 return(NULL);
1066 } 1066 }
1067 1067
1068 while ((line = get_line_from_file(list_stream)) != NULL) { 1068 while ((line = bb_get_chomped_line_from_file(list_stream)) != NULL) {
1069 file_list = xrealloc(file_list, sizeof(char *) * (count + 2)); 1069 file_list = xrealloc(file_list, sizeof(char *) * (count + 2));
1070 chomp(line);
1071 file_list[count] = line; 1070 file_list[count] = line;
1072 count++; 1071 count++;
1073 } 1072 }
@@ -1228,7 +1227,7 @@ void remove_package(const unsigned int package_num)
1228 /* run prerm script */ 1227 /* run prerm script */
1229 return_value = run_package_script(package_name, "prerm"); 1228 return_value = run_package_script(package_name, "prerm");
1230 if (return_value == -1) { 1229 if (return_value == -1) {
1231 error_msg_and_die("script failed, prerm failure"); 1230 bb_error_msg_and_die("script failed, prerm failure");
1232 } 1231 }
1233 1232
1234 /* Create a list of files to remove, and a seperate list of those to keep */ 1233 /* Create a list of files to remove, and a seperate list of those to keep */
@@ -1245,7 +1244,7 @@ void remove_package(const unsigned int package_num)
1245 1244
1246 /* Create a list of files in /var/lib/dpkg/info/<package>.* to keep */ 1245 /* Create a list of files in /var/lib/dpkg/info/<package>.* to keep */
1247 exclude_files = xmalloc(sizeof(char*) * 3); 1246 exclude_files = xmalloc(sizeof(char*) * 3);
1248 exclude_files[0] = xstrdup(conffile_name); 1247 exclude_files[0] = bb_xstrdup(conffile_name);
1249 exclude_files[1] = xmalloc(package_name_length + 27); 1248 exclude_files[1] = xmalloc(package_name_length + 27);
1250 sprintf(exclude_files[1], "/var/lib/dpkg/info/%s.postrm", package_name); 1249 sprintf(exclude_files[1], "/var/lib/dpkg/info/%s.postrm", package_name);
1251 exclude_files[2] = NULL; 1250 exclude_files[2] = NULL;
@@ -1275,7 +1274,7 @@ void purge_package(const unsigned int package_num)
1275 1274
1276 /* run prerm script */ 1275 /* run prerm script */
1277 if (run_package_script(package_name, "prerm") != 0) { 1276 if (run_package_script(package_name, "prerm") != 0) {
1278 error_msg_and_die("script failed, prerm failure"); 1277 bb_error_msg_and_die("script failed, prerm failure");
1279 } 1278 }
1280 1279
1281 /* Create a list of files to remove */ 1280 /* Create a list of files to remove */
@@ -1297,7 +1296,7 @@ void purge_package(const unsigned int package_num)
1297 1296
1298 /* run postrm script */ 1297 /* run postrm script */
1299 if (run_package_script(package_name, "postrm") == -1) { 1298 if (run_package_script(package_name, "postrm") == -1) {
1300 error_msg_and_die("postrm fialure.. set status to what?"); 1299 bb_error_msg_and_die("postrm fialure.. set status to what?");
1301 } 1300 }
1302 1301
1303 /* Change package status */ 1302 /* Change package status */
@@ -1312,7 +1311,7 @@ static archive_handle_t *init_archive_deb_ar(const char *filename)
1312 /* Setup an ar archive handle that refers to the gzip sub archive */ 1311 /* Setup an ar archive handle that refers to the gzip sub archive */
1313 ar_handle = init_handle(); 1312 ar_handle = init_handle();
1314 ar_handle->filter = filter_accept_list_reassign; 1313 ar_handle->filter = filter_accept_list_reassign;
1315 ar_handle->src_fd = xopen(filename, O_RDONLY); 1314 ar_handle->src_fd = bb_xopen(filename, O_RDONLY);
1316 1315
1317 return(ar_handle); 1316 return(ar_handle);
1318} 1317}
@@ -1420,7 +1419,7 @@ static void unpack_package(deb_file_t *deb_file)
1420 /* Run the preinst prior to extracting */ 1419 /* Run the preinst prior to extracting */
1421 if (run_package_script(package_name, "preinst") != 0) { 1420 if (run_package_script(package_name, "preinst") != 0) {
1422 /* when preinst returns exit code != 0 then quit installation process */ 1421 /* when preinst returns exit code != 0 then quit installation process */
1423 error_msg_and_die("subprocess pre-installation script returned error."); 1422 bb_error_msg_and_die("subprocess pre-installation script returned error.");
1424 } 1423 }
1425 1424
1426 /* Extract data.tar.gz to the root directory */ 1425 /* Extract data.tar.gz to the root directory */
@@ -1430,7 +1429,7 @@ static void unpack_package(deb_file_t *deb_file)
1430 1429
1431 /* Create the list file */ 1430 /* Create the list file */
1432 strcat(info_prefix, "list"); 1431 strcat(info_prefix, "list");
1433 out_stream = xfopen(info_prefix, "w"); 1432 out_stream = bb_xfopen(info_prefix, "w");
1434 while (archive_handle->passed) { 1433 while (archive_handle->passed) {
1435 /* blindly skip over the leading '.' */ 1434 /* blindly skip over the leading '.' */
1436 fputs(archive_handle->passed->data + 1, out_stream); 1435 fputs(archive_handle->passed->data + 1, out_stream);
@@ -1457,7 +1456,7 @@ void configure_package(deb_file_t *deb_file)
1457 /* Run the postinst script */ 1456 /* Run the postinst script */
1458 if (run_package_script(package_name, "postinst") != 0) { 1457 if (run_package_script(package_name, "postinst") != 0) {
1459 /* TODO: handle failure gracefully */ 1458 /* TODO: handle failure gracefully */
1460 error_msg_and_die("postrm failure.. set status to what?"); 1459 bb_error_msg_and_die("postrm failure.. set status to what?");
1461 } 1460 }
1462 /* Change status to reflect success */ 1461 /* Change status to reflect success */
1463 set_status(status_num, "install", 1); 1462 set_status(status_num, "install", 1);
@@ -1506,12 +1505,12 @@ int dpkg_main(int argc, char **argv)
1506 dpkg_opt |= dpkg_opt_filename; 1505 dpkg_opt |= dpkg_opt_filename;
1507 break; 1506 break;
1508 default: 1507 default:
1509 show_usage(); 1508 bb_show_usage();
1510 } 1509 }
1511 } 1510 }
1512 /* check for non-otion argument if expected */ 1511 /* check for non-otion argument if expected */
1513 if ((dpkg_opt == 0) || ((argc == optind) && !(dpkg_opt && dpkg_opt_list_installed))) { 1512 if ((dpkg_opt == 0) || ((argc == optind) && !(dpkg_opt && dpkg_opt_list_installed))) {
1514 show_usage(); 1513 bb_show_usage();
1515 } 1514 }
1516 1515
1517/* puts("(Reading database ... xxxxx files and directories installed.)"); */ 1516/* puts("(Reading database ... xxxxx files and directories installed.)"); */
@@ -1538,13 +1537,13 @@ int dpkg_main(int argc, char **argv)
1538 init_archive_deb_control(archive_handle); 1537 init_archive_deb_control(archive_handle);
1539 deb_file[deb_count]->control_file = deb_extract_control_file_to_buffer(archive_handle, control_list); 1538 deb_file[deb_count]->control_file = deb_extract_control_file_to_buffer(archive_handle, control_list);
1540 if (deb_file[deb_count]->control_file == NULL) { 1539 if (deb_file[deb_count]->control_file == NULL) {
1541 error_msg_and_die("Couldnt extract control file"); 1540 bb_error_msg_and_die("Couldnt extract control file");
1542 } 1541 }
1543 deb_file[deb_count]->filename = xstrdup(argv[optind]); 1542 deb_file[deb_count]->filename = bb_xstrdup(argv[optind]);
1544 package_num = fill_package_struct(deb_file[deb_count]->control_file); 1543 package_num = fill_package_struct(deb_file[deb_count]->control_file);
1545 1544
1546 if (package_num == -1) { 1545 if (package_num == -1) {
1547 error_msg("Invalid control file in %s", argv[optind]); 1546 bb_error_msg("Invalid control file in %s", argv[optind]);
1548 continue; 1547 continue;
1549 } 1548 }
1550 deb_file[deb_count]->package = (unsigned int) package_num; 1549 deb_file[deb_count]->package = (unsigned int) package_num;
@@ -1574,7 +1573,7 @@ int dpkg_main(int argc, char **argv)
1574 search_name_hashtable(argv[optind]), 1573 search_name_hashtable(argv[optind]),
1575 search_name_hashtable("ANY"), VER_ANY); 1574 search_name_hashtable("ANY"), VER_ANY);
1576 if (package_hashtable[deb_file[deb_count]->package] == NULL) { 1575 if (package_hashtable[deb_file[deb_count]->package] == NULL) {
1577 error_msg_and_die("Package %s is uninstalled or unknown\n", argv[optind]); 1576 bb_error_msg_and_die("Package %s is uninstalled or unknown\n", argv[optind]);
1578 } 1577 }
1579 state_status = get_status(search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]), 3); 1578 state_status = get_status(search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]), 3);
1580 1579
@@ -1582,13 +1581,13 @@ int dpkg_main(int argc, char **argv)
1582 if (dpkg_opt & dpkg_opt_remove) { 1581 if (dpkg_opt & dpkg_opt_remove) {
1583 if ((strcmp(name_hashtable[state_status], "not-installed") == 0) || 1582 if ((strcmp(name_hashtable[state_status], "not-installed") == 0) ||
1584 (strcmp(name_hashtable[state_status], "config-files") == 0)) { 1583 (strcmp(name_hashtable[state_status], "config-files") == 0)) {
1585 error_msg_and_die("%s is already removed.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); 1584 bb_error_msg_and_die("%s is already removed.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
1586 } 1585 }
1587 } 1586 }
1588 else if (dpkg_opt & dpkg_opt_purge) { 1587 else if (dpkg_opt & dpkg_opt_purge) {
1589 /* if package status is "conf-files" then its ok */ 1588 /* if package status is "conf-files" then its ok */
1590 if (strcmp(name_hashtable[state_status], "not-installed") == 0) { 1589 if (strcmp(name_hashtable[state_status], "not-installed") == 0) {
1591 error_msg_and_die("%s is already purged.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); 1590 bb_error_msg_and_die("%s is already purged.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
1592 } 1591 }
1593 } 1592 }
1594 } 1593 }
@@ -1601,7 +1600,7 @@ int dpkg_main(int argc, char **argv)
1601 /* TODO: check dependencies before removing */ 1600 /* TODO: check dependencies before removing */
1602 if ((dpkg_opt & dpkg_opt_force_ignore_depends) != dpkg_opt_force_ignore_depends) { 1601 if ((dpkg_opt & dpkg_opt_force_ignore_depends) != dpkg_opt_force_ignore_depends) {
1603 if (!check_deps(deb_file, 0, deb_count)) { 1602 if (!check_deps(deb_file, 0, deb_count)) {
1604 error_msg_and_die("Dependency check failed"); 1603 bb_error_msg_and_die("Dependency check failed");
1605 } 1604 }
1606 } 1605 }
1607 1606
diff --git a/archival/dpkg_deb.c b/archival/dpkg_deb.c
index 2c338227c..2d7383fd7 100644
--- a/archival/dpkg_deb.c
+++ b/archival/dpkg_deb.c
@@ -82,15 +82,15 @@ extern int dpkg_deb_main(int argc, char **argv)
82 tar_archive->action_data = data_extract_all; 82 tar_archive->action_data = data_extract_all;
83 break; 83 break;
84 default: 84 default:
85 show_usage(); 85 bb_show_usage();
86 } 86 }
87 } 87 }
88 88
89 if (optind + 2 < argc) { 89 if (optind + 2 < argc) {
90 show_usage(); 90 bb_show_usage();
91 } 91 }
92 92
93 tar_archive->src_fd = ar_archive->src_fd = xopen(argv[optind++], O_RDONLY); 93 tar_archive->src_fd = ar_archive->src_fd = bb_xopen(argv[optind++], O_RDONLY);
94 94
95 /* Workout where to extract the files */ 95 /* Workout where to extract the files */
96 /* 2nd argument is a dir name */ 96 /* 2nd argument is a dir name */
diff --git a/archival/gunzip.c b/archival/gunzip.c
index e9963a8d2..3350da052 100644
--- a/archival/gunzip.c
+++ b/archival/gunzip.c
@@ -82,7 +82,7 @@ extern int gunzip_main(int argc, char **argv)
82 int opt; 82 int opt;
83 83
84 /* if called as zcat */ 84 /* if called as zcat */
85 if (strcmp(applet_name, "zcat") == 0) { 85 if (strcmp(bb_applet_name, "zcat") == 0) {
86 flags |= gunzip_to_stdout; 86 flags |= gunzip_to_stdout;
87 } 87 }
88 88
@@ -100,7 +100,7 @@ extern int gunzip_main(int argc, char **argv)
100 case 'd': /* Used to convert gzip to gunzip. */ 100 case 'd': /* Used to convert gzip to gunzip. */
101 break; 101 break;
102 default: 102 default:
103 show_usage(); /* exit's inside usage */ 103 bb_show_usage(); /* exit's inside usage */
104 } 104 }
105 } 105 }
106 106
@@ -118,29 +118,29 @@ extern int gunzip_main(int argc, char **argv)
118 src_fd = fileno(stdin); 118 src_fd = fileno(stdin);
119 flags |= gunzip_to_stdout; 119 flags |= gunzip_to_stdout;
120 } else { 120 } else {
121 src_fd = xopen(old_path, O_RDONLY); 121 src_fd = bb_xopen(old_path, O_RDONLY);
122 122
123 /* Get the time stamp on the input file. */ 123 /* Get the time stamp on the input file. */
124 if (stat(old_path, &stat_buf) < 0) { 124 if (stat(old_path, &stat_buf) < 0) {
125 error_msg_and_die("Couldn't stat file %s", old_path); 125 bb_error_msg_and_die("Couldn't stat file %s", old_path);
126 } 126 }
127 } 127 }
128 128
129 /* Check that the input is sane. */ 129 /* Check that the input is sane. */
130 if (isatty(src_fd) && ((flags & gunzip_force) == 0)) { 130 if (isatty(src_fd) && ((flags & gunzip_force) == 0)) {
131 error_msg_and_die 131 bb_error_msg_and_die
132 ("compressed data not read from terminal. Use -f to force it."); 132 ("compressed data not read from terminal. Use -f to force it.");
133 } 133 }
134 134
135 /* Set output filename and number */ 135 /* Set output filename and number */
136 if (flags & gunzip_test) { 136 if (flags & gunzip_test) {
137 dst_fd = xopen("/dev/null", O_WRONLY); /* why does test use filenum 2 ? */ 137 dst_fd = bb_xopen("/dev/null", O_WRONLY); /* why does test use filenum 2 ? */
138 } else if (flags & gunzip_to_stdout) { 138 } else if (flags & gunzip_to_stdout) {
139 dst_fd = fileno(stdout); 139 dst_fd = fileno(stdout);
140 } else { 140 } else {
141 char *extension; 141 char *extension;
142 142
143 new_path = xstrdup(old_path); 143 new_path = bb_xstrdup(old_path);
144 144
145 extension = strrchr(new_path, '.'); 145 extension = strrchr(new_path, '.');
146#ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS 146#ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
@@ -154,11 +154,11 @@ extern int gunzip_main(int argc, char **argv)
154 extension[2] = 'a'; 154 extension[2] = 'a';
155 extension[3] = 'r'; 155 extension[3] = 'r';
156 } else { 156 } else {
157 error_msg_and_die("Invalid extension"); 157 bb_error_msg_and_die("Invalid extension");
158 } 158 }
159 159
160 /* Open output file */ 160 /* Open output file */
161 dst_fd = xopen(new_path, O_WRONLY | O_CREAT); 161 dst_fd = bb_xopen(new_path, O_WRONLY | O_CREAT);
162 162
163 /* Set permissions on the file */ 163 /* Set permissions on the file */
164 chmod(new_path, stat_buf.st_mode); 164 chmod(new_path, stat_buf.st_mode);
@@ -168,10 +168,10 @@ extern int gunzip_main(int argc, char **argv)
168 } 168 }
169 169
170 /* do the decompression, and cleanup */ 170 /* do the decompression, and cleanup */
171 if (xread_char(src_fd) == 0x1f) { 171 if (bb_xread_char(src_fd) == 0x1f) {
172 unsigned char magic2; 172 unsigned char magic2;
173 173
174 magic2 = xread_char(src_fd); 174 magic2 = bb_xread_char(src_fd);
175#ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS 175#ifdef CONFIG_FEATURE_GUNZIP_UNCOMPRESS
176 if (magic2 == 0x9d) { 176 if (magic2 == 0x9d) {
177 status = uncompress(src_fd, dst_fd); 177 status = uncompress(src_fd, dst_fd);
@@ -181,14 +181,14 @@ extern int gunzip_main(int argc, char **argv)
181 check_header_gzip(src_fd); 181 check_header_gzip(src_fd);
182 status = inflate(src_fd, dst_fd); 182 status = inflate(src_fd, dst_fd);
183 if (status != 0) { 183 if (status != 0) {
184 error_msg_and_die("Error inflating"); 184 bb_error_msg_and_die("Error inflating");
185 } 185 }
186 check_trailer_gzip(src_fd); 186 check_trailer_gzip(src_fd);
187 } else { 187 } else {
188 error_msg_and_die("Invalid magic"); 188 bb_error_msg_and_die("Invalid magic");
189 } 189 }
190 } else { 190 } else {
191 error_msg_and_die("Invalid magic"); 191 bb_error_msg_and_die("Invalid magic");
192 } 192 }
193 193
194 if ((status != EXIT_SUCCESS) && (new_path)) { 194 if ((status != EXIT_SUCCESS) && (new_path)) {
@@ -205,7 +205,7 @@ extern int gunzip_main(int argc, char **argv)
205 205
206 /* delete_path will be NULL if in test mode or from stdin */ 206 /* delete_path will be NULL if in test mode or from stdin */
207 if (delete_path && (unlink(delete_path) == -1)) { 207 if (delete_path && (unlink(delete_path) == -1)) {
208 error_msg_and_die("Couldn't remove %s", delete_path); 208 bb_error_msg_and_die("Couldn't remove %s", delete_path);
209 } 209 }
210 210
211 free(new_path); 211 free(new_path);
diff --git a/archival/gzip.c b/archival/gzip.c
index 971724d74..cabc4aa2f 100644
--- a/archival/gzip.c
+++ b/archival/gzip.c
@@ -187,7 +187,7 @@ typedef int file_t; /* Do not use stdio */
187 187
188/* Diagnostic functions */ 188/* Diagnostic functions */
189#ifdef DEBUG 189#ifdef DEBUG
190# define Assert(cond,msg) {if(!(cond)) error_msg(msg);} 190# define Assert(cond,msg) {if(!(cond)) bb_error_msg(msg);}
191# define Trace(x) fprintf x 191# define Trace(x) fprintf x
192# define Tracev(x) {if (verbose) fprintf x ;} 192# define Tracev(x) {if (verbose) fprintf x ;}
193# define Tracevv(x) {if (verbose>1) fprintf x ;} 193# define Tracevv(x) {if (verbose>1) fprintf x ;}
@@ -352,10 +352,10 @@ static void clear_bufs(void)
352 bytes_in = 0L; 352 bytes_in = 0L;
353} 353}
354 354
355static void write_error_msg(void) 355static void write_bb_error_msg(void)
356{ 356{
357 fputc('\n', stderr); 357 fputc('\n', stderr);
358 perror_msg(""); 358 bb_perror_nomsg();
359 abort_gzip(); 359 abort_gzip();
360} 360}
361 361
@@ -369,7 +369,7 @@ static void write_buf(int fd, void *buf, unsigned cnt)
369 369
370 while ((n = write(fd, buf, cnt)) != cnt) { 370 while ((n = write(fd, buf, cnt)) != cnt) {
371 if (n == (unsigned) (-1)) { 371 if (n == (unsigned) (-1)) {
372 write_error_msg(); 372 write_bb_error_msg();
373 } 373 }
374 cnt -= n; 374 cnt -= n;
375 buf = (void *) ((char *) buf + n); 375 buf = (void *) ((char *) buf + n);
@@ -977,11 +977,11 @@ static void check_match(IPos start, IPos match, int length)
977 /* check that the match is indeed a match */ 977 /* check that the match is indeed a match */
978 if (memcmp((char *) window + match, 978 if (memcmp((char *) window + match,
979 (char *) window + start, length) != EQUAL) { 979 (char *) window + start, length) != EQUAL) {
980 error_msg(" start %d, match %d, length %d", start, match, length); 980 bb_error_msg(" start %d, match %d, length %d", start, match, length);
981 error_msg("invalid match"); 981 bb_error_msg("invalid match");
982 } 982 }
983 if (verbose > 1) { 983 if (verbose > 1) {
984 error_msg("\\[%d,%d]", start - match, length); 984 bb_error_msg("\\[%d,%d]", start - match, length);
985 do { 985 do {
986 putc(window[start++], stderr); 986 putc(window[start++], stderr);
987 } while (--length != 0); 987 } while (--length != 0);
@@ -1232,7 +1232,7 @@ int gzip_main(int argc, char **argv)
1232 return gunzip_main(argc, argv); 1232 return gunzip_main(argc, argv);
1233#endif 1233#endif
1234 default: 1234 default:
1235 show_usage(); 1235 bb_show_usage();
1236 } 1236 }
1237 } 1237 }
1238 1238
@@ -1282,7 +1282,7 @@ int gzip_main(int argc, char **argv)
1282 } else { 1282 } else {
1283 inFileNum = open(argv[i], O_RDONLY); 1283 inFileNum = open(argv[i], O_RDONLY);
1284 if (inFileNum < 0 || fstat(inFileNum, &statBuf) < 0) 1284 if (inFileNum < 0 || fstat(inFileNum, &statBuf) < 0)
1285 perror_msg_and_die("%s", argv[i]); 1285 bb_perror_msg_and_die("%s", argv[i]);
1286 time_stamp = statBuf.st_ctime; 1286 time_stamp = statBuf.st_ctime;
1287 ifile_size = statBuf.st_size; 1287 ifile_size = statBuf.st_size;
1288 1288
@@ -1299,7 +1299,7 @@ int gzip_main(int argc, char **argv)
1299 outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL); 1299 outFileNum = open(path, O_RDWR | O_CREAT | O_EXCL);
1300#endif 1300#endif
1301 if (outFileNum < 0) { 1301 if (outFileNum < 0) {
1302 perror_msg("%s", path); 1302 bb_perror_msg("%s", path);
1303 free(path); 1303 free(path);
1304 continue; 1304 continue;
1305 } 1305 }
@@ -1311,7 +1311,7 @@ int gzip_main(int argc, char **argv)
1311 } 1311 }
1312 1312
1313 if (path == NULL && isatty(outFileNum) && force == 0) { 1313 if (path == NULL && isatty(outFileNum) && force == 0) {
1314 error_msg 1314 bb_error_msg
1315 ("compressed data not written to a terminal. Use -f to force compression."); 1315 ("compressed data not written to a terminal. Use -f to force compression.");
1316 free(path); 1316 free(path);
1317 continue; 1317 continue;
@@ -1330,7 +1330,7 @@ int gzip_main(int argc, char **argv)
1330 delFileName = path; 1330 delFileName = path;
1331 1331
1332 if (unlink(delFileName) < 0) 1332 if (unlink(delFileName) < 0)
1333 perror_msg("%s", delFileName); 1333 bb_perror_msg("%s", delFileName);
1334 } 1334 }
1335 1335
1336 free(path); 1336 free(path);
@@ -1655,7 +1655,7 @@ static void set_file_type(void);
1655 1655
1656#else /* DEBUG */ 1656#else /* DEBUG */
1657# define send_code(c, tree) \ 1657# define send_code(c, tree) \
1658 { if (verbose>1) error_msg("\ncd %3d ",(c)); \ 1658 { if (verbose>1) bb_error_msg("\ncd %3d ",(c)); \
1659 send_bits(tree[c].Code, tree[c].Len); } 1659 send_bits(tree[c].Code, tree[c].Len); }
1660#endif 1660#endif
1661 1661
@@ -2035,7 +2035,7 @@ static void build_tree(tree_desc * desc)
2035 tree[n].Dad = tree[m].Dad = (ush) node; 2035 tree[n].Dad = tree[m].Dad = (ush) node;
2036#ifdef DUMP_BL_TREE 2036#ifdef DUMP_BL_TREE
2037 if (tree == bl_tree) { 2037 if (tree == bl_tree) {
2038 error_msg("\nnode %d(%d), sons %d(%d) %d(%d)", 2038 bb_error_msg("\nnode %d(%d), sons %d(%d) %d(%d)",
2039 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq); 2039 node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
2040 } 2040 }
2041#endif 2041#endif
@@ -2273,7 +2273,7 @@ static ulg flush_block(char *buf, ulg stored_len, int eof)
2273 if (stored_len <= opt_lenb && eof && compressed_len == 0L && seekable()) { 2273 if (stored_len <= opt_lenb && eof && compressed_len == 0L && seekable()) {
2274 /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */ 2274 /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
2275 if (buf == (char *) 0) 2275 if (buf == (char *) 0)
2276 error_msg("block vanished"); 2276 bb_error_msg("block vanished");
2277 2277
2278 copy_block(buf, (unsigned) stored_len, 0); /* without header */ 2278 copy_block(buf, (unsigned) stored_len, 0); /* without header */
2279 compressed_len = stored_len << 3; 2279 compressed_len = stored_len << 3;
@@ -2442,7 +2442,7 @@ static void set_file_type()
2442 bin_freq += dyn_ltree[n++].Freq; 2442 bin_freq += dyn_ltree[n++].Freq;
2443 *file_type = bin_freq > (ascii_freq >> 2) ? BINARY : ASCII; 2443 *file_type = bin_freq > (ascii_freq >> 2) ? BINARY : ASCII;
2444 if (*file_type == BINARY && translate_eol) { 2444 if (*file_type == BINARY && translate_eol) {
2445 error_msg("-l used on binary file"); 2445 bb_error_msg("-l used on binary file");
2446 } 2446 }
2447} 2447}
2448 2448
diff --git a/archival/libunarchive/archive_copy_file.c b/archival/libunarchive/archive_copy_file.c
index faa8059ef..675bc6ffe 100644
--- a/archival/libunarchive/archive_copy_file.c
+++ b/archival/libunarchive/archive_copy_file.c
@@ -35,7 +35,7 @@ extern void archive_copy_file(const archive_handle_t *archive_handle, const int
35 size = archive_xread(archive_handle, buffer, size); 35 size = archive_xread(archive_handle, buffer, size);
36 36
37 if (write(dst_fd, buffer, size) != size) { 37 if (write(dst_fd, buffer, size) != size) {
38 error_msg_and_die ("Short write"); 38 bb_error_msg_and_die ("Short write");
39 } 39 }
40 chunksize -= size; 40 chunksize -= size;
41 } 41 }
diff --git a/archival/libunarchive/archive_xread.c b/archival/libunarchive/archive_xread.c
index 7fde4c0b1..0b29dbfb9 100644
--- a/archival/libunarchive/archive_xread.c
+++ b/archival/libunarchive/archive_xread.c
@@ -26,7 +26,7 @@ extern ssize_t archive_xread(const archive_handle_t *archive_handle, unsigned ch
26 26
27 size = archive_handle->read(archive_handle->src_fd, buf, count); 27 size = archive_handle->read(archive_handle->src_fd, buf, count);
28 if (size == -1) { 28 if (size == -1) {
29 perror_msg_and_die("Read error"); 29 bb_perror_msg_and_die("Read error");
30 } 30 }
31 31
32 return(size); 32 return(size);
diff --git a/archival/libunarchive/archive_xread_all.c b/archival/libunarchive/archive_xread_all.c
index ef8cc0141..cfe046b27 100644
--- a/archival/libunarchive/archive_xread_all.c
+++ b/archival/libunarchive/archive_xread_all.c
@@ -26,7 +26,7 @@ extern void archive_xread_all(const archive_handle_t *archive_handle, void *buf,
26 26
27 size = archive_xread(archive_handle, buf, count); 27 size = archive_xread(archive_handle, buf, count);
28 if (size != count) { 28 if (size != count) {
29 error_msg_and_die("Short read"); 29 bb_error_msg_and_die("Short read");
30 } 30 }
31 return; 31 return;
32} 32}
diff --git a/archival/libunarchive/archive_xread_all_eof.c b/archival/libunarchive/archive_xread_all_eof.c
index 3cfbbd8d1..23719cd7b 100644
--- a/archival/libunarchive/archive_xread_all_eof.c
+++ b/archival/libunarchive/archive_xread_all_eof.c
@@ -26,7 +26,7 @@ extern ssize_t archive_xread_all_eof(archive_handle_t *archive_handle, unsigned
26 26
27 size = archive_xread(archive_handle, buf, count); 27 size = archive_xread(archive_handle, buf, count);
28 if ((size != 0) && (size != count)) { 28 if ((size != 0) && (size != count)) {
29 perror_msg_and_die("Short read, read %d of %d", size, count); 29 bb_perror_msg_and_die("Short read, read %d of %d", size, count);
30 } 30 }
31 return(size); 31 return(size);
32} 32}
diff --git a/archival/libunarchive/check_header_gzip.c b/archival/libunarchive/check_header_gzip.c
index d661df7cc..13832c240 100644
--- a/archival/libunarchive/check_header_gzip.c
+++ b/archival/libunarchive/check_header_gzip.c
@@ -15,11 +15,11 @@ extern void check_header_gzip(int src_fd)
15 } formated; 15 } formated;
16 } header; 16 } header;
17 17
18 xread_all(src_fd, header.raw, 8); 18 bb_xread_all(src_fd, header.raw, 8);
19 19
20 /* Check the compression method */ 20 /* Check the compression method */
21 if (header.formated.method != 8) { 21 if (header.formated.method != 8) {
22 error_msg_and_die("Unknown compression method %d", 22 bb_error_msg_and_die("Unknown compression method %d",
23 header.formated.method); 23 header.formated.method);
24 } 24 }
25 25
@@ -27,10 +27,10 @@ extern void check_header_gzip(int src_fd)
27 /* bit 2 set: extra field present */ 27 /* bit 2 set: extra field present */
28 unsigned char extra_short; 28 unsigned char extra_short;
29 29
30 extra_short = xread_char(src_fd) + (xread_char(src_fd) << 8); 30 extra_short = bb_xread_char(src_fd) + (bb_xread_char(src_fd) << 8);
31 while (extra_short > 0) { 31 while (extra_short > 0) {
32 /* Ignore extra field */ 32 /* Ignore extra field */
33 xread_char(src_fd); 33 bb_xread_char(src_fd);
34 extra_short--; 34 extra_short--;
35 } 35 }
36 } 36 }
@@ -38,19 +38,19 @@ extern void check_header_gzip(int src_fd)
38 /* Discard original name if any */ 38 /* Discard original name if any */
39 if (header.formated.flags & 0x08) { 39 if (header.formated.flags & 0x08) {
40 /* bit 3 set: original file name present */ 40 /* bit 3 set: original file name present */
41 while(xread_char(src_fd) != 0); 41 while(bb_xread_char(src_fd) != 0);
42 } 42 }
43 43
44 /* Discard file comment if any */ 44 /* Discard file comment if any */
45 if (header.formated.flags & 0x10) { 45 if (header.formated.flags & 0x10) {
46 /* bit 4 set: file comment present */ 46 /* bit 4 set: file comment present */
47 while(xread_char(src_fd) != 0); 47 while(bb_xread_char(src_fd) != 0);
48 } 48 }
49 49
50 /* Read the header checksum */ 50 /* Read the header checksum */
51 if (header.formated.flags & 0x02) { 51 if (header.formated.flags & 0x02) {
52 xread_char(src_fd); 52 bb_xread_char(src_fd);
53 xread_char(src_fd); 53 bb_xread_char(src_fd);
54 } 54 }
55 55
56 return; 56 return;
diff --git a/archival/libunarchive/data_extract_all.c b/archival/libunarchive/data_extract_all.c
index 1eb8bb388..77b4de593 100644
--- a/archival/libunarchive/data_extract_all.c
+++ b/archival/libunarchive/data_extract_all.c
@@ -34,8 +34,8 @@ extern void data_extract_all(archive_handle_t *archive_handle)
34 int res; 34 int res;
35 35
36 if (archive_handle->flags & ARCHIVE_CREATE_LEADING_DIRS) { 36 if (archive_handle->flags & ARCHIVE_CREATE_LEADING_DIRS) {
37 char *name = xstrdup(file_header->name); 37 char *name = bb_xstrdup(file_header->name);
38 make_directory (dirname(name), 0777, FILEUTILS_RECUR); 38 bb_make_directory (dirname(name), 0777, FILEUTILS_RECUR);
39 free(name); 39 free(name);
40 } 40 }
41 41
@@ -47,13 +47,13 @@ extern void data_extract_all(archive_handle_t *archive_handle)
47 /* hard link */ 47 /* hard link */
48 res = link(file_header->link_name, file_header->name); 48 res = link(file_header->link_name, file_header->name);
49 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) { 49 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
50 perror_msg("Couldnt create hard link"); 50 bb_perror_msg("Couldnt create hard link");
51 } 51 }
52 } else 52 } else
53#endif 53#endif
54 { 54 {
55 /* Regular file */ 55 /* Regular file */
56 dst_fd = xopen(file_header->name, O_WRONLY | O_CREAT); 56 dst_fd = bb_xopen(file_header->name, O_WRONLY | O_CREAT);
57 archive_copy_file(archive_handle, dst_fd); 57 archive_copy_file(archive_handle, dst_fd);
58 close(dst_fd); 58 close(dst_fd);
59 } 59 }
@@ -63,7 +63,7 @@ extern void data_extract_all(archive_handle_t *archive_handle)
63 unlink(file_header->name); 63 unlink(file_header->name);
64 res = mkdir(file_header->name, file_header->mode); 64 res = mkdir(file_header->name, file_header->mode);
65 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) { 65 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
66 perror_msg("extract_archive: %s", file_header->name); 66 bb_perror_msg("extract_archive: %s", file_header->name);
67 } 67 }
68 break; 68 break;
69 case S_IFLNK: 69 case S_IFLNK:
@@ -71,7 +71,7 @@ extern void data_extract_all(archive_handle_t *archive_handle)
71 unlink(file_header->name); 71 unlink(file_header->name);
72 res = symlink(file_header->link_name, file_header->name); 72 res = symlink(file_header->link_name, file_header->name);
73 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) { 73 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
74 perror_msg("Cannot create symlink from %s to '%s'", file_header->name, file_header->link_name); 74 bb_perror_msg("Cannot create symlink from %s to '%s'", file_header->name, file_header->link_name);
75 } 75 }
76 break; 76 break;
77 case S_IFSOCK: 77 case S_IFSOCK:
@@ -81,11 +81,11 @@ extern void data_extract_all(archive_handle_t *archive_handle)
81 unlink(file_header->name); 81 unlink(file_header->name);
82 res = mknod(file_header->name, file_header->mode, file_header->device); 82 res = mknod(file_header->name, file_header->mode, file_header->device);
83 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) { 83 if ((res == -1) && !(archive_handle->flags & ARCHIVE_EXTRACT_QUIET)) {
84 perror_msg("Cannot create node %s", file_header->name); 84 bb_perror_msg("Cannot create node %s", file_header->name);
85 } 85 }
86 break; 86 break;
87 default: 87 default:
88 error_msg_and_die("Unrecognised file type"); 88 bb_error_msg_and_die("Unrecognised file type");
89 } 89 }
90 90
91 chmod(file_header->name, file_header->mode); 91 chmod(file_header->name, file_header->mode);
diff --git a/archival/libunarchive/decompress_bunzip2.c b/archival/libunarchive/decompress_bunzip2.c
index 4b611b833..0164b77e0 100644
--- a/archival/libunarchive/decompress_bunzip2.c
+++ b/archival/libunarchive/decompress_bunzip2.c
@@ -1548,7 +1548,7 @@ extern ssize_t read_bz2(int fd, void *buf, size_t count)
1548 1548
1549 while (1) { 1549 while (1) {
1550 if (bzf->strm.avail_in == 0) { 1550 if (bzf->strm.avail_in == 0) {
1551 n = xread(bzf->fd, bzf->buf, BZ_MAX_UNUSED); 1551 n = bb_xread(bzf->fd, bzf->buf, BZ_MAX_UNUSED);
1552 if (n == 0) { 1552 if (n == 0) {
1553 break; 1553 break;
1554 } 1554 }
@@ -1560,7 +1560,7 @@ extern ssize_t read_bz2(int fd, void *buf, size_t count)
1560 ret = BZ2_bzDecompress(&(bzf->strm)); 1560 ret = BZ2_bzDecompress(&(bzf->strm));
1561 1561
1562 if ((ret != BZ_OK) && (ret != BZ_STREAM_END)) { 1562 if ((ret != BZ_OK) && (ret != BZ_STREAM_END)) {
1563 error_msg_and_die("Error decompressing"); 1563 bb_error_msg_and_die("Error decompressing");
1564 } 1564 }
1565 1565
1566 if (ret == BZ_STREAM_END) { 1566 if (ret == BZ_STREAM_END) {
@@ -1628,12 +1628,12 @@ extern unsigned char uncompressStream(int src_fd, int dst_fd)
1628 while (bzerr == BZ_OK) { 1628 while (bzerr == BZ_OK) {
1629 nread = read_bz2(src_fd, obuf, 5000); 1629 nread = read_bz2(src_fd, obuf, 5000);
1630 if (bzerr == BZ_DATA_ERROR_MAGIC) { 1630 if (bzerr == BZ_DATA_ERROR_MAGIC) {
1631 error_msg_and_die("invalid magic"); 1631 bb_error_msg_and_die("invalid magic");
1632 } 1632 }
1633 if (((bzerr == BZ_OK) || (bzerr == BZ_STREAM_END)) && (nread > 0)) { 1633 if (((bzerr == BZ_OK) || (bzerr == BZ_STREAM_END)) && (nread > 0)) {
1634 if (write(dst_fd, obuf, nread) != nread) { 1634 if (write(dst_fd, obuf, nread) != nread) {
1635 BZ2_bzReadClose(); 1635 BZ2_bzReadClose();
1636 perror_msg_and_die("Couldnt write to file"); 1636 bb_perror_msg_and_die("Couldnt write to file");
1637 } 1637 }
1638 } 1638 }
1639 } 1639 }
diff --git a/archival/libunarchive/decompress_uncompress.c b/archival/libunarchive/decompress_uncompress.c
index 192e98126..9851ca39d 100644
--- a/archival/libunarchive/decompress_uncompress.c
+++ b/archival/libunarchive/decompress_uncompress.c
@@ -121,14 +121,14 @@ extern int uncompress(int fd_in, int fd_out)
121 121
122 insize = 0; 122 insize = 0;
123 123
124 inbuf[0] = xread_char(fd_in); 124 inbuf[0] = bb_xread_char(fd_in);
125 125
126 maxbits = inbuf[0] & BIT_MASK; 126 maxbits = inbuf[0] & BIT_MASK;
127 block_mode = inbuf[0] & BLOCK_MODE; 127 block_mode = inbuf[0] & BLOCK_MODE;
128 maxmaxcode = MAXCODE(maxbits); 128 maxmaxcode = MAXCODE(maxbits);
129 129
130 if (maxbits > BITS) { 130 if (maxbits > BITS) {
131 error_msg("compressed with %d bits, can only handle %d bits", maxbits, 131 bb_error_msg("compressed with %d bits, can only handle %d bits", maxbits,
132 BITS); 132 BITS);
133 return -1; 133 return -1;
134 } 134 }
@@ -227,11 +227,11 @@ extern int uncompress(int fd_in, int fd_out)
227 posbits -= n_bits; 227 posbits -= n_bits;
228 p = &inbuf[posbits >> 3]; 228 p = &inbuf[posbits >> 3];
229 229
230 error_msg 230 bb_error_msg
231 ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)", 231 ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
232 insize, posbits, p[-1], p[0], p[1], p[2], p[3], 232 insize, posbits, p[-1], p[0], p[1], p[2], p[3],
233 (posbits & 07)); 233 (posbits & 07));
234 error_msg("uncompress: corrupt input"); 234 bb_error_msg("uncompress: corrupt input");
235 return -1; 235 return -1;
236 } 236 }
237 237
diff --git a/archival/libunarchive/decompress_unzip.c b/archival/libunarchive/decompress_unzip.c
index 3a7334ce9..2401cf831 100644
--- a/archival/libunarchive/decompress_unzip.c
+++ b/archival/libunarchive/decompress_unzip.c
@@ -150,7 +150,7 @@ static void fill_bytebuffer(void)
150 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer 150 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
151 * to the front of the bytebuffer, leave 4 bytes free at end of tail 151 * to the front of the bytebuffer, leave 4 bytes free at end of tail
152 * so we can easily top up buffer in check_trailer_gzip() */ 152 * so we can easily top up buffer in check_trailer_gzip() */
153 bytebuffer_size = 4 + xread(gunzip_src_fd, &bytebuffer[4], BYTEBUFFER_MAX - 8); 153 bytebuffer_size = 4 + bb_xread(gunzip_src_fd, &bytebuffer[4], BYTEBUFFER_MAX - 8);
154 bytebuffer_offset = 4; 154 bytebuffer_offset = 4;
155 } 155 }
156} 156}
@@ -448,7 +448,7 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_b
448 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) 448 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
449 do { 449 do {
450 if (e == 99) { 450 if (e == 99) {
451 error_msg_and_die("inflate_codes error 1");; 451 bb_error_msg_and_die("inflate_codes error 1");;
452 } 452 }
453 b >>= t->b; 453 b >>= t->b;
454 k -= t->b; 454 k -= t->b;
@@ -484,7 +484,7 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_b
484 if ((e = (t = td + ((unsigned) b & md))->e) > 16) 484 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
485 do { 485 do {
486 if (e == 99) 486 if (e == 99)
487 error_msg_and_die("inflate_codes error 2");; 487 bb_error_msg_and_die("inflate_codes error 2");;
488 b >>= t->b; 488 b >>= t->b;
489 k -= t->b; 489 k -= t->b;
490 e -= 16; 490 e -= 16;
@@ -821,7 +821,7 @@ static int inflate_block(int *e)
821 821
822 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { 822 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
823 if (i == 1) { 823 if (i == 1) {
824 error_msg_and_die("Incomplete literal tree"); 824 bb_error_msg_and_die("Incomplete literal tree");
825 huft_free(tl); 825 huft_free(tl);
826 } 826 }
827 return i; /* incomplete code set */ 827 return i; /* incomplete code set */
@@ -830,7 +830,7 @@ static int inflate_block(int *e)
830 bd = dbits; 830 bd = dbits;
831 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) { 831 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
832 if (i == 1) { 832 if (i == 1) {
833 error_msg_and_die("incomplete distance tree"); 833 bb_error_msg_and_die("incomplete distance tree");
834 huft_free(td); 834 huft_free(td);
835 } 835 }
836 huft_free(tl); 836 huft_free(tl);
@@ -846,7 +846,7 @@ static int inflate_block(int *e)
846 } 846 }
847 default: 847 default:
848 /* bad block type */ 848 /* bad block type */
849 error_msg_and_die("bad block type %d\n", t); 849 bb_error_msg_and_die("bad block type %d\n", t);
850 } 850 }
851} 851}
852 852
@@ -884,7 +884,7 @@ static int inflate_get_next_window(void)
884 break; 884 break;
885 case -2: ret = inflate_codes(0,0,0,0,0); 885 case -2: ret = inflate_codes(0,0,0,0,0);
886 break; 886 break;
887 default: error_msg_and_die("inflate error %d", method); 887 default: bb_error_msg_and_die("inflate error %d", method);
888 } 888 }
889 889
890 if (ret == 1) { 890 if (ret == 1) {
@@ -975,16 +975,16 @@ extern void GZ_gzReadClose(void)
975 ssize_t nread, nwrote; 975 ssize_t nread, nwrote;
976 976
977 GZ_gzReadOpen(in, 0, 0); 977 GZ_gzReadOpen(in, 0, 0);
978 while(1) { // Robbed from copyfd.c 978 while(1) { // Robbed from bb_copyfd.c
979 nread = read_gz(in, buf, sizeof(buf)); 979 nread = read_gz(in, buf, sizeof(buf));
980 if (nread == 0) break; // no data to write 980 if (nread == 0) break; // no data to write
981 else if (nread == -1) { 981 else if (nread == -1) {
982 perror_msg("read"); 982 bb_perror_msg("read");
983 return -1; 983 return -1;
984 } 984 }
985 nwrote = full_write(out, buf, nread); 985 nwrote = bb_full_write(out, buf, nread);
986 if (nwrote == -1) { 986 if (nwrote == -1) {
987 perror_msg("write"); 987 bb_perror_msg("write");
988 return -1; 988 return -1;
989 } 989 }
990 } 990 }
@@ -998,9 +998,9 @@ extern int inflate(int in, int out)
998 GZ_gzReadOpen(in, 0, 0); 998 GZ_gzReadOpen(in, 0, 0);
999 while(1) { 999 while(1) {
1000 int ret = inflate_get_next_window(); 1000 int ret = inflate_get_next_window();
1001 nwrote = full_write(out, gunzip_window, gunzip_outbuf_count); 1001 nwrote = bb_full_write(out, gunzip_window, gunzip_outbuf_count);
1002 if (nwrote == -1) { 1002 if (nwrote == -1) {
1003 perror_msg("write"); 1003 bb_perror_msg("write");
1004 return -1; 1004 return -1;
1005 } 1005 }
1006 if (ret == 0) break; 1006 if (ret == 0) break;
@@ -1017,7 +1017,7 @@ extern void check_trailer_gzip(int src_fd)
1017 /* top up the input buffer with the rest of the trailer */ 1017 /* top up the input buffer with the rest of the trailer */
1018 count = bytebuffer_size - bytebuffer_offset; 1018 count = bytebuffer_size - bytebuffer_offset;
1019 if (count < 8) { 1019 if (count < 8) {
1020 xread_all(src_fd, &bytebuffer[bytebuffer_size], 8 - count); 1020 bb_xread_all(src_fd, &bytebuffer[bytebuffer_size], 8 - count);
1021 bytebuffer_size += 8 - count; 1021 bytebuffer_size += 8 - count;
1022 } 1022 }
1023 for (count = 0; count != 4; count++) { 1023 for (count = 0; count != 4; count++) {
@@ -1027,14 +1027,14 @@ extern void check_trailer_gzip(int src_fd)
1027 1027
1028 /* Validate decompression - crc */ 1028 /* Validate decompression - crc */
1029 if (stored_crc != (gunzip_crc ^ 0xffffffffL)) { 1029 if (stored_crc != (gunzip_crc ^ 0xffffffffL)) {
1030 error_msg_and_die("crc error"); 1030 bb_error_msg_and_die("crc error");
1031 } 1031 }
1032 1032
1033 /* Validate decompression - size */ 1033 /* Validate decompression - size */
1034 if (gunzip_bytes_out != 1034 if (gunzip_bytes_out !=
1035 (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) | 1035 (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
1036 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) { 1036 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) {
1037 error_msg_and_die("Incorrect length, but crc is correct"); 1037 bb_error_msg_and_die("Incorrect length, but crc is correct");
1038 } 1038 }
1039 1039
1040} 1040}
diff --git a/archival/libunarchive/get_header_ar.c b/archival/libunarchive/get_header_ar.c
index 1e0e6c12d..6c576a8da 100644
--- a/archival/libunarchive/get_header_ar.c
+++ b/archival/libunarchive/get_header_ar.c
@@ -41,7 +41,7 @@ extern char get_header_ar(archive_handle_t *archive_handle)
41 static unsigned int ar_long_name_size; 41 static unsigned int ar_long_name_size;
42#endif 42#endif
43 43
44 /* dont use xread as we want to handle the error ourself */ 44 /* dont use bb_xread as we want to handle the error ourself */
45 if (read(archive_handle->src_fd, ar.raw, 60) != 60) { 45 if (read(archive_handle->src_fd, ar.raw, 60) != 60) {
46 /* End Of File */ 46 /* End Of File */
47 return(EXIT_FAILURE); 47 return(EXIT_FAILURE);
@@ -51,14 +51,14 @@ extern char get_header_ar(archive_handle_t *archive_handle)
51 if (ar.raw[0] == '\n') { 51 if (ar.raw[0] == '\n') {
52 /* fix up the header, we started reading 1 byte too early */ 52 /* fix up the header, we started reading 1 byte too early */
53 memmove(ar.raw, &ar.raw[1], 59); 53 memmove(ar.raw, &ar.raw[1], 59);
54 ar.raw[59] = xread_char(archive_handle->src_fd); 54 ar.raw[59] = bb_xread_char(archive_handle->src_fd);
55 archive_handle->offset++; 55 archive_handle->offset++;
56 } 56 }
57 archive_handle->offset += 60; 57 archive_handle->offset += 60;
58 58
59 /* align the headers based on the header magic */ 59 /* align the headers based on the header magic */
60 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) { 60 if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) {
61 error_msg_and_die("Invalid ar header"); 61 bb_error_msg_and_die("Invalid ar header");
62 } 62 }
63 63
64 typed->mode = strtol(ar.formated.mode, NULL, 8); 64 typed->mode = strtol(ar.formated.mode, NULL, 8);
@@ -76,7 +76,7 @@ extern char get_header_ar(archive_handle_t *archive_handle)
76 * in static variable long_names for use in future entries */ 76 * in static variable long_names for use in future entries */
77 ar_long_name_size = typed->size; 77 ar_long_name_size = typed->size;
78 ar_long_names = xmalloc(ar_long_name_size); 78 ar_long_names = xmalloc(ar_long_name_size);
79 xread_all(archive_handle->src_fd, ar_long_names, ar_long_name_size); 79 bb_xread_all(archive_handle->src_fd, ar_long_names, ar_long_name_size);
80 archive_handle->offset += ar_long_name_size; 80 archive_handle->offset += ar_long_name_size;
81 /* This ar entries data section only contained filenames for other records 81 /* This ar entries data section only contained filenames for other records
82 * they are stored in the static ar_long_names for future reference */ 82 * they are stored in the static ar_long_names for future reference */
@@ -90,16 +90,16 @@ extern char get_header_ar(archive_handle_t *archive_handle)
90 (saved in variable long_name) that conatains the real filename */ 90 (saved in variable long_name) that conatains the real filename */
91 const unsigned int long_offset = atoi(&ar.formated.name[1]); 91 const unsigned int long_offset = atoi(&ar.formated.name[1]);
92 if (long_offset >= ar_long_name_size) { 92 if (long_offset >= ar_long_name_size) {
93 error_msg_and_die("Cant resolve long filename"); 93 bb_error_msg_and_die("Cant resolve long filename");
94 } 94 }
95 typed->name = xstrdup(ar_long_names + long_offset); 95 typed->name = bb_xstrdup(ar_long_names + long_offset);
96 } 96 }
97#else 97#else
98 error_msg_and_die("long filenames not supported"); 98 bb_error_msg_and_die("long filenames not supported");
99#endif 99#endif
100 } else { 100 } else {
101 /* short filenames */ 101 /* short filenames */
102 typed->name = xstrndup(ar.formated.name, 16); 102 typed->name = bb_xstrndup(ar.formated.name, 16);
103 } 103 }
104 104
105 typed->name[strcspn(typed->name, " /")] = '\0'; 105 typed->name[strcspn(typed->name, " /")] = '\0';
diff --git a/archival/libunarchive/get_header_cpio.c b/archival/libunarchive/get_header_cpio.c
index ea0857840..975e2a4bd 100644
--- a/archival/libunarchive/get_header_cpio.c
+++ b/archival/libunarchive/get_header_cpio.c
@@ -46,7 +46,7 @@ extern char get_header_cpio(archive_handle_t *archive_handle)
46 oldtmp = NULL; 46 oldtmp = NULL;
47 47
48 while (tmp) { 48 while (tmp) {
49 error_msg_and_die("need to fix this\n"); 49 bb_error_msg_and_die("need to fix this\n");
50 if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */ 50 if (tmp->entry->link_name) { /* Found a hardlink ready to be extracted */
51 file_header = tmp->entry; 51 file_header = tmp->entry;
52 if (oldtmp) { 52 if (oldtmp) {
@@ -78,11 +78,11 @@ extern char get_header_cpio(archive_handle_t *archive_handle)
78 cpio_header[2], 78 cpio_header[2],
79 cpio_header[3], 79 cpio_header[3],
80 cpio_header[4]); 80 cpio_header[4]);
81 error_msg_and_die("Unsupported cpio format"); 81 bb_error_msg_and_die("Unsupported cpio format");
82 } 82 }
83 83
84 if ((cpio_header[5] != '1') && (cpio_header[5] != '2')) { 84 if ((cpio_header[5] != '1') && (cpio_header[5] != '2')) {
85 error_msg_and_die("Unsupported cpio format, use newc or crc"); 85 bb_error_msg_and_die("Unsupported cpio format, use newc or crc");
86 } 86 }
87 87
88 { 88 {
@@ -109,7 +109,7 @@ extern char get_header_cpio(archive_handle_t *archive_handle)
109 hardlinks_t *tmp = saved_hardlinks; 109 hardlinks_t *tmp = saved_hardlinks;
110 hardlinks_t *oldtmp = NULL; 110 hardlinks_t *oldtmp = NULL;
111 while (tmp) { 111 while (tmp) {
112 error_msg("%s not created: cannot resolve hardlink", tmp->entry->name); 112 bb_error_msg("%s not created: cannot resolve hardlink", tmp->entry->name);
113 oldtmp = tmp; 113 oldtmp = tmp;
114 tmp = tmp->next; 114 tmp = tmp->next;
115 free (oldtmp->entry->name); 115 free (oldtmp->entry->name);
@@ -142,13 +142,13 @@ extern char get_header_cpio(archive_handle_t *archive_handle)
142 pending_hardlinks = 1; 142 pending_hardlinks = 1;
143 while (tmp) { 143 while (tmp) {
144 if (tmp->inode == inode) { 144 if (tmp->inode == inode) {
145 tmp->entry->link_name = xstrdup(file_header->name); 145 tmp->entry->link_name = bb_xstrdup(file_header->name);
146 nlink--; 146 nlink--;
147 } 147 }
148 tmp = tmp->next; 148 tmp = tmp->next;
149 } 149 }
150 if (nlink > 1) { 150 if (nlink > 1) {
151 error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?"); 151 bb_error_msg("error resolving hardlink: did you create the archive with GNU cpio 2.0-2.2?");
152 } 152 }
153 } 153 }
154 } 154 }
@@ -165,11 +165,11 @@ extern char get_header_cpio(archive_handle_t *archive_handle)
165 if ((archive_handle->flags & ARCHIVE_EXTRACT_UNCONDITIONAL) || (statbuf.st_mtime < file_header->mtime)) { 165 if ((archive_handle->flags & ARCHIVE_EXTRACT_UNCONDITIONAL) || (statbuf.st_mtime < file_header->mtime)) {
166 /* Remove file if flag set or its older than the file to be extracted */ 166 /* Remove file if flag set or its older than the file to be extracted */
167 if (unlink(file_header->name) == -1) { 167 if (unlink(file_header->name) == -1) {
168 perror_msg_and_die("Couldnt remove old file"); 168 bb_perror_msg_and_die("Couldnt remove old file");
169 } 169 }
170 } else { 170 } else {
171 if (! archive_handle->flags & ARCHIVE_EXTRACT_QUIET) { 171 if (! archive_handle->flags & ARCHIVE_EXTRACT_QUIET) {
172 error_msg("%s not created: newer or same age file exists", file_header->name); 172 bb_error_msg("%s not created: newer or same age file exists", file_header->name);
173 } 173 }
174 extract_flag = FALSE; 174 extract_flag = FALSE;
175 } 175 }
diff --git a/archival/libunarchive/get_header_tar.c b/archival/libunarchive/get_header_tar.c
index 2cb141ede..365f464dd 100644
--- a/archival/libunarchive/get_header_tar.c
+++ b/archival/libunarchive/get_header_tar.c
@@ -75,7 +75,7 @@ extern char get_header_tar(archive_handle_t *archive_handle)
75#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY 75#ifdef CONFIG_FEATURE_TAR_OLDGNU_COMPATABILITY
76 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0) 76 if (strncmp(tar.formated.magic, "\0\0\0\0\0", 5) != 0)
77#endif 77#endif
78 error_msg_and_die("Invalid tar magic"); 78 bb_error_msg_and_die("Invalid tar magic");
79 } 79 }
80 /* Do checksum on headers */ 80 /* Do checksum on headers */
81 for (i = 0; i < 148 ; i++) { 81 for (i = 0; i < 148 ; i++) {
@@ -86,7 +86,7 @@ extern char get_header_tar(archive_handle_t *archive_handle)
86 sum += tar.raw[i]; 86 sum += tar.raw[i];
87 } 87 }
88 if (sum != strtol(tar.formated.chksum, NULL, 8)) { 88 if (sum != strtol(tar.formated.chksum, NULL, 8)) {
89 error_msg("Invalid tar header checksum"); 89 bb_error_msg("Invalid tar header checksum");
90 return(EXIT_FAILURE); 90 return(EXIT_FAILURE);
91 } 91 }
92 92
@@ -116,7 +116,7 @@ extern char get_header_tar(archive_handle_t *archive_handle)
116 file_header->size = strtol(tar.formated.size, NULL, 8); 116 file_header->size = strtol(tar.formated.size, NULL, 8);
117 file_header->mtime = strtol(tar.formated.mtime, NULL, 8); 117 file_header->mtime = strtol(tar.formated.mtime, NULL, 8);
118 file_header->link_name = (tar.formated.linkname[0] != '\0') ? 118 file_header->link_name = (tar.formated.linkname[0] != '\0') ?
119 xstrdup(tar.formated.linkname) : NULL; 119 bb_xstrdup(tar.formated.linkname) : NULL;
120 file_header->device = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) + 120 file_header->device = (dev_t) ((strtol(tar.formated.devmajor, NULL, 8) << 8) +
121 strtol(tar.formated.devminor, NULL, 8)); 121 strtol(tar.formated.devminor, NULL, 8));
122 122
@@ -129,7 +129,7 @@ extern char get_header_tar(archive_handle_t *archive_handle)
129 file_header->mode |= S_IFREG; 129 file_header->mode |= S_IFREG;
130 break; 130 break;
131 case '1': 131 case '1':
132 error_msg("Internal hard link not supported"); 132 bb_error_msg("Internal hard link not supported");
133 break; 133 break;
134 case '2': 134 case '2':
135 file_header->mode |= S_IFLNK; 135 file_header->mode |= S_IFLNK;
@@ -170,7 +170,7 @@ extern char get_header_tar(archive_handle_t *archive_handle)
170 case 'N': 170 case 'N':
171 case 'S': 171 case 'S':
172 case 'V': 172 case 'V':
173 error_msg("Ignoring GNU extension type %c", tar.formated.typeflag); 173 bb_error_msg("Ignoring GNU extension type %c", tar.formated.typeflag);
174# endif 174# endif
175 } 175 }
176#endif 176#endif
diff --git a/archival/libunarchive/get_header_tar_gz.c b/archival/libunarchive/get_header_tar_gz.c
index a4355d24c..7792432ae 100644
--- a/archival/libunarchive/get_header_tar_gz.c
+++ b/archival/libunarchive/get_header_tar_gz.c
@@ -25,7 +25,7 @@ extern char get_header_tar_gz(archive_handle_t *archive_handle)
25 25
26 archive_xread_all(archive_handle, &magic, 2); 26 archive_xread_all(archive_handle, &magic, 2);
27 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { 27 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
28 error_msg_and_die("Invalid gzip magic"); 28 bb_error_msg_and_die("Invalid gzip magic");
29 } 29 }
30 30
31 check_header_gzip(archive_handle->src_fd); 31 check_header_gzip(archive_handle->src_fd);
diff --git a/archival/libunarchive/header_verbose_list.c b/archival/libunarchive/header_verbose_list.c
index ff7b3bca2..6739dd393 100644
--- a/archival/libunarchive/header_verbose_list.c
+++ b/archival/libunarchive/header_verbose_list.c
@@ -9,7 +9,7 @@ extern void header_verbose_list(const file_header_t *file_header)
9 struct tm *mtime = localtime(&file_header->mtime); 9 struct tm *mtime = localtime(&file_header->mtime);
10 10
11 printf("%s %d/%d%10u %4u-%02u-%02u %02u:%02u:%02u %s", 11 printf("%s %d/%d%10u %4u-%02u-%02u %02u:%02u:%02u %s",
12 mode_string(file_header->mode), 12 bb_mode_string(file_header->mode),
13 file_header->uid, 13 file_header->uid,
14 file_header->gid, 14 file_header->gid,
15 (unsigned int) file_header->size, 15 (unsigned int) file_header->size,
diff --git a/archival/libunarchive/seek_by_jump.c b/archival/libunarchive/seek_by_jump.c
index 282397616..578870d9b 100644
--- a/archival/libunarchive/seek_by_jump.c
+++ b/archival/libunarchive/seek_by_jump.c
@@ -30,6 +30,6 @@ extern void seek_by_jump(const archive_handle_t *archive_handle, const unsigned
30 seek_by_char(archive_handle, amount); 30 seek_by_char(archive_handle, amount);
31 } else 31 } else
32#endif 32#endif
33 perror_msg_and_die("Seek failure"); 33 bb_perror_msg_and_die("Seek failure");
34 } 34 }
35} 35}
diff --git a/archival/libunarchive/uncompress.c b/archival/libunarchive/uncompress.c
index 192e98126..9851ca39d 100644
--- a/archival/libunarchive/uncompress.c
+++ b/archival/libunarchive/uncompress.c
@@ -121,14 +121,14 @@ extern int uncompress(int fd_in, int fd_out)
121 121
122 insize = 0; 122 insize = 0;
123 123
124 inbuf[0] = xread_char(fd_in); 124 inbuf[0] = bb_xread_char(fd_in);
125 125
126 maxbits = inbuf[0] & BIT_MASK; 126 maxbits = inbuf[0] & BIT_MASK;
127 block_mode = inbuf[0] & BLOCK_MODE; 127 block_mode = inbuf[0] & BLOCK_MODE;
128 maxmaxcode = MAXCODE(maxbits); 128 maxmaxcode = MAXCODE(maxbits);
129 129
130 if (maxbits > BITS) { 130 if (maxbits > BITS) {
131 error_msg("compressed with %d bits, can only handle %d bits", maxbits, 131 bb_error_msg("compressed with %d bits, can only handle %d bits", maxbits,
132 BITS); 132 BITS);
133 return -1; 133 return -1;
134 } 134 }
@@ -227,11 +227,11 @@ extern int uncompress(int fd_in, int fd_out)
227 posbits -= n_bits; 227 posbits -= n_bits;
228 p = &inbuf[posbits >> 3]; 228 p = &inbuf[posbits >> 3];
229 229
230 error_msg 230 bb_error_msg
231 ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)", 231 ("insize:%d posbits:%d inbuf:%02X %02X %02X %02X %02X (%d)",
232 insize, posbits, p[-1], p[0], p[1], p[2], p[3], 232 insize, posbits, p[-1], p[0], p[1], p[2], p[3],
233 (posbits & 07)); 233 (posbits & 07));
234 error_msg("uncompress: corrupt input"); 234 bb_error_msg("uncompress: corrupt input");
235 return -1; 235 return -1;
236 } 236 }
237 237
diff --git a/archival/libunarchive/unpack_ar_archive.c b/archival/libunarchive/unpack_ar_archive.c
index afa3672ad..e8f113bcf 100644
--- a/archival/libunarchive/unpack_ar_archive.c
+++ b/archival/libunarchive/unpack_ar_archive.c
@@ -26,7 +26,7 @@ extern void unpack_ar_archive(archive_handle_t *ar_archive)
26 26
27 archive_xread_all(ar_archive, magic, 7); 27 archive_xread_all(ar_archive, magic, 7);
28 if (strncmp(magic, "!<arch>", 7) != 0) { 28 if (strncmp(magic, "!<arch>", 7) != 0) {
29 error_msg_and_die("Invalid ar magic"); 29 bb_error_msg_and_die("Invalid ar magic");
30 } 30 }
31 ar_archive->offset += 7; 31 ar_archive->offset += 7;
32 32
diff --git a/archival/libunarchive/unzip.c b/archival/libunarchive/unzip.c
index 3a7334ce9..2401cf831 100644
--- a/archival/libunarchive/unzip.c
+++ b/archival/libunarchive/unzip.c
@@ -150,7 +150,7 @@ static void fill_bytebuffer(void)
150 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer 150 /* Leave the first 4 bytes empty so we can always unwind the bitbuffer
151 * to the front of the bytebuffer, leave 4 bytes free at end of tail 151 * to the front of the bytebuffer, leave 4 bytes free at end of tail
152 * so we can easily top up buffer in check_trailer_gzip() */ 152 * so we can easily top up buffer in check_trailer_gzip() */
153 bytebuffer_size = 4 + xread(gunzip_src_fd, &bytebuffer[4], BYTEBUFFER_MAX - 8); 153 bytebuffer_size = 4 + bb_xread(gunzip_src_fd, &bytebuffer[4], BYTEBUFFER_MAX - 8);
154 bytebuffer_offset = 4; 154 bytebuffer_offset = 4;
155 } 155 }
156} 156}
@@ -448,7 +448,7 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_b
448 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16) 448 if ((e = (t = tl + ((unsigned) b & ml))->e) > 16)
449 do { 449 do {
450 if (e == 99) { 450 if (e == 99) {
451 error_msg_and_die("inflate_codes error 1");; 451 bb_error_msg_and_die("inflate_codes error 1");;
452 } 452 }
453 b >>= t->b; 453 b >>= t->b;
454 k -= t->b; 454 k -= t->b;
@@ -484,7 +484,7 @@ static int inflate_codes(huft_t * my_tl, huft_t * my_td, const unsigned int my_b
484 if ((e = (t = td + ((unsigned) b & md))->e) > 16) 484 if ((e = (t = td + ((unsigned) b & md))->e) > 16)
485 do { 485 do {
486 if (e == 99) 486 if (e == 99)
487 error_msg_and_die("inflate_codes error 2");; 487 bb_error_msg_and_die("inflate_codes error 2");;
488 b >>= t->b; 488 b >>= t->b;
489 k -= t->b; 489 k -= t->b;
490 e -= 16; 490 e -= 16;
@@ -821,7 +821,7 @@ static int inflate_block(int *e)
821 821
822 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { 822 if ((i = huft_build(ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) {
823 if (i == 1) { 823 if (i == 1) {
824 error_msg_and_die("Incomplete literal tree"); 824 bb_error_msg_and_die("Incomplete literal tree");
825 huft_free(tl); 825 huft_free(tl);
826 } 826 }
827 return i; /* incomplete code set */ 827 return i; /* incomplete code set */
@@ -830,7 +830,7 @@ static int inflate_block(int *e)
830 bd = dbits; 830 bd = dbits;
831 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) { 831 if ((i = huft_build(ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) {
832 if (i == 1) { 832 if (i == 1) {
833 error_msg_and_die("incomplete distance tree"); 833 bb_error_msg_and_die("incomplete distance tree");
834 huft_free(td); 834 huft_free(td);
835 } 835 }
836 huft_free(tl); 836 huft_free(tl);
@@ -846,7 +846,7 @@ static int inflate_block(int *e)
846 } 846 }
847 default: 847 default:
848 /* bad block type */ 848 /* bad block type */
849 error_msg_and_die("bad block type %d\n", t); 849 bb_error_msg_and_die("bad block type %d\n", t);
850 } 850 }
851} 851}
852 852
@@ -884,7 +884,7 @@ static int inflate_get_next_window(void)
884 break; 884 break;
885 case -2: ret = inflate_codes(0,0,0,0,0); 885 case -2: ret = inflate_codes(0,0,0,0,0);
886 break; 886 break;
887 default: error_msg_and_die("inflate error %d", method); 887 default: bb_error_msg_and_die("inflate error %d", method);
888 } 888 }
889 889
890 if (ret == 1) { 890 if (ret == 1) {
@@ -975,16 +975,16 @@ extern void GZ_gzReadClose(void)
975 ssize_t nread, nwrote; 975 ssize_t nread, nwrote;
976 976
977 GZ_gzReadOpen(in, 0, 0); 977 GZ_gzReadOpen(in, 0, 0);
978 while(1) { // Robbed from copyfd.c 978 while(1) { // Robbed from bb_copyfd.c
979 nread = read_gz(in, buf, sizeof(buf)); 979 nread = read_gz(in, buf, sizeof(buf));
980 if (nread == 0) break; // no data to write 980 if (nread == 0) break; // no data to write
981 else if (nread == -1) { 981 else if (nread == -1) {
982 perror_msg("read"); 982 bb_perror_msg("read");
983 return -1; 983 return -1;
984 } 984 }
985 nwrote = full_write(out, buf, nread); 985 nwrote = bb_full_write(out, buf, nread);
986 if (nwrote == -1) { 986 if (nwrote == -1) {
987 perror_msg("write"); 987 bb_perror_msg("write");
988 return -1; 988 return -1;
989 } 989 }
990 } 990 }
@@ -998,9 +998,9 @@ extern int inflate(int in, int out)
998 GZ_gzReadOpen(in, 0, 0); 998 GZ_gzReadOpen(in, 0, 0);
999 while(1) { 999 while(1) {
1000 int ret = inflate_get_next_window(); 1000 int ret = inflate_get_next_window();
1001 nwrote = full_write(out, gunzip_window, gunzip_outbuf_count); 1001 nwrote = bb_full_write(out, gunzip_window, gunzip_outbuf_count);
1002 if (nwrote == -1) { 1002 if (nwrote == -1) {
1003 perror_msg("write"); 1003 bb_perror_msg("write");
1004 return -1; 1004 return -1;
1005 } 1005 }
1006 if (ret == 0) break; 1006 if (ret == 0) break;
@@ -1017,7 +1017,7 @@ extern void check_trailer_gzip(int src_fd)
1017 /* top up the input buffer with the rest of the trailer */ 1017 /* top up the input buffer with the rest of the trailer */
1018 count = bytebuffer_size - bytebuffer_offset; 1018 count = bytebuffer_size - bytebuffer_offset;
1019 if (count < 8) { 1019 if (count < 8) {
1020 xread_all(src_fd, &bytebuffer[bytebuffer_size], 8 - count); 1020 bb_xread_all(src_fd, &bytebuffer[bytebuffer_size], 8 - count);
1021 bytebuffer_size += 8 - count; 1021 bytebuffer_size += 8 - count;
1022 } 1022 }
1023 for (count = 0; count != 4; count++) { 1023 for (count = 0; count != 4; count++) {
@@ -1027,14 +1027,14 @@ extern void check_trailer_gzip(int src_fd)
1027 1027
1028 /* Validate decompression - crc */ 1028 /* Validate decompression - crc */
1029 if (stored_crc != (gunzip_crc ^ 0xffffffffL)) { 1029 if (stored_crc != (gunzip_crc ^ 0xffffffffL)) {
1030 error_msg_and_die("crc error"); 1030 bb_error_msg_and_die("crc error");
1031 } 1031 }
1032 1032
1033 /* Validate decompression - size */ 1033 /* Validate decompression - size */
1034 if (gunzip_bytes_out != 1034 if (gunzip_bytes_out !=
1035 (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) | 1035 (bytebuffer[bytebuffer_offset] | (bytebuffer[bytebuffer_offset+1] << 8) |
1036 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) { 1036 (bytebuffer[bytebuffer_offset+2] << 16) | (bytebuffer[bytebuffer_offset+3] << 24))) {
1037 error_msg_and_die("Incorrect length, but crc is correct"); 1037 bb_error_msg_and_die("Incorrect length, but crc is correct");
1038 } 1038 }
1039 1039
1040} 1040}
diff --git a/archival/rpm.c b/archival/rpm.c
index 61486cb03..5bde53285 100644
--- a/archival/rpm.c
+++ b/archival/rpm.c
@@ -116,7 +116,7 @@ int rpm_main(int argc, char **argv)
116 break; 116 break;
117 case 'q': // First arg: Query mode 117 case 'q': // First arg: Query mode
118 if (!func) func |= rpm_query; 118 if (!func) func |= rpm_query;
119 else show_usage(); 119 else bb_show_usage();
120 break; 120 break;
121 case 'p': // Query a package 121 case 'p': // Query a package
122 func |= rpm_query_package; 122 func |= rpm_query_package;
@@ -133,13 +133,13 @@ int rpm_main(int argc, char **argv)
133 func |= rpm_query_list_config; 133 func |= rpm_query_list_config;
134 break; 134 break;
135 default: 135 default:
136 show_usage(); 136 bb_show_usage();
137 } 137 }
138 } 138 }
139 139
140 if (optind == argc) show_usage(); 140 if (optind == argc) bb_show_usage();
141 while (optind < argc) { 141 while (optind < argc) {
142 rpm_fd = xopen(argv[optind], O_RDONLY); 142 rpm_fd = bb_xopen(argv[optind], O_RDONLY);
143 mytags = rpm_gettags(rpm_fd, (int *) &tagcount); 143 mytags = rpm_gettags(rpm_fd, (int *) &tagcount);
144 offset = lseek(rpm_fd, 0, SEEK_CUR); 144 offset = lseek(rpm_fd, 0, SEEK_CUR);
145 if (!mytags) { printf("Error reading rpm header\n"); exit(-1); } 145 if (!mytags) { printf("Error reading rpm header\n"); exit(-1); }
@@ -206,9 +206,9 @@ void extract_cpio_gz(int fd) {
206 archive_handle->src_fd = fd; 206 archive_handle->src_fd = fd;
207 archive_handle->offset = 0; 207 archive_handle->offset = 0;
208 208
209 xread_all(archive_handle->src_fd, &magic, 2); 209 bb_xread_all(archive_handle->src_fd, &magic, 2);
210 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { 210 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
211 error_msg_and_die("Invalid gzip magic"); 211 bb_error_msg_and_die("Invalid gzip magic");
212 } 212 }
213 check_header_gzip(archive_handle->src_fd); 213 check_header_gzip(archive_handle->src_fd);
214 chdir("/"); // Install RPM's to root 214 chdir("/"); // Install RPM's to root
@@ -314,7 +314,7 @@ void fileaction_dobackup(char *filename, int fileref)
314 if (rpm_getint(RPMTAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { /* Only need to backup config files */ 314 if (rpm_getint(RPMTAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { /* Only need to backup config files */
315 stat_res = lstat (filename, &oldfile); 315 stat_res = lstat (filename, &oldfile);
316 if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { /* File already exists - really should check MD5's etc to see if different */ 316 if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { /* File already exists - really should check MD5's etc to see if different */
317 newname = xstrdup(filename); 317 newname = bb_xstrdup(filename);
318 newname = strcat(newname, ".rpmorig"); 318 newname = strcat(newname, ".rpmorig");
319 copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS); 319 copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
320 remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE); 320 remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
diff --git a/archival/rpm2cpio.c b/archival/rpm2cpio.c
index 66f680869..bb9f69573 100644
--- a/archival/rpm2cpio.c
+++ b/archival/rpm2cpio.c
@@ -52,12 +52,12 @@ void skip_header(int rpm_fd)
52{ 52{
53 struct rpm_header header; 53 struct rpm_header header;
54 54
55 xread_all(rpm_fd, &header, sizeof(struct rpm_header)); 55 bb_xread_all(rpm_fd, &header, sizeof(struct rpm_header));
56 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) { 56 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) {
57 error_msg_and_die("Invalid RPM header magic"); /* Invalid magic */ 57 bb_error_msg_and_die("Invalid RPM header magic"); /* Invalid magic */
58 } 58 }
59 if (header.version != 1) { 59 if (header.version != 1) {
60 error_msg_and_die("Unsupported RPM header version"); /* This program only supports v1 headers */ 60 bb_error_msg_and_die("Unsupported RPM header version"); /* This program only supports v1 headers */
61 } 61 }
62 header.entries = ntohl(header.entries); 62 header.entries = ntohl(header.entries);
63 header.size = ntohl(header.size); 63 header.size = ntohl(header.size);
@@ -75,12 +75,12 @@ extern int rpm2cpio_main(int argc, char **argv)
75 if (argc == 1) { 75 if (argc == 1) {
76 rpm_fd = fileno(stdin); 76 rpm_fd = fileno(stdin);
77 } else { 77 } else {
78 rpm_fd = xopen(argv[1], O_RDONLY); 78 rpm_fd = bb_xopen(argv[1], O_RDONLY);
79 } 79 }
80 80
81 xread_all(rpm_fd, &lead, sizeof(struct rpm_lead)); 81 bb_xread_all(rpm_fd, &lead, sizeof(struct rpm_lead));
82 if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) { 82 if (strncmp((char *) &lead.magic, RPM_MAGIC, 4) != 0) {
83 error_msg_and_die("Invalid RPM magic"); /* Just check the magic, the rest is irrelevant */ 83 bb_error_msg_and_die("Invalid RPM magic"); /* Just check the magic, the rest is irrelevant */
84 } 84 }
85 85
86 /* Skip the signature header */ 86 /* Skip the signature header */
@@ -90,14 +90,14 @@ extern int rpm2cpio_main(int argc, char **argv)
90 /* Skip the main header */ 90 /* Skip the main header */
91 skip_header(rpm_fd); 91 skip_header(rpm_fd);
92 92
93 xread_all(rpm_fd, &magic, 2); 93 bb_xread_all(rpm_fd, &magic, 2);
94 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { 94 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
95 error_msg_and_die("Invalid gzip magic"); 95 bb_error_msg_and_die("Invalid gzip magic");
96 } 96 }
97 97
98 check_header_gzip(rpm_fd); 98 check_header_gzip(rpm_fd);
99 if (inflate(rpm_fd, fileno(stdout)) != 0) { 99 if (inflate(rpm_fd, fileno(stdout)) != 0) {
100 error_msg("Error inflating"); 100 bb_error_msg("Error inflating");
101 } 101 }
102 check_trailer_gzip(rpm_fd); 102 check_trailer_gzip(rpm_fd);
103 103
diff --git a/archival/tar.c b/archival/tar.c
index fa1c57512..c39d5787f 100644
--- a/archival/tar.c
+++ b/archival/tar.c
@@ -278,7 +278,7 @@ static inline int writeTarHeader(struct TarBallInfo *tbInfo,
278 header.typeflag = REGTYPE; 278 header.typeflag = REGTYPE;
279 putOctal(header.size, sizeof(header.size), statbuf->st_size); 279 putOctal(header.size, sizeof(header.size), statbuf->st_size);
280 } else { 280 } else {
281 error_msg("%s: Unknown file type", real_name); 281 bb_error_msg("%s: Unknown file type", real_name);
282 return (FALSE); 282 return (FALSE);
283 } 283 }
284 284
@@ -295,9 +295,9 @@ static inline int writeTarHeader(struct TarBallInfo *tbInfo,
295 295
296 /* Now write the header out to disk */ 296 /* Now write the header out to disk */
297 if ((size = 297 if ((size =
298 full_write(tbInfo->tarFd, (char *) &header, 298 bb_full_write(tbInfo->tarFd, (char *) &header,
299 sizeof(struct TarHeader))) < 0) { 299 sizeof(struct TarHeader))) < 0) {
300 error_msg(io_error, real_name); 300 bb_error_msg(bb_msg_io_error, real_name);
301 return (FALSE); 301 return (FALSE);
302 } 302 }
303 /* Pad the header up to the tar block size */ 303 /* Pad the header up to the tar block size */
@@ -368,7 +368,7 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
368 368
369 /* It is against the rules to archive a socket */ 369 /* It is against the rules to archive a socket */
370 if (S_ISSOCK(statbuf->st_mode)) { 370 if (S_ISSOCK(statbuf->st_mode)) {
371 error_msg("%s: socket ignored", fileName); 371 bb_error_msg("%s: socket ignored", fileName);
372 return (TRUE); 372 return (TRUE);
373 } 373 }
374 374
@@ -377,7 +377,7 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
377 * the new tarball */ 377 * the new tarball */
378 if (tbInfo->statBuf.st_dev == statbuf->st_dev && 378 if (tbInfo->statBuf.st_dev == statbuf->st_dev &&
379 tbInfo->statBuf.st_ino == statbuf->st_ino) { 379 tbInfo->statBuf.st_ino == statbuf->st_ino) {
380 error_msg("%s: file is the archive; skipping", fileName); 380 bb_error_msg("%s: file is the archive; skipping", fileName);
381 return (TRUE); 381 return (TRUE);
382 } 382 }
383 383
@@ -386,14 +386,14 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
386 static int alreadyWarned = FALSE; 386 static int alreadyWarned = FALSE;
387 387
388 if (alreadyWarned == FALSE) { 388 if (alreadyWarned == FALSE) {
389 error_msg("Removing leading '/' from member names"); 389 bb_error_msg("Removing leading '/' from member names");
390 alreadyWarned = TRUE; 390 alreadyWarned = TRUE;
391 } 391 }
392 header_name++; 392 header_name++;
393 } 393 }
394 394
395 if (strlen(fileName) >= NAME_SIZE) { 395 if (strlen(fileName) >= NAME_SIZE) {
396 error_msg(name_longer_than_foo, NAME_SIZE); 396 bb_error_msg(bb_msg_name_longer_than_foo, NAME_SIZE);
397 return (TRUE); 397 return (TRUE);
398 } 398 }
399 399
@@ -419,21 +419,21 @@ static int writeFileToTarball(const char *fileName, struct stat *statbuf,
419 419
420 /* open the file we want to archive, and make sure all is well */ 420 /* open the file we want to archive, and make sure all is well */
421 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) { 421 if ((inputFileFd = open(fileName, O_RDONLY)) < 0) {
422 perror_msg("%s: Cannot open", fileName); 422 bb_perror_msg("%s: Cannot open", fileName);
423 return (FALSE); 423 return (FALSE);
424 } 424 }
425 425
426 /* write the file to the archive */ 426 /* write the file to the archive */
427 while ((size = full_read(inputFileFd, buffer, sizeof(buffer))) > 0) { 427 while ((size = bb_full_read(inputFileFd, buffer, sizeof(buffer))) > 0) {
428 if (full_write(tbInfo->tarFd, buffer, size) != size) { 428 if (bb_full_write(tbInfo->tarFd, buffer, size) != size) {
429 /* Output file seems to have a problem */ 429 /* Output file seems to have a problem */
430 error_msg(io_error, fileName); 430 bb_error_msg(bb_msg_io_error, fileName);
431 return (FALSE); 431 return (FALSE);
432 } 432 }
433 readSize += size; 433 readSize += size;
434 } 434 }
435 if (size == -1) { 435 if (size == -1) {
436 error_msg(io_error, fileName); 436 bb_error_msg(bb_msg_io_error, fileName);
437 return (FALSE); 437 return (FALSE);
438 } 438 }
439 /* Pad the file up to the tar block size */ 439 /* Pad the file up to the tar block size */
@@ -464,7 +464,7 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag,
464 464
465 /* Make sure there is at least one file to tar up. */ 465 /* Make sure there is at least one file to tar up. */
466 if (include == NULL) { 466 if (include == NULL) {
467 error_msg_and_die("Cowardly refusing to create an empty archive"); 467 bb_error_msg_and_die("Cowardly refusing to create an empty archive");
468 } 468 }
469 469
470 /* Open the tar file for writing. */ 470 /* Open the tar file for writing. */
@@ -477,7 +477,7 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag,
477 } 477 }
478 478
479 if (tbInfo.tarFd < 0) { 479 if (tbInfo.tarFd < 0) {
480 perror_msg("%s: Cannot open", tarName); 480 bb_perror_msg("%s: Cannot open", tarName);
481 freeHardLinkInfo(&tbInfo.hlInfoHead); 481 freeHardLinkInfo(&tbInfo.hlInfoHead);
482 return (FALSE); 482 return (FALSE);
483 } 483 }
@@ -485,12 +485,12 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag,
485 /* Store the stat info for the tarball's file, so 485 /* Store the stat info for the tarball's file, so
486 * can avoid including the tarball into itself.... */ 486 * can avoid including the tarball into itself.... */
487 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0) 487 if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
488 error_msg_and_die(io_error, tarName); 488 bb_error_msg_and_die(bb_msg_io_error, tarName);
489 489
490#ifdef CONFIG_FEATURE_TAR_GZIP 490#ifdef CONFIG_FEATURE_TAR_GZIP
491 if (gzip) { 491 if (gzip) {
492 if (pipe(gzipDataPipe) < 0 || pipe(gzipStatusPipe) < 0) { 492 if (pipe(gzipDataPipe) < 0 || pipe(gzipStatusPipe) < 0) {
493 perror_msg_and_die("Failed to create gzip pipe"); 493 bb_perror_msg_and_die("Failed to create gzip pipe");
494 } 494 }
495 495
496 signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */ 496 signal(SIGPIPE, SIG_IGN); /* we only want EPIPE on errors */
@@ -529,7 +529,7 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag,
529 529
530 if (n == 0 && vfork_exec_errno != 0) { 530 if (n == 0 && vfork_exec_errno != 0) {
531 errno = vfork_exec_errno; 531 errno = vfork_exec_errno;
532 perror_msg_and_die("Could not exec gzip process"); 532 bb_perror_msg_and_die("Could not exec gzip process");
533 } else if ((n < 0) && (errno == EAGAIN || errno == EINTR)) 533 } else if ((n < 0) && (errno == EAGAIN || errno == EINTR))
534 continue; /* try it again */ 534 continue; /* try it again */
535 break; 535 break;
@@ -538,7 +538,7 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag,
538 538
539 tbInfo.tarFd = gzipDataPipe[1]; 539 tbInfo.tarFd = gzipDataPipe[1];
540 } else { 540 } else {
541 perror_msg_and_die("Failed to vfork gzip process"); 541 bb_perror_msg_and_die("Failed to vfork gzip process");
542 } 542 }
543 } 543 }
544#endif 544#endif
@@ -567,7 +567,7 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag,
567 /* Hang up the tools, close up shop, head home */ 567 /* Hang up the tools, close up shop, head home */
568 close(tbInfo.tarFd); 568 close(tbInfo.tarFd);
569 if (errorFlag) 569 if (errorFlag)
570 error_msg("Error exit delayed from previous errors"); 570 bb_error_msg("Error exit delayed from previous errors");
571 571
572 freeHardLinkInfo(&tbInfo.hlInfoHead); 572 freeHardLinkInfo(&tbInfo.hlInfoHead);
573 573
@@ -585,10 +585,9 @@ static inline int writeTarFile(const char *tarName, const int verboseFlag,
585#ifdef CONFIG_FEATURE_TAR_EXCLUDE 585#ifdef CONFIG_FEATURE_TAR_EXCLUDE
586static llist_t *append_file_list_to_list(const char *filename, llist_t *list) 586static llist_t *append_file_list_to_list(const char *filename, llist_t *list)
587{ 587{
588 FILE *src_stream = xfopen(filename, "r"); 588 FILE *src_stream = bb_xfopen(filename, "r");
589 char *line; 589 char *line;
590 while((line = get_line_from_file(src_stream)) != NULL) { 590 while((line = bb_get_chomped_line_from_file(src_stream)) != NULL) {
591 chomp(line);
592 list = llist_add_to(list, line); 591 list = llist_add_to(list, line);
593 } 592 }
594 fclose(src_stream); 593 fclose(src_stream);
@@ -611,7 +610,7 @@ int tar_main(int argc, char **argv)
611 unsigned char ctx_flag = 0; 610 unsigned char ctx_flag = 0;
612 611
613 if (argc < 2) { 612 if (argc < 2) {
614 show_usage(); 613 bb_show_usage();
615 } 614 }
616 615
617 /* Prepend '-' to the first argument if required */ 616 /* Prepend '-' to the first argument if required */
@@ -690,13 +689,13 @@ int tar_main(int argc, char **argv)
690 break; 689 break;
691#endif 690#endif
692 default: 691 default:
693 show_usage(); 692 bb_show_usage();
694 } 693 }
695 } 694 }
696 695
697 /* Check one and only one context option was given */ 696 /* Check one and only one context option was given */
698 if ((ctx_flag != CTX_CREATE) && (ctx_flag != CTX_TEST) && (ctx_flag != CTX_EXTRACT)) { 697 if ((ctx_flag != CTX_CREATE) && (ctx_flag != CTX_TEST) && (ctx_flag != CTX_EXTRACT)) {
699 show_usage(); 698 bb_show_usage();
700 } 699 }
701 700
702 /* Check if we are reading from stdin */ 701 /* Check if we are reading from stdin */
@@ -740,11 +739,11 @@ int tar_main(int argc, char **argv)
740 tar_handle->src_fd = fileno(stdin); 739 tar_handle->src_fd = fileno(stdin);
741 tar_handle->seek = seek_by_char; 740 tar_handle->seek = seek_by_char;
742 } else { 741 } else {
743 tar_handle->src_fd = xopen(tar_filename, O_RDONLY); 742 tar_handle->src_fd = bb_xopen(tar_filename, O_RDONLY);
744 } 743 }
745 744
746 if ((base_dir) && (chdir(base_dir))) { 745 if ((base_dir) && (chdir(base_dir))) {
747 perror_msg_and_die("Couldnt chdir"); 746 bb_perror_msg_and_die("Couldnt chdir");
748 } 747 }
749 748
750 while (get_header_ptr(tar_handle) == EXIT_SUCCESS); 749 while (get_header_ptr(tar_handle) == EXIT_SUCCESS);
@@ -753,7 +752,7 @@ int tar_main(int argc, char **argv)
753 while (tar_handle->accept) { 752 while (tar_handle->accept) {
754 if (find_list_entry(tar_handle->reject, tar_handle->accept->data) == NULL) { 753 if (find_list_entry(tar_handle->reject, tar_handle->accept->data) == NULL) {
755 if (find_list_entry(tar_handle->passed, tar_handle->accept->data) == NULL) { 754 if (find_list_entry(tar_handle->passed, tar_handle->accept->data) == NULL) {
756 error_msg_and_die("%s: Not found in archive\n", tar_handle->accept->data); 755 bb_error_msg_and_die("%s: Not found in archive\n", tar_handle->accept->data);
757 } 756 }
758 } 757 }
759 tar_handle->accept = tar_handle->accept->link; 758 tar_handle->accept = tar_handle->accept->link;
diff --git a/archival/uncompress.c b/archival/uncompress.c
index 877bcc784..2d2145d18 100644
--- a/archival/uncompress.c
+++ b/archival/uncompress.c
@@ -45,7 +45,7 @@ int uncompress_main(int argc, char **argv)
45 flags |= gunzip_force; 45 flags |= gunzip_force;
46 break; 46 break;
47 default: 47 default:
48 show_usage(); /* exit's inside usage */ 48 bb_show_usage(); /* exit's inside usage */
49 } 49 }
50 } 50 }
51 51
@@ -63,17 +63,17 @@ int uncompress_main(int argc, char **argv)
63 src_fd = fileno(stdin); 63 src_fd = fileno(stdin);
64 flags |= gunzip_to_stdout; 64 flags |= gunzip_to_stdout;
65 } else { 65 } else {
66 src_fd = xopen(old_path, O_RDONLY); 66 src_fd = bb_xopen(old_path, O_RDONLY);
67 67
68 /* Get the time stamp on the input file. */ 68 /* Get the time stamp on the input file. */
69 if (stat(old_path, &stat_buf) < 0) { 69 if (stat(old_path, &stat_buf) < 0) {
70 error_msg_and_die("Couldn't stat file %s", old_path); 70 bb_error_msg_and_die("Couldn't stat file %s", old_path);
71 } 71 }
72 } 72 }
73 73
74 /* Check that the input is sane. */ 74 /* Check that the input is sane. */
75 if (isatty(src_fd) && ((flags & gunzip_force) == 0)) { 75 if (isatty(src_fd) && ((flags & gunzip_force) == 0)) {
76 error_msg_and_die 76 bb_error_msg_and_die
77 ("compressed data not read from terminal. Use -f to force it."); 77 ("compressed data not read from terminal. Use -f to force it.");
78 } 78 }
79 79
@@ -83,16 +83,16 @@ int uncompress_main(int argc, char **argv)
83 } else { 83 } else {
84 char *extension; 84 char *extension;
85 85
86 new_path = xstrdup(old_path); 86 new_path = bb_xstrdup(old_path);
87 87
88 extension = strrchr(new_path, '.'); 88 extension = strrchr(new_path, '.');
89 if (!extension || (strcmp(extension, ".Z") != 0)) { 89 if (!extension || (strcmp(extension, ".Z") != 0)) {
90 error_msg_and_die("Invalid extension"); 90 bb_error_msg_and_die("Invalid extension");
91 } 91 }
92 *extension = '\0'; 92 *extension = '\0';
93 93
94 /* Open output file */ 94 /* Open output file */
95 dst_fd = xopen(new_path, O_WRONLY | O_CREAT); 95 dst_fd = bb_xopen(new_path, O_WRONLY | O_CREAT);
96 96
97 /* Set permissions on the file */ 97 /* Set permissions on the file */
98 chmod(new_path, stat_buf.st_mode); 98 chmod(new_path, stat_buf.st_mode);
@@ -102,10 +102,10 @@ int uncompress_main(int argc, char **argv)
102 } 102 }
103 103
104 /* do the decompression, and cleanup */ 104 /* do the decompression, and cleanup */
105 if ((xread_char(src_fd) == 0x1f) && (xread_char(src_fd) == 0x9d)) { 105 if ((bb_xread_char(src_fd) == 0x1f) && (bb_xread_char(src_fd) == 0x9d)) {
106 status = uncompress(src_fd, dst_fd); 106 status = uncompress(src_fd, dst_fd);
107 } else { 107 } else {
108 error_msg_and_die("Invalid magic"); 108 bb_error_msg_and_die("Invalid magic");
109 } 109 }
110 110
111 if ((status != EXIT_SUCCESS) && (new_path)) { 111 if ((status != EXIT_SUCCESS) && (new_path)) {
@@ -122,7 +122,7 @@ int uncompress_main(int argc, char **argv)
122 122
123 /* delete_path will be NULL if in test mode or from stdin */ 123 /* delete_path will be NULL if in test mode or from stdin */
124 if (delete_path && (unlink(delete_path) == -1)) { 124 if (delete_path && (unlink(delete_path) == -1)) {
125 error_msg_and_die("Couldn't remove %s", delete_path); 125 bb_error_msg_and_die("Couldn't remove %s", delete_path);
126 } 126 }
127 127
128 free(new_path); 128 free(new_path);
diff --git a/archival/unzip.c b/archival/unzip.c
index 86416d327..f2d7f4918 100644
--- a/archival/unzip.c
+++ b/archival/unzip.c
@@ -120,12 +120,12 @@ extern int unzip_main(int argc, char **argv)
120 break; 120 break;
121#endif 121#endif
122 default: 122 default:
123 show_usage(); 123 bb_show_usage();
124 } 124 }
125 } 125 }
126 126
127 if (argc == optind) { 127 if (argc == optind) {
128 show_usage(); 128 bb_show_usage();
129 } 129 }
130 130
131 printf("Archive: %s\n", argv[optind]); 131 printf("Archive: %s\n", argv[optind]);
@@ -138,11 +138,11 @@ extern int unzip_main(int argc, char **argv)
138 archive_handle->src_fd = fileno(stdin); 138 archive_handle->src_fd = fileno(stdin);
139 archive_handle->seek = seek_by_char; 139 archive_handle->seek = seek_by_char;
140 } else { 140 } else {
141 archive_handle->src_fd = xopen(argv[optind++], O_RDONLY); 141 archive_handle->src_fd = bb_xopen(argv[optind++], O_RDONLY);
142 } 142 }
143 143
144 if ((base_dir) && (chdir(base_dir))) { 144 if ((base_dir) && (chdir(base_dir))) {
145 perror_msg_and_die("Couldnt chdir"); 145 bb_perror_msg_and_die("Couldnt chdir");
146 } 146 }
147 147
148 while (optind < argc) { 148 while (optind < argc) {
@@ -163,7 +163,7 @@ extern int unzip_main(int argc, char **argv)
163 break; 163 break;
164 } 164 }
165 else if (magic != ZIP_FILEHEADER_MAGIC) { 165 else if (magic != ZIP_FILEHEADER_MAGIC) {
166 error_msg_and_die("Invlaide zip magic"); 166 bb_error_msg_and_die("Invlaide zip magic");
167 } 167 }
168 168
169 /* Read the file header */ 169 /* Read the file header */
@@ -172,7 +172,7 @@ extern int unzip_main(int argc, char **argv)
172 archive_handle->file_header->mode = S_IFREG | 0777; 172 archive_handle->file_header->mode = S_IFREG | 0777;
173 173
174 if (zip_header.formated.method != 8) { 174 if (zip_header.formated.method != 8) {
175 error_msg_and_die("Unsupported compression method %d\n", zip_header.formated.method); 175 bb_error_msg_and_die("Unsupported compression method %d\n", zip_header.formated.method);
176 } 176 }
177 177
178 /* Read filename */ 178 /* Read filename */
@@ -198,19 +198,19 @@ extern int unzip_main(int argc, char **argv)
198 if (archive_handle->action_data) { 198 if (archive_handle->action_data) {
199 archive_handle->action_data(archive_handle); 199 archive_handle->action_data(archive_handle);
200 } else { 200 } else {
201 dst_fd = xopen(archive_handle->file_header->name, O_WRONLY | O_CREAT); 201 dst_fd = bb_xopen(archive_handle->file_header->name, O_WRONLY | O_CREAT);
202 inflate(archive_handle->src_fd, dst_fd); 202 inflate(archive_handle->src_fd, dst_fd);
203 close(dst_fd); 203 close(dst_fd);
204 chmod(archive_handle->file_header->name, archive_handle->file_header->mode); 204 chmod(archive_handle->file_header->name, archive_handle->file_header->mode);
205 205
206 /* Validate decompression - crc */ 206 /* Validate decompression - crc */
207 if (zip_header.formated.crc32 != (gunzip_crc ^ 0xffffffffL)) { 207 if (zip_header.formated.crc32 != (gunzip_crc ^ 0xffffffffL)) {
208 error_msg("Invalid compressed data--crc error"); 208 bb_error_msg("Invalid compressed data--crc error");
209 } 209 }
210 210
211 /* Validate decompression - size */ 211 /* Validate decompression - size */
212 if (gunzip_bytes_out != zip_header.formated.ucmpsize) { 212 if (gunzip_bytes_out != zip_header.formated.ucmpsize) {
213 error_msg("Invalid compressed data--length error"); 213 bb_error_msg("Invalid compressed data--length error");
214 } 214 }
215 } 215 }
216 216
diff --git a/console-tools/chvt.c b/console-tools/chvt.c
index 8136f1c15..11e1078b7 100644
--- a/console-tools/chvt.c
+++ b/console-tools/chvt.c
@@ -23,13 +23,13 @@ int chvt_main(int argc, char **argv)
23 int fd, num; 23 int fd, num;
24 24
25 if ((argc != 2) || (**(argv + 1) == '-')) 25 if ((argc != 2) || (**(argv + 1) == '-'))
26 show_usage(); 26 bb_show_usage();
27 fd = get_console_fd(); 27 fd = get_console_fd();
28 num = atoi(argv[1]); 28 num = atoi(argv[1]);
29 if (ioctl(fd, VT_ACTIVATE, num)) 29 if (ioctl(fd, VT_ACTIVATE, num))
30 perror_msg_and_die("VT_ACTIVATE"); 30 bb_perror_msg_and_die("VT_ACTIVATE");
31 if (ioctl(fd, VT_WAITACTIVE, num)) 31 if (ioctl(fd, VT_WAITACTIVE, num))
32 perror_msg_and_die("VT_WAITACTIVE"); 32 bb_perror_msg_and_die("VT_WAITACTIVE");
33 return EXIT_SUCCESS; 33 return EXIT_SUCCESS;
34} 34}
35 35
diff --git a/console-tools/deallocvt.c b/console-tools/deallocvt.c
index b7dcc9e22..a4ba851c8 100644
--- a/console-tools/deallocvt.c
+++ b/console-tools/deallocvt.c
@@ -19,23 +19,23 @@ int deallocvt_main(int argc, char *argv[])
19 19
20 //if ((argc > 2) || ((argv == 2) && (**(argv + 1) == '-'))) 20 //if ((argc > 2) || ((argv == 2) && (**(argv + 1) == '-')))
21 if (argc > 2) 21 if (argc > 2)
22 show_usage(); 22 bb_show_usage();
23 23
24 fd = get_console_fd(); 24 fd = get_console_fd();
25 25
26 if (argc == 1) { 26 if (argc == 1) {
27 /* deallocate all unused consoles */ 27 /* deallocate all unused consoles */
28 if (ioctl(fd, VT_DISALLOCATE, 0)) 28 if (ioctl(fd, VT_DISALLOCATE, 0))
29 perror_msg_and_die("VT_DISALLOCATE"); 29 bb_perror_msg_and_die("VT_DISALLOCATE");
30 } else { 30 } else {
31 for (i = 1; i < argc; i++) { 31 for (i = 1; i < argc; i++) {
32 num = atoi(argv[i]); 32 num = atoi(argv[i]);
33 if (num == 0) 33 if (num == 0)
34 error_msg("0: illegal VT number"); 34 bb_error_msg("0: illegal VT number");
35 else if (num == 1) 35 else if (num == 1)
36 error_msg("VT 1 cannot be deallocated"); 36 bb_error_msg("VT 1 cannot be deallocated");
37 else if (ioctl(fd, VT_DISALLOCATE, num)) 37 else if (ioctl(fd, VT_DISALLOCATE, num))
38 perror_msg_and_die("VT_DISALLOCATE"); 38 bb_perror_msg_and_die("VT_DISALLOCATE");
39 } 39 }
40 } 40 }
41 41
diff --git a/console-tools/dumpkmap.c b/console-tools/dumpkmap.c
index d2bb6dcff..85926556c 100644
--- a/console-tools/dumpkmap.c
+++ b/console-tools/dumpkmap.c
@@ -48,12 +48,12 @@ int dumpkmap_main(int argc, char **argv)
48 char flags[MAX_NR_KEYMAPS], magic[] = "bkeymap"; 48 char flags[MAX_NR_KEYMAPS], magic[] = "bkeymap";
49 49
50 if (argc>=2 && *argv[1]=='-') { 50 if (argc>=2 && *argv[1]=='-') {
51 show_usage(); 51 bb_show_usage();
52 } 52 }
53 53
54 fd = open(CURRENT_VC, O_RDWR); 54 fd = open(CURRENT_VC, O_RDWR);
55 if (fd < 0) { 55 if (fd < 0) {
56 perror_msg("Error opening " CURRENT_VC); 56 bb_perror_msg("Error opening " CURRENT_VC);
57 return EXIT_FAILURE; 57 return EXIT_FAILURE;
58 } 58 }
59 59
@@ -81,7 +81,7 @@ int dumpkmap_main(int argc, char **argv)
81 ke.kb_table = i; 81 ke.kb_table = i;
82 if (ioctl(fd, KDGKBENT, &ke) < 0) { 82 if (ioctl(fd, KDGKBENT, &ke) < 0) {
83 83
84 error_msg("ioctl returned: %m, %s, %s, %xqq", (char *)&ke.kb_index,(char *)&ke.kb_table,(int)&ke.kb_value); 84 bb_error_msg("ioctl returned: %m, %s, %s, %xqq", (char *)&ke.kb_index,(char *)&ke.kb_table,(int)&ke.kb_value);
85 } 85 }
86 else { 86 else {
87 write(1,(void*)&ke.kb_value,2); 87 write(1,(void*)&ke.kb_value,2);
diff --git a/console-tools/loadacm.c b/console-tools/loadacm.c
index 3fb4e7665..640aa4401 100644
--- a/console-tools/loadacm.c
+++ b/console-tools/loadacm.c
@@ -34,16 +34,16 @@ int loadacm_main(int argc, char **argv)
34 int fd; 34 int fd;
35 35
36 if (argc>=2 && *argv[1]=='-') { 36 if (argc>=2 && *argv[1]=='-') {
37 show_usage(); 37 bb_show_usage();
38 } 38 }
39 39
40 fd = open(CURRENT_VC, O_RDWR); 40 fd = open(CURRENT_VC, O_RDWR);
41 if (fd < 0) { 41 if (fd < 0) {
42 perror_msg_and_die("Error opening " CURRENT_VC); 42 bb_perror_msg_and_die("Error opening " CURRENT_VC);
43 } 43 }
44 44
45 if (screen_map_load(fd, stdin)) { 45 if (screen_map_load(fd, stdin)) {
46 perror_msg_and_die("Error loading acm"); 46 bb_perror_msg_and_die("Error loading acm");
47 } 47 }
48 48
49 write(fd, "\033(K", 3); 49 write(fd, "\033(K", 3);
@@ -60,7 +60,7 @@ static int screen_map_load(int fd, FILE * fp)
60 int is_unicode; 60 int is_unicode;
61 61
62 if (fstat(fileno(fp), &stbuf)) 62 if (fstat(fileno(fp), &stbuf))
63 perror_msg_and_die("Cannot stat map file"); 63 bb_perror_msg_and_die("Cannot stat map file");
64 64
65 /* first try a UTF screen-map: either ASCII (no restriction) or binary (regular file) */ 65 /* first try a UTF screen-map: either ASCII (no restriction) or binary (regular file) */
66 if (! 66 if (!
@@ -70,16 +70,16 @@ static int screen_map_load(int fd, FILE * fp)
70 if (parse_failed) { 70 if (parse_failed) {
71 if (-1 == fseek(fp, 0, SEEK_SET)) { 71 if (-1 == fseek(fp, 0, SEEK_SET)) {
72 if (errno == ESPIPE) 72 if (errno == ESPIPE)
73 error_msg_and_die("16bit screen-map MUST be a regular file."); 73 bb_error_msg_and_die("16bit screen-map MUST be a regular file.");
74 else 74 else
75 perror_msg_and_die("fseek failed reading binary 16bit screen-map"); 75 bb_perror_msg_and_die("fseek failed reading binary 16bit screen-map");
76 } 76 }
77 77
78 if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1) 78 if (fread(wbuf, sizeof(unicode) * E_TABSZ, 1, fp) != 1)
79 perror_msg_and_die("Cannot read [new] map from file"); 79 bb_perror_msg_and_die("Cannot read [new] map from file");
80#if 0 80#if 0
81 else 81 else
82 error_msg("Input screen-map is binary."); 82 bb_error_msg("Input screen-map is binary.");
83#endif 83#endif
84 } 84 }
85 85
@@ -87,7 +87,7 @@ static int screen_map_load(int fd, FILE * fp)
87 /* same if it was binary, ie. if parse_failed */ 87 /* same if it was binary, ie. if parse_failed */
88 if (parse_failed || is_unicode) { 88 if (parse_failed || is_unicode) {
89 if (ioctl(fd, PIO_UNISCRNMAP, wbuf)) 89 if (ioctl(fd, PIO_UNISCRNMAP, wbuf))
90 perror_msg_and_die("PIO_UNISCRNMAP ioctl"); 90 bb_perror_msg_and_die("PIO_UNISCRNMAP ioctl");
91 else 91 else
92 return 0; 92 return 0;
93 } 93 }
@@ -96,10 +96,10 @@ static int screen_map_load(int fd, FILE * fp)
96 /* rewind... */ 96 /* rewind... */
97 if (-1 == fseek(fp, 0, SEEK_SET)) { 97 if (-1 == fseek(fp, 0, SEEK_SET)) {
98 if (errno == ESPIPE) 98 if (errno == ESPIPE)
99 error_msg("Assuming 8bit screen-map - MUST be a regular file."), 99 bb_error_msg("Assuming 8bit screen-map - MUST be a regular file."),
100 exit(1); 100 exit(1);
101 else 101 else
102 perror_msg_and_die("fseek failed assuming 8bit screen-map"); 102 bb_perror_msg_and_die("fseek failed assuming 8bit screen-map");
103 } 103 }
104 104
105 /* ... and try an old 8-bit screen-map */ 105 /* ... and try an old 8-bit screen-map */
@@ -109,25 +109,25 @@ static int screen_map_load(int fd, FILE * fp)
109 if (-1 == fseek(fp, 0, SEEK_SET)) { 109 if (-1 == fseek(fp, 0, SEEK_SET)) {
110 if (errno == ESPIPE) 110 if (errno == ESPIPE)
111 /* should not - it succedeed above */ 111 /* should not - it succedeed above */
112 error_msg_and_die("fseek() returned ESPIPE !"); 112 bb_error_msg_and_die("fseek() returned ESPIPE !");
113 else 113 else
114 perror_msg_and_die("fseek for binary 8bit screen-map"); 114 bb_perror_msg_and_die("fseek for binary 8bit screen-map");
115 } 115 }
116 116
117 if (fread(buf, E_TABSZ, 1, fp) != 1) 117 if (fread(buf, E_TABSZ, 1, fp) != 1)
118 perror_msg_and_die("Cannot read [old] map from file"); 118 bb_perror_msg_and_die("Cannot read [old] map from file");
119#if 0 119#if 0
120 else 120 else
121 error_msg("Input screen-map is binary."); 121 bb_error_msg("Input screen-map is binary.");
122#endif 122#endif
123 } 123 }
124 124
125 if (ioctl(fd, PIO_SCRNMAP, buf)) 125 if (ioctl(fd, PIO_SCRNMAP, buf))
126 perror_msg_and_die("PIO_SCRNMAP ioctl"); 126 bb_perror_msg_and_die("PIO_SCRNMAP ioctl");
127 else 127 else
128 return 0; 128 return 0;
129 } 129 }
130 error_msg("Error parsing symbolic map"); 130 bb_error_msg("Error parsing symbolic map");
131 return(1); 131 return(1);
132} 132}
133 133
@@ -172,7 +172,7 @@ static int uni_screen_map_read_ascii(FILE * fp, unicode buf[], int *is_unicode)
172 if (feof(fp)) 172 if (feof(fp))
173 break; 173 break;
174 else 174 else
175 perror_msg_and_die("uni_screen_map_read_ascii() can't read line"); 175 bb_perror_msg_and_die("uni_screen_map_read_ascii() can't read line");
176 } 176 }
177 177
178 /* get "charset-relative charcode", stripping leading spaces */ 178 /* get "charset-relative charcode", stripping leading spaces */
diff --git a/console-tools/loadfont.c b/console-tools/loadfont.c
index d66500195..6108151a6 100644
--- a/console-tools/loadfont.c
+++ b/console-tools/loadfont.c
@@ -44,11 +44,11 @@ extern int loadfont_main(int argc, char **argv)
44 int fd; 44 int fd;
45 45
46 if (argc != 1) 46 if (argc != 1)
47 show_usage(); 47 bb_show_usage();
48 48
49 fd = open(CURRENT_VC, O_RDWR); 49 fd = open(CURRENT_VC, O_RDWR);
50 if (fd < 0) 50 if (fd < 0)
51 perror_msg_and_die("Error opening " CURRENT_VC); 51 bb_perror_msg_and_die("Error opening " CURRENT_VC);
52 loadnewfont(fd); 52 loadnewfont(fd);
53 53
54 return EXIT_SUCCESS; 54 return EXIT_SUCCESS;
@@ -62,7 +62,7 @@ static void do_loadfont(int fd, char *inbuf, int unit, int fontsize)
62 memset(buf, 0, sizeof(buf)); 62 memset(buf, 0, sizeof(buf));
63 63
64 if (unit < 1 || unit > 32) 64 if (unit < 1 || unit > 32)
65 error_msg_and_die("Bad character size %d", unit); 65 bb_error_msg_and_die("Bad character size %d", unit);
66 66
67 for (i = 0; i < fontsize; i++) 67 for (i = 0; i < fontsize; i++)
68 memcpy(buf + (32 * i), inbuf + (unit * i), unit); 68 memcpy(buf + (32 * i), inbuf + (unit * i), unit);
@@ -77,11 +77,11 @@ static void do_loadfont(int fd, char *inbuf, int unit, int fontsize)
77 77
78 if (ioctl(fd, PIO_FONTX, &cfd) == 0) 78 if (ioctl(fd, PIO_FONTX, &cfd) == 0)
79 return; /* success */ 79 return; /* success */
80 perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)"); 80 bb_perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)");
81 } 81 }
82#endif 82#endif
83 if (ioctl(fd, PIO_FONT, buf)) 83 if (ioctl(fd, PIO_FONT, buf))
84 perror_msg_and_die("PIO_FONT ioctl error"); 84 bb_perror_msg_and_die("PIO_FONT ioctl error");
85} 85}
86 86
87static void 87static void
@@ -119,11 +119,11 @@ do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
119 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) { 119 if (ioctl(fd, PIO_UNIMAPCLR, &advice)) {
120#ifdef ENOIOCTLCMD 120#ifdef ENOIOCTLCMD
121 if (errno == ENOIOCTLCMD) { 121 if (errno == ENOIOCTLCMD) {
122 error_msg("It seems this kernel is older than 1.1.92"); 122 bb_error_msg("It seems this kernel is older than 1.1.92");
123 error_msg_and_die("No Unicode mapping table loaded."); 123 bb_error_msg_and_die("No Unicode mapping table loaded.");
124 } else 124 } else
125#endif 125#endif
126 perror_msg_and_die("PIO_UNIMAPCLR"); 126 bb_perror_msg_and_die("PIO_UNIMAPCLR");
127 } 127 }
128 ud.entry_ct = ct; 128 ud.entry_ct = ct;
129 ud.entries = up; 129 ud.entries = up;
@@ -133,7 +133,7 @@ do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize)
133 /* change advice parameters */ 133 /* change advice parameters */
134 } 134 }
135#endif 135#endif
136 perror_msg_and_die("PIO_UNIMAP"); 136 bb_perror_msg_and_die("PIO_UNIMAP");
137 } 137 }
138} 138}
139 139
@@ -150,13 +150,13 @@ static void loadnewfont(int fd)
150 */ 150 */
151 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin); 151 inputlth = fread(inbuf, 1, sizeof(inbuf), stdin);
152 if (ferror(stdin)) 152 if (ferror(stdin))
153 perror_msg_and_die("Error reading input font"); 153 bb_perror_msg_and_die("Error reading input font");
154 /* use malloc/realloc in case of giant files; 154 /* use malloc/realloc in case of giant files;
155 maybe these do not occur: 16kB for the font, 155 maybe these do not occur: 16kB for the font,
156 and 16kB for the map leaves 32 unicode values 156 and 16kB for the map leaves 32 unicode values
157 for each font position */ 157 for each font position */
158 if (!feof(stdin)) 158 if (!feof(stdin))
159 perror_msg_and_die("Font too large"); 159 bb_perror_msg_and_die("Font too large");
160 160
161 /* test for psf first */ 161 /* test for psf first */
162 { 162 {
@@ -174,11 +174,11 @@ static void loadnewfont(int fd)
174 goto no_psf; 174 goto no_psf;
175 175
176 if (psfhdr.mode > PSF_MAXMODE) 176 if (psfhdr.mode > PSF_MAXMODE)
177 error_msg_and_die("Unsupported psf file mode"); 177 bb_error_msg_and_die("Unsupported psf file mode");
178 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256); 178 fontsize = ((psfhdr.mode & PSF_MODE512) ? 512 : 256);
179#if !defined( PIO_FONTX ) || defined( __sparc__ ) 179#if !defined( PIO_FONTX ) || defined( __sparc__ )
180 if (fontsize != 256) 180 if (fontsize != 256)
181 error_msg_and_die("Only fontsize 256 supported"); 181 bb_error_msg_and_die("Only fontsize 256 supported");
182#endif 182#endif
183 hastable = (psfhdr.mode & PSF_MODEHASTAB); 183 hastable = (psfhdr.mode & PSF_MODEHASTAB);
184 unit = psfhdr.charsize; 184 unit = psfhdr.charsize;
@@ -186,7 +186,7 @@ static void loadnewfont(int fd)
186 186
187 head = head0 + fontsize * unit; 187 head = head0 + fontsize * unit;
188 if (head > inputlth || (!hastable && head != inputlth)) 188 if (head > inputlth || (!hastable && head != inputlth))
189 error_msg_and_die("Input file: bad length"); 189 bb_error_msg_and_die("Input file: bad length");
190 do_loadfont(fd, inbuf + head0, unit, fontsize); 190 do_loadfont(fd, inbuf + head0, unit, fontsize);
191 if (hastable) 191 if (hastable)
192 do_loadtable(fd, inbuf + head, inputlth - head, fontsize); 192 do_loadtable(fd, inbuf + head, inputlth - head, fontsize);
@@ -201,7 +201,7 @@ static void loadnewfont(int fd)
201 } else { 201 } else {
202 /* bare font */ 202 /* bare font */
203 if (inputlth & 0377) 203 if (inputlth & 0377)
204 error_msg_and_die("Bad input file size"); 204 bb_error_msg_and_die("Bad input file size");
205 offset = 0; 205 offset = 0;
206 unit = inputlth / 256; 206 unit = inputlth / 256;
207 } 207 }
diff --git a/console-tools/loadkmap.c b/console-tools/loadkmap.c
index 4f217d630..f4208df63 100644
--- a/console-tools/loadkmap.c
+++ b/console-tools/loadkmap.c
@@ -51,18 +51,18 @@ int loadkmap_main(int argc, char **argv)
51 char flags[MAX_NR_KEYMAPS], buff[7]; 51 char flags[MAX_NR_KEYMAPS], buff[7];
52 52
53 if (argc != 1) 53 if (argc != 1)
54 show_usage(); 54 bb_show_usage();
55 55
56 fd = open(CURRENT_VC, O_RDWR); 56 fd = open(CURRENT_VC, O_RDWR);
57 if (fd < 0) 57 if (fd < 0)
58 perror_msg_and_die("Error opening " CURRENT_VC); 58 bb_perror_msg_and_die("Error opening " CURRENT_VC);
59 59
60 read(0, buff, 7); 60 read(0, buff, 7);
61 if (0 != strncmp(buff, BINARY_KEYMAP_MAGIC, 7)) 61 if (0 != strncmp(buff, BINARY_KEYMAP_MAGIC, 7))
62 error_msg_and_die("This is not a valid binary keymap."); 62 bb_error_msg_and_die("This is not a valid binary keymap.");
63 63
64 if (MAX_NR_KEYMAPS != read(0, flags, MAX_NR_KEYMAPS)) 64 if (MAX_NR_KEYMAPS != read(0, flags, MAX_NR_KEYMAPS))
65 perror_msg_and_die("Error reading keymap flags"); 65 bb_perror_msg_and_die("Error reading keymap flags");
66 66
67 ibuff = (u_short *) xmalloc(ibuffsz); 67 ibuff = (u_short *) xmalloc(ibuffsz);
68 68
@@ -71,7 +71,7 @@ int loadkmap_main(int argc, char **argv)
71 pos = 0; 71 pos = 0;
72 while (pos < ibuffsz) { 72 while (pos < ibuffsz) {
73 if ((readsz = read(0, (char *) ibuff + pos, ibuffsz - pos)) < 0) 73 if ((readsz = read(0, (char *) ibuff + pos, ibuffsz - pos)) < 0)
74 perror_msg_and_die("Error reading keymap"); 74 bb_perror_msg_and_die("Error reading keymap");
75 pos += readsz; 75 pos += readsz;
76 } 76 }
77 for (j = 0; j < NR_KEYS; j++) { 77 for (j = 0; j < NR_KEYS; j++) {
diff --git a/console-tools/openvt.c b/console-tools/openvt.c
index 77c897597..519513623 100644
--- a/console-tools/openvt.c
+++ b/console-tools/openvt.c
@@ -43,16 +43,16 @@ int openvt_main(int argc, char **argv)
43 char * cmd_args = NULL; 43 char * cmd_args = NULL;
44 44
45 if (argc < 3) 45 if (argc < 3)
46 show_usage(); 46 bb_show_usage();
47 47
48 if (!isdigit(argv[1][0])) 48 if (!isdigit(argv[1][0]))
49 show_usage(); 49 bb_show_usage();
50 50
51 vtno = (int) atol(argv[1]); 51 vtno = (int) atol(argv[1]);
52 52
53 /* if (vtno <= 0 || vtno > 63) */ 53 /* if (vtno <= 0 || vtno > 63) */
54 if (vtno <= 0 || vtno > 12) 54 if (vtno <= 0 || vtno > 12)
55 error_msg_and_die("Illegal vt number (%d)", vtno); 55 bb_error_msg_and_die("Illegal vt number (%d)", vtno);
56 56
57 sprintf(vtname, VTNAME, vtno); 57 sprintf(vtname, VTNAME, vtno);
58 58
@@ -69,13 +69,13 @@ int openvt_main(int argc, char **argv)
69 if (setsid() < 0) { 69 if (setsid() < 0) {
70#endif 70#endif
71 71
72 perror_msg_and_die("Unable to set new session"); 72 bb_perror_msg_and_die("Unable to set new session");
73 } 73 }
74 close(0); /* so that new vt becomes stdin */ 74 close(0); /* so that new vt becomes stdin */
75 75
76 /* and grab new one */ 76 /* and grab new one */
77 if ((fd = open(vtname, O_RDWR)) == -1) 77 if ((fd = open(vtname, O_RDWR)) == -1)
78 perror_msg_and_die("could not open %s", vtname); 78 bb_perror_msg_and_die("could not open %s", vtname);
79 79
80 /* Reassign stdout and sterr */ 80 /* Reassign stdout and sterr */
81 close(1); 81 close(1);
diff --git a/console-tools/setkeycodes.c b/console-tools/setkeycodes.c
index 85612c8b1..796bab285 100644
--- a/console-tools/setkeycodes.c
+++ b/console-tools/setkeycodes.c
@@ -43,7 +43,7 @@ setkeycodes_main(int argc, char** argv)
43 struct kbkeycode a; 43 struct kbkeycode a;
44 44
45 if (argc % 2 != 1 || argc < 2) { 45 if (argc % 2 != 1 || argc < 2) {
46 show_usage(); 46 bb_show_usage();
47 } 47 }
48 48
49 fd = get_console_fd(); 49 fd = get_console_fd();
@@ -52,18 +52,18 @@ setkeycodes_main(int argc, char** argv)
52 a.keycode = atoi(argv[2]); 52 a.keycode = atoi(argv[2]);
53 a.scancode = sc = strtol(argv[1], &ep, 16); 53 a.scancode = sc = strtol(argv[1], &ep, 16);
54 if (*ep) { 54 if (*ep) {
55 error_msg_and_die("error reading SCANCODE: '%s'", argv[1]); 55 bb_error_msg_and_die("error reading SCANCODE: '%s'", argv[1]);
56 } 56 }
57 if (a.scancode > 127) { 57 if (a.scancode > 127) {
58 a.scancode -= 0xe000; 58 a.scancode -= 0xe000;
59 a.scancode += 128; 59 a.scancode += 128;
60 } 60 }
61 if (a.scancode > 255 || a.keycode > 127) { 61 if (a.scancode > 255 || a.keycode > 127) {
62 error_msg_and_die("SCANCODE or KEYCODE outside bounds"); 62 bb_error_msg_and_die("SCANCODE or KEYCODE outside bounds");
63 } 63 }
64 if (ioctl(fd,KDSETKEYCODE,&a)) { 64 if (ioctl(fd,KDSETKEYCODE,&a)) {
65 perror("KDSETKEYCODE"); 65 perror("KDSETKEYCODE");
66 error_msg_and_die("failed to set SCANCODE %x to KEYCODE %d", sc, a.keycode); 66 bb_error_msg_and_die("failed to set SCANCODE %x to KEYCODE %d", sc, a.keycode);
67 } 67 }
68 argc -= 2; 68 argc -= 2;
69 argv += 2; 69 argv += 2;
diff --git a/coreutils/Config.in b/coreutils/Config.in
index 5b9996d5b..9f5c379a3 100644
--- a/coreutils/Config.in
+++ b/coreutils/Config.in
@@ -111,17 +111,31 @@ config CONFIG_DOS2UNIX
111 Please submit a patch to add help text for this item. 111 Please submit a patch to add help text for this item.
112 112
113config CONFIG_DU 113config CONFIG_DU
114 bool "du" 114 bool "du (default blocksize of 512 bytes)"
115 default n 115 default n
116 help 116 help
117 Please submit a patch to add help text for this item. 117 Please submit a patch to add help text for this item.
118 118
119config CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K
120 bool " Use a default blocksize of 1024 bytes (1K)"
121 default y
122 depends on CONFIG_DU
123 help
124 Please submit a patch to add help text for this item.
125
119config CONFIG_ECHO 126config CONFIG_ECHO
120 bool "echo" 127 bool "echo (basic SUSv3 version taking no options"
121 default n 128 default n
122 help 129 help
123 Please submit a patch to add help text for this item. 130 Please submit a patch to add help text for this item.
124 131
132config CONFIG_FEATURE_FANCY_ECHO
133 bool " Enable echo options (-n and -e)"
134 default y
135 depends on CONFIG_ECHO
136 help
137 Please submit a patch to add help text for this item.
138
125config CONFIG_ENV 139config CONFIG_ENV
126 bool "env" 140 bool "env"
127 default n 141 default n
@@ -154,6 +168,13 @@ config CONFIG_HEAD
154 help 168 help
155 Please submit a patch to add help text for this item. 169 Please submit a patch to add help text for this item.
156 170
171config CONFIG_FEATURE_FANCY_HEAD
172 bool " Enable head options (-c, -q, and -v)"
173 default n
174 depends on CONFIG_HEAD
175 help
176 Please submit a patch to add help text for this item.
177
157config CONFIG_HOSTID 178config CONFIG_HOSTID
158 bool "hostid" 179 bool "hostid"
159 default n 180 default n
@@ -313,8 +334,15 @@ config CONFIG_SHA1SUM
313 Compute and check SHA1 message digest 334 Compute and check SHA1 message digest
314 335
315config CONFIG_SLEEP 336config CONFIG_SLEEP
316 bool "sleep" 337 bool "sleep (single integer arg with no suffix)"
338 default n
339 help
340 Please submit a patch to add help text for this item.
341
342config CONFIG_FEATURE_FANCY_SLEEP
343 bool " Enable multiple integer args and optional time suffixes"
317 default n 344 default n
345 depends on CONFIG_SLEEP
318 help 346 help
319 Please submit a patch to add help text for this item. 347 Please submit a patch to add help text for this item.
320 348
@@ -369,6 +397,13 @@ config CONFIG_TEE
369 help 397 help
370 Please submit a patch to add help text for this item. 398 Please submit a patch to add help text for this item.
371 399
400config CONFIG_FEATURE_TEE_USE_BLOCK_IO
401 bool " Enable block i/o (larger/faster) instead of byte i/o."
402 default n
403 depends on CONFIG_TEE
404 help
405 Please submit a patch to add help text for this item.
406
372if CONFIG_ASH || CONFIG_HUSH || CONFIG_LASH || CONFIG_MSH 407if CONFIG_ASH || CONFIG_HUSH || CONFIG_LASH || CONFIG_MSH
373 config CONFIG_TEST 408 config CONFIG_TEST
374 default y 409 default y
diff --git a/coreutils/basename.c b/coreutils/basename.c
index bdbcec17a..71bb9b3bd 100644
--- a/coreutils/basename.c
+++ b/coreutils/basename.c
@@ -21,32 +21,43 @@
21 * 21 *
22 */ 22 */
23 23
24/* getopt not needed */ 24/* BB_AUDIT SUSv3 compliant */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/basename.html */
26
27
28/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
29 *
30 * Changes:
31 * 1) Now checks for too many args. Need at least one and at most two.
32 * 2) Don't check for options, as per SUSv3.
33 * 3) Save some space by using strcmp(). Calling strncmp() here was silly.
34 */
25 35
26#include <stdlib.h> 36#include <stdlib.h>
27#include "busybox.h" 37#include <stdio.h>
28#include <string.h> 38#include <string.h>
39#include "busybox.h"
29 40
30extern int basename_main(int argc, char **argv) 41extern int basename_main(int argc, char **argv)
31{ 42{
32 int m, n; 43 size_t m, n;
33 char *s; 44 char *s;
34 45
35 if ((argc < 2) || (**(argv + 1) == '-')) { 46 if (((unsigned int)(argc-2)) >= 2) {
36 show_usage(); 47 bb_show_usage();
37 } 48 }
38 49
39 argv++; 50 s = bb_get_last_path_component(*++argv);
40
41 s = get_last_path_component(*argv);
42 51
43 if (argc>2) { 52 if (*++argv) {
44 argv++;
45 n = strlen(*argv); 53 n = strlen(*argv);
46 m = strlen(s); 54 m = strlen(s);
47 if (m>n && strncmp(s+m-n, *argv, n)==0) 55 if ((m > n) && ((strcmp)(s+m-n, *argv) == 0)) {
48 s[m-n] = '\0'; 56 s[m-n] = '\0';
57 }
49 } 58 }
59
50 puts(s); 60 puts(s);
51 return EXIT_SUCCESS; 61
62 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
52} 63}
diff --git a/coreutils/cal.c b/coreutils/cal.c
index ab631576a..ed480dd78 100644
--- a/coreutils/cal.c
+++ b/coreutils/cal.c
@@ -1,3 +1,14 @@
1/* NOTE:
2 *
3 * Apparently, all "Steven J. Merrifield" did was grab the util-linux cal applet,
4 * spend maybe 5 minutes integrating it into busybox, slapped a copyright on it,
5 * and submitted it. I certainly saw no evidence of any attempt at size reduction.
6 * Not only do I consider his copyright below meaningless, I also consider his
7 * actions shameful.
8 *
9 * Manuel Novoa III (mjn3@codepoet.org)
10 */
11
1/* 12/*
2 * Calendar implementation for busybox 13 * Calendar implementation for busybox
3 * 14 *
@@ -20,7 +31,16 @@
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * 32 *
22*/ 33*/
23 34
35/* BB_AUDIT SUSv3 compliant with -j and -y extensions (from util-linux). */
36/* BB_AUDIT BUG: The output of 'cal -j 1752' is incorrect. The upstream
37 * BB_AUDIT BUG: version in util-linux seems to be broken as well. */
38/* http://www.opengroup.org/onlinepubs/007904975/utilities/cal.html */
39
40/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
41 *
42 * Major size reduction... over 50% (>1.5k) on i386.
43 */
24 44
25#include <sys/types.h> 45#include <sys/types.h>
26#include <ctype.h> 46#include <ctype.h>
@@ -46,44 +66,30 @@
46#define MAXDAYS 42 /* max slots in a month array */ 66#define MAXDAYS 42 /* max slots in a month array */
47#define SPACE -1 /* used in day array */ 67#define SPACE -1 /* used in day array */
48 68
49static int days_in_month[2][13] = { 69static const char days_in_month[] = {
50 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 70 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
51 {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
52}; 71};
53 72
54int sep1752[MAXDAYS] = { 73static const char sep1752[] = {
55 SPACE, SPACE, 1, 2, 14, 15, 16, 74 1, 2, 14, 15, 16,
56 17, 18, 19, 20, 21, 22, 23, 75 17, 18, 19, 20, 21, 22, 23,
57 24, 25, 26, 27, 28, 29, 30, 76 24, 25, 26, 27, 28, 29, 30
58 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
59 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
60 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
61}, j_sep1752[MAXDAYS] = {
62 SPACE, SPACE, 245, 246, 258, 259, 260,
63 261, 262, 263, 264, 265, 266, 267,
64 268, 269, 270, 271, 272, 273, 274,
65 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
66 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
67 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
68}, empty[MAXDAYS] = {
69 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
70 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
71 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
72 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
73 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
74 SPACE, SPACE, SPACE, SPACE, SPACE, SPACE, SPACE,
75}; 77};
76 78
77char *month_names[12]; 79static int julian;
78
79char day_headings[] = " ";
80char j_day_headings[] = " ";
81 80
82/* leap year -- account for gregorian reformation in 1752 */ 81/* leap year -- account for gregorian reformation in 1752 */
83#define leap_year(yr) \ 82#define leap_year(yr) \
84 ((yr) <= 1752 ? !((yr) % 4) : \ 83 ((yr) <= 1752 ? !((yr) % 4) : \
85 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400)) 84 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))
86 85
86static int is_leap_year(int year)
87{
88 return leap_year(year);
89}
90#undef leap_year
91#define leap_year(yr) is_leap_year(yr)
92
87/* number of centuries since 1700, not inclusive */ 93/* number of centuries since 1700, not inclusive */
88#define centuries_since_1700(yr) \ 94#define centuries_since_1700(yr) \
89 ((yr) > 1700 ? (yr) / 100 - 17 : 0) 95 ((yr) > 1700 ? (yr) / 100 - 17 : 0)
@@ -96,178 +102,129 @@ char j_day_headings[] = " ";
96#define leap_years_since_year_1(yr) \ 102#define leap_years_since_year_1(yr) \
97 ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr)) 103 ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
98 104
99int julian; 105static void center __P((char *, int, int));
100void ascii_day __P((char *, int)); 106static void day_array __P((int, int, int *));
101void center __P((char *, int, int)); 107static void trim_trailing_spaces_and_print __P((char *));
102void day_array __P((int, int, int *)); 108
103int day_in_week __P((int, int, int)); 109static void blank_string(char *buf, size_t buflen);
104int day_in_year __P((int, int, int)); 110static char *build_row(char *p, int *dp);
105void j_yearly __P((int)); 111
106void monthly __P((int, int)); 112#define DAY_LEN 3 /* 3 spaces per day */
107void trim_trailing_spaces __P((char *)); 113#define J_DAY_LEN (DAY_LEN + 1)
108void yearly __P((int)); 114#define WEEK_LEN 20 /* 7 * 3 - one space at the end */
115#define J_WEEK_LEN (WEEK_LEN + 7)
116#define HEAD_SEP 2 /* spaces between day headings */
109 117
110int cal_main(int argc, char **argv) 118int cal_main(int argc, char **argv)
111{ 119{
112 struct tm *local_time; 120 struct tm *local_time;
113 static struct tm zero_tm; 121 struct tm zero_tm;
114 time_t now; 122 time_t now;
115 int ch, month, year, yflag, i; 123 int month, year, flags, i;
124 char *month_names[12];
125 char day_headings[28]; /* 28 for julian, 21 for nonjulian */
116 char buf[40]; 126 char buf[40];
117 127
118#ifdef CONFIG_LOCALE_SUPPORT 128#ifdef CONFIG_LOCALE_SUPPORT
119 setlocale(LC_TIME, ""); 129 setlocale(LC_TIME, "");
120#endif 130#endif
121 131
122 yflag = 0; 132 flags = bb_getopt_ulflags(argc, argv, "jy");
123 while ((ch = getopt(argc, argv, "jy")) != -1) 133
124 switch(ch) { 134 julian = flags & 1;
125 case 'j': 135
126 julian = 1;
127 break;
128 case 'y':
129 yflag = 1;
130 break;
131 default:
132 show_usage();
133 }
134 argc -= optind;
135 argv += optind; 136 argv += optind;
136 137
137 month = 0; 138 month = 0;
138 switch(argc) { 139
139 case 2: 140 if ((argc -= optind) > 2) {
140 if ((month = atoi(*argv++)) < 1 || month > 12) 141 bb_show_usage();
141 error_msg_and_die("Illegal month value: use 1-12"); 142 }
142 /* FALLTHROUGH */ 143
143 case 1: 144 if (!argc) {
144 if ((year = atoi(*argv)) < 1 || year > 9999)
145 error_msg_and_die("Illegal year value: use 1-9999");
146 break;
147 case 0:
148 time(&now); 145 time(&now);
149 local_time = localtime(&now); 146 local_time = localtime(&now);
150 year = local_time->tm_year + 1900; 147 year = local_time->tm_year + 1900;
151 if (!yflag) 148 if (!(flags & 2)) {
152 month = local_time->tm_mon + 1; 149 month = local_time->tm_mon + 1;
153 break; 150 }
154 default: 151 } else {
155 show_usage(); 152 if (argc == 2) {
153 month = bb_xgetularg10_bnd(*argv++, 1, 12);
154 }
155 year = bb_xgetularg10_bnd(*argv, 1, 9999);
156 } 156 }
157 157
158 for (i = 0; i < 12; i++) { 158 blank_string(day_headings, sizeof(day_headings) - 7 + 7*julian);
159
160 i = 0;
161 do {
159 zero_tm.tm_mon = i; 162 zero_tm.tm_mon = i;
160 strftime(buf, sizeof(buf), "%B", &zero_tm); 163 strftime(buf, sizeof(buf), "%B", &zero_tm);
161 month_names[i] = xstrdup(buf); 164 month_names[i] = bb_xstrdup(buf);
162 }
163 for (i = 0; i < 7; i++) {
164 zero_tm.tm_wday = i;
165 strftime(buf, sizeof(buf), "%a", &zero_tm);
166 strncpy(day_headings + i * 3, buf, 2);
167 strncpy(j_day_headings + i * 4 + 1, buf, 2);
168 }
169
170 if (month)
171 monthly(month, year);
172 else if (julian)
173 j_yearly(year);
174 else
175 yearly(year);
176 exit(0);
177}
178 165
179#define DAY_LEN 3 /* 3 spaces per day */ 166 if (i < 7) {
180#define J_DAY_LEN 4 /* 4 spaces per day */ 167 zero_tm.tm_wday = i;
181#define WEEK_LEN 20 /* 7 * 3 - one space at the end */ 168 strftime(buf, sizeof(buf), "%a", &zero_tm);
182#define J_WEEK_LEN 27 /* 7 * 4 - one space at the end */ 169 strncpy(day_headings + i * (3+julian) + julian, buf, 2);
183#define HEAD_SEP 2 /* spaces between day headings */
184#define J_HEAD_SEP 2
185
186void monthly(int month, int year)
187{
188 int col, row, len, days[MAXDAYS];
189 char *p, lineout[30];
190
191 day_array(month, year, days);
192 len = sprintf(lineout, "%s %d", month_names[month - 1], year);
193 printf("%*s%s\n%s\n",
194 ((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "",
195 lineout, julian ? j_day_headings : day_headings);
196 for (row = 0; row < 6; row++) {
197 for (col = 0, p = lineout; col < 7; col++,
198 p += julian ? J_DAY_LEN : DAY_LEN)
199 ascii_day(p, days[row * 7 + col]);
200 *p = '\0';
201 trim_trailing_spaces(lineout);
202 printf("%s\n", lineout);
203 }
204}
205
206void j_yearly(int year)
207{
208 int col, *dp, i, month, row, which_cal;
209 int days[12][MAXDAYS];
210 char *p, lineout[80];
211
212 sprintf(lineout, "%d", year);
213 center(lineout, J_WEEK_LEN * 2 + J_HEAD_SEP, 0);
214 printf("\n\n");
215 for (i = 0; i < 12; i++)
216 day_array(i + 1, year, days[i]);
217 memset(lineout, ' ', sizeof(lineout) - 1);
218 lineout[sizeof(lineout) - 1] = '\0';
219 for (month = 0; month < 12; month += 2) {
220 center(month_names[month], J_WEEK_LEN, J_HEAD_SEP);
221 center(month_names[month + 1], J_WEEK_LEN, 0);
222 printf("\n%s%*s%s\n", j_day_headings, J_HEAD_SEP, "",
223 j_day_headings);
224 for (row = 0; row < 6; row++) {
225 for (which_cal = 0; which_cal < 2; which_cal++) {
226 p = lineout + which_cal * (J_WEEK_LEN + 2);
227 dp = &days[month + which_cal][row * 7];
228 for (col = 0; col < 7; col++, p += J_DAY_LEN)
229 ascii_day(p, *dp++);
230 }
231 *p = '\0';
232 trim_trailing_spaces(lineout);
233 printf("%s\n", lineout);
234 } 170 }
235 } 171 } while (++i < 12);
236 printf("\n"); 172
237} 173 if (month) {
238 174 int row, len, days[MAXDAYS];
239void yearly(int year) 175 int *dp = days;
240{ 176 char lineout[30];
241 int col, *dp, i, month, row, which_cal; 177
242 int days[12][MAXDAYS]; 178 day_array(month, year, dp);
243 char *p, lineout[80]; 179 len = sprintf(lineout, "%s %d", month_names[month - 1], year);
244 180 bb_printf("%*s%s\n%s\n",
245 sprintf(lineout, "%d", year); 181 ((7*julian + WEEK_LEN) - len) / 2, "",
246 center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0); 182 lineout, day_headings);
247 printf("\n\n");
248 for (i = 0; i < 12; i++)
249 day_array(i + 1, year, days[i]);
250 memset(lineout, ' ', sizeof(lineout) - 1);
251 lineout[sizeof(lineout) - 1] = '\0';
252 for (month = 0; month < 12; month += 3) {
253 center(month_names[month], WEEK_LEN, HEAD_SEP);
254 center(month_names[month + 1], WEEK_LEN, HEAD_SEP);
255 center(month_names[month + 2], WEEK_LEN, 0);
256 printf("\n%s%*s%s%*s%s\n", day_headings, HEAD_SEP,
257 "", day_headings, HEAD_SEP, "", day_headings);
258 for (row = 0; row < 6; row++) { 183 for (row = 0; row < 6; row++) {
259 for (which_cal = 0; which_cal < 3; which_cal++) { 184 build_row(lineout, dp)[0] = '\0';
260 p = lineout + which_cal * (WEEK_LEN + 2); 185 dp += 7;
261 dp = &days[month + which_cal][row * 7]; 186 trim_trailing_spaces_and_print(lineout);
262 for (col = 0; col < 7; col++, p += DAY_LEN) 187 }
263 ascii_day(p, *dp++); 188 } else {
189 int row, which_cal, week_len, days[12][MAXDAYS];
190 int *dp;
191 char lineout[80];
192
193 sprintf(lineout, "%d", year);
194 center(lineout,
195 (WEEK_LEN * 3 + HEAD_SEP * 2)
196 + julian * (J_WEEK_LEN * 2 + HEAD_SEP
197 - (WEEK_LEN * 3 + HEAD_SEP * 2)),
198 0);
199 puts("\n"); /* two \n's */
200 for (i = 0; i < 12; i++) {
201 day_array(i + 1, year, days[i]);
202 }
203 blank_string(lineout, sizeof(lineout));
204 week_len = WEEK_LEN + julian * (J_WEEK_LEN - WEEK_LEN);
205 for (month = 0; month < 12; month += 3-julian) {
206 center(month_names[month], week_len, HEAD_SEP);
207 if (!julian) {
208 center(month_names[month + 1], week_len, HEAD_SEP);
209 }
210 center(month_names[month + 2 - julian], week_len, 0);
211 bb_printf("\n%s%*s%s", day_headings, HEAD_SEP, "", day_headings);
212 if (!julian) {
213 bb_printf("%*s%s", HEAD_SEP, "", day_headings);
214 }
215 putchar('\n');
216 for (row = 0; row < (6*7); row += 7) {
217 for (which_cal = 0; which_cal < 3-julian; which_cal++) {
218 dp = days[month + which_cal] + row;
219 build_row(lineout + which_cal * (week_len + 2), dp);
220 }
221 /* blank_string took care of nul termination. */
222 trim_trailing_spaces_and_print(lineout);
264 } 223 }
265 *p = '\0';
266 trim_trailing_spaces(lineout);
267 printf("%s\n", lineout);
268 } 224 }
269 } 225 }
270 printf("\n"); 226
227 bb_fflush_stdout_and_exit(0);
271} 228}
272 229
273/* 230/*
@@ -277,118 +234,129 @@ void yearly(int year)
277 * out end to end. You would have 42 numbers or spaces. This routine 234 * out end to end. You would have 42 numbers or spaces. This routine
278 * builds that array for any month from Jan. 1 through Dec. 9999. 235 * builds that array for any month from Jan. 1 through Dec. 9999.
279 */ 236 */
280void day_array(int month, int year, int *days) 237static void day_array(int month, int year, int *days)
281{ 238{
239 long temp;
240 int i;
241 int j_offset;
282 int day, dw, dm; 242 int day, dw, dm;
283 243
244 memset(days, SPACE, MAXDAYS * sizeof(int));
245
284 if ((month == 9) && (year == 1752)) { 246 if ((month == 9) && (year == 1752)) {
285 memmove(days, 247 j_offset = julian * 244;
286 julian ? j_sep1752 : sep1752, MAXDAYS * sizeof(int)); 248 i = 0;
249 do {
250 days[i+2] = sep1752[i] + j_offset;
251 } while (++i < sizeof(sep1752));
252
287 return; 253 return;
288 } 254 }
289 memmove(days, empty, MAXDAYS * sizeof(int));
290 dm = days_in_month[leap_year(year)][month];
291 dw = day_in_week(1, month, year);
292 day = julian ? day_in_year(1, month, year) : 1;
293 while (dm--)
294 days[dw++] = day++;
295}
296 255
297/* 256 /* day_in_year
298 * day_in_year -- 257 * return the 1 based day number within the year
299 * return the 1 based day number within the year 258 */
300 */ 259 day = 1;
301int day_in_year(int day, int month, int year) 260 if ((month > 2) && leap_year(year)) {
302{ 261 ++day;
303 int i, leap; 262 }
304
305 leap = leap_year(year);
306 for (i = 1; i < month; i++)
307 day += days_in_month[leap][i];
308 return (day);
309}
310 263
311/* 264 i = month;
312 * day_in_week 265 while (i) {
313 * return the 0 based day number for any date from 1 Jan. 1 to 266 day += days_in_month[--i];
314 * 31 Dec. 9999. Assumes the Gregorian reformation eliminates 267 }
315 * 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all
316 * missing days.
317 */
318int day_in_week(int day, int month, int year)
319{
320 long temp;
321 268
269 /* day_in_week
270 * return the 0 based day number for any date from 1 Jan. 1 to
271 * 31 Dec. 9999. Assumes the Gregorian reformation eliminates
272 * 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all
273 * missing days.
274 */
275 dw = THURSDAY;
322 temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1) 276 temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
323 + day_in_year(day, month, year); 277 + day;
324 if (temp < FIRST_MISSING_DAY) 278 if (temp < FIRST_MISSING_DAY) {
325 return ((temp - 1 + SATURDAY) % 7); 279 dw = ((temp - 1 + SATURDAY) % 7);
326 if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS)) 280 } else if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS)) {
327 return (((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7); 281 dw = (((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
328 return (THURSDAY); 282 }
283
284 if (!julian) {
285 day = 1;
286 }
287
288 dm = days_in_month[month];
289 if ((month == 2) && leap_year(year)) {
290 ++dm;
291 }
292
293 while (dm) {
294 days[dw++] = day++;
295 --dm;
296 }
329} 297}
330 298
331void ascii_day(char *p, int day) 299static void trim_trailing_spaces_and_print(char *s)
332{ 300{
333 int display, val; 301 char *p = s;
334 static char *aday[] = { 302
335 "", 303 while (*p) {
336 " 1", " 2", " 3", " 4", " 5", " 6", " 7", 304 ++p;
337 " 8", " 9", "10", "11", "12", "13", "14",
338 "15", "16", "17", "18", "19", "20", "21",
339 "22", "23", "24", "25", "26", "27", "28",
340 "29", "30", "31",
341 };
342
343 if (day == SPACE) {
344 memset(p, ' ', julian ? J_DAY_LEN : DAY_LEN);
345 return;
346 } 305 }
347 if (julian) { 306 while (p > s) {
348 if ((val = day / 100) != 0) { 307 --p;
349 day %= 100; 308 if (!(isspace)(*p)) { /* We want the function... not the inline. */
350 *p++ = val + '0'; 309 p[1] = '\0';
351 display = 1; 310 break;
352 } else {
353 *p++ = ' ';
354 display = 0;
355 } 311 }
356 val = day / 10;
357 if (val || display)
358 *p++ = val + '0';
359 else
360 *p++ = ' ';
361 *p++ = day % 10 + '0';
362 } else {
363 *p++ = aday[day][0];
364 *p++ = aday[day][1];
365 } 312 }
366 *p = ' '; 313
314 puts(s);
367} 315}
368 316
369void trim_trailing_spaces(char *s) 317static void center(char *str, int len, int separate)
370{ 318{
371 char *p; 319 int n = strlen(str);
320 len -= n;
321 bb_printf("%*s%*s", (len/2) + n, str, (len/2) + (len % 2) + separate, "");
322}
372 323
373 for (p = s; *p; ++p) 324static void blank_string(char *buf, size_t buflen)
374 continue; 325{
375 while (p > s && (--p, isspace(*p))) 326 memset(buf, ' ', buflen);
376 continue; 327 buf[buflen-1] = '\0';
377 if (p > s)
378 ++p;
379 *p = '\0';
380} 328}
381 329
382void center(char *str, int len, int separate) 330static char *build_row(char *p, int *dp)
383{ 331{
332 int col, val, day;
333
334 memset(p, ' ', (julian + DAY_LEN) * 7);
335
336 col = 0;
337 do {
338 if ((day = *dp++) != SPACE) {
339 if (julian) {
340 *++p;
341 if (day >= 100) {
342 *p = '0';
343 p[-1] = (day / 100) + '0';
344 day %= 100;
345 }
346 }
347 if ((val = day / 10) > 0) {
348 *p = val + '0';
349 }
350 *++p = day % 10 + '0';
351 p += 2;
352 } else {
353 p += DAY_LEN + julian;
354 }
355 } while (++col < 7);
384 356
385 len -= strlen(str); 357 return p;
386 printf("%*s%s%*s", len / 2, "", str, len / 2 + len % 2, "");
387 if (separate)
388 printf("%*s", separate, "");
389} 358}
390 359
391
392/* 360/*
393 * Copyright (c) 1989, 1993, 1994 361 * Copyright (c) 1989, 1993, 1994
394 * The Regents of the University of California. All rights reserved. 362 * The Regents of the University of California. All rights reserved.
diff --git a/coreutils/cat.c b/coreutils/cat.c
index 33f15da71..865275767 100644
--- a/coreutils/cat.c
+++ b/coreutils/cat.c
@@ -1,9 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini Cat implementation for busybox 3 * cat implementation for busybox
4 * 4 *
5 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -21,33 +20,48 @@
21 * 20 *
22 */ 21 */
23 22
23/* BB_AUDIT SUSv3 compliant */
24/* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */
25
26/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
27 *
28 * This is a new implementation of 'cat' which aims to be SUSv3 compliant.
29 *
30 * Changes from the previous implementation include:
31 * 1) Multiple '-' args are accepted as required by SUSv3. The previous
32 * implementation would close stdin and segfault on a subsequent '-'.
33 * 2) The '-u' options is required by SUSv3. Note that the specified
34 * behavior for '-u' is done by default, so all we need do is accept
35 * the option.
36 */
37
24#include <stdlib.h> 38#include <stdlib.h>
25#include <string.h> 39#include <stdio.h>
40#include <unistd.h>
26#include "busybox.h" 41#include "busybox.h"
27 42
28extern int cat_main(int argc, char **argv) 43extern int cat_main(int argc, char **argv)
29{ 44{
30 int status = EXIT_SUCCESS; 45 FILE *f;
46 int retval = EXIT_SUCCESS;
31 47
32 if (argc == 1) { 48 bb_getopt_ulflags(argc, argv, "u");
33 print_file(stdin); 49
34 return status; 50 argv += optind;
51 if (!*argv) {
52 *--argv = "-";
35 } 53 }
36 54
37 while (--argc > 0) { 55 do {
38 if(!(strcmp(*++argv, "-"))) { 56 if ((f = bb_wfopen_input(*argv)) != NULL) {
39 print_file(stdin); 57 int r = bb_copyfd(fileno(f), STDOUT_FILENO, 0);
40 } else if (! print_file_by_name(*argv)) { 58 bb_fclose_nonstdin(f);
41 status = EXIT_FAILURE; 59 if (r >= 0) {
60 continue;
61 }
42 } 62 }
43 } 63 retval = EXIT_FAILURE;
44 return status; 64 } while (*++argv);
45}
46 65
47/* 66 return retval;
48Local Variables: 67}
49c-file-style: "linux"
50c-basic-offset: 4
51tab-width: 4
52End:
53*/
diff --git a/coreutils/chgrp.c b/coreutils/chgrp.c
index 968b448c0..f5e5d29f1 100644
--- a/coreutils/chgrp.c
+++ b/coreutils/chgrp.c
@@ -21,9 +21,12 @@
21 * 21 *
22 */ 22 */
23 23
24#include <stdio.h> 24/* BB_AUDIT SUSv3 defects - unsupported options -h, -H, -L, and -P. */
25/* BB_AUDIT GNU defects - unsupported options -h, -c, -f, -v, and long options. */
26/* BB_AUDIT Note: gnu chgrp does not support -H, -L, or -P. */
27/* http://www.opengroup.org/onlinepubs/007904975/utilities/chgrp.html */
28
25#include <stdlib.h> 29#include <stdlib.h>
26#include <string.h>
27#include <unistd.h> 30#include <unistd.h>
28#include "busybox.h" 31#include "busybox.h"
29 32
@@ -32,53 +35,46 @@
32#define lchown chown 35#define lchown chown
33#endif 36#endif
34 37
35
36static long gid;
37
38static int fileAction(const char *fileName, struct stat *statbuf, void* junk) 38static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
39{ 39{
40 if (lchown(fileName, statbuf->st_uid, (gid == -1) ? statbuf->st_gid : gid) == 0) { 40 if (lchown(fileName, statbuf->st_uid, *((long *) junk)) == 0) {
41 return (TRUE); 41 return (TRUE);
42 } 42 }
43 perror(fileName); 43 bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */
44 return (FALSE); 44 return (FALSE);
45} 45}
46 46
47int chgrp_main(int argc, char **argv) 47int chgrp_main(int argc, char **argv)
48{ 48{
49 int opt; 49 long gid;
50 int recursiveFlag = FALSE; 50 int recursiveFlag;;
51 char *p=NULL; 51 int retval = EXIT_SUCCESS;
52 char *p;
52 53
53 /* do normal option parsing */ 54 recursiveFlag = bb_getopt_ulflags(argc, argv, "R");
54 while ((opt = getopt(argc, argv, "R")) > 0) { 55
55 switch (opt) { 56 if (argc - optind < 2) {
56 case 'R': 57 bb_show_usage();
57 recursiveFlag = TRUE;
58 break;
59 default:
60 show_usage();
61 }
62 } 58 }
63 59
64 if (argc > optind && argc > 2 && argv[optind]) { 60 argv += optind;
65 /* Find the selected group */ 61
66 gid = strtoul(argv[optind], &p, 10); /* maybe it's already numeric */ 62 /* Find the selected group */
67 if (argv[optind] == p) 63 gid = strtoul(*argv, &p, 10); /* maybe it's already numeric */
68 gid = my_getgrnam(argv[optind]); 64 if (*p || (p == *argv)) { /* trailing chars or nonnumeric */
69 } else { 65 gid = my_getgrnam(*argv);
70 error_msg_and_die(too_few_args);
71 } 66 }
67 ++argv;
72 68
73 /* Ok, ready to do the deed now */ 69 /* Ok, ready to do the deed now */
74 while (++optind < argc) { 70 do {
75 if (! recursive_action (argv[optind], recursiveFlag, FALSE, FALSE, 71 if (! recursive_action (*argv, recursiveFlag, FALSE, FALSE,
76 fileAction, fileAction, NULL)) { 72 fileAction, fileAction, &gid)) {
77 return EXIT_FAILURE; 73 retval = EXIT_FAILURE;
78 } 74 }
79 } 75 } while (*++argv);
80 return EXIT_SUCCESS;
81 76
77 return retval;
82} 78}
83 79
84/* 80/*
diff --git a/coreutils/chmod.c b/coreutils/chmod.c
index ba80e020a..28c98552a 100644
--- a/coreutils/chmod.c
+++ b/coreutils/chmod.c
@@ -24,67 +24,84 @@
24 * 24 *
25 */ 25 */
26 26
27/* BB_AUDIT SUSv3 compliant */
28/* BB_AUDIT GNU defects - unsupported options -c, -f, -v, and long options. */
29/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
30
27#include <stdio.h> 31#include <stdio.h>
28#include <stdlib.h> 32#include <stdlib.h>
29#include <string.h> 33#include <string.h>
30#include <unistd.h> 34#include <unistd.h>
31#include <getopt.h> 35#include <sys/stat.h>
32#include "busybox.h" 36#include "busybox.h"
33 37
34static int fileAction(const char *fileName, struct stat *statbuf, void* junk) 38static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
35{ 39{
36 if (!parse_mode((char *)junk, &(statbuf->st_mode))) 40 if (!bb_parse_mode((char *)junk, &(statbuf->st_mode)))
37 error_msg_and_die( "unknown mode: %s", (char *)junk); 41 bb_error_msg_and_die( "unknown mode: %s", (char *)junk);
38 if (chmod(fileName, statbuf->st_mode) == 0) 42 if (chmod(fileName, statbuf->st_mode) == 0)
39 return (TRUE); 43 return (TRUE);
40 perror(fileName); 44 bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */
41 return (FALSE); 45 return (FALSE);
42} 46}
43 47
44int chmod_main(int argc, char **argv) 48int chmod_main(int argc, char **argv)
45{ 49{
46 int opt; 50 int retval = EXIT_SUCCESS;
47 int recursiveFlag = FALSE; 51 int recursiveFlag = FALSE;
48 int modeind = 0; /* Index of the mode argument in `argv'. */ 52 int count;
49 char *smode; 53 char *smode;
50 static const char chmod_modes[] = "Rrwxstugoa,+-="; 54 char **p;
55 char *p0;
56 char opt = '-';
51 57
52 /* do normal option parsing */ 58 ++argv;
53 while (1) { 59 count = 0;
54 int thisind = optind ? optind : 1;
55 60
56 opt = getopt(argc, argv, chmod_modes); 61 for (p = argv ; *p ; p++) {
57 if (opt == EOF) 62 p0 = p[0];
58 break; 63 if (p0[0] == opt) {
59 smode = strchr(chmod_modes, opt); 64 if ((p0[1] == '-') && !p0[2]) {
60 if(smode == NULL) 65 opt = 0; /* Disable further option processing. */
61 show_usage(); 66 continue;
62 if(smode == chmod_modes) { /* 'R' */ 67 }
63 recursiveFlag = TRUE; 68 if (p0[1] == 'R') {
64 } else { 69 char *s = p0 + 2;
65 if (modeind != 0 && modeind != thisind) 70 while (*s == 'R') {
66 show_usage(); 71 ++s;
67 modeind = thisind; 72 }
73 if (*s) {
74 bb_show_usage();
75 }
76 recursiveFlag = TRUE;
77 continue;
78 }
79 if (count) {
80 bb_show_usage();
81 }
68 } 82 }
83 argv[count] = p0;
84 ++count;
69 } 85 }
70 86
71 if (modeind == 0) 87 argv[count] = NULL;
72 modeind = optind++;
73 88
74 opt = optind; 89 if (count < 2) {
75 if (opt >= argc) { 90 bb_show_usage();
76 error_msg_and_die(too_few_args);
77 } 91 }
78 92
79 smode = argv[modeind]; 93 smode = *argv;
94 ++argv;
95
80 /* Ok, ready to do the deed now */ 96 /* Ok, ready to do the deed now */
81 for (; opt < argc; opt++) { 97 do {
82 if (! recursive_action (argv[opt], recursiveFlag, FALSE, FALSE, fileAction, 98 if (! recursive_action (*argv, recursiveFlag, FALSE, FALSE,
83 fileAction, smode)) { 99 fileAction, fileAction, smode)) {
84 return EXIT_FAILURE; 100 retval = EXIT_FAILURE;
85 } 101 }
86 } 102 } while (*++argv);
87 return EXIT_SUCCESS; 103
104 return retval;
88} 105}
89 106
90/* 107/*
diff --git a/coreutils/chown.c b/coreutils/chown.c
index 4e766a90b..3e983cfa6 100644
--- a/coreutils/chown.c
+++ b/coreutils/chown.c
@@ -21,10 +21,14 @@
21 * 21 *
22 */ 22 */
23 23
24#include <stdio.h> 24/* BB_AUDIT SUSv3 defects - unsupported options -h, -H, -L, and -P. */
25/* BB_AUDIT GNU defects - unsupported options -h, -c, -f, -v, and long options. */
26/* BB_AUDIT Note: gnu chown does not support -H, -L, or -P. */
27/* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
28
25#include <stdlib.h> 29#include <stdlib.h>
26#include <string.h>
27#include <unistd.h> 30#include <unistd.h>
31#include <string.h>
28#include "busybox.h" 32#include "busybox.h"
29 33
30/* Don't use lchown for libc5 or glibc older then 2.1.x */ 34/* Don't use lchown for libc5 or glibc older then 2.1.x */
@@ -42,65 +46,67 @@ static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
42 if (chown_func(fileName, uid, (gid == -1) ? statbuf->st_gid : gid) == 0) { 46 if (chown_func(fileName, uid, (gid == -1) ? statbuf->st_gid : gid) == 0) {
43 return (TRUE); 47 return (TRUE);
44 } 48 }
45 perror(fileName); 49 bb_perror_msg("%s", fileName); /* Avoid multibyte problems. */
46 return (FALSE); 50 return (FALSE);
47} 51}
48 52
53#define FLAG_R 1
54#define FLAG_h 2
55
56static unsigned long get_ug_id(const char *s, long (*my_getxxnam)(const char *))
57{
58 unsigned long r;
59 char *p;
60
61 r = strtoul(s, &p, 10);
62 if (*p || (s == p)) {
63 r = my_getxxnam(s);
64 }
65
66 return r;
67}
68
49int chown_main(int argc, char **argv) 69int chown_main(int argc, char **argv)
50{ 70{
51 int opt; 71 int flags;
52 int recursiveFlag = FALSE, 72 int retval = EXIT_SUCCESS;
53 noderefFlag = FALSE; 73 char *groupName;
54 char *groupName=NULL; 74
55 char *p=NULL; 75 flags = bb_getopt_ulflags(argc, argv, "Rh");
56 76
57 /* do normal option parsing */ 77 if (flags & FLAG_h) chown_func = lchown;
58 while ((opt = getopt(argc, argv, "Rh")) > 0) { 78
59 switch (opt) { 79 if (argc - optind < 2) {
60 case 'R': 80 bb_show_usage();
61 recursiveFlag = TRUE;
62 break;
63 case 'h':
64 noderefFlag = TRUE;
65 break;
66 default:
67 show_usage();
68 }
69 } 81 }
70 82
71 if (noderefFlag) chown_func = lchown; 83 argv += optind;
72 84
73 if (argc > optind && argc > 2 && argv[optind]) { 85 /* First, check if there is a group name here */
74 /* First, check if there is a group name here */ 86 if ((groupName = strchr(*argv, '.')) == NULL) {
75 groupName = strchr(argv[optind], '.'); 87 groupName = strchr(*argv, ':');
76 if (groupName == NULL) 88 }
77 groupName = strchr(argv[optind], ':'); 89
78 if (groupName) { 90 gid = -1;
79 *groupName++ = '\0'; 91 if (groupName) {
80 gid = strtoul(groupName, &p, 10); 92 *groupName++ = '\0';
81 if (groupName == p) 93 gid = get_ug_id(groupName, my_getgrnam);
82 gid = my_getgrnam(groupName);
83 } else {
84 gid = -1;
85 }
86 /* Now check for the username */
87 uid = strtoul(argv[optind], &p, 10); /* Is is numeric? */
88 if (argv[optind] == p) {
89 uid = my_getpwnam(argv[optind]);
90 }
91 } else {
92 error_msg_and_die(too_few_args);
93 } 94 }
94 95
96 /* Now check for the username */
97 uid = get_ug_id(*argv, my_getpwnam);
98
99 ++argv;
100
95 /* Ok, ready to do the deed now */ 101 /* Ok, ready to do the deed now */
96 while (++optind < argc) { 102 do {
97 if (! recursive_action (argv[optind], recursiveFlag, FALSE, FALSE, 103 if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE,
98 fileAction, fileAction, NULL)) { 104 fileAction, fileAction, NULL)) {
99 return EXIT_FAILURE; 105 retval = EXIT_FAILURE;
100 } 106 }
101 } 107 } while (*++argv);
102 return EXIT_SUCCESS;
103 108
109 return retval;
104} 110}
105 111
106/* 112/*
diff --git a/coreutils/chroot.c b/coreutils/chroot.c
index ba3e5f864..01e4d564c 100644
--- a/coreutils/chroot.c
+++ b/coreutils/chroot.c
@@ -21,6 +21,8 @@
21 * 21 *
22 */ 22 */
23 23
24/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
25
24#include <stdlib.h> 26#include <stdlib.h>
25#include <stdio.h> 27#include <stdio.h>
26#include <unistd.h> 28#include <unistd.h>
@@ -29,46 +31,24 @@
29 31
30int chroot_main(int argc, char **argv) 32int chroot_main(int argc, char **argv)
31{ 33{
32 char *prog; 34 if (argc < 2) {
33 35 bb_show_usage();
34 if ((argc < 2) || (**(argv + 1) == '-')) {
35 show_usage();
36 } 36 }
37 argc--;
38 argv++;
39 37
38 ++argv;
40 if (chroot(*argv) || (chdir("/"))) { 39 if (chroot(*argv) || (chdir("/"))) {
41 perror_msg_and_die("cannot change root directory to %s", *argv); 40 bb_perror_msg_and_die("cannot change root directory to %s", *argv);
42 } 41 }
43 42
44 argc--; 43 ++argv;
45 argv++; 44 if (argc == 2) {
46 if (argc >= 1) { 45 argv -= 2;
47 prog = *argv; 46 if (!(*argv = getenv("SHELL"))) {
48 execvp(*argv, argv); 47 *argv = (char *) "/bin/sh";
49 } else { 48 }
50#if defined shell_main && defined CONFIG_FEATURE_SH_STANDALONE_SHELL 49 argv[1] = (char *) "-i";
51 char shell[] = "/bin/sh";
52 char *shell_argv[2] = { shell, NULL };
53 applet_name = shell;
54 shell_main(1, shell_argv);
55 return EXIT_SUCCESS;
56#else
57 prog = getenv("SHELL");
58 if (!prog)
59 prog = "/bin/sh";
60 execlp(prog, prog, NULL);
61#endif
62 } 50 }
63 perror_msg_and_die("cannot execute %s", prog);
64 51
52 execvp(*argv, argv);
53 bb_perror_msg_and_die("cannot execute %s", *argv);
65} 54}
66
67
68/*
69Local Variables:
70c-file-style: "linux"
71c-basic-offset: 4
72tab-width: 4
73End:
74*/
diff --git a/coreutils/cmp.c b/coreutils/cmp.c
index 07bf3be92..43dbc842f 100644
--- a/coreutils/cmp.c
+++ b/coreutils/cmp.c
@@ -20,59 +20,133 @@
20 * 20 *
21 */ 21 */
22 22
23/* BB_AUDIT SUSv3 (virtually) compliant -- uses nicer GNU format for -l. */
24/* http://www.opengroup.org/onlinepubs/007904975/utilities/cmp.html */
25
26/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
27 *
28 * Original version majorly reworked for SUSv3 compliance, bug fixes, and
29 * size optimizations. Changes include:
30 * 1) Now correctly distingusishes between errors and actual file differences.
31 * 2) Proper handling of '-' args.
32 * 3) Actual error checking of i/o.
33 * 4) Accept SUSv3 -l option. Note that we use the slightly nicer gnu format
34 * in the '-l' case.
35 */
36
23#include <stdio.h> 37#include <stdio.h>
24#include <string.h>
25#include <errno.h>
26#include <stdlib.h> 38#include <stdlib.h>
27#include <getopt.h> 39#include <unistd.h>
28#include "busybox.h" 40#include "busybox.h"
29 41
42static FILE *cmp_xfopen_input(const char *filename)
43{
44 FILE *fp;
45
46 if ((fp = bb_wfopen_input(filename)) != NULL) {
47 return fp;
48 }
49
50 exit(bb_default_error_retval); /* We already output an error message. */
51}
52
53static const char fmt_eof[] = "cmp: EOF on %s\n";
54static const char fmt_differ[] = "%s %s differ: char %d, line %d\n";
55#if 0
56static const char fmt_l_opt[] = "%.0s%.0s%d %o %o\n"; /* SUSv3 format */
57#else
58static const char fmt_l_opt[] = "%.0s%.0s%d %3o %3o\n"; /* nicer gnu format */
59#endif
60
61static const char opt_chars[] = "sl";
62
63enum {
64 OPT_s = 1,
65 OPT_l = 2
66};
67
30int cmp_main(int argc, char **argv) 68int cmp_main(int argc, char **argv)
31{ 69{
32 FILE *fp1 = NULL, *fp2 = stdin; 70 FILE *fp1, *fp2, *outfile = stdout;
33 char *filename1, *filename2 = "-"; 71 const char *filename1, *filename2;
34 int c, c1, c2, char_pos = 1, line_pos = 1, silent = FALSE; 72 const char *fmt;
35 73 int c1, c2, char_pos, line_pos;
36 while ((c = getopt(argc, argv, "s")) != EOF) { 74 int opt_flags;
37 switch (c) { 75 int exit_val = 0;
38 case 's': 76
39 silent = TRUE; 77 bb_default_error_retval = 2; /* 1 is returned if files are different. */
40 break; 78
41 default: 79 opt_flags = bb_getopt_ulflags(argc, argv, opt_chars);
42 show_usage(); 80
43 } 81 if ((opt_flags == 3) || (((unsigned int)(--argc - optind)) > 1)) {
82 bb_show_usage();
44 } 83 }
45 84
46 filename1 = argv[optind]; 85 fp1 = cmp_xfopen_input(filename1 = *(argv += optind));
47 switch (argc - optind) { 86
48 case 2: 87 filename2 = "-";
49 fp2 = xfopen(filename2 = argv[optind + 1], "r"); 88 if (*++argv) {
50 case 1: 89 filename2 = *argv;
51 fp1 = xfopen(filename1, "r");
52 break;
53 default:
54 show_usage();
55 } 90 }
91 fp2 = cmp_xfopen_input(filename2);
56 92
93 if (fp1 == fp2) { /* Paranioa check... stdin == stdin? */
94 /* Note that we don't bother reading stdin. Neither does gnu wc.
95 * But perhaps we should, so that other apps down the chain don't
96 * get the input. Consider 'echo hello | (cmp - - && cat -)'.
97 */
98 return 0;
99 }
100
101 fmt = fmt_differ;
102 if (opt_flags == OPT_l) {
103 fmt = fmt_l_opt;
104 }
105
106 char_pos = 0;
107 line_pos = 1;
57 do { 108 do {
58 c1 = fgetc(fp1); 109 c1 = getc(fp1);
59 c2 = fgetc(fp2); 110 c2 = getc(fp2);
60 if (c1 != c2) { 111 ++char_pos;
61 if (silent) 112 if (c1 != c2) { /* Remember -- a read error may have occurred. */
62 return EXIT_FAILURE; 113 exit_val = 1; /* But assume the files are different for now. */
63 if (c1 == EOF) 114 if (c2 == EOF) {
64 printf("EOF on %s\n", filename1); 115 /* We know that fp1 isn't at EOF or in an error state. But to
65 else if (c2 == EOF) 116 * save space below, things are setup to expect an EOF in fp1
66 printf("EOF on %s\n", filename2); 117 * if an EOF occurred. So, swap things around.
67 else 118 */
68 printf("%s %s differ: char %d, line %d\n", filename1, filename2, 119 fp1 = fp2;
69 char_pos, line_pos); 120 filename1 = filename2;
70 return EXIT_FAILURE; 121 c1 = c2;
122 }
123 if (c1 == EOF) {
124 bb_xferror(fp1, filename1);
125 fmt = fmt_eof; /* Well, no error, so it must really be EOF. */
126 outfile = stderr;
127 /* There may have been output to stdout (option -l), so
128 * make sure we fflush before writing to stderr. */
129 bb_xfflush_stdout();
130 }
131 if (opt_flags != OPT_s) {
132 if (opt_flags == OPT_l) {
133 line_pos = c1; /* line_pos is unused in the -l case. */
134 }
135 bb_fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2);
136 if (opt_flags) { /* This must be -l since not -s. */
137 /* If we encountered and EOF, the while check will catch it. */
138 continue;
139 }
140 }
141 break;
142 }
143 if (c1 == '\n') {
144 ++line_pos;
71 } 145 }
72 char_pos++;
73 if (c1 == '\n')
74 line_pos++;
75 } while (c1 != EOF); 146 } while (c1 != EOF);
76 147
77 return EXIT_SUCCESS; 148 bb_xferror(fp1, filename1);
149 bb_xferror(fp2, filename2);
150
151 bb_fflush_stdout_and_exit(exit_val);
78} 152}
diff --git a/coreutils/cp.c b/coreutils/cp.c
index 8f8fe5ed3..c5dd31ec3 100644
--- a/coreutils/cp.c
+++ b/coreutils/cp.c
@@ -2,7 +2,6 @@
2/* 2/*
3 * Mini cp implementation for busybox 3 * Mini cp implementation for busybox
4 * 4 *
5 *
6 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> 5 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
@@ -21,6 +20,15 @@
21 * 20 *
22 */ 21 */
23 22
23/* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */
24/* BB_AUDIT GNU defects - only extension options supported are -a and -d. */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
26
27/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
28 *
29 * Size reduction.
30 */
31
24#include <sys/types.h> 32#include <sys/types.h>
25#include <sys/stat.h> 33#include <sys/stat.h>
26#include <unistd.h> 34#include <unistd.h>
@@ -29,86 +37,76 @@
29#include <errno.h> 37#include <errno.h>
30#include <dirent.h> 38#include <dirent.h>
31#include <stdlib.h> 39#include <stdlib.h>
32 40#include <assert.h>
33#include "busybox.h" 41#include "busybox.h"
42#include "libcoreutils/coreutils.h"
43
44static const char cp_opts[] = "pdRfia"; /* WARNING!! ORDER IS IMPORTANT!! */
34 45
35extern int cp_main(int argc, char **argv) 46extern int cp_main(int argc, char **argv)
36{ 47{
48 struct stat source_stat;
49 struct stat dest_stat;
50 const char *last;
51 const char *dest;
52 int s_flags;
53 int d_flags;
54 int flags;
37 int status = 0; 55 int status = 0;
38 int opt;
39 int flags = FILEUTILS_DEREFERENCE;
40 int i;
41 56
42 while ((opt = getopt(argc, argv, "adfipR")) != -1) 57 /* Since these are enums, #if tests will not work. So use assert()s. */
43 switch (opt) { 58 assert(FILEUTILS_PRESERVE_STATUS == 1);
44 case 'a': 59 assert(FILEUTILS_DEREFERENCE == 2);
45 flags |= FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR; 60 assert(FILEUTILS_RECUR == 4);
46 /* fallthrough */ 61 assert(FILEUTILS_FORCE == 8);
47 case 'd': 62 assert(FILEUTILS_INTERACTIVE == 16);
48 flags &= ~FILEUTILS_DEREFERENCE;
49 break;
50 case 'f':
51 flags |= FILEUTILS_FORCE;
52 break;
53 case 'i':
54 flags |= FILEUTILS_INTERACTIVE;
55 break;
56 case 'p':
57 flags |= FILEUTILS_PRESERVE_STATUS;
58 break;
59 case 'R':
60 flags |= FILEUTILS_RECUR;
61 break;
62 default:
63 show_usage();
64 }
65
66 if (optind + 2 > argc)
67 show_usage();
68 63
69 /* If there are only two arguments and... */ 64 flags = bb_getopt_ulflags(argc, argv, cp_opts);
70 if (optind + 2 == argc) {
71 struct stat source_stat;
72 struct stat dest_stat;
73 int source_exists = 1;
74 int dest_exists = 1;
75 65
76 if ((!(flags & FILEUTILS_DEREFERENCE) && 66 if (flags & 32) {
77 lstat(argv[optind], &source_stat) < 0) || 67 flags |= (FILEUTILS_PRESERVE_STATUS | FILEUTILS_RECUR | FILEUTILS_DEREFERENCE);
78 ((flags & FILEUTILS_DEREFERENCE) && 68 }
79 stat(argv[optind], &source_stat))) { 69
80 if (errno != ENOENT) 70 flags ^= FILEUTILS_DEREFERENCE; /* The sense of this flag was reversed. */
81 perror_msg_and_die("unable to stat `%s'", argv[optind]); 71
82 source_exists = 0; 72 if (optind + 2 > argc) {
83 } 73 bb_show_usage();
74 }
75
76 last = argv[argc - 1];
77 argv += optind;
84 78
85 if (stat(argv[optind + 1], &dest_stat) < 0) { 79 /* If there are only two arguments and... */
86 if (errno != ENOENT) 80 if (optind + 2 == argc) {
87 perror_msg_and_die("unable to stat `%s'", argv[optind + 1]); 81 s_flags = cp_mv_stat2(*argv, &source_stat,
88 dest_exists = 0; 82 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
83 if ((s_flags < 0) || ((d_flags = cp_mv_stat(last, &dest_stat)) < 0)) {
84 exit(EXIT_FAILURE);
89 } 85 }
90
91 /* ...if neither is a directory or... */ 86 /* ...if neither is a directory or... */
92 if (((!source_exists || !S_ISDIR(source_stat.st_mode)) && 87 if ( !((s_flags | d_flags) & 2) ||
93 (!dest_exists || !S_ISDIR(dest_stat.st_mode))) || 88 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
94 /* ...recursing, the first is a directory, and the 89 /* ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) */
95 * second doesn't exist, then... */ 90 /* Simplify the above since FILEUTILS_RECUR >> 1 == 2. */
96 ((flags & FILEUTILS_RECUR) && S_ISDIR(source_stat.st_mode) && 91 ((((flags & FILEUTILS_RECUR) >> 1) & s_flags) && !d_flags)
97 !dest_exists)) { 92 ) {
98 /* ...do a simple copy. */ 93 /* ...do a simple copy. */
99 if (copy_file(argv[optind], argv[optind + 1], flags) < 0) 94 dest = last;
100 status = 1; 95 goto DO_COPY; /* Note: optind+2==argc implies argv[1]==last below. */
101 return status;
102 } 96 }
103 } 97 }
104 98
105 for (i = optind; i < argc - 1; i++) { 99 do {
106 char *dest = concat_path_file(argv[argc - 1], 100 dest = concat_path_file(last, bb_get_last_path_component(*argv));
107 get_last_path_component(argv[i])); 101 DO_COPY:
108 if (copy_file(argv[i], dest, flags) < 0) 102 if (copy_file(*argv, dest, flags) < 0) {
109 status = 1; 103 status = 1;
110 free(dest); 104 }
111 } 105 if (*++argv == last) {
106 break;
107 }
108 free((void *) dest);
109 } while (1);
112 110
113 return status; 111 exit(status);
114} 112}
diff --git a/coreutils/cut.c b/coreutils/cut.c
index 3ed264870..c24cf6611 100644
--- a/coreutils/cut.c
+++ b/coreutils/cut.c
@@ -92,7 +92,7 @@ static void parse_lists(char *lists)
92 } else { 92 } else {
93 s = strtoul(ntok, &junk, 10); 93 s = strtoul(ntok, &junk, 10);
94 if(*junk != '\0' || s < 0) 94 if(*junk != '\0' || s < 0)
95 error_msg_and_die("invalid byte or field list"); 95 bb_error_msg_and_die("invalid byte or field list");
96 96
97 /* account for the fact that arrays are zero based, while the user 97 /* account for the fact that arrays are zero based, while the user
98 * expects the first char on the line to be char # 1 */ 98 * expects the first char on the line to be char # 1 */
@@ -109,7 +109,7 @@ static void parse_lists(char *lists)
109 } else { 109 } else {
110 e = strtoul(ntok, &junk, 10); 110 e = strtoul(ntok, &junk, 10);
111 if(*junk != '\0' || e < 0) 111 if(*junk != '\0' || e < 0)
112 error_msg_and_die("invalid byte or field list"); 112 bb_error_msg_and_die("invalid byte or field list");
113 /* if the user specified and end position of 0, that means "til the 113 /* if the user specified and end position of 0, that means "til the
114 * end of the line */ 114 * end of the line */
115 if (e == 0) 115 if (e == 0)
@@ -121,7 +121,7 @@ static void parse_lists(char *lists)
121 121
122 /* if there's something left to tokenize, the user past an invalid list */ 122 /* if there's something left to tokenize, the user past an invalid list */
123 if (ltok) 123 if (ltok)
124 error_msg_and_die("invalid byte or field list"); 124 bb_error_msg_and_die("invalid byte or field list");
125 125
126 /* add the new list */ 126 /* add the new list */
127 cut_lists = xrealloc(cut_lists, sizeof(struct cut_list) * (++nlists)); 127 cut_lists = xrealloc(cut_lists, sizeof(struct cut_list) * (++nlists));
@@ -131,7 +131,7 @@ static void parse_lists(char *lists)
131 131
132 /* make sure we got some cut positions out of all that */ 132 /* make sure we got some cut positions out of all that */
133 if (nlists == 0) 133 if (nlists == 0)
134 error_msg_and_die("missing list of positions"); 134 bb_error_msg_and_die("missing list of positions");
135 135
136 /* now that the lists are parsed, we need to sort them to make life easier 136 /* now that the lists are parsed, we need to sort them to make life easier
137 * on us when it comes time to print the chars / fields / lines */ 137 * on us when it comes time to print the chars / fields / lines */
@@ -267,8 +267,7 @@ static void cut_file(FILE *file)
267 unsigned int linenum = 0; /* keep these zero-based to be consistent */ 267 unsigned int linenum = 0; /* keep these zero-based to be consistent */
268 268
269 /* go through every line in the file */ 269 /* go through every line in the file */
270 while ((line = get_line_from_file(file)) != NULL) { 270 while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
271 chomp(line);
272 271
273 /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */ 272 /* cut based on chars/bytes XXX: only works when sizeof(char) == byte */
274 if (part == 'c' || part == 'b') 273 if (part == 'c' || part == 'b')
@@ -299,14 +298,14 @@ extern int cut_main(int argc, char **argv)
299 case 'f': 298 case 'f':
300 /* make sure they didn't ask for two types of lists */ 299 /* make sure they didn't ask for two types of lists */
301 if (part != 0) { 300 if (part != 0) {
302 error_msg_and_die("only one type of list may be specified"); 301 bb_error_msg_and_die("only one type of list may be specified");
303 } 302 }
304 part = (char)opt; 303 part = (char)opt;
305 parse_lists(optarg); 304 parse_lists(optarg);
306 break; 305 break;
307 case 'd': 306 case 'd':
308 if (strlen(optarg) > 1) { 307 if (strlen(optarg) > 1) {
309 error_msg_and_die("the delimiter must be a single character"); 308 bb_error_msg_and_die("the delimiter must be a single character");
310 } 309 }
311 delim = optarg[0]; 310 delim = optarg[0];
312 break; 311 break;
@@ -320,17 +319,17 @@ extern int cut_main(int argc, char **argv)
320 } 319 }
321 320
322 if (part == 0) { 321 if (part == 0) {
323 error_msg_and_die("you must specify a list of bytes, characters, or fields"); 322 bb_error_msg_and_die("you must specify a list of bytes, characters, or fields");
324 } 323 }
325 324
326 /* non-field (char or byte) cutting has some special handling */ 325 /* non-field (char or byte) cutting has some special handling */
327 if (part != 'f') { 326 if (part != 'f') {
328 if (supress_non_delimited_lines) { 327 if (supress_non_delimited_lines) {
329 error_msg_and_die("suppressing non-delimited lines makes sense" 328 bb_error_msg_and_die("suppressing non-delimited lines makes sense"
330 " only when operating on fields"); 329 " only when operating on fields");
331 } 330 }
332 if (delim != '\t' && part != 'f') { 331 if (delim != '\t' && part != 'f') {
333 error_msg_and_die("a delimiter may be specified only when operating on fields"); 332 bb_error_msg_and_die("a delimiter may be specified only when operating on fields");
334 } 333 }
335 } 334 }
336 335
@@ -344,7 +343,7 @@ extern int cut_main(int argc, char **argv)
344 int i; 343 int i;
345 FILE *file; 344 FILE *file;
346 for (i = optind; i < argc; i++) { 345 for (i = optind; i < argc; i++) {
347 file = wfopen(argv[i], "r"); 346 file = bb_wfopen(argv[i], "r");
348 if(file) { 347 if(file) {
349 cut_file(file); 348 cut_file(file);
350 fclose(file); 349 fclose(file);
diff --git a/coreutils/date.c b/coreutils/date.c
index d68c04a8d..afbedb90d 100644
--- a/coreutils/date.c
+++ b/coreutils/date.c
@@ -53,7 +53,7 @@ static struct tm *date_conv_time(struct tm *tm_time, const char *t_string)
53 &(tm_time->tm_year)); 53 &(tm_time->tm_year));
54 54
55 if (nr < 4 || nr > 5) { 55 if (nr < 4 || nr > 5) {
56 error_msg_and_die(invalid_date, t_string); 56 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
57 } 57 }
58 58
59 /* correct for century - minor Y2K problem here? */ 59 /* correct for century - minor Y2K problem here? */
@@ -108,7 +108,7 @@ static struct tm *date_conv_ftime(struct tm *tm_time, const char *t_string)
108 t.tm_year -= 1900; /* Adjust years */ 108 t.tm_year -= 1900; /* Adjust years */
109 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */ 109 t.tm_mon -= 1; /* Adjust dates from 1-12 to 0-11 */
110 } else { 110 } else {
111 error_msg_and_die(invalid_date, t_string); 111 bb_error_msg_and_die(bb_msg_invalid_date, t_string);
112 } 112 }
113 *tm_time = t; 113 *tm_time = t;
114 return (tm_time); 114 return (tm_time);
@@ -145,25 +145,25 @@ int date_main(int argc, char **argv)
145 case 's': 145 case 's':
146 set_time = 1; 146 set_time = 1;
147 if ((date_str != NULL) || ((date_str = optarg) == NULL)) { 147 if ((date_str != NULL) || ((date_str = optarg) == NULL)) {
148 show_usage(); 148 bb_show_usage();
149 } 149 }
150 break; 150 break;
151 case 'u': 151 case 'u':
152 utc = 1; 152 utc = 1;
153 if (putenv("TZ=UTC0") != 0) 153 if (putenv("TZ=UTC0") != 0)
154 error_msg_and_die(memory_exhausted); 154 bb_error_msg_and_die(bb_msg_memory_exhausted);
155 break; 155 break;
156 case 'd': 156 case 'd':
157 use_arg = 1; 157 use_arg = 1;
158 if ((date_str != NULL) || ((date_str = optarg) == NULL)) 158 if ((date_str != NULL) || ((date_str = optarg) == NULL))
159 show_usage(); 159 bb_show_usage();
160 break; 160 break;
161#ifdef CONFIG_FEATURE_DATE_ISOFMT 161#ifdef CONFIG_FEATURE_DATE_ISOFMT
162 case 'I': 162 case 'I':
163 if (!optarg) 163 if (!optarg)
164 ifmt = 1; 164 ifmt = 1;
165 else { 165 else {
166 int ifmt_len = xstrlen(optarg); 166 int ifmt_len = bb_strlen(optarg);
167 167
168 if ((ifmt_len <= 4) 168 if ((ifmt_len <= 4)
169 && (strncmp(optarg, "date", ifmt_len) == 0)) { 169 && (strncmp(optarg, "date", ifmt_len) == 0)) {
@@ -180,11 +180,11 @@ int date_main(int argc, char **argv)
180 } 180 }
181 } 181 }
182 if (ifmt) { 182 if (ifmt) {
183 break; /* else show_usage(); */ 183 break; /* else bb_show_usage(); */
184 } 184 }
185#endif 185#endif
186 default: 186 default:
187 show_usage(); 187 bb_show_usage();
188 } 188 }
189 } 189 }
190 190
@@ -220,15 +220,15 @@ int date_main(int argc, char **argv)
220 /* Correct any day of week and day of year etc. fields */ 220 /* Correct any day of week and day of year etc. fields */
221 tm = mktime(&tm_time); 221 tm = mktime(&tm_time);
222 if (tm < 0) { 222 if (tm < 0) {
223 error_msg_and_die(invalid_date, date_str); 223 bb_error_msg_and_die(bb_msg_invalid_date, date_str);
224 } 224 }
225 if (utc && (putenv("TZ=UTC0") != 0)) { 225 if (utc && (putenv("TZ=UTC0") != 0)) {
226 error_msg_and_die(memory_exhausted); 226 bb_error_msg_and_die(bb_msg_memory_exhausted);
227 } 227 }
228 228
229 /* if setting time, set it */ 229 /* if setting time, set it */
230 if (set_time && (stime(&tm) < 0)) { 230 if (set_time && (stime(&tm) < 0)) {
231 perror_msg("cannot set date"); 231 bb_perror_msg("cannot set date");
232 } 232 }
233 } 233 }
234 234
diff --git a/coreutils/dd.c b/coreutils/dd.c
index 8c7272b5c..11508614f 100644
--- a/coreutils/dd.c
+++ b/coreutils/dd.c
@@ -62,19 +62,19 @@ int dd_main(int argc, char **argv)
62 int ifd; 62 int ifd;
63 int ofd; 63 int ofd;
64 int i; 64 int i;
65 char *infile = NULL; 65 const char *infile = NULL;
66 char *outfile = NULL; 66 const char *outfile = NULL;
67 char *buf; 67 char *buf;
68 68
69 for (i = 1; i < argc; i++) { 69 for (i = 1; i < argc; i++) {
70 if (strncmp("bs=", argv[i], 3) == 0) 70 if (strncmp("bs=", argv[i], 3) == 0)
71 bs = parse_number(argv[i]+3, dd_suffixes); 71 bs = bb_xparse_number(argv[i]+3, dd_suffixes);
72 else if (strncmp("count=", argv[i], 6) == 0) 72 else if (strncmp("count=", argv[i], 6) == 0)
73 count = parse_number(argv[i]+6, dd_suffixes); 73 count = bb_xparse_number(argv[i]+6, dd_suffixes);
74 else if (strncmp("seek=", argv[i], 5) == 0) 74 else if (strncmp("seek=", argv[i], 5) == 0)
75 seek = parse_number(argv[i]+5, dd_suffixes); 75 seek = bb_xparse_number(argv[i]+5, dd_suffixes);
76 else if (strncmp("skip=", argv[i], 5) == 0) 76 else if (strncmp("skip=", argv[i], 5) == 0)
77 skip = parse_number(argv[i]+5, dd_suffixes); 77 skip = bb_xparse_number(argv[i]+5, dd_suffixes);
78 else if (strncmp("if=", argv[i], 3) == 0) 78 else if (strncmp("if=", argv[i], 3) == 0)
79 infile = argv[i]+3; 79 infile = argv[i]+3;
80 else if (strncmp("of=", argv[i], 3) == 0) 80 else if (strncmp("of=", argv[i], 3) == 0)
@@ -92,7 +92,7 @@ int dd_main(int argc, char **argv)
92 noerror = TRUE; 92 noerror = TRUE;
93 buf += 7; 93 buf += 7;
94 } else { 94 } else {
95 error_msg_and_die("invalid conversion `%s'", argv[i]+5); 95 bb_error_msg_and_die("invalid conversion `%s'", argv[i]+5);
96 } 96 }
97 if (buf[0] == '\0') 97 if (buf[0] == '\0')
98 break; 98 break;
@@ -100,18 +100,18 @@ int dd_main(int argc, char **argv)
100 buf++; 100 buf++;
101 } 101 }
102 } else 102 } else
103 show_usage(); 103 bb_show_usage();
104 } 104 }
105 105
106 buf = xmalloc(bs); 106 buf = xmalloc(bs);
107 107
108 if (infile != NULL) { 108 if (infile != NULL) {
109 if ((ifd = open(infile, O_RDONLY)) < 0) { 109 if ((ifd = open(infile, O_RDONLY)) < 0) {
110 perror_msg_and_die("%s", infile); 110 bb_perror_msg_and_die("%s", infile);
111 } 111 }
112 } else { 112 } else {
113 ifd = STDIN_FILENO; 113 ifd = STDIN_FILENO;
114 infile = "standard input"; 114 infile = bb_msg_standard_input;
115 } 115 }
116 116
117 if (outfile != NULL) { 117 if (outfile != NULL) {
@@ -122,7 +122,7 @@ int dd_main(int argc, char **argv)
122 } 122 }
123 123
124 if ((ofd = open(outfile, oflag, 0666)) < 0) { 124 if ((ofd = open(outfile, oflag, 0666)) < 0) {
125 perror_msg_and_die("%s", outfile); 125 bb_perror_msg_and_die("%s", outfile);
126 } 126 }
127 127
128 if (seek && trunc) { 128 if (seek && trunc) {
@@ -131,24 +131,24 @@ int dd_main(int argc, char **argv)
131 131
132 if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) || 132 if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) ||
133 S_ISDIR (st.st_mode)) { 133 S_ISDIR (st.st_mode)) {
134 perror_msg_and_die("%s", outfile); 134 bb_perror_msg_and_die("%s", outfile);
135 } 135 }
136 } 136 }
137 } 137 }
138 } else { 138 } else {
139 ofd = STDOUT_FILENO; 139 ofd = STDOUT_FILENO;
140 outfile = "standard output"; 140 outfile = bb_msg_standard_output;
141 } 141 }
142 142
143 if (skip) { 143 if (skip) {
144 if (lseek(ifd, skip * bs, SEEK_CUR) < 0) { 144 if (lseek(ifd, skip * bs, SEEK_CUR) < 0) {
145 perror_msg_and_die("%s", infile); 145 bb_perror_msg_and_die("%s", infile);
146 } 146 }
147 } 147 }
148 148
149 if (seek) { 149 if (seek) {
150 if (lseek(ofd, seek * bs, SEEK_CUR) < 0) { 150 if (lseek(ofd, seek * bs, SEEK_CUR) < 0) {
151 perror_msg_and_die("%s", outfile); 151 bb_perror_msg_and_die("%s", outfile);
152 } 152 }
153 } 153 }
154 154
@@ -161,9 +161,9 @@ int dd_main(int argc, char **argv)
161 if (n < 0) { 161 if (n < 0) {
162 if (noerror) { 162 if (noerror) {
163 n = bs; 163 n = bs;
164 perror_msg("%s", infile); 164 bb_perror_msg("%s", infile);
165 } else { 165 } else {
166 perror_msg_and_die("%s", infile); 166 bb_perror_msg_and_die("%s", infile);
167 } 167 }
168 } 168 }
169 if (n == 0) { 169 if (n == 0) {
@@ -178,9 +178,9 @@ int dd_main(int argc, char **argv)
178 memset(buf + n, '\0', bs - n); 178 memset(buf + n, '\0', bs - n);
179 n = bs; 179 n = bs;
180 } 180 }
181 n = full_write(ofd, buf, n); 181 n = bb_full_write(ofd, buf, n);
182 if (n < 0) { 182 if (n < 0) {
183 perror_msg_and_die("%s", outfile); 183 bb_perror_msg_and_die("%s", outfile);
184 } 184 }
185 if (n == bs) { 185 if (n == bs) {
186 out_full++; 186 out_full++;
@@ -190,15 +190,16 @@ int dd_main(int argc, char **argv)
190 } 190 }
191 191
192 if (close (ifd) < 0) { 192 if (close (ifd) < 0) {
193 perror_msg_and_die("%s", infile); 193 bb_perror_msg_and_die("%s", infile);
194 } 194 }
195 195
196 if (close (ofd) < 0) { 196 if (close (ofd) < 0) {
197 perror_msg_and_die("%s", outfile); 197 bb_perror_msg_and_die("%s", outfile);
198 } 198 }
199 199
200 fprintf(stderr, "%ld+%ld records in\n", (long)in_full, (long)in_part); 200 fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n",
201 fprintf(stderr, "%ld+%ld records out\n", (long)out_full, (long)out_part); 201 (long)in_full, (long)in_part,
202 (long)out_full, (long)out_part);
202 203
203 return EXIT_SUCCESS; 204 return EXIT_SUCCESS;
204} 205}
diff --git a/coreutils/df.c b/coreutils/df.c
index 0e9e5d61a..7d007a003 100644
--- a/coreutils/df.c
+++ b/coreutils/df.c
@@ -22,74 +22,45 @@
22 * 22 *
23 */ 23 */
24 24
25/* BB_AUDIT SUSv3 _NOT_ compliant -- options -P and -t missing. Also blocksize. */
26/* http://www.opengroup.org/onlinepubs/007904975/utilities/df.html */
27
28/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
29 *
30 * Size reduction. Removed floating point dependency. Added error checking
31 * on output. Output stats on 0-sized filesystems if specificly listed on
32 * the command line. Properly round *-blocks, Used, and Available quantities.
33 */
34
25#include <stdio.h> 35#include <stdio.h>
26#include <stdlib.h> 36#include <stdlib.h>
27#include <string.h> 37#include <string.h>
38#include <unistd.h>
28#include <mntent.h> 39#include <mntent.h>
29#include <sys/vfs.h> 40#include <sys/vfs.h>
30#include <getopt.h>
31#include "busybox.h" 41#include "busybox.h"
32 42
33extern const char mtab_file[]; /* Defined in utility.c */ 43#ifndef CONFIG_FEATURE_HUMAN_READABLE
34#ifdef CONFIG_FEATURE_HUMAN_READABLE 44static long kscale(long b, long bs)
35static unsigned long df_disp_hr = KILOBYTE; 45{
46 return ( b * (long long) bs + KILOBYTE/2 ) / KILOBYTE;
47}
36#endif 48#endif
37 49
38static int do_df(char *device, const char *mount_point) 50extern int df_main(int argc, char **argv)
39{ 51{
40 struct statfs s;
41 long blocks_used; 52 long blocks_used;
42 long blocks_percent_used; 53 long blocks_percent_used;
43
44 if (statfs(mount_point, &s) != 0) {
45 perror_msg("%s", mount_point);
46 return FALSE;
47 }
48
49 if (s.f_blocks > 0) {
50 blocks_used = s.f_blocks - s.f_bfree;
51 if(blocks_used == 0)
52 blocks_percent_used = 0;
53 else {
54 blocks_percent_used = (long)
55 (blocks_used * 100.0 / (blocks_used + s.f_bavail) + 0.5);
56 }
57 if (strcmp(device, "/dev/root") == 0) {
58 /* Adjusts device to be the real root device,
59 * or leaves device alone if it can't find it */
60 device = find_real_root_device_name(device);
61 if(device==NULL)
62 return FALSE;
63 }
64#ifdef CONFIG_FEATURE_HUMAN_READABLE 54#ifdef CONFIG_FEATURE_HUMAN_READABLE
65 printf("%-20s %9s ", device, 55 unsigned long df_disp_hr = KILOBYTE;
66 make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
67
68 printf("%9s ",
69 make_human_readable_str( (s.f_blocks - s.f_bfree), s.f_bsize, df_disp_hr));
70
71 printf("%9s %3ld%% %s\n",
72 make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
73 blocks_percent_used, mount_point);
74#else
75 printf("%-20s %9ld %9ld %9ld %3ld%% %s\n",
76 device,
77 (long) (s.f_blocks * (s.f_bsize / (double)KILOBYTE)),
78 (long) ((s.f_blocks - s.f_bfree)*(s.f_bsize/(double)KILOBYTE)),
79 (long) (s.f_bavail * (s.f_bsize / (double)KILOBYTE)),
80 blocks_percent_used, mount_point);
81#endif 56#endif
82 }
83
84 return TRUE;
85}
86
87extern int df_main(int argc, char **argv)
88{
89 int status = EXIT_SUCCESS; 57 int status = EXIT_SUCCESS;
90 int opt = 0; 58 int opt;
91 int i = 0; 59 FILE *mount_table;
92 char disp_units_hdr[80] = "1k-blocks"; /* default display is kilobytes */ 60 struct mntent *mount_entry;
61 struct statfs s;
62 static const char hdr_1k[] = "1k-blocks"; /* default display is kilobytes */;
63 const char *disp_units_hdr = hdr_1k;
93 64
94 while ((opt = getopt(argc, argv, "k" 65 while ((opt = getopt(argc, argv, "k"
95#ifdef CONFIG_FEATURE_HUMAN_READABLE 66#ifdef CONFIG_FEATURE_HUMAN_READABLE
@@ -101,52 +72,106 @@ extern int df_main(int argc, char **argv)
101#ifdef CONFIG_FEATURE_HUMAN_READABLE 72#ifdef CONFIG_FEATURE_HUMAN_READABLE
102 case 'h': 73 case 'h':
103 df_disp_hr = 0; 74 df_disp_hr = 0;
104 strcpy(disp_units_hdr, " Size"); 75 disp_units_hdr = " Size";
105 break; 76 break;
106 case 'm': 77 case 'm':
107 df_disp_hr = MEGABYTE; 78 df_disp_hr = MEGABYTE;
108 strcpy(disp_units_hdr, "1M-blocks"); 79 disp_units_hdr = "1M-blocks";
109 break; 80 break;
110#endif 81#endif
111 case 'k': 82 case 'k':
112 /* default display is kilobytes */ 83 /* default display is kilobytes */
84#ifdef CONFIG_FEATURE_HUMAN_READABLE
85 df_disp_hr = KILOBYTE;
86 disp_units_hdr = hdr_1k;
87#endif
113 break; 88 break;
114 default: 89 default:
115 show_usage(); 90 bb_show_usage();
116 } 91 }
117 } 92 }
118 93
119 printf("%-20s %-14s %s %s %s %s\n", "Filesystem", disp_units_hdr, 94 bb_printf("Filesystem%11s%-15sUsed Available Use%% Mounted on\n",
120 "Used", "Available", "Use%", "Mounted on"); 95 "", disp_units_hdr);
121 96
122 if(optind < argc) { 97 mount_table = NULL;
123 struct mntent *mount_entry; 98 argv += optind;
124 for(i = optind; i < argc; i++) 99 if (optind >= argc) {
125 { 100 if (!(mount_table = setmntent(bb_path_mtab_file, "r"))) {
126 if ((mount_entry = find_mount_point(argv[i], mtab_file)) == 0) { 101 bb_perror_msg_and_die(bb_path_mtab_file);
127 error_msg("%s: can't find mount point.", argv[i]);
128 status = EXIT_FAILURE;
129 } else if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir))
130 status = EXIT_FAILURE;
131 } 102 }
132 } else { 103 }
133 FILE *mount_table;
134 struct mntent *mount_entry;
135 104
136 mount_table = setmntent(mtab_file, "r"); 105 do {
137 if (mount_table == 0) { 106 const char *device;
138 perror_msg("%s", mtab_file); 107 const char *mount_point;
139 return EXIT_FAILURE;
140 }
141 108
142 while ((mount_entry = getmntent(mount_table))) { 109 if (mount_table) {
143 if (!do_df(mount_entry->mnt_fsname, mount_entry->mnt_dir)) 110 if (!(mount_entry = getmntent(mount_table))) {
111 endmntent(mount_table);
112 break;
113 }
114 } else {
115 if (!(mount_point = *argv++)) {
116 break;
117 }
118 if (!(mount_entry = find_mount_point(mount_point, bb_path_mtab_file))) {
119 bb_error_msg("%s: can't find mount point.", mount_point);
120 SET_ERROR:
144 status = EXIT_FAILURE; 121 status = EXIT_FAILURE;
122 continue;
123 }
145 } 124 }
146 endmntent(mount_table);
147 }
148 125
149 return status; 126 device = mount_entry->mnt_fsname;
127 mount_point = mount_entry->mnt_dir;
128
129 if (statfs(mount_point, &s) != 0) {
130 bb_perror_msg("%s", mount_point);
131 goto SET_ERROR;
132 }
133
134 if ((s.f_blocks > 0) || !mount_table){
135 blocks_used = s.f_blocks - s.f_bfree;
136 blocks_percent_used = 0;
137 if (blocks_used + s.f_bavail) {
138 blocks_percent_used = (((long long) blocks_used) * 100
139 + (blocks_used + s.f_bavail)/2
140 ) / (blocks_used + s.f_bavail);
141 }
142
143 if (strcmp(device, "/dev/root") == 0) {
144 /* Adjusts device to be the real root device,
145 * or leaves device alone if it can't find it */
146 if ((device = find_real_root_device_name(device)) != NULL) {
147 goto SET_ERROR;
148 }
149 }
150
151#ifdef CONFIG_FEATURE_HUMAN_READABLE
152 bb_printf("%-21s%9s ", device,
153 make_human_readable_str(s.f_blocks, s.f_bsize, df_disp_hr));
154
155 bb_printf("%9s ",
156 make_human_readable_str( (s.f_blocks - s.f_bfree),
157 s.f_bsize, df_disp_hr));
158
159 bb_printf("%9s %3ld%% %s\n",
160 make_human_readable_str(s.f_bavail, s.f_bsize, df_disp_hr),
161 blocks_percent_used, mount_point);
162#else
163 bb_printf("%-21s%9ld %9ld %9ld %3ld%% %s\n",
164 device,
165 kscale(s.f_blocks, s.f_bsize),
166 kscale(s.f_blocks-s.f_bfree, s.f_bsize),
167 kscale(s.f_bavail, s.f_bsize),
168 blocks_percent_used, mount_point);
169#endif
170 }
171
172 } while (1);
173
174 bb_fflush_stdout_and_exit(status);
150} 175}
151 176
152/* 177/*
diff --git a/coreutils/dirname.c b/coreutils/dirname.c
index 387233789..4c6115e66 100644
--- a/coreutils/dirname.c
+++ b/coreutils/dirname.c
@@ -21,20 +21,20 @@
21 * 21 *
22 */ 22 */
23 23
24/* getopt not needed */ 24/* BB_AUDIT SUSv3 compliant */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/dirname.html */
25 26
26#include <stdio.h> 27#include <stdio.h>
27#include <stdlib.h> 28#include <stdlib.h>
28#include <string.h>
29#include "busybox.h" 29#include "busybox.h"
30 30
31extern int dirname_main(int argc, char **argv) 31extern int dirname_main(int argc, char **argv)
32{ 32{
33 if ((argc < 2) || (**(argv + 1) == '-')) 33 if (argc != 2) {
34 show_usage(); 34 bb_show_usage();
35 argv++; 35 }
36 36
37 puts (dirname (argv[0])); 37 puts(dirname(argv[1]));
38 38
39 return EXIT_SUCCESS; 39 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
40} 40}
diff --git a/coreutils/dos2unix.c b/coreutils/dos2unix.c
index b1d0a9d70..c28e6a8b9 100644
--- a/coreutils/dos2unix.c
+++ b/coreutils/dos2unix.c
@@ -35,6 +35,10 @@
35#include <sys/time.h> 35#include <sys/time.h>
36#include "busybox.h" 36#include "busybox.h"
37 37
38#define CT_AUTO 0
39#define CT_UNIX2DOS 1
40#define CT_DOS2UNIX 2
41
38/* We are making a lame pseudo-random string generator here. in 42/* We are making a lame pseudo-random string generator here. in
39 * convert(), each pass through the while loop will add more and more 43 * convert(), each pass through the while loop will add more and more
40 * stuff into value, which is _supposed_ to wrap. We don't care about 44 * stuff into value, which is _supposed_ to wrap. We don't care about
@@ -55,15 +59,13 @@ static int convert(char *fn, int ConvType)
55 FILE *in = stdin, *out = stdout; 59 FILE *in = stdin, *out = stdout;
56 60
57 if (fn != NULL) { 61 if (fn != NULL) {
58 if ((in = wfopen(fn, "rw")) == NULL) { 62 in = bb_xfopen(fn, "rw");
59 return -1;
60 }
61 safe_strncpy(tempFn, fn, sizeof(tempFn)); 63 safe_strncpy(tempFn, fn, sizeof(tempFn));
62 c = strlen(tempFn); 64 c = strlen(tempFn);
63 tempFn[c] = '.'; 65 tempFn[c] = '.';
64 while(1) { 66 while(1) {
65 if (c >=BUFSIZ) 67 if (c >=BUFSIZ)
66 error_msg_and_die("unique name not found"); 68 bb_error_msg_and_die("unique name not found");
67 /* Get some semi random stuff to try and make a 69 /* Get some semi random stuff to try and make a
68 * random filename based (and in the same dir as) 70 * random filename based (and in the same dir as)
69 * the input file... */ 71 * the input file... */
@@ -92,7 +94,7 @@ static int convert(char *fn, int ConvType)
92 // file is alredy in DOS format so it is not necessery to touch it 94 // file is alredy in DOS format so it is not necessery to touch it
93 remove(tempFn); 95 remove(tempFn);
94 if (fclose(in) < 0 || fclose(out) < 0) { 96 if (fclose(in) < 0 || fclose(out) < 0) {
95 perror_msg(NULL); 97 bb_perror_nomsg();
96 return -2; 98 return -2;
97 } 99 }
98 return 0; 100 return 0;
@@ -106,7 +108,7 @@ static int convert(char *fn, int ConvType)
106 // file is alredy in UNIX format so it is not necessery to touch it 108 // file is alredy in UNIX format so it is not necessery to touch it
107 remove(tempFn); 109 remove(tempFn);
108 if ((fclose(in) < 0) || (fclose(out) < 0)) { 110 if ((fclose(in) < 0) || (fclose(out) < 0)) {
109 perror_msg(NULL); 111 bb_perror_nomsg();
110 return -2; 112 return -2;
111 } 113 }
112 return 0; 114 return 0;
@@ -137,7 +139,7 @@ static int convert(char *fn, int ConvType)
137 139
138 if (fn != NULL) { 140 if (fn != NULL) {
139 if (fclose(in) < 0 || fclose(out) < 0) { 141 if (fclose(in) < 0 || fclose(out) < 0) {
140 perror_msg(NULL); 142 bb_perror_nomsg();
141 remove(tempFn); 143 remove(tempFn);
142 return -2; 144 return -2;
143 } 145 }
@@ -146,7 +148,7 @@ static int convert(char *fn, int ConvType)
146 * should be true since we put them into the same directory 148 * should be true since we put them into the same directory
147 * so we _should_ be ok, but you never know... */ 149 * so we _should_ be ok, but you never know... */
148 if (rename(tempFn, fn) < 0) { 150 if (rename(tempFn, fn) < 0) {
149 perror_msg("unable to rename '%s' as '%s'", tempFn, fn); 151 bb_perror_msg("unable to rename '%s' as '%s'", tempFn, fn);
150 return -1; 152 return -1;
151 } 153 }
152 } 154 }
@@ -177,7 +179,7 @@ int dos2unix_main(int argc, char *argv[])
177 ConvType = CT_DOS2UNIX; 179 ConvType = CT_DOS2UNIX;
178 break; 180 break;
179 default: 181 default:
180 show_usage(); 182 bb_show_usage();
181 } 183 }
182 } 184 }
183 185
diff --git a/coreutils/du.c b/coreutils/du.c
index 2e49b2147..702a9aa14 100644
--- a/coreutils/du.c
+++ b/coreutils/du.c
@@ -22,45 +22,65 @@
22 * 22 *
23 */ 23 */
24 24
25#include <sys/types.h> 25/* BB_AUDIT SUSv3 compliant (unless default blocksize set to 1k) */
26#include <fcntl.h> 26/* http://www.opengroup.org/onlinepubs/007904975/utilities/du.html */
27#include <dirent.h> 27
28#include <stdio.h> 28/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
29 *
30 * Mostly rewritten for SUSv3 compliance and to fix bugs/defects.
31 * 1) Added support for SUSv3 -a, -H, -L, gnu -c, and (busybox) -d options.
32 * The -d option allows setting of max depth (similar to gnu --max-depth).
33 * 2) Fixed incorrect size calculations for links and directories, especially
34 * when errors occurred. Calculates sizes should now match gnu du output.
35 * 3) Added error checking of output.
36 * 4) Fixed busybox bug #1284 involving long overflow with human_readable.
37 */
38
29#include <stdlib.h> 39#include <stdlib.h>
30#include <getopt.h> 40#include <limits.h>
31#include <string.h> 41#include <unistd.h>
32#include <errno.h> 42#include <dirent.h>
43#include <sys/stat.h>
33#include "busybox.h" 44#include "busybox.h"
34 45
35
36#ifdef CONFIG_FEATURE_HUMAN_READABLE 46#ifdef CONFIG_FEATURE_HUMAN_READABLE
47# ifdef CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K
37static unsigned long disp_hr = KILOBYTE; 48static unsigned long disp_hr = KILOBYTE;
49# else
50static unsigned long disp_hr = 512;
51# endif
52#elif defined CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K
53static unsigned int disp_k = 1;
54#else
55static unsigned int disp_k; /* bss inits to 0 */
38#endif 56#endif
39 57
40static int du_depth /*= 0*/; 58static int max_print_depth = INT_MAX;
41static int count_hardlinks /*= 0*/; 59static int count_hardlinks = INT_MAX;
42static int one_file_system /*= 0*/; 60
61static int status
62#if EXIT_SUCCESS == 0
63 = EXIT_SUCCESS
64#endif
65 ;
66static int print_files;
67static int slink_depth;
68static int du_depth;
69static int one_file_system;
43static dev_t dir_dev; 70static dev_t dir_dev;
44 71
45static void (*print) (long, char *);
46 72
47static void print_normal(long size, char *filename) 73static void print(long size, char *filename)
48{ 74{
75 /* TODO - May not want to defer error checking here. */
49#ifdef CONFIG_FEATURE_HUMAN_READABLE 76#ifdef CONFIG_FEATURE_HUMAN_READABLE
50 printf("%s\t%s\n", make_human_readable_str(size << 10, 1, disp_hr), 77 bb_printf("%s\t%s\n", make_human_readable_str(size, 512, disp_hr),
51 filename); 78 filename);
52#else 79#else
53 printf("%ld\t%s\n", size, filename); 80 bb_printf("%ld\t%s\n", size >> disp_k, filename);
54#endif 81#endif
55} 82}
56 83
57static void print_summary(long size, char *filename)
58{
59 if (du_depth == 1) {
60 print_normal(size, filename);
61 }
62}
63
64/* tiny recursive du */ 84/* tiny recursive du */
65static long du(char *filename) 85static long du(char *filename)
66{ 86{
@@ -68,23 +88,43 @@ static long du(char *filename)
68 long sum; 88 long sum;
69 89
70 if ((lstat(filename, &statbuf)) != 0) { 90 if ((lstat(filename, &statbuf)) != 0) {
71 perror_msg("%s", filename); 91 bb_perror_msg("%s", filename);
92 status = EXIT_FAILURE;
72 return 0; 93 return 0;
73 } 94 }
74 if (du_depth == 0)
75 dir_dev = statbuf.st_dev;
76 else if (one_file_system && dir_dev != statbuf.st_dev)
77 return 0;
78 95
79 du_depth++; 96 if (one_file_system) {
80 sum = (statbuf.st_blocks >> 1); 97 if (du_depth == 0) {
98 dir_dev = statbuf.st_dev;
99 } else if (dir_dev != statbuf.st_dev) {
100 return 0;
101 }
102 }
103
104 sum = statbuf.st_blocks;
81 105
82 /* Don't add in stuff pointed to by symbolic links */
83 if (S_ISLNK(statbuf.st_mode)) { 106 if (S_ISLNK(statbuf.st_mode)) {
84 sum = 0L; 107 if (slink_depth > du_depth) { /* -H or -L */
85 if (du_depth == 1) { 108 if ((stat(filename, &statbuf)) != 0) {
109 bb_perror_msg("%s", filename);
110 status = EXIT_FAILURE;
111 return 0;
112 }
113 sum = statbuf.st_blocks;
114 if (slink_depth == 1) {
115 slink_depth = INT_MAX; /* Convert -H to -L. */
116 }
86 } 117 }
87 } 118 }
119
120 if (statbuf.st_nlink > count_hardlinks) {
121 /* Add files/directories with links only once */
122 if (is_in_ino_dev_hashtable(&statbuf, NULL)) {
123 return 0;
124 }
125 add_to_ino_dev_hashtable(&statbuf, NULL);
126 }
127
88 if (S_ISDIR(statbuf.st_mode)) { 128 if (S_ISDIR(statbuf.st_mode)) {
89 DIR *dir; 129 DIR *dir;
90 struct dirent *entry; 130 struct dirent *entry;
@@ -92,8 +132,9 @@ static long du(char *filename)
92 132
93 dir = opendir(filename); 133 dir = opendir(filename);
94 if (!dir) { 134 if (!dir) {
95 du_depth--; 135 bb_perror_msg("%s", filename);
96 return 0; 136 status = EXIT_FAILURE;
137 return sum;
97 } 138 }
98 139
99 newfile = last_char_is(filename, '/'); 140 newfile = last_char_is(filename, '/');
@@ -103,54 +144,86 @@ static long du(char *filename)
103 while ((entry = readdir(dir))) { 144 while ((entry = readdir(dir))) {
104 char *name = entry->d_name; 145 char *name = entry->d_name;
105 146
106 if ((strcmp(name, "..") == 0) 147 if ((name[0] == '.') && (!name[1] || (name[1] == '.' && !name[2]))) {
107 || (strcmp(name, ".") == 0)) {
108 continue; 148 continue;
109 } 149 }
110 newfile = concat_path_file(filename, name); 150 newfile = concat_path_file(filename, name);
151 ++du_depth;
111 sum += du(newfile); 152 sum += du(newfile);
153 --du_depth;
112 free(newfile); 154 free(newfile);
113 } 155 }
114 closedir(dir); 156 closedir(dir);
157 } else if (du_depth > print_files) {
158 return sum;
159 }
160 if (du_depth <= max_print_depth) {
115 print(sum, filename); 161 print(sum, filename);
116 } else if (statbuf.st_nlink > 1 && !count_hardlinks) {
117 /* Add files with hard links only once */
118 if (is_in_ino_dev_hashtable(&statbuf, NULL)) {
119 sum = 0L;
120 if (du_depth == 1)
121 print(sum, filename);
122 } else {
123 add_to_ino_dev_hashtable(&statbuf, NULL);
124 }
125 } 162 }
126 du_depth--;
127 return sum; 163 return sum;
128} 164}
129 165
130int du_main(int argc, char **argv) 166int du_main(int argc, char **argv)
131{ 167{
132 int status = EXIT_SUCCESS; 168 long total;
133 int i; 169 int slink_depth_save;
170 int print_final_total = 0;
134 int c; 171 int c;
135 172
136 /* default behaviour */ 173#ifdef CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K
137 print = print_normal; 174 if (getenv("POSIXLY_CORRECT")) { /* TODO - a new libbb function? */
175#ifdef CONFIG_FEATURE_HUMAN_READABLE
176 disp_hr = 512;
177#else
178 disp_k = 0;
179#endif
180 }
181#endif
182
183 /* Note: SUSv3 specifies that -a and -s options can not be used together
184 * in strictly conforming applications. However, it also says that some
185 * du implementations may produce output when -a and -s are used together.
186 * gnu du exits with an error code in this case. We choose to simply
187 * ignore -a. This is consistent with -s being equivalent to -d 0.
188 */
138 189
139 /* parse argv[] */ 190 while ((c = getopt(argc, argv, "aHkLsx" "d:" "lc"
140 while ((c = getopt(argc, argv, "slx"
141#ifdef CONFIG_FEATURE_HUMAN_READABLE 191#ifdef CONFIG_FEATURE_HUMAN_READABLE
142 "hm" 192 "hm"
143#endif 193#endif
144 "k")) != EOF) { 194 )) > 0) {
145 switch (c) { 195 switch (c) {
196 case 'a':
197 print_files = INT_MAX;
198 break;
199 case 'H':
200 slink_depth = 1;
201 break;
202 case 'k':
203#ifdef CONFIG_FEATURE_HUMAN_READABLE
204 disp_hr = KILOBYTE;
205#elif !defined CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K
206 disp_k = 1;
207#endif
208 break;
209 case 'L':
210 slink_depth = INT_MAX;
211 break;
146 case 's': 212 case 's':
147 print = print_summary; 213 max_print_depth = 0;
214 break;
215 case 'x':
216 one_file_system = 1;
217 break;
218
219 case 'd':
220 max_print_depth = bb_xgetularg10_bnd(optarg, 0, INT_MAX);
148 break; 221 break;
149 case 'l': 222 case 'l':
150 count_hardlinks = 1; 223 count_hardlinks = 1;
151 break; 224 break;
152 case 'x': 225 case 'c':
153 one_file_system = 1; 226 print_final_total = 1;
154 break; 227 break;
155#ifdef CONFIG_FEATURE_HUMAN_READABLE 228#ifdef CONFIG_FEATURE_HUMAN_READABLE
156 case 'h': 229 case 'h':
@@ -160,37 +233,31 @@ int du_main(int argc, char **argv)
160 disp_hr = MEGABYTE; 233 disp_hr = MEGABYTE;
161 break; 234 break;
162#endif 235#endif
163 case 'k':
164 break;
165 default: 236 default:
166 show_usage(); 237 bb_show_usage();
167 } 238 }
168 } 239 }
169 240
170 /* go through remaining args (if any) */ 241 /* go through remaining args (if any) */
242 argv += optind;
171 if (optind >= argc) { 243 if (optind >= argc) {
172 if (du(".") == 0) 244 *--argv = ".";
173 status = EXIT_FAILURE; 245 if (slink_depth == 1) {
174 } else { 246 slink_depth = 0;
175 long sum;
176
177 for (i = optind; i < argc; i++) {
178 sum = du(argv[i]);
179 if (is_directory(argv[i], FALSE, NULL) == FALSE) {
180 print_normal(sum, argv[i]);
181 }
182 reset_ino_dev_hashtable();
183 } 247 }
184 } 248 }
185 249
186 return status; 250 slink_depth_save = slink_depth;
187} 251 total = 0;
252 do {
253 total += du(*argv);
254 slink_depth = slink_depth_save;
255 } while (*++argv);
256 reset_ino_dev_hashtable();
188 257
189/* $Id: du.c,v 1.55 2002/08/23 07:28:45 aaronl Exp $ */ 258 if (print_final_total) {
190/* 259 print(total, "total");
191Local Variables: 260 }
192c-file-style: "linux" 261
193c-basic-offset: 4 262 bb_fflush_stdout_and_exit(status);
194tab-width: 4 263}
195End:
196*/
diff --git a/coreutils/echo.c b/coreutils/echo.c
index 31c031528..b600a1fbd 100644
--- a/coreutils/echo.c
+++ b/coreutils/echo.c
@@ -22,94 +22,107 @@
22 * Original copyright notice is retained at the end of this file. 22 * Original copyright notice is retained at the end of this file.
23 */ 23 */
24 24
25/* BB_AUDIT SUSv3 compliant -- unless configured as fancy echo. */
26/* http://www.opengroup.org/onlinepubs/007904975/utilities/echo.html */
27
28/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
29 *
30 * Because of behavioral differences, implemented configureable SUSv3
31 * or 'fancy' gnu-ish behaviors. Also, reduced size and fixed bugs.
32 * 1) In handling '\c' escape, the previous version only suppressed the
33 * trailing newline. SUSv3 specifies _no_ output after '\c'.
34 * 2) SUSv3 specifies that octal escapes are of the form \0{#{#{#}}}.
35 * The previous version version did not allow 4-digit octals.
36 */
37
25#include <stdio.h> 38#include <stdio.h>
26#include <string.h> 39#include <string.h>
27#include <stdlib.h> 40#include <stdlib.h>
28#include "busybox.h" 41#include "busybox.h"
29 42
30extern int 43extern int echo_main(int argc, char** argv)
31echo_main(int argc, char** argv)
32{ 44{
33 int nflag = 0; 45#ifndef CONFIG_FEATURE_FANCY_ECHO
46#define eflag '\\'
47 ++argv;
48#else
49 const char *p;
50 int nflag = 1;
34 int eflag = 0; 51 int eflag = 0;
35 52
36 /* Skip argv[0]. */ 53 while (*++argv && (**argv == '-')) {
37 argc--; 54 /* If it appears that we are handling options, then make sure
38 argv++;
39
40 while (argc > 0 && *argv[0] == '-')
41 {
42 register char *temp;
43 register int ix;
44
45 /*
46 * If it appears that we are handling options, then make sure
47 * that all of the options specified are actually valid. 55 * that all of the options specified are actually valid.
48 * Otherwise, the string should just be echoed. 56 * Otherwise, the string should just be echoed.
49 */ 57 */
50 temp = argv[0] + 1; 58
51 59 if (!*(p = *argv + 1)) { /* A single '-', so echo it. */
52 for (ix = 0; temp[ix]; ix++)
53 {
54 if (strrchr("neE", temp[ix]) == 0)
55 goto just_echo;
56 }
57
58 if (!*temp)
59 goto just_echo; 60 goto just_echo;
61 }
60 62
61 /* 63 do {
62 * All of the options in temp are valid options to echo. 64 if (strrchr("neE", *p) == 0) {
63 * Handle them.
64 */
65 while (*temp)
66 {
67 if (*temp == 'n')
68 nflag = 1;
69 else if (*temp == 'e')
70 eflag = 1;
71 else if (*temp == 'E')
72 eflag = 0;
73 else
74 goto just_echo; 65 goto just_echo;
66 }
67 } while (*++p);
75 68
76 temp++; 69 /* All of the options in this arg are valid, so handle them. */
77 } 70 p = *argv + 1;
78 argc--; 71 do {
79 argv++; 72 if (*p == 'n') {
73 nflag = 0;
74 } else if (*p == 'e') {
75 eflag = '\\';
76 } else {
77 eflag = 0;
78 }
79 } while (*++p);
80 } 80 }
81 81
82just_echo: 82just_echo:
83 while (argc > 0) { 83#endif
84 const char *arg = argv[0]; 84 while (*argv) {
85 register int c; 85 register int c;
86 86
87 while ((c = *arg++)) { 87 while ((c = *(*argv)++)) {
88 88 if (c == eflag) { /* Check for escape seq. */
89 /* Check for escape sequence. */ 89 if (**argv == 'c') {
90 if (c == '\\' && eflag && *arg) { 90 /* '\c' means cancel newline and
91 if (*arg == 'c') { 91 * ignore all subsequent chars. */
92 /* '\c' means cancel newline. */ 92 goto DONE;
93 nflag = 1; 93 }
94 arg++; 94#ifndef CONFIG_FEATURE_FANCY_ECHO
95 continue; 95 /* SUSv3 specifies that octal escapes must begin with '0'. */
96 } else { 96 if (((unsigned int)(**argv - '1')) >= 7)
97 c = process_escape_sequence(&arg); 97#endif
98 {
99 /* Since SUSv3 mandates a first digit of 0, 4-digit octals
100 * of the form \0### are accepted. */
101 if ((**argv == '0') && (((unsigned int)(argv[0][1] - '0')) < 8)) {
102 (*argv)++;
103 }
104 /* bb_process_escape_sequence can handle nul correctly */
105 c = bb_process_escape_sequence((const char **) argv);
98 } 106 }
99 } 107 }
100
101 putchar(c); 108 putchar(c);
102 } 109 }
103 argc--; 110
104 argv++; 111 if (*++argv) {
105 if (argc > 0)
106 putchar(' '); 112 putchar(' ');
113 }
107 } 114 }
108 if (!nflag) 115
116#ifdef CONFIG_FEATURE_FANCY_ECHO
117 if (nflag) {
109 putchar('\n'); 118 putchar('\n');
110 fflush(stdout); 119 }
120#else
121 putchar('\n');
122#endif
111 123
112 return EXIT_SUCCESS; 124DONE:
125 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
113} 126}
114 127
115/*- 128/*-
diff --git a/coreutils/env.c b/coreutils/env.c
index 8bb690b72..db13b3aed 100644
--- a/coreutils/env.c
+++ b/coreutils/env.c
@@ -24,50 +24,66 @@
24 * Modified for BusyBox by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> 24 * Modified for BusyBox by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
25 */ 25 */
26 26
27/* BB_AUDIT SUSv3 compliant */
28/* http://www.opengroup.org/onlinepubs/007904975/utilities/env.html */
29
30/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
31 *
32 * Fixed bug involving exit return codes if execvp fails. Also added
33 * output error checking.
34 */
35
27#include <stdio.h> 36#include <stdio.h>
28#include <string.h> 37#include <string.h>
29#include <getopt.h>
30#include <stdlib.h> 38#include <stdlib.h>
39#include <errno.h>
31#include <unistd.h> 40#include <unistd.h>
32#include "busybox.h" 41#include "busybox.h"
33 42
34extern int env_main(int argc, char** argv) 43extern int env_main(int argc, char** argv)
35{ 44{
36 char **ep, *p; 45 char **ep, *p;
37 char *cleanenv[1]; 46 char *cleanenv[1] = { NULL };
38 int ignore_environment = 0;
39 int ch; 47 int ch;
40 48
41 while ((ch = getopt(argc, argv, "+iu:")) != -1) { 49 while ((ch = getopt(argc, argv, "iu:")) > 0) {
42 switch(ch) { 50 switch(ch) {
43 case 'i': 51 case 'i':
44 ignore_environment = 1; 52 environ = cleanenv;
45 break; 53 break;
46 case 'u': 54 case 'u':
47 unsetenv(optarg); 55 unsetenv(optarg);
48 break; 56 break;
49 default: 57 default:
50 show_usage(); 58 bb_show_usage();
51 } 59 }
52 } 60 }
53 if (optind != argc && !strcmp(argv[optind], "-")) { 61
54 ignore_environment = 1; 62 argv += optind;
55 argv++; 63
56 } 64 if (*argv && (argv[0][0] == '-') && !argv[0][1]) {
57 if (ignore_environment) {
58 environ = cleanenv; 65 environ = cleanenv;
59 cleanenv[0] = NULL; 66 ++argv;
60 } 67 }
61 for (argv += optind; *argv && (p = strchr(*argv, '=')); ++argv) 68
62 if (putenv(*argv) < 0) 69 while (*argv && ((p = strchr(*argv, '=')) != NULL)) {
63 perror_msg_and_die("%s", *argv); 70 if (putenv(*argv) < 0) {
71 bb_perror_msg_and_die("putenv");
72 }
73 ++argv;
74 }
75
64 if (*argv) { 76 if (*argv) {
65 execvp(*argv, argv); 77 execvp(*argv, argv);
66 perror_msg_and_die("%s", *argv); 78 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
79 return (errno == ENOENT) ? 127 : 126; /* SUSv3-mandated exit codes. */
67 } 80 }
68 for (ep = environ; *ep; ep++) 81
82 for (ep = environ; *ep; ep++) {
69 puts(*ep); 83 puts(*ep);
70 return 0; 84 }
85
86 bb_fflush_stdout_and_exit(0);
71} 87}
72 88
73/* 89/*
diff --git a/coreutils/expr.c b/coreutils/expr.c
index 55ae3a969..ecba825d5 100644
--- a/coreutils/expr.c
+++ b/coreutils/expr.c
@@ -78,14 +78,14 @@ int expr_main (int argc, char **argv)
78 VALUE *v; 78 VALUE *v;
79 79
80 if (argc == 1) { 80 if (argc == 1) {
81 error_msg_and_die("too few arguments"); 81 bb_error_msg_and_die("too few arguments");
82 } 82 }
83 83
84 args = argv + 1; 84 args = argv + 1;
85 85
86 v = eval (); 86 v = eval ();
87 if (*args) 87 if (*args)
88 error_msg_and_die ("syntax error"); 88 bb_error_msg_and_die ("syntax error");
89 89
90 if (v->type == integer) 90 if (v->type == integer)
91 printf ("%d\n", v->u.i); 91 printf ("%d\n", v->u.i);
@@ -147,7 +147,7 @@ static int null (VALUE *v)
147static void tostring (VALUE *v) 147static void tostring (VALUE *v)
148{ 148{
149 if (v->type == integer) { 149 if (v->type == integer) {
150 bb_asprintf (&(v->u.s), "%d", v->u.i); 150 bb_xasprintf (&(v->u.s), "%d", v->u.i);
151 v->type = string; 151 v->type = string;
152 } 152 }
153} 153}
@@ -216,7 +216,7 @@ static \
216int name (VALUE *l, VALUE *r) \ 216int name (VALUE *l, VALUE *r) \
217{ \ 217{ \
218 if (!toarith (l) || !toarith (r)) \ 218 if (!toarith (l) || !toarith (r)) \
219 error_msg_and_die ("non-numeric argument"); \ 219 bb_error_msg_and_die ("non-numeric argument"); \
220 return l->u.i op r->u.i; \ 220 return l->u.i op r->u.i; \
221} 221}
222 222
@@ -224,9 +224,9 @@ int name (VALUE *l, VALUE *r) \
224static int name (VALUE *l, VALUE *r) \ 224static int name (VALUE *l, VALUE *r) \
225{ \ 225{ \
226 if (!toarith (l) || !toarith (r)) \ 226 if (!toarith (l) || !toarith (r)) \
227 error_msg_and_die ( "non-numeric argument"); \ 227 bb_error_msg_and_die ( "non-numeric argument"); \
228 if (r->u.i == 0) \ 228 if (r->u.i == 0) \
229 error_msg_and_die ( "division by zero"); \ 229 bb_error_msg_and_die ( "division by zero"); \
230 return l->u.i op r->u.i; \ 230 return l->u.i op r->u.i; \
231} 231}
232 232
@@ -270,7 +270,7 @@ of a basic regular expression is not portable; it is being ignored",
270 re_syntax_options = RE_SYNTAX_POSIX_BASIC; 270 re_syntax_options = RE_SYNTAX_POSIX_BASIC;
271 errmsg = re_compile_pattern (pv->u.s, len, &re_buffer); 271 errmsg = re_compile_pattern (pv->u.s, len, &re_buffer);
272 if (errmsg) { 272 if (errmsg) {
273 error_msg_and_die("%s", errmsg); 273 bb_error_msg_and_die("%s", errmsg);
274 } 274 }
275 275
276 len = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs); 276 len = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs);
@@ -301,19 +301,19 @@ static VALUE *eval7 (void)
301 VALUE *v; 301 VALUE *v;
302 302
303 if (!*args) 303 if (!*args)
304 error_msg_and_die ( "syntax error"); 304 bb_error_msg_and_die ( "syntax error");
305 305
306 if (nextarg ("(")) { 306 if (nextarg ("(")) {
307 args++; 307 args++;
308 v = eval (); 308 v = eval ();
309 if (!nextarg (")")) 309 if (!nextarg (")"))
310 error_msg_and_die ( "syntax error"); 310 bb_error_msg_and_die ( "syntax error");
311 args++; 311 args++;
312 return v; 312 return v;
313 } 313 }
314 314
315 if (nextarg (")")) 315 if (nextarg (")"))
316 error_msg_and_die ( "syntax error"); 316 bb_error_msg_and_die ( "syntax error");
317 317
318 return str_value (*args++); 318 return str_value (*args++);
319} 319}
@@ -327,7 +327,7 @@ static VALUE *eval6 (void)
327 if (nextarg ("quote")) { 327 if (nextarg ("quote")) {
328 args++; 328 args++;
329 if (!*args) 329 if (!*args)
330 error_msg_and_die ( "syntax error"); 330 bb_error_msg_and_die ( "syntax error");
331 return str_value (*args++); 331 return str_value (*args++);
332 } 332 }
333 else if (nextarg ("length")) { 333 else if (nextarg ("length")) {
@@ -373,7 +373,7 @@ static VALUE *eval6 (void)
373 else { 373 else {
374 v = xmalloc (sizeof(VALUE)); 374 v = xmalloc (sizeof(VALUE));
375 v->type = string; 375 v->type = string;
376 v->u.s = xstrndup(l->u.s + i1->u.i - 1, i2->u.i); 376 v->u.s = bb_xstrndup(l->u.s + i1->u.i - 1, i2->u.i);
377 } 377 }
378 freev (l); 378 freev (l);
379 freev (i1); 379 freev (i1);
diff --git a/coreutils/false.c b/coreutils/false.c
index 96b18498a..c17de7693 100644
--- a/coreutils/false.c
+++ b/coreutils/false.c
@@ -21,7 +21,8 @@
21 * 21 *
22 */ 22 */
23 23
24/* getopt not needed */ 24/* BB_AUDIT SUSv3 compliant */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/false.html */
25 26
26#include <stdlib.h> 27#include <stdlib.h>
27#include "busybox.h" 28#include "busybox.h"
diff --git a/coreutils/head.c b/coreutils/head.c
index ad21e1b95..dab4de11b 100644
--- a/coreutils/head.c
+++ b/coreutils/head.c
@@ -1,9 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini head implementation for busybox 3 * head implementation for busybox
4 * 4 *
5 * Copyright (C) 1999 by Lineo, inc. and John Beppu 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -21,82 +20,119 @@
21 * 20 *
22 */ 21 */
23 22
23/* BB_AUDIT SUSv3 compliant */
24/* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/head.html */
26
24#include <stdio.h> 27#include <stdio.h>
25#include <getopt.h>
26#include <stdlib.h> 28#include <stdlib.h>
27#include <string.h> 29#include <limits.h>
28#include <ctype.h> 30#include <ctype.h>
31#include <unistd.h>
29#include "busybox.h" 32#include "busybox.h"
30 33
31static int head(int len, FILE *fp) 34static const char head_opts[] =
32{ 35 "n:"
33 int i; 36#ifdef CONFIG_FEATURE_FANCY_HEAD
34 char *input; 37 "c:qv"
38#endif
39 ;
35 40
36 for (i = 0; i < len; i++) { 41static const char header_fmt_str[] = "\n==> %s <==\n";
37 if ((input = get_line_from_file(fp)) == NULL)
38 break;
39 fputs(input, stdout);
40 free(input);
41 }
42 return 0;
43}
44 42
45/* BusyBoxed head(1) */
46int head_main(int argc, char **argv) 43int head_main(int argc, char **argv)
47{ 44{
45 unsigned long count = 10;
46 unsigned long i;
47#ifdef CONFIG_FEATURE_FANCY_HEAD
48 int count_bytes = 0;
49 int header_threshhold = 1;
50#endif
51
48 FILE *fp; 52 FILE *fp;
49 int need_headers, opt, len = 10, status = EXIT_SUCCESS; 53 const char *fmt;
54 char *p;
55 int opt;
56 int c;
57 int retval = EXIT_SUCCESS;
50 58
51 if (( argc >= 2 ) && ( argv [1][0] == '-' ) && isdigit ( argv [1][1] )) { 59 /* Allow legacy syntax of an initial numeric option without -n. */
52 len = atoi ( &argv [1][1] ); 60 if ((argc > 1) && (argv[1][0] == '-')
53 optind = 2; 61 /* && (isdigit)(argv[1][1]) */
62 && (((unsigned int)(argv[1][1] - '0')) <= 9)
63 ) {
64 --argc;
65 ++argv;
66 p = (*argv) + 1;
67 goto GET_COUNT;
54 } 68 }
55 69
56 /* parse argv[] */ 70 while ((opt = getopt(argc, argv, head_opts)) > 0) {
57 while ((opt = getopt(argc, argv, "n:")) > 0) { 71 switch(opt) {
58 switch (opt) { 72#ifdef CONFIG_FEATURE_FANCY_HEAD
59 case 'n': 73 case 'q':
60 len = atoi(optarg); 74 header_threshhold = INT_MAX;
61 if (len >= 0) 75 break;
76 case 'v':
77 header_threshhold = -1;
62 break; 78 break;
63 /* fallthrough */ 79 case 'c':
64 default: 80 count_bytes = 1;
65 show_usage(); 81 /* fall through */
82#endif
83 case 'n':
84 p = optarg;
85 GET_COUNT:
86 count = bb_xgetularg10(p);
87 break;
88 default:
89 bb_show_usage();
66 } 90 }
67 } 91 }
68 92
69 /* get rest of argv[] or stdin if nothing's left */ 93 argv += optind;
70 if (argv[optind] == NULL) { 94 if (!*argv) {
71 head(len, stdin); 95 *--argv = "-";
72 return status; 96 }
73 }
74 97
75 need_headers = optind != (argc - 1); 98 fmt = header_fmt_str + 1;
76 while (argv[optind]) { 99#ifdef CONFIG_FEATURE_FANCY_HEAD
77 if (strcmp(argv[optind], "-") == 0) { 100 if (argc - optind <= header_threshhold) {
78 fp = stdin; 101 header_threshhold = 0;
79 argv[optind] = "standard input"; 102 }
80 } else { 103#else
81 if ((fp = wfopen(argv[optind], "r")) == NULL) 104 if (argc <= optind + 1) {
82 status = EXIT_FAILURE; 105 fmt += 11;
83 } 106 }
84 if (fp) { 107 /* Now define some things here to avoid #ifdefs in the code below.
85 if (need_headers) { 108 * These should optimize out of the if conditions below. */
86 printf("==> %s <==\n", argv[optind]); 109#define header_threshhold 1
110#define count_bytes 0
111#endif
112
113 do {
114 if ((fp = bb_wfopen_input(*argv)) != NULL) {
115 if (fp == stdin) {
116 *argv = (char *) bb_msg_standard_input;
87 } 117 }
88 head(len, fp); 118 if (header_threshhold) {
89 if (ferror(fp)) { 119 bb_printf(fmt, *argv);
90 perror_msg("%s", argv[optind]);
91 status = EXIT_FAILURE;
92 } 120 }
93 if (optind < argc - 1) 121 i = count;
94 putchar('\n'); 122 while (i && ((c = getc(fp)) != EOF)) {
95 if (fp != stdin) 123 if (count_bytes || (c == '\n')) {
96 fclose(fp); 124 --i;
125 }
126 putchar(c);
127 }
128 if (bb_fclose_nonstdin(fp)) {
129 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
130 retval = EXIT_FAILURE;
131 }
132 bb_xferror_stdout();
97 } 133 }
98 optind++; 134 fmt = header_fmt_str;
99 } 135 } while (*++argv);
100 136
101 return status; 137 bb_fflush_stdout_and_exit(retval);
102} 138}
diff --git a/coreutils/hostid.c b/coreutils/hostid.c
index 68a2cc659..917dc223e 100644
--- a/coreutils/hostid.c
+++ b/coreutils/hostid.c
@@ -20,13 +20,19 @@
20 * 20 *
21 */ 21 */
22 22
23#include <stdio.h> 23/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
24
24#include <stdlib.h> 25#include <stdlib.h>
25#include <unistd.h> 26#include <unistd.h>
26#include "busybox.h" 27#include "busybox.h"
27 28
28extern int hostid_main(int argc, char **argv) 29extern int hostid_main(int argc, char **argv)
29{ 30{
30 printf("%lx\n", gethostid()); 31 if (argc > 1) {
31 return EXIT_SUCCESS; 32 bb_show_usage();
33 }
34
35 bb_printf("%lx\n", gethostid());
36
37 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
32} 38}
diff --git a/coreutils/id.c b/coreutils/id.c
index c7f61532d..9b2d60dc7 100644
--- a/coreutils/id.c
+++ b/coreutils/id.c
@@ -20,6 +20,8 @@
20 * 20 *
21 */ 21 */
22 22
23/* BB_AUDIT SUSv3 _NOT_ compliant -- option -G is not currently supported. */
24
23#include "busybox.h" 25#include "busybox.h"
24#include <stdio.h> 26#include <stdio.h>
25#include <unistd.h> 27#include <unistd.h>
@@ -27,70 +29,58 @@
27#include <string.h> 29#include <string.h>
28#include <sys/types.h> 30#include <sys/types.h>
29 31
32#define NO_GROUP 1
33#define NO_USER 2
34#define PRINT_REAL 4
35#define NAME_NOT_NUMBER 8
36
30extern int id_main(int argc, char **argv) 37extern int id_main(int argc, char **argv)
31{ 38{
32 int no_user = 0, no_group = 0, print_real = 0;
33 int name_not_number = 0;
34 char user[9], group[9]; 39 char user[9], group[9];
35 long gid;
36 long pwnam, grnam; 40 long pwnam, grnam;
37 int opt; 41 int uid, gid;
42 int flags;
38 43
39 gid = 0; 44 flags = bb_getopt_ulflags(argc, argv, "ugrn");
40 45
41 while ((opt = getopt(argc, argv, "ugrn")) > 0) { 46 if (((flags & (NO_USER | NO_GROUP)) == (NO_USER | NO_GROUP))
42 switch (opt) { 47 || (argc > optind + 1)
43 case 'u': 48 ) {
44 no_group++; 49 bb_show_usage();
45 break;
46 case 'g':
47 no_user++;
48 break;
49 case 'r':
50 print_real++;
51 break;
52 case 'n':
53 name_not_number++;
54 break;
55 default:
56 show_usage();
57 }
58 } 50 }
59 51
60 if (no_user && no_group) show_usage();
61
62 if (argv[optind] == NULL) { 52 if (argv[optind] == NULL) {
63 if (print_real) { 53 if (flags & PRINT_REAL) {
64 my_getpwuid(user, getuid()); 54 uid = getuid();
65 my_getgrgid(group, getgid()); 55 gid = getgid();
66 } else { 56 } else {
67 my_getpwuid(user, geteuid()); 57 uid = geteuid();
68 my_getgrgid(group, getegid()); 58 gid = getegid();
69 } 59 }
60 my_getpwuid(user, uid);
70 } else { 61 } else {
71 safe_strncpy(user, argv[optind], sizeof(user)); 62 safe_strncpy(user, argv[optind], sizeof(user));
72 gid = my_getpwnamegid(user); 63 gid = my_getpwnamegid(user);
73 my_getgrgid(group, gid);
74 } 64 }
65 my_getgrgid(group, gid);
75 66
76 pwnam=my_getpwnam(user); 67 pwnam=my_getpwnam(user);
77 grnam=my_getgrnam(group); 68 grnam=my_getgrnam(group);
78 69
79 if (no_group) { 70 if (flags & (NO_GROUP | NO_USER)) {
80 if(name_not_number) 71 char *s = group;
81 puts(user); 72 if (flags & NO_GROUP) {
82 else 73 s = user;
83 printf("%ld\n", pwnam); 74 grnam = pwnam;
84 } else if (no_user) { 75 }
85 if(name_not_number) 76 if (flags & NAME_NOT_NUMBER) {
86 puts(group); 77 puts(s);
87 else 78 } else {
88 printf("%ld\n", grnam); 79 printf("%ld\n", grnam);
80 }
89 } else { 81 } else {
90 printf("uid=%ld(%s) gid=%ld(%s)\n", pwnam, user, grnam, group); 82 printf("uid=%ld(%s) gid=%ld(%s)\n", pwnam, user, grnam, group);
91 } 83 }
92 return(0);
93}
94 84
95 85 bb_fflush_stdout_and_exit(0);
96/* END CODE */ 86}
diff --git a/coreutils/length.c b/coreutils/length.c
index 73becd28a..bce43ab3f 100644
--- a/coreutils/length.c
+++ b/coreutils/length.c
@@ -1,4 +1,7 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2
3/* BB_AUDIT SUSv3 N/A -- Apparently a busybox (obsolete?) extension. */
4
2#include <stdlib.h> 5#include <stdlib.h>
3#include <string.h> 6#include <string.h>
4#include <stdio.h> 7#include <stdio.h>
@@ -6,8 +9,11 @@
6 9
7extern int length_main(int argc, char **argv) 10extern int length_main(int argc, char **argv)
8{ 11{
9 if (argc != 2 || **(argv + 1) == '-') 12 if ((argc != 2) || (**(++argv) == '-')) {
10 show_usage(); 13 bb_show_usage();
11 printf("%lu\n", (long)strlen(argv[1])); 14 }
12 return EXIT_SUCCESS; 15
16 bb_printf("%lu\n", (unsigned long)strlen(*argv));
17
18 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
13} 19}
diff --git a/coreutils/libcoreutils/Makefile b/coreutils/libcoreutils/Makefile
new file mode 100644
index 000000000..59ec24ed9
--- /dev/null
+++ b/coreutils/libcoreutils/Makefile
@@ -0,0 +1,30 @@
1# Makefile for busybox
2#
3# Copyright (C) 1999-2002 Erik Andersen <andersee@debian.org>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18#
19
20TOPDIR:= ../../
21LIBCOREUTILS_DIR:=./
22include $(TOPDIR).config
23include $(TOPDIR)Rules.mak
24include Makefile.in
25all: $(libraries-y)
26-include $(TOPDIR).depend
27
28clean:
29 rm -f *.o *.a $(AR_TARGET)
30
diff --git a/coreutils/libcoreutils/Makefile.in b/coreutils/libcoreutils/Makefile.in
new file mode 100644
index 000000000..47391dd49
--- /dev/null
+++ b/coreutils/libcoreutils/Makefile.in
@@ -0,0 +1,32 @@
1# Makefile for busybox
2#
3# Copyright (C) 1999-2002 Erik Andersen <andersee@debian.org>
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18#
19
20LIBCOREUTILS_AR:=libcoreutils.a
21ifndef $(LIBCOREUTILS_DIR)
22LIBCOREUTILS_DIR:=$(TOPDIR)coreutils/libcoreutils/
23endif
24
25LIBCOREUTILS_SRC:= cp_mv_stat.c getopt_mk_fifo_nod.c xgetoptfile_sort_uniq.c
26
27LIBCOREUTILS_OBJS=$(patsubst %.c,$(LIBCOREUTILS_DIR)%.o, $(LIBCOREUTILS_SRC))
28
29libraries-y+=$(LIBCOREUTILS_DIR)$(LIBCOREUTILS_AR)
30
31$(LIBCOREUTILS_DIR)$(LIBCOREUTILS_AR): $(LIBCOREUTILS_OBJS)
32 $(AR) -ro $@ $(LIBCOREUTILS_OBJS)
diff --git a/coreutils/libcoreutils/coreutils.h b/coreutils/libcoreutils/coreutils.h
new file mode 100644
index 000000000..eabca8204
--- /dev/null
+++ b/coreutils/libcoreutils/coreutils.h
@@ -0,0 +1,12 @@
1#ifndef COREUTILS_H
2#define COREUTILS_H 1
3
4typedef int (*stat_func)(const char *fn, struct stat *ps);
5
6extern int cp_mv_stat2(const char *fn, struct stat *fn_stat, stat_func sf);
7extern int cp_mv_stat(const char *fn, struct stat *fn_stat);
8
9extern mode_t getopt_mk_fifo_nod(int argc, char **argv);
10extern FILE *xgetoptfile_sort_uniq(char **argv, const char *mode);
11
12#endif
diff --git a/coreutils/libcoreutils/cp_mv_stat.c b/coreutils/libcoreutils/cp_mv_stat.c
new file mode 100644
index 000000000..5a70b0221
--- /dev/null
+++ b/coreutils/libcoreutils/cp_mv_stat.c
@@ -0,0 +1,45 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * coreutils utility routine
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <errno.h>
24#include <sys/stat.h>
25#include "libbb.h"
26#include "coreutils.h"
27
28extern int cp_mv_stat2(const char *fn, struct stat *fn_stat, stat_func sf)
29{
30 if (sf(fn, fn_stat) < 0) {
31 if (errno != ENOENT) {
32 bb_perror_msg("unable to stat `%s'", fn);
33 return -1;
34 }
35 return 0;
36 } else if (S_ISDIR(fn_stat->st_mode)) {
37 return 3;
38 }
39 return 1;
40}
41
42extern int cp_mv_stat(const char *fn, struct stat *fn_stat)
43{
44 return cp_mv_stat2(fn, fn_stat, stat);
45}
diff --git a/coreutils/libcoreutils/getopt_mk_fifo_nod.c b/coreutils/libcoreutils/getopt_mk_fifo_nod.c
new file mode 100644
index 000000000..0872bdcf0
--- /dev/null
+++ b/coreutils/libcoreutils/getopt_mk_fifo_nod.c
@@ -0,0 +1,45 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * coreutils utility routine
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <unistd.h>
26#include "libbb.h"
27#include "coreutils.h"
28
29extern mode_t getopt_mk_fifo_nod(int argc, char **argv)
30{
31 mode_t mode = 0666;
32 int opt;
33
34 while ((opt = getopt(argc, argv, "m:")) > 0) {
35 if (opt == 'm') {
36 mode = 0666;
37 if (bb_parse_mode(optarg, &mode)) {
38 umask(0);
39 continue;
40 }
41 }
42 bb_show_usage();
43 }
44 return mode;
45}
diff --git a/coreutils/libcoreutils/xgetoptfile_sort_uniq.c b/coreutils/libcoreutils/xgetoptfile_sort_uniq.c
new file mode 100644
index 000000000..a63daf97b
--- /dev/null
+++ b/coreutils/libcoreutils/xgetoptfile_sort_uniq.c
@@ -0,0 +1,38 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * coreutils utility routine
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <stdio.h>
24#include <unistd.h>
25#include "libbb.h"
26#include "coreutils.h"
27
28extern FILE *xgetoptfile_sort_uniq(char **argv, const char *mode)
29{
30 const char *n;
31
32 if ((n = *argv) != NULL) {
33 if ((*n != '-') || n[1]) {
34 return bb_xfopen(n, mode);
35 }
36 }
37 return (*mode == 'r') ? stdin : stdout;
38}
diff --git a/coreutils/ln.c b/coreutils/ln.c
index 427ffcc6e..2edece104 100644
--- a/coreutils/ln.c
+++ b/coreutils/ln.c
@@ -21,113 +21,86 @@
21 * 21 *
22 */ 22 */
23 23
24#include <stdio.h> 24/* BB_AUDIT SUSv3 compliant */
25#include <dirent.h> 25/* BB_AUDIT GNU options missing: -b, -d, -F, -i, -S, and -v. */
26#include <string.h> 26/* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */
27
28/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
29 *
30 * Fixed bug involving -n option. Essentially, -n was always in effect.
31 */
32
27#include <stdlib.h> 33#include <stdlib.h>
28#include <errno.h>
29#include <unistd.h> 34#include <unistd.h>
30#include "busybox.h" 35#include "busybox.h"
31 36
37#define LN_SYMLINK 1
38#define LN_FORCE 2
39#define LN_NODEREFERENCE 4
32 40
33static const int LN_SYMLINK = 1; 41extern int ln_main(int argc, char **argv)
34static const int LN_FORCE = 2;
35static const int LN_NODEREFERENCE = 4;
36
37/*
38 * linkDestName is where the link points to,
39 * linkSrcName is the name of the link to be created.
40 */
41static int fs_link(const char *link_destname, const char *link_srcname,
42 const int flag)
43{ 42{
44 int status; 43 int status = EXIT_SUCCESS;
45 int src_is_dir; 44 int flag;
46 char *src_name = 0; 45 char *last;
46 char *src_name;
47 const char *src; 47 const char *src;
48 int (*link_func)(const char *, const char *);
48 49
49 if (link_destname==NULL) 50 flag = bb_getopt_ulflags(argc, argv, "sfn");
50 return(FALSE); 51
51 52 if (argc == optind) {
52 if (link_srcname==NULL) 53 bb_show_usage();
53 src = link_destname;
54 else
55 src = link_srcname;
56
57 if (flag&LN_NODEREFERENCE)
58 src_is_dir = is_directory(src, TRUE, NULL);
59 else
60 src_is_dir = is_directory(src, FALSE, NULL);
61
62 if ((src_is_dir==TRUE)&&((flag&LN_NODEREFERENCE)==0)) {
63 char* srcdir_name;
64
65 srcdir_name = xstrdup(link_destname);
66 src_name = concat_path_file(src, get_last_path_component(srcdir_name));
67 src = src_name;
68 free(srcdir_name);
69 } 54 }
70 55
71 if (flag&LN_FORCE) 56 last = argv[argc - 1];
72 unlink(src); 57 argv += optind;
73 58
74 if (flag&LN_SYMLINK) 59 if (argc == optind + 1) {
75 status = symlink(link_destname, src); 60 *--argv = last;
76 else 61 last = bb_get_last_path_component(bb_xstrdup(last));
77 status = link(link_destname, src);
78
79 if (status != 0) {
80 perror_msg(src);
81 status = FALSE;
82 } else {
83 status = TRUE;
84 } 62 }
85 free(src_name);
86 return status;
87}
88 63
89extern int ln_main(int argc, char **argv) 64 do {
90{ 65 src_name = 0;
91 int status = EXIT_SUCCESS; 66 src = last;
92 int flag = 0; 67
93 int opt; 68 if (is_directory(src,
94 69 (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE,
95 /* Parse any options */ 70 NULL)) {
96 while ((opt=getopt(argc, argv, "sfn")) != -1) { 71 src_name = bb_xstrdup(*argv);
97 switch(opt) { 72 src = concat_path_file(src, bb_get_last_path_component(src_name));
98 case 's': 73 free(src_name);
99 flag |= LN_SYMLINK; 74 src_name = (char *)src;
100 break;
101 case 'f':
102 flag |= LN_FORCE;
103 break;
104 case 'n':
105 flag |= LN_NODEREFERENCE;
106 break;
107 default:
108 show_usage();
109 } 75 }
110 } 76
111 if (optind > (argc-1)) { 77 if (flag & LN_FORCE) {
112 show_usage(); 78 unlink(src);
113 } 79 }
114 if (optind == (argc-1)) { 80
115 if (fs_link(argv[optind], 81 link_func = link;
116 get_last_path_component(argv[optind]), flag)==FALSE) 82 if (flag & LN_SYMLINK) {
117 status = EXIT_FAILURE; 83 link_func = symlink;
118 } 84 }
119 while(optind<(argc-1)) { 85
120 if (fs_link(argv[optind], argv[argc-1], flag)==FALSE) 86 if (link_func(*argv, src) != 0) {
121 status = EXIT_FAILURE; 87 bb_perror_msg(src);
122 optind++; 88 status = EXIT_FAILURE;;
123 } 89 }
90
91 free(src_name);
92
93 } while ((++argv)[1]);
94
124 return status; 95 return status;
125} 96}
126 97
127/* 98
128Local Variables: 99
129c-file-style: "linux" 100
130c-basic-offset: 4 101
131tab-width: 4 102
132End: 103
133*/ 104
105
106
diff --git a/coreutils/logname.c b/coreutils/logname.c
index 3e10fba6f..9cedff027 100644
--- a/coreutils/logname.c
+++ b/coreutils/logname.c
@@ -20,6 +20,19 @@
20 * 20 *
21 */ 21 */
22 22
23/* BB_AUDIT SUSv3 compliant */
24/* http://www.opengroup.org/onlinepubs/007904975/utilities/logname.html */
25
26/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
27 *
28 * SUSv3 specifies the string used is that returned from getlogin().
29 * The previous implementation used getpwuid() for geteuid(), which
30 * is _not_ the same. Erik apparently made this change almost 3 years
31 * ago to avoid failing when no utmp was available. However, the
32 * correct course of action wrt SUSv3 for a failing getlogin() is
33 * a dianostic message and an error return.
34 */
35
23#include <stdio.h> 36#include <stdio.h>
24#include <stdlib.h> 37#include <stdlib.h>
25#include <unistd.h> 38#include <unistd.h>
@@ -27,14 +40,16 @@
27 40
28extern int logname_main(int argc, char **argv) 41extern int logname_main(int argc, char **argv)
29{ 42{
30 char user[9]; 43 const char *p;
31 44
32 if (argc > 1) 45 if (argc > 1) {
33 show_usage(); 46 bb_show_usage();
47 }
34 48
35 if (my_getpwuid(user, geteuid())) { 49 if ((p = getlogin()) != NULL) {
36 puts(user); 50 puts(p);
37 return EXIT_SUCCESS; 51 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
38 } 52 }
39 error_msg_and_die("no login name"); 53
54 bb_perror_msg_and_die("getlogin");
40} 55}
diff --git a/coreutils/ls.c b/coreutils/ls.c
index 64ec0fee6..4a4956611 100644
--- a/coreutils/ls.c
+++ b/coreutils/ls.c
@@ -73,58 +73,76 @@ enum {
73#endif 73#endif
74 74
75/* what is the overall style of the listing */ 75/* what is the overall style of the listing */
76enum { 76#define STYLE_AUTO (0)
77 STYLE_AUTO = 0, 77#define STYLE_COLUMNS (1U<<21) /* fill columns */
78 STYLE_LONG = 1, /* one record per line, extended info */ 78#define STYLE_LONG (2U<<21) /* one record per line, extended info */
79 STYLE_SINGLE = 2, /* one record per line */ 79#define STYLE_SINGLE (3U<<21) /* one record per line */
80 STYLE_COLUMNS = 3 /* fill columns */ 80
81}; 81#define STYLE_MASK STYLE_SINGLE
82#define STYLE_ONE_RECORD_FLAG STYLE_LONG
82 83
83/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */ 84/* 51306 lrwxrwxrwx 1 root root 2 May 11 01:43 /bin/view -> vi* */
84/* what file information will be listed */ 85/* what file information will be listed */
85#define LIST_INO (1<<0) 86#define LIST_INO (1U<<0)
86#define LIST_BLOCKS (1<<1) 87#define LIST_BLOCKS (1U<<1)
87#define LIST_MODEBITS (1<<2) 88#define LIST_MODEBITS (1U<<2)
88#define LIST_NLINKS (1<<3) 89#define LIST_NLINKS (1U<<3)
89#define LIST_ID_NAME (1<<4) 90#define LIST_ID_NAME (1U<<4)
90#define LIST_ID_NUMERIC (1<<5) 91#define LIST_ID_NUMERIC (1U<<5)
91#define LIST_SIZE (1<<6) 92#define LIST_SIZE (1U<<6)
92#define LIST_DEV (1<<7) 93#define LIST_DEV (1U<<7)
93#define LIST_DATE_TIME (1<<8) 94#define LIST_DATE_TIME (1U<<8)
94#define LIST_FULLTIME (1<<9) 95#define LIST_FULLTIME (1U<<9)
95#define LIST_FILENAME (1<<10) 96#define LIST_FILENAME (1U<<10)
96#define LIST_SYMLINK (1<<11) 97#define LIST_SYMLINK (1U<<11)
97#define LIST_FILETYPE (1<<12) 98#define LIST_FILETYPE (1U<<12)
98#define LIST_EXEC (1<<13) 99#define LIST_EXEC (1U<<13)
100
101#define LIST_MASK ((LIST_EXEC << 1) - 1)
99 102
100/* what files will be displayed */ 103/* what files will be displayed */
101#define DISP_NORMAL (0) /* show normal filenames */ 104/* TODO -- We may be able to make DISP_NORMAL 0 to save a bit slot. */
102#define DISP_DIRNAME (1<<0) /* 2 or more items? label directories */ 105#define DISP_NORMAL (1U<<14) /* show normal filenames */
103#define DISP_HIDDEN (1<<1) /* show filenames starting with . */ 106#define DISP_DIRNAME (1U<<15) /* 2 or more items? label directories */
104#define DISP_DOT (1<<2) /* show . and .. */ 107#define DISP_HIDDEN (1U<<16) /* show filenames starting with . */
105#define DISP_NOLIST (1<<3) /* show directory as itself, not contents */ 108#define DISP_DOT (1U<<17) /* show . and .. */
106#define DISP_RECURSIVE (1<<4) /* show directory and everything below it */ 109#define DISP_NOLIST (1U<<18) /* show directory as itself, not contents */
107#define DISP_ROWS (1<<5) /* print across rows */ 110#define DISP_RECURSIVE (1U<<19) /* show directory and everything below it */
111#define DISP_ROWS (1U<<20) /* print across rows */
112
113#define DISP_MASK (((DISP_ROWS << 1) - 1) & ~(DISP_NORMAL - 1))
108 114
109#ifdef CONFIG_FEATURE_LS_SORTFILES 115#ifdef CONFIG_FEATURE_LS_SORTFILES
110/* how will the files be sorted */ 116/* how will the files be sorted */
111static const int SORT_FORWARD = 0; /* sort in reverse order */ 117#define SORT_ORDER_FORWARD 0 /* sort in reverse order */
112static const int SORT_REVERSE = 1; /* sort in reverse order */ 118#define SORT_ORDER_REVERSE (1U<<27) /* sort in reverse order */
113static const int SORT_NAME = 2; /* sort by file name */ 119
114static const int SORT_SIZE = 3; /* sort by file size */ 120#define SORT_NAME 0 /* sort by file name */
115static const int SORT_ATIME = 4; /* sort by last access time */ 121#define SORT_SIZE (1U<<28) /* sort by file size */
116static const int SORT_CTIME = 5; /* sort by last change time */ 122#define SORT_ATIME (2U<<28) /* sort by last access time */
117static const int SORT_MTIME = 6; /* sort by last modification time */ 123#define SORT_CTIME (3U<<28) /* sort by last change time */
118static const int SORT_VERSION = 7; /* sort by version */ 124#define SORT_MTIME (4U<<28) /* sort by last modification time */
119static const int SORT_EXT = 8; /* sort by file name extension */ 125#define SORT_VERSION (5U<<28) /* sort by version */
120static const int SORT_DIR = 9; /* sort by file or directory */ 126#define SORT_EXT (6U<<28) /* sort by file name extension */
127#define SORT_DIR (7U<<28) /* sort by file or directory */
128
129#define SORT_MASK (7U<<28)
121#endif 130#endif
122 131
123#ifdef CONFIG_FEATURE_LS_TIMESTAMPS 132#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
124/* which of the three times will be used */ 133/* which of the three times will be used */
125static const int TIME_MOD = 0; 134#define TIME_MOD 0
126static const int TIME_CHANGE = 1; 135#define TIME_CHANGE (1U<<23)
127static const int TIME_ACCESS = 2; 136#define TIME_ACCESS (1U<<24)
137
138#define TIME_MASK (3U<<23)
139#endif
140
141#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
142#define FOLLOW_LINKS (1U<<25)
143#endif
144#ifdef CONFIG_FEATURE_HUMAN_READABLE
145#define LS_DISP_HR (1U<<26)
128#endif 146#endif
129 147
130#define LIST_SHORT (LIST_FILENAME) 148#define LIST_SHORT (LIST_FILENAME)
@@ -133,9 +151,9 @@ static const int TIME_ACCESS = 2;
133 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK) 151 LIST_DATE_TIME | LIST_FILENAME | LIST_SYMLINK)
134#define LIST_ILONG (LIST_INO | LIST_LONG) 152#define LIST_ILONG (LIST_INO | LIST_LONG)
135 153
136static const int SPLIT_DIR = 0; 154#define SPLIT_DIR 1
137static const int SPLIT_FILE = 1; 155#define SPLIT_FILE 0
138static const int SPLIT_SUBDIR = 2; 156#define SPLIT_SUBDIR 2
139 157
140#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) 158#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f)
141#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) 159#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)])
@@ -169,20 +187,7 @@ static struct dnode **list_dir(const char *);
169static struct dnode **dnalloc(int); 187static struct dnode **dnalloc(int);
170static int list_single(struct dnode *); 188static int list_single(struct dnode *);
171 189
172static unsigned int disp_opts; 190static unsigned int all_fmt;
173static unsigned int style_fmt;
174static unsigned int list_fmt;
175
176#ifdef CONFIG_FEATURE_LS_SORTFILES
177static unsigned int sort_opts;
178static unsigned int sort_order;
179#endif
180#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
181static unsigned int time_fmt;
182#endif
183#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
184static unsigned int follow_links = FALSE;
185#endif
186 191
187#ifdef CONFIG_FEATURE_AUTOWIDTH 192#ifdef CONFIG_FEATURE_AUTOWIDTH
188static unsigned short terminal_width = TERMINAL_WIDTH; 193static unsigned short terminal_width = TERMINAL_WIDTH;
@@ -194,26 +199,22 @@ static unsigned short tabstops = COLUMN_GAP;
194 199
195static int status = EXIT_SUCCESS; 200static int status = EXIT_SUCCESS;
196 201
197#ifdef CONFIG_FEATURE_HUMAN_READABLE
198static unsigned long ls_disp_hr = 0;
199#endif
200
201static struct dnode *my_stat(char *fullname, char *name) 202static struct dnode *my_stat(char *fullname, char *name)
202{ 203{
203 struct stat dstat; 204 struct stat dstat;
204 struct dnode *cur; 205 struct dnode *cur;
205 206
206#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS 207#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
207 if (follow_links) { 208 if (all_fmt & FOLLOW_LINKS) {
208 if (stat(fullname, &dstat)) { 209 if (stat(fullname, &dstat)) {
209 perror_msg("%s", fullname); 210 bb_perror_msg("%s", fullname);
210 status = EXIT_FAILURE; 211 status = EXIT_FAILURE;
211 return 0; 212 return 0;
212 } 213 }
213 } else 214 } else
214#endif 215#endif
215 if (lstat(fullname, &dstat)) { 216 if (lstat(fullname, &dstat)) {
216 perror_msg("%s", fullname); 217 bb_perror_msg("%s", fullname);
217 status = EXIT_FAILURE; 218 status = EXIT_FAILURE;
218 return 0; 219 return 0;
219 } 220 }
@@ -253,9 +254,9 @@ static char bgcolor(mode_t mode)
253#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR) 254#if defined(CONFIG_FEATURE_LS_FILETYPES) || defined(CONFIG_FEATURE_LS_COLOR)
254static char append_char(mode_t mode) 255static char append_char(mode_t mode)
255{ 256{
256 if (!(list_fmt & LIST_FILETYPE)) 257 if (!(all_fmt & LIST_FILETYPE))
257 return '\0'; 258 return '\0';
258 if ((list_fmt & LIST_EXEC) && S_ISREG(mode) 259 if ((all_fmt & LIST_EXEC) && S_ISREG(mode)
259 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH))) 260 && (mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
260 return '*'; 261 return '*';
261 return APPCHAR(mode); 262 return APPCHAR(mode);
@@ -263,13 +264,11 @@ static char append_char(mode_t mode)
263#endif 264#endif
264 265
265/*----------------------------------------------------------------------*/ 266/*----------------------------------------------------------------------*/
266static int is_subdir(struct dnode *dn)
267{
268 return (S_ISDIR(dn->dstat.st_mode) && strcmp(dn->name, ".") != 0 &&
269 strcmp(dn->name, "..") != 0);
270}
271 267
272static int countdirs(struct dnode **dn, int nfiles) 268#define countdirs(A,B) count_dirs((A), (B), 1)
269#define countsubdirs(A,B) count_dirs((A), (B), 0)
270
271static int count_dirs(struct dnode **dn, int nfiles, int notsubdirs)
273{ 272{
274 int i, dirs; 273 int i, dirs;
275 274
@@ -277,25 +276,17 @@ static int countdirs(struct dnode **dn, int nfiles)
277 return (0); 276 return (0);
278 dirs = 0; 277 dirs = 0;
279 for (i = 0; i < nfiles; i++) { 278 for (i = 0; i < nfiles; i++) {
280 if (S_ISDIR(dn[i]->dstat.st_mode)) 279 if (S_ISDIR(dn[i]->dstat.st_mode)
280 && (notsubdirs
281 || ((dn[i]->name[0] != '.')
282 || (dn[i]->name[1]
283 && ((dn[i]->name[1] != '.')
284 || dn[i]->name[2])))))
281 dirs++; 285 dirs++;
282 } 286 }
283 return (dirs); 287 return (dirs);
284} 288}
285 289
286static int countsubdirs(struct dnode **dn, int nfiles)
287{
288 int i, subdirs;
289
290 if (dn == NULL || nfiles < 1)
291 return 0;
292 subdirs = 0;
293 for (i = 0; i < nfiles; i++)
294 if (is_subdir(dn[i]))
295 subdirs++;
296 return subdirs;
297}
298
299static int countfiles(struct dnode **dnp) 290static int countfiles(struct dnode **dnp)
300{ 291{
301 int nfiles; 292 int nfiles;
@@ -364,18 +355,18 @@ static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
364 355
365 /* copy the entrys into the file or dir array */ 356 /* copy the entrys into the file or dir array */
366 for (d = i = 0; i < nfiles; i++) { 357 for (d = i = 0; i < nfiles; i++) {
367 if (which == SPLIT_DIR) { 358 if (S_ISDIR(dn[i]->dstat.st_mode)) {
368 if (S_ISDIR(dn[i]->dstat.st_mode)) { 359 if (which & (SPLIT_DIR|SPLIT_SUBDIR)) {
369 dnp[d++] = dn[i]; 360 if ((which & SPLIT_DIR)
370 } /* else skip the file */ 361 || ((dn[i]->name[0] != '.')
371 } else if (which == SPLIT_SUBDIR) { 362 || (dn[i]->name[1]
372 if (is_subdir(dn[i])) { 363 && ((dn[i]->name[1] != '.')
373 dnp[d++] = dn[i]; 364 || dn[i]->name[2])))) {
374 } /* else skip the file or dir */ 365 dnp[d++] = dn[i];
375 } else { 366 }
376 if (!(S_ISDIR(dn[i]->dstat.st_mode))) { 367 }
377 dnp[d++] = dn[i]; 368 } else if (!(which & (SPLIT_DIR|SPLIT_SUBDIR))) {
378 } /* else skip the dir */ 369 dnp[d++] = dn[i];
379 } 370 }
380 } 371 }
381 return (dnp); 372 return (dnp);
@@ -385,29 +376,24 @@ static struct dnode **splitdnarray(struct dnode **dn, int nfiles, int which)
385#ifdef CONFIG_FEATURE_LS_SORTFILES 376#ifdef CONFIG_FEATURE_LS_SORTFILES
386static int sortcmp(struct dnode *d1, struct dnode *d2) 377static int sortcmp(struct dnode *d1, struct dnode *d2)
387{ 378{
388 int cmp, dif; 379 unsigned int sort_opts = all_fmt & SORT_MASK;
380 int dif;
389 381
390 cmp = 0; 382 dif = 0; /* assume SORT_NAME */
391 if (sort_opts == SORT_SIZE) { 383 if (sort_opts == SORT_SIZE) {
392 dif = (int) (d1->dstat.st_size - d2->dstat.st_size); 384 dif = (int) (d2->dstat.st_size - d1->dstat.st_size);
393 } else if (sort_opts == SORT_ATIME) { 385 } else if (sort_opts == SORT_ATIME) {
394 dif = (int) (d1->dstat.st_atime - d2->dstat.st_atime); 386 dif = (int) (d2->dstat.st_atime - d1->dstat.st_atime);
395 } else if (sort_opts == SORT_CTIME) { 387 } else if (sort_opts == SORT_CTIME) {
396 dif = (int) (d1->dstat.st_ctime - d2->dstat.st_ctime); 388 dif = (int) (d2->dstat.st_ctime - d1->dstat.st_ctime);
397 } else if (sort_opts == SORT_MTIME) { 389 } else if (sort_opts == SORT_MTIME) {
398 dif = (int) (d1->dstat.st_mtime - d2->dstat.st_mtime); 390 dif = (int) (d2->dstat.st_mtime - d1->dstat.st_mtime);
399 } else if (sort_opts == SORT_DIR) { 391 } else if (sort_opts == SORT_DIR) {
400 dif = S_ISDIR(d1->dstat.st_mode) - S_ISDIR(d2->dstat.st_mode); 392 dif = S_ISDIR(d2->dstat.st_mode) - S_ISDIR(d1->dstat.st_mode);
401 /* } else if (sort_opts == SORT_VERSION) { */ 393 /* } else if (sort_opts == SORT_VERSION) { */
402 /* } else if (sort_opts == SORT_EXT) { */ 394 /* } else if (sort_opts == SORT_EXT) { */
403 } else { /* assume SORT_NAME */
404 dif = 0;
405 } 395 }
406 396
407 if (dif > 0)
408 cmp = -1;
409 if (dif < 0)
410 cmp = 1;
411 if (dif == 0) { 397 if (dif == 0) {
412 /* sort by name- may be a tie_breaker for time or size cmp */ 398 /* sort by name- may be a tie_breaker for time or size cmp */
413#ifdef CONFIG_LOCALE_SUPPORT 399#ifdef CONFIG_LOCALE_SUPPORT
@@ -415,16 +401,12 @@ static int sortcmp(struct dnode *d1, struct dnode *d2)
415#else 401#else
416 dif = strcmp(d1->name, d2->name); 402 dif = strcmp(d1->name, d2->name);
417#endif 403#endif
418 if (dif > 0)
419 cmp = 1;
420 if (dif < 0)
421 cmp = -1;
422 } 404 }
423 405
424 if (sort_order == SORT_REVERSE) { 406 if (all_fmt & SORT_ORDER_REVERSE) {
425 cmp = -1 * cmp; 407 dif = -dif;
426 } 408 }
427 return (cmp); 409 return (dif);
428} 410}
429 411
430/*----------------------------------------------------------------------*/ 412/*----------------------------------------------------------------------*/
@@ -463,20 +445,17 @@ static void showfiles(struct dnode **dn, int nfiles)
463 if (dn == NULL || nfiles < 1) 445 if (dn == NULL || nfiles < 1)
464 return; 446 return;
465 447
466 switch (style_fmt) { 448 if (all_fmt & STYLE_ONE_RECORD_FLAG) {
467 case STYLE_LONG: /* one record per line, extended info */
468 case STYLE_SINGLE: /* one record per line */
469 ncols = 1; 449 ncols = 1;
470 break; 450 } else {
471 default: 451 /* find the longest file name- use that as the column width */
472 /* find the longest file name- use that as the column width */ 452 for (i = 0; i < nfiles; i++) {
473 for (i = 0; i < nfiles; i++) {
474 int len = strlen(dn[i]->name) + 453 int len = strlen(dn[i]->name) +
475 ((list_fmt & LIST_INO) ? 8 : 0) + 454 ((all_fmt & LIST_INO) ? 8 : 0) +
476 ((list_fmt & LIST_BLOCKS) ? 5 : 0); 455 ((all_fmt & LIST_BLOCKS) ? 5 : 0);
477 if (column_width < len) 456 if (column_width < len)
478 column_width = len; 457 column_width = len;
479 } 458 }
480 column_width += tabstops; 459 column_width += tabstops;
481 ncols = (int) (terminal_width / column_width); 460 ncols = (int) (terminal_width / column_width);
482 } 461 }
@@ -494,7 +473,7 @@ static void showfiles(struct dnode **dn, int nfiles)
494 for (nc = 0; nc < ncols; nc++) { 473 for (nc = 0; nc < ncols; nc++) {
495 /* reach into the array based on the column and row */ 474 /* reach into the array based on the column and row */
496 i = (nc * nrows) + row; /* assume display by column */ 475 i = (nc * nrows) + row; /* assume display by column */
497 if (disp_opts & DISP_ROWS) 476 if (all_fmt & DISP_ROWS)
498 i = (row * ncols) + nc; /* display across row */ 477 i = (row * ncols) + nc; /* display across row */
499 if (i < nfiles) { 478 if (i < nfiles) {
500 if (column > 0) { 479 if (column > 0) {
@@ -528,7 +507,7 @@ static void showdirs(struct dnode **dn, int ndirs)
528 return; 507 return;
529 508
530 for (i = 0; i < ndirs; i++) { 509 for (i = 0; i < ndirs; i++) {
531 if (disp_opts & (DISP_DIRNAME | DISP_RECURSIVE)) { 510 if (all_fmt & (DISP_DIRNAME | DISP_RECURSIVE)) {
532 printf("\n%s:\n", dn[i]->fullname); 511 printf("\n%s:\n", dn[i]->fullname);
533 } 512 }
534 subdnp = list_dir(dn[i]->fullname); 513 subdnp = list_dir(dn[i]->fullname);
@@ -540,7 +519,7 @@ static void showdirs(struct dnode **dn, int ndirs)
540#endif 519#endif
541 showfiles(subdnp, nfiles); 520 showfiles(subdnp, nfiles);
542#ifdef CONFIG_FEATURE_LS_RECURSIVE 521#ifdef CONFIG_FEATURE_LS_RECURSIVE
543 if (disp_opts & DISP_RECURSIVE) { 522 if (all_fmt & DISP_RECURSIVE) {
544 /* recursive- list the sub-dirs */ 523 /* recursive- list the sub-dirs */
545 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR); 524 dnd = splitdnarray(subdnp, nfiles, SPLIT_SUBDIR);
546 dndirs = countsubdirs(subdnp, nfiles); 525 dndirs = countsubdirs(subdnp, nfiles);
@@ -573,7 +552,7 @@ static struct dnode **list_dir(const char *path)
573 nfiles = 0; 552 nfiles = 0;
574 dir = opendir(path); 553 dir = opendir(path);
575 if (dir == NULL) { 554 if (dir == NULL) {
576 perror_msg("%s", path); 555 bb_perror_msg("%s", path);
577 status = EXIT_FAILURE; 556 status = EXIT_FAILURE;
578 return (NULL); /* could not open the dir */ 557 return (NULL); /* could not open the dir */
579 } 558 }
@@ -585,9 +564,9 @@ static struct dnode **list_dir(const char *path)
585 if ((entry->d_name[1] == 0 || ( 564 if ((entry->d_name[1] == 0 || (
586 entry->d_name[1] == '.' 565 entry->d_name[1] == '.'
587 && entry->d_name[2] == 0)) 566 && entry->d_name[2] == 0))
588 && !(disp_opts & DISP_DOT)) 567 && !(all_fmt & DISP_DOT))
589 continue; 568 continue;
590 if (!(disp_opts & DISP_HIDDEN)) 569 if (!(all_fmt & DISP_HIDDEN))
591 continue; 570 continue;
592 } 571 }
593 fullname = concat_path_file(path, entry->d_name); 572 fullname = concat_path_file(path, entry->d_name);
@@ -636,9 +615,9 @@ static int list_single(struct dnode *dn)
636 615
637#ifdef CONFIG_FEATURE_LS_TIMESTAMPS 616#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
638 ttime = dn->dstat.st_mtime; /* the default time */ 617 ttime = dn->dstat.st_mtime; /* the default time */
639 if (time_fmt & TIME_ACCESS) 618 if (all_fmt & TIME_ACCESS)
640 ttime = dn->dstat.st_atime; 619 ttime = dn->dstat.st_atime;
641 if (time_fmt & TIME_CHANGE) 620 if (all_fmt & TIME_CHANGE)
642 ttime = dn->dstat.st_ctime; 621 ttime = dn->dstat.st_ctime;
643 filetime = ctime(&ttime); 622 filetime = ctime(&ttime);
644#endif 623#endif
@@ -647,7 +626,7 @@ static int list_single(struct dnode *dn)
647#endif 626#endif
648 627
649 for (i = 0; i <= 31; i++) { 628 for (i = 0; i <= 31; i++) {
650 switch (list_fmt & (1 << i)) { 629 switch (all_fmt & (1 << i)) {
651 case LIST_INO: 630 case LIST_INO:
652 column += printf("%7ld ", (long int) dn->dstat.st_ino); 631 column += printf("%7ld ", (long int) dn->dstat.st_ino);
653 break; 632 break;
@@ -659,7 +638,7 @@ static int list_single(struct dnode *dn)
659#endif 638#endif
660 break; 639 break;
661 case LIST_MODEBITS: 640 case LIST_MODEBITS:
662 column += printf("%-10s ", (char *) mode_string(dn->dstat.st_mode)); 641 column += printf("%-10s ", (char *) bb_mode_string(dn->dstat.st_mode));
663 break; 642 break;
664 case LIST_NLINKS: 643 case LIST_NLINKS:
665 column += printf("%4ld ", (long) dn->dstat.st_nlink); 644 column += printf("%4ld ", (long) dn->dstat.st_nlink);
@@ -683,7 +662,7 @@ static int list_single(struct dnode *dn)
683 (int) MINOR(dn->dstat.st_rdev)); 662 (int) MINOR(dn->dstat.st_rdev));
684 } else { 663 } else {
685#ifdef CONFIG_FEATURE_HUMAN_READABLE 664#ifdef CONFIG_FEATURE_HUMAN_READABLE
686 if (ls_disp_hr == TRUE) { 665 if (all_fmt & LS_DISP_HR) {
687 column += printf("%9s ", 666 column += printf("%9s ",
688 make_human_readable_str(dn->dstat.st_size, 1, 0)); 667 make_human_readable_str(dn->dstat.st_size, 1, 0));
689 } else 668 } else
@@ -700,7 +679,7 @@ static int list_single(struct dnode *dn)
700#ifdef CONFIG_FEATURE_LS_TIMESTAMPS 679#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
701 case LIST_FULLTIME: 680 case LIST_FULLTIME:
702 case LIST_DATE_TIME: 681 case LIST_DATE_TIME:
703 if (list_fmt & LIST_FULLTIME) { 682 if (all_fmt & LIST_FULLTIME) {
704 printf("%24.24s ", filetime); 683 printf("%24.24s ", filetime);
705 column += 25; 684 column += 25;
706 break; 685 break;
@@ -774,47 +753,8 @@ static int list_single(struct dnode *dn)
774} 753}
775 754
776/*----------------------------------------------------------------------*/ 755/*----------------------------------------------------------------------*/
777extern int ls_main(int argc, char **argv)
778{
779 struct dnode **dnf, **dnd;
780 int dnfiles, dndirs;
781 struct dnode *dn, *cur, **dnp;
782 int i, nfiles;
783 int opt;
784 int oi, ac;
785 char **av;
786
787#ifdef CONFIG_FEATURE_AUTOWIDTH
788 struct winsize win = { 0, 0, 0, 0 };
789#endif
790
791 disp_opts = DISP_NORMAL;
792 style_fmt = STYLE_AUTO;
793 list_fmt = LIST_SHORT;
794#ifdef CONFIG_FEATURE_LS_SORTFILES
795 sort_opts = SORT_NAME;
796 sort_order = SORT_FORWARD;
797#endif
798#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
799 time_fmt = TIME_MOD;
800#endif
801#ifdef CONFIG_FEATURE_AUTOWIDTH
802 ioctl(fileno(stdout), TIOCGWINSZ, &win);
803 if (win.ws_col > 0)
804 terminal_width = win.ws_col - 1;
805#endif
806 nfiles = 0;
807 756
808#ifdef CONFIG_FEATURE_LS_COLOR 757static const char ls_opts[] = "1AaCdgilnsx"
809 if (isatty(fileno(stdout)))
810 show_color = 1;
811#endif
812
813 /* process options */
814 while ((opt = getopt(argc, argv, "1AaCdgilnsx"
815#ifdef CONFIG_FEATURE_AUTOWIDTH
816 "T:w:"
817#endif
818#ifdef CONFIG_FEATURE_LS_FILETYPES 758#ifdef CONFIG_FEATURE_LS_FILETYPES
819 "Fp" 759 "Fp"
820#endif 760#endif
@@ -825,7 +765,7 @@ extern int ls_main(int argc, char **argv)
825 "rSvX" 765 "rSvX"
826#endif 766#endif
827#ifdef CONFIG_FEATURE_LS_TIMESTAMPS 767#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
828 "cetu" 768 "ecut"
829#endif 769#endif
830#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS 770#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
831 "L" 771 "L"
@@ -833,141 +773,180 @@ extern int ls_main(int argc, char **argv)
833#ifdef CONFIG_FEATURE_HUMAN_READABLE 773#ifdef CONFIG_FEATURE_HUMAN_READABLE
834 "h" 774 "h"
835#endif 775#endif
836 "k")) > 0) { 776 "k"
837 switch (opt) { 777#ifdef CONFIG_FEATURE_AUTOWIDTH
838 case '1': 778 "T:w:"
839 style_fmt = STYLE_SINGLE;
840 list_fmt = LIST_SHORT;
841 break;
842 case 'A':
843 disp_opts |= DISP_HIDDEN;
844 break;
845 case 'a':
846 disp_opts |= DISP_HIDDEN | DISP_DOT;
847 break;
848 case 'C':
849 style_fmt = STYLE_COLUMNS;
850 list_fmt = LIST_SHORT;
851 break;
852 case 'd':
853 disp_opts |= DISP_NOLIST;
854 break;
855 case 'g': /* ignore -- for ftp servers */
856 break;
857 case 'i':
858 list_fmt |= LIST_INO;
859 break;
860 case 'l':
861 style_fmt = STYLE_LONG;
862 list_fmt |= LIST_LONG;
863#ifdef CONFIG_FEATURE_HUMAN_READABLE
864 ls_disp_hr = FALSE;
865#endif 779#endif
866 break; 780 ;
867 case 'n': 781
868 list_fmt |= LIST_ID_NUMERIC; 782#define LIST_MASK_TRIGGER LIST_SHORT
869 break; 783#define STYLE_MASK_TRIGGER STYLE_MASK
870 case 's': 784#define SORT_MASK_TRIGGER SORT_MASK
871 list_fmt |= LIST_BLOCKS; 785#define DISP_MASK_TRIGGER DISP_ROWS
872 break; 786#define TIME_MASK_TRIGGER TIME_MASK
873 case 'x': 787
874 disp_opts = DISP_ROWS; 788static const unsigned opt_flags[] = {
875 break; 789 LIST_SHORT | STYLE_SINGLE, /* 1 */
790 DISP_HIDDEN, /* A */
791 DISP_HIDDEN | DISP_DOT, /* a */
792 LIST_SHORT | STYLE_COLUMNS, /* C */
793 DISP_NOLIST, /* d */
794 0, /* g - ingored */
795 LIST_INO, /* i */
796 LIST_LONG | STYLE_LONG, /* l - remember LS_DISP_HR in mask! */
797 LIST_ID_NUMERIC, /* n */
798 LIST_BLOCKS, /* s */
799 DISP_ROWS, /* x */
876#ifdef CONFIG_FEATURE_LS_FILETYPES 800#ifdef CONFIG_FEATURE_LS_FILETYPES
877 case 'F': 801 LIST_FILETYPE | LIST_EXEC, /* F */
878 list_fmt |= LIST_FILETYPE | LIST_EXEC; 802 LIST_FILETYPE, /* p */
879 break;
880 case 'p':
881 list_fmt |= LIST_FILETYPE;
882 break;
883#endif 803#endif
884#ifdef CONFIG_FEATURE_LS_RECURSIVE 804#ifdef CONFIG_FEATURE_LS_RECURSIVE
885 case 'R': 805 DISP_RECURSIVE, /* R */
886 disp_opts |= DISP_RECURSIVE;
887 break;
888#endif 806#endif
889#ifdef CONFIG_FEATURE_LS_SORTFILES 807#ifdef CONFIG_FEATURE_LS_SORTFILES
890 case 'r': 808 SORT_ORDER_REVERSE, /* r */
891 sort_order |= SORT_REVERSE; 809 SORT_SIZE, /* S */
892 break; 810 SORT_VERSION, /* v */
893 case 'S': 811 SORT_EXT, /* v */
894 sort_opts = SORT_SIZE;
895 break;
896 case 'v':
897 sort_opts = SORT_VERSION;
898 break;
899 case 'X':
900 sort_opts = SORT_EXT;
901 break;
902#endif 812#endif
903#ifdef CONFIG_FEATURE_LS_TIMESTAMPS 813#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
904 case 'e': 814 LIST_FULLTIME, /* e */
905 list_fmt |= LIST_FULLTIME;
906 break;
907 case 'c':
908 time_fmt = TIME_CHANGE;
909#ifdef CONFIG_FEATURE_LS_SORTFILES 815#ifdef CONFIG_FEATURE_LS_SORTFILES
910 sort_opts = SORT_CTIME; 816 TIME_CHANGE | SORT_CTIME, /* c */
817#else
818 TIME_CHANGE, /* c */
911#endif 819#endif
912 break;
913 case 'u':
914 time_fmt = TIME_ACCESS;
915#ifdef CONFIG_FEATURE_LS_SORTFILES 820#ifdef CONFIG_FEATURE_LS_SORTFILES
916 sort_opts = SORT_ATIME; 821 TIME_ACCESS | SORT_ATIME, /* u */
822#else
823 TIME_ACCESS, /* u */
917#endif 824#endif
918 break;
919 case 't':
920#ifdef CONFIG_FEATURE_LS_SORTFILES 825#ifdef CONFIG_FEATURE_LS_SORTFILES
921 sort_opts = SORT_MTIME; 826 SORT_MTIME, /* t */
827#else
828 0, /* t - ignored -- is this correct? */
922#endif 829#endif
923 break;
924#endif 830#endif
925#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS 831#ifdef CONFIG_FEATURE_LS_FOLLOWLINKS
926 case 'L': 832 FOLLOW_LINKS, /* L */
927 follow_links = TRUE; 833#endif
928 break; 834#ifdef CONFIG_FEATURE_HUMAN_READABLE
835LS_DISP_HR, /* h */
836#endif
837 0, /* k - ingored */
838};
839
840
841/*----------------------------------------------------------------------*/
842
843extern int ls_main(int argc, char **argv)
844{
845 struct dnode **dnf, **dnd;
846 int dnfiles, dndirs;
847 struct dnode *dn, *cur, **dnp;
848 int i, nfiles;
849 int opt;
850 int oi, ac;
851 char **av;
852
853#ifdef CONFIG_FEATURE_AUTOWIDTH
854 struct winsize win = { 0, 0, 0, 0 };
855#endif
856
857 all_fmt = LIST_SHORT | DISP_NORMAL | STYLE_AUTO
858#ifdef CONFIG_FEATURE_LS_TIMESTAMPS
859 | TIME_MOD
860#endif
861#ifdef CONFIG_FEATURE_LS_SORTFILES
862 | SORT_NAME | SORT_ORDER_FORWARD
863#endif
864 ;
865#ifdef CONFIG_FEATURE_AUTOWIDTH
866 ioctl(fileno(stdout), TIOCGWINSZ, &win);
867 if (win.ws_col > 0)
868 terminal_width = win.ws_col - 1;
869#endif
870 nfiles = 0;
871
872#ifdef CONFIG_FEATURE_LS_COLOR
873 if (isatty(fileno(stdout)))
874 show_color = 1;
929#endif 875#endif
876
877 /* process options */
878 while ((opt = getopt(argc, argv, ls_opts)) > 0) {
930#ifdef CONFIG_FEATURE_AUTOWIDTH 879#ifdef CONFIG_FEATURE_AUTOWIDTH
931 case 'T': 880 if (opt == 'T') {
932 tabstops = atoi(optarg); 881 tabstops = atoi(optarg);
933 break; 882 continue;
934 case 'w': 883 }
884 if (opt == 'w') {
935 terminal_width = atoi(optarg); 885 terminal_width = atoi(optarg);
936 break; 886 continue;
887 }
888 if (opt == ':') {
889 goto print_usage_message;
890 }
937#endif 891#endif
892 {
893 unsigned int flags;
894 const char *p = strchr(ls_opts, opt);
895 if (!p) { /* shouldn't be necessary */
896 goto print_usage_message;
897 }
898 flags = opt_flags[(int)(p - ls_opts)];
899 if (flags & LIST_MASK_TRIGGER) {
900 all_fmt &= ~LIST_MASK;
901 }
902 if (flags & STYLE_MASK_TRIGGER) {
903 all_fmt &= ~STYLE_MASK;
904 }
905 if (flags & SORT_MASK_TRIGGER) {
906 all_fmt &= ~SORT_MASK;
907 }
908 if (flags & DISP_MASK_TRIGGER) {
909 all_fmt &= ~DISP_MASK;
910 }
911 if (flags & TIME_MASK_TRIGGER) {
912 all_fmt &= ~TIME_MASK;
913 }
938#ifdef CONFIG_FEATURE_HUMAN_READABLE 914#ifdef CONFIG_FEATURE_HUMAN_READABLE
939 case 'h': 915 if (opt == 'l') {
940 ls_disp_hr = TRUE; 916 all_fmt &= ~LS_DISP_HR;
941 break; 917 }
942#endif 918#endif
943 case 'k': 919 all_fmt |= flags;
944 break;
945 default:
946 goto print_usage_message;
947 } 920 }
948 } 921 }
949 922
923
950 /* sort out which command line options take precedence */ 924 /* sort out which command line options take precedence */
951#ifdef CONFIG_FEATURE_LS_RECURSIVE 925#ifdef CONFIG_FEATURE_LS_RECURSIVE
952 if (disp_opts & DISP_NOLIST) 926 if (all_fmt & DISP_NOLIST)
953 disp_opts &= ~DISP_RECURSIVE; /* no recurse if listing only dir */ 927 all_fmt &= ~DISP_RECURSIVE; /* no recurse if listing only dir */
954#endif 928#endif
955#if defined (CONFIG_FEATURE_LS_TIMESTAMPS) && defined (CONFIG_FEATURE_LS_SORTFILES) 929#if defined (CONFIG_FEATURE_LS_TIMESTAMPS) && defined (CONFIG_FEATURE_LS_SORTFILES)
956 if (time_fmt & TIME_CHANGE) 930 if (all_fmt & TIME_CHANGE)
957 sort_opts = SORT_CTIME; 931 all_fmt = (all_fmt & ~SORT_MASK) | SORT_CTIME;
958 if (time_fmt & TIME_ACCESS) 932 if (all_fmt & TIME_ACCESS)
959 sort_opts = SORT_ATIME; 933 all_fmt = (all_fmt & ~SORT_MASK) | SORT_ATIME;
960#endif 934#endif
961 if (style_fmt != STYLE_LONG) /* only for long list */ 935 if ((all_fmt & STYLE_MASK) != STYLE_LONG) /* only for long list */
962 list_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC); 936 all_fmt &= ~(LIST_ID_NUMERIC|LIST_FULLTIME|LIST_ID_NAME|LIST_ID_NUMERIC);
963#ifdef CONFIG_FEATURE_LS_USERNAME 937#ifdef CONFIG_FEATURE_LS_USERNAME
964 if (style_fmt == STYLE_LONG && (list_fmt & LIST_ID_NUMERIC)) 938 if ((all_fmt & STYLE_MASK) == STYLE_LONG && (all_fmt & LIST_ID_NUMERIC))
965 list_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */ 939 all_fmt &= ~LIST_ID_NAME; /* don't list names if numeric uid */
966#endif 940#endif
967 941
968 /* choose a display format */ 942 /* choose a display format */
969 if (style_fmt == STYLE_AUTO) 943 if ((all_fmt & STYLE_MASK) == STYLE_AUTO)
970 style_fmt = isatty(fileno(stdout)) ? STYLE_COLUMNS : STYLE_SINGLE; 944#if STYLE_AUTO != 0
945 all_fmt = (all_fmt & ~STYLE_MASK)
946 | (isatty(fileno(stdout)) ? STYLE_COLUMNS : STYLE_SINGLE);
947#else
948 all_fmt |= (isatty(fileno(stdout)) ? STYLE_COLUMNS : STYLE_SINGLE);
949#endif
971 950
972 /* 951 /*
973 * when there are no cmd line args we have to supply a default "." arg. 952 * when there are no cmd line args we have to supply a default "." arg.
@@ -979,7 +958,7 @@ extern int ls_main(int argc, char **argv)
979 ac = argc - optind; /* how many cmd line args are left */ 958 ac = argc - optind; /* how many cmd line args are left */
980 if (ac < 1) { 959 if (ac < 1) {
981 av = (char **) xcalloc((size_t) 1, (size_t) (sizeof(char *))); 960 av = (char **) xcalloc((size_t) 1, (size_t) (sizeof(char *)));
982 av[0] = xstrdup("."); 961 av[0] = bb_xstrdup(".");
983 ac = 1; 962 ac = 1;
984 } else { 963 } else {
985 av = (char **) xcalloc((size_t) ac, (size_t) (sizeof(char *))); 964 av = (char **) xcalloc((size_t) ac, (size_t) (sizeof(char *)));
@@ -990,12 +969,12 @@ extern int ls_main(int argc, char **argv)
990 969
991 /* now, everything is in the av array */ 970 /* now, everything is in the av array */
992 if (ac > 1) 971 if (ac > 1)
993 disp_opts |= DISP_DIRNAME; /* 2 or more items? label directories */ 972 all_fmt |= DISP_DIRNAME; /* 2 or more items? label directories */
994 973
995 /* stuff the command line file names into an dnode array */ 974 /* stuff the command line file names into an dnode array */
996 dn = NULL; 975 dn = NULL;
997 for (oi = 0; oi < ac; oi++) { 976 for (oi = 0; oi < ac; oi++) {
998 char *fullname = xstrdup(av[oi]); 977 char *fullname = bb_xstrdup(av[oi]);
999 978
1000 cur = my_stat(fullname, fullname); 979 cur = my_stat(fullname, fullname);
1001 if (!cur) 980 if (!cur)
@@ -1015,7 +994,7 @@ extern int ls_main(int argc, char **argv)
1015 } 994 }
1016 995
1017 996
1018 if (disp_opts & DISP_NOLIST) { 997 if (all_fmt & DISP_NOLIST) {
1019#ifdef CONFIG_FEATURE_LS_SORTFILES 998#ifdef CONFIG_FEATURE_LS_SORTFILES
1020 shellsort(dnp, nfiles); 999 shellsort(dnp, nfiles);
1021#endif 1000#endif
@@ -1042,5 +1021,5 @@ extern int ls_main(int argc, char **argv)
1042 return (status); 1021 return (status);
1043 1022
1044 print_usage_message: 1023 print_usage_message:
1045 show_usage(); 1024 bb_show_usage();
1046} 1025}
diff --git a/coreutils/md5sum.c b/coreutils/md5sum.c
index f5be4486c..c1480cd32 100644
--- a/coreutils/md5sum.c
+++ b/coreutils/md5sum.c
@@ -770,13 +770,13 @@ static int md5_file(const char *filename,
770 have_read_stdin = 1; 770 have_read_stdin = 1;
771 fp = stdin; 771 fp = stdin;
772 } else { 772 } else {
773 fp = wfopen(filename, "r"); 773 fp = bb_wfopen(filename, "r");
774 if (fp == NULL) 774 if (fp == NULL)
775 return FALSE; 775 return FALSE;
776 } 776 }
777 777
778 if (md5_stream(fp, md5_result)) { 778 if (md5_stream(fp, md5_result)) {
779 perror_msg("%s", filename); 779 bb_perror_msg("%s", filename);
780 780
781 if (fp != stdin) 781 if (fp != stdin)
782 fclose(fp); 782 fclose(fp);
@@ -784,7 +784,7 @@ static int md5_file(const char *filename,
784 } 784 }
785 785
786 if (fp != stdin && fclose(fp) == EOF) { 786 if (fp != stdin && fclose(fp) == EOF) {
787 perror_msg("%s", filename); 787 bb_perror_msg("%s", filename);
788 return FALSE; 788 return FALSE;
789 } 789 }
790 790
@@ -805,7 +805,7 @@ static int md5_check(const char *checkfile_name)
805 have_read_stdin = 1; 805 have_read_stdin = 1;
806 checkfile_stream = stdin; 806 checkfile_stream = stdin;
807 } else { 807 } else {
808 checkfile_stream = wfopen(checkfile_name, "r"); 808 checkfile_stream = bb_wfopen(checkfile_name, "r");
809 if (checkfile_stream == NULL) 809 if (checkfile_stream == NULL)
810 return FALSE; 810 return FALSE;
811 } 811 }
@@ -836,7 +836,7 @@ static int md5_check(const char *checkfile_name)
836 if (split_3(line, line_length, &md5num, &filename) 836 if (split_3(line, line_length, &md5num, &filename)
837 || !hex_digits(md5num)) { 837 || !hex_digits(md5num)) {
838 if (warn) { 838 if (warn) {
839 error_msg("%s: %lu: improperly formatted MD5 checksum line", 839 bb_error_msg("%s: %lu: improperly formatted MD5 checksum line",
840 checkfile_name, (unsigned long) line_number); 840 checkfile_name, (unsigned long) line_number);
841 } 841 }
842 } else { 842 } else {
@@ -881,18 +881,18 @@ static int md5_check(const char *checkfile_name)
881 while (!feof(checkfile_stream) && !ferror(checkfile_stream)); 881 while (!feof(checkfile_stream) && !ferror(checkfile_stream));
882 882
883 if (ferror(checkfile_stream)) { 883 if (ferror(checkfile_stream)) {
884 error_msg("%s: read error", checkfile_name); 884 bb_error_msg("%s: read error", checkfile_name);
885 return FALSE; 885 return FALSE;
886 } 886 }
887 887
888 if (checkfile_stream != stdin && fclose(checkfile_stream) == EOF) { 888 if (checkfile_stream != stdin && fclose(checkfile_stream) == EOF) {
889 perror_msg("md5sum: %s", checkfile_name); 889 bb_perror_msg("md5sum: %s", checkfile_name);
890 return FALSE; 890 return FALSE;
891 } 891 }
892 892
893 if (n_properly_formated_lines == 0) { 893 if (n_properly_formated_lines == 0) {
894 /* Warn if no tests are found. */ 894 /* Warn if no tests are found. */
895 error_msg("%s: no properly formatted MD5 checksum lines found", 895 bb_error_msg("%s: no properly formatted MD5 checksum lines found",
896 checkfile_name); 896 checkfile_name);
897 return FALSE; 897 return FALSE;
898 } else { 898 } else {
@@ -901,13 +901,13 @@ static int md5_check(const char *checkfile_name)
901 - n_open_or_read_failures); 901 - n_open_or_read_failures);
902 902
903 if (n_open_or_read_failures > 0) { 903 if (n_open_or_read_failures > 0) {
904 error_msg("WARNING: %d of %d listed files could not be read", 904 bb_error_msg("WARNING: %d of %d listed files could not be read",
905 n_open_or_read_failures, n_properly_formated_lines); 905 n_open_or_read_failures, n_properly_formated_lines);
906 return FALSE; 906 return FALSE;
907 } 907 }
908 908
909 if (n_mismatched_checksums > 0) { 909 if (n_mismatched_checksums > 0) {
910 error_msg("WARNING: %d of %d computed checksums did NOT match", 910 bb_error_msg("WARNING: %d of %d computed checksums did NOT match",
911 n_mismatched_checksums, n_computed_checkums); 911 n_mismatched_checksums, n_computed_checkums);
912 return FALSE; 912 return FALSE;
913 } 913 }
@@ -965,31 +965,31 @@ int md5sum_main(int argc,
965 break; 965 break;
966 966
967 default: 967 default:
968 show_usage(); 968 bb_show_usage();
969 } 969 }
970 } 970 }
971 971
972 if (file_type_specified && do_check) { 972 if (file_type_specified && do_check) {
973 error_msg_and_die("the -b and -t options are meaningless when verifying checksums"); 973 bb_error_msg_and_die("the -b and -t options are meaningless when verifying checksums");
974 } 974 }
975 975
976 if (n_strings > 0 && do_check) { 976 if (n_strings > 0 && do_check) {
977 error_msg_and_die("the -g and -c options are mutually exclusive"); 977 bb_error_msg_and_die("the -g and -c options are mutually exclusive");
978 } 978 }
979 979
980 if (status_only && !do_check) { 980 if (status_only && !do_check) {
981 error_msg_and_die("the -s option is meaningful only when verifying checksums"); 981 bb_error_msg_and_die("the -s option is meaningful only when verifying checksums");
982 } 982 }
983 983
984 if (warn && !do_check) { 984 if (warn && !do_check) {
985 error_msg_and_die("the -w option is meaningful only when verifying checksums"); 985 bb_error_msg_and_die("the -w option is meaningful only when verifying checksums");
986 } 986 }
987 987
988 if (n_strings > 0) { 988 if (n_strings > 0) {
989 size_t i; 989 size_t i;
990 990
991 if (optind < argc) { 991 if (optind < argc) {
992 error_msg_and_die("no files may be specified when using -g"); 992 bb_error_msg_and_die("no files may be specified when using -g");
993 } 993 }
994 for (i = 0; i < n_strings; ++i) { 994 for (i = 0; i < n_strings; ++i) {
995 size_t cnt; 995 size_t cnt;
@@ -1002,7 +1002,7 @@ int md5sum_main(int argc,
1002 } 1002 }
1003 } else if (do_check) { 1003 } else if (do_check) {
1004 if (optind + 1 < argc) { 1004 if (optind + 1 < argc) {
1005 error_msg("only one argument may be specified when using -c"); 1005 bb_error_msg("only one argument may be specified when using -c");
1006 } 1006 }
1007 1007
1008 err = md5_check ((optind == argc) ? "-" : argv[optind]); 1008 err = md5_check ((optind == argc) ? "-" : argv[optind]);
@@ -1060,11 +1060,11 @@ int md5sum_main(int argc,
1060 } 1060 }
1061 1061
1062 if (fclose (stdout) == EOF) { 1062 if (fclose (stdout) == EOF) {
1063 error_msg_and_die("write error"); 1063 bb_error_msg_and_die("write error");
1064 } 1064 }
1065 1065
1066 if (have_read_stdin && fclose (stdin) == EOF) { 1066 if (have_read_stdin && fclose (stdin) == EOF) {
1067 error_msg_and_die("standard input"); 1067 bb_error_msg_and_die(bb_msg_standard_input);
1068 } 1068 }
1069 1069
1070 if (err == 0) 1070 if (err == 0)
diff --git a/coreutils/mkdir.c b/coreutils/mkdir.c
index f003db99f..b018ac181 100644
--- a/coreutils/mkdir.c
+++ b/coreutils/mkdir.c
@@ -20,46 +20,50 @@
20 * 20 *
21 */ 21 */
22 22
23#include <errno.h> 23/* BB_AUDIT SUSv3 compliant */
24#include <getopt.h> 24/* http://www.opengroup.org/onlinepubs/007904975/utilities/mkdir.html */
25#include <sys/stat.h> 25
26#include <sys/types.h> 26/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
27#include <fcntl.h> 27 *
28#include <unistd.h> 28 * Fixed broken permission setting when -p was used; especially in
29#include <stdlib.h> 29 * conjunction with -m.
30#include <string.h> 30 */
31 31
32#include <stdlib.h>
33#include <unistd.h>
32#include "busybox.h" 34#include "busybox.h"
33 35
34extern int mkdir_main (int argc, char **argv) 36extern int mkdir_main (int argc, char **argv)
35{ 37{
36 mode_t mode = -1; 38 mode_t mode = (mode_t)(-1);
39 int status = EXIT_SUCCESS;
37 int flags = 0; 40 int flags = 0;
38 int i, opt; 41 int opt;
39 42
40 while ((opt = getopt (argc, argv, "m:p")) != -1) { 43 while ((opt = getopt (argc, argv, "m:p")) > 0) {
41 switch (opt) { 44 if (opt == 'm') {
42 case 'm':
43 mode = 0777; 45 mode = 0777;
44 if (!parse_mode (optarg, &mode)) { 46 if (!bb_parse_mode (optarg, &mode)) {
45 error_msg_and_die ("invalid mode `%s'", optarg); 47 bb_error_msg_and_die ("invalid mode `%s'", optarg);
46 } 48 }
47 umask(0); 49 } else if (opt == 'p') {
48 break;
49 case 'p':
50 flags |= FILEUTILS_RECUR; 50 flags |= FILEUTILS_RECUR;
51 break; 51 } else {
52 default: 52 bb_show_usage();
53 show_usage ();
54 } 53 }
55 } 54 }
56 55
57 if (optind == argc) 56 if (optind == argc) {
58 show_usage (); 57 bb_show_usage();
59
60 for (i = optind; i < argc; i++) {
61 make_directory (argv[i], mode, flags);
62 } 58 }
63 59
64 return(EXIT_SUCCESS); 60 argv += optind;
61
62 do {
63 if (bb_make_directory(*argv, mode, flags)) {
64 status = EXIT_FAILURE;
65 }
66 } while (*++argv);
67
68 return status;
65} 69}
diff --git a/coreutils/mkfifo.c b/coreutils/mkfifo.c
index ca217fa23..77e0e6dd8 100644
--- a/coreutils/mkfifo.c
+++ b/coreutils/mkfifo.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini mkfifo implementation for busybox 3 * mkfifo implementation for busybox
4 * 4 *
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -20,41 +20,32 @@
20 * 20 *
21 */ 21 */
22 22
23#include <stdio.h> 23/* BB_AUDIT SUSv3 compliant */
24#include <sys/types.h> 24/* http://www.opengroup.org/onlinepubs/007904975/utilities/mkfifo.html */
25#include <errno.h> 25
26#include <stdlib.h> 26#include <stdlib.h>
27#include <unistd.h>
28#include <sys/types.h>
27#include "busybox.h" 29#include "busybox.h"
30#include "libcoreutils/coreutils.h"
28 31
29extern int mkfifo_main(int argc, char **argv) 32extern int mkfifo_main(int argc, char **argv)
30{ 33{
31 char *thisarg; 34 mode_t mode;
32 mode_t mode = 0666; 35 int retval = EXIT_SUCCESS;
33 36
34 argc--; 37 mode = getopt_mk_fifo_nod(argc, argv);
35 argv++;
36 38
37 /* Parse any options */ 39 if (!*(argv += optind)) {
38 while (argc > 1) { 40 bb_show_usage();
39 if (**argv != '-')
40 show_usage();
41 thisarg = *argv;
42 thisarg++;
43 switch (*thisarg) {
44 case 'm':
45 argc--;
46 argv++;
47 parse_mode(*argv, &mode);
48 break;
49 default:
50 show_usage();
51 }
52 argc--;
53 argv++;
54 } 41 }
55 if (argc < 1 || *argv[0] == '-') 42
56 show_usage(); 43 do {
57 if (mkfifo(*argv, mode) < 0) 44 if (mkfifo(*argv, mode) < 0) {
58 perror_msg_and_die("mkfifo"); 45 bb_perror_msg("%s", *argv); /* Avoid multibyte problems. */
59 return EXIT_SUCCESS; 46 retval = EXIT_FAILURE;
47 }
48 } while (*++argv);
49
50 return retval;
60} 51}
diff --git a/coreutils/mknod.c b/coreutils/mknod.c
index 432ec2b25..59294e9cb 100644
--- a/coreutils/mknod.c
+++ b/coreutils/mknod.c
@@ -1,9 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini mknod implementation for busybox 3 * mknod implementation for busybox
4 * 4 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * Copyright (C) 1999-2002 by Erik Andersen <andersee@debian.org>
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -21,73 +20,44 @@
21 * 20 *
22 */ 21 */
23 22
24#include <stdio.h> 23/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
25#include <errno.h> 24
26#include <fcntl.h>
27#include <unistd.h>
28#include <string.h>
29#include <stdlib.h> 25#include <stdlib.h>
30#include <sys/types.h> 26#include <string.h>
31#include <sys/stat.h> 27#include <sys/stat.h>
28#include <unistd.h>
32#include "busybox.h" 29#include "busybox.h"
30#include "libcoreutils/coreutils.h"
31
32static const char modes_chars[] = { 'p', 'c', 'u', 'b', 0, 1, 1, 2 };
33static const mode_t modes_cubp[] = { S_IFIFO, S_IFCHR, S_IFBLK };
33 34
34int mknod_main(int argc, char **argv) 35extern int mknod_main(int argc, char **argv)
35{ 36{
36 char *thisarg; 37 mode_t mode;
37 mode_t mode = 0; 38 dev_t dev;
38 mode_t perm = 0666; 39 const char *name;
39 dev_t dev = (dev_t) 0; 40
41 mode = getopt_mk_fifo_nod(argc, argv);
42 argv += optind;
43 argc -= optind;
40 44
41 argc--; 45 if ((argc >= 2) && ((name = strchr(modes_chars, argv[1][0])) != NULL)) {
42 argv++; 46 mode |= modes_cubp[(int)(name[4])];
43 47
44 /* Parse any options */ 48 dev = 0;
45 while (argc > 1) { 49 if ((*name != 'p') && ((argc -= 2) == 0)) {
46 if (**argv != '-') 50 dev = (bb_xgetularg10_bnd(argv[2], 0, 255) << 8)
47 break; 51 + bb_xgetularg10_bnd(argv[3], 0, 255);
48 thisarg = *argv;
49 thisarg++;
50 switch (*thisarg) {
51 case 'm':
52 argc--;
53 argv++;
54 parse_mode(*argv, &perm);
55 umask(0);
56 break;
57 default:
58 show_usage();
59 } 52 }
60 argc--; 53
61 argv++; 54 if (argc == 2) {
62 } 55 name = *argv;
63 if (argc != 4 && argc != 2) { 56 if (mknod(name, mode, dev) == 0) {
64 show_usage(); 57 return EXIT_SUCCESS;
65 } 58 }
66 switch (argv[1][0]) { 59 bb_perror_msg_and_die("%s", name);
67 case 'c':
68 case 'u':
69 mode = S_IFCHR;
70 break;
71 case 'b':
72 mode = S_IFBLK;
73 break;
74 case 'p':
75 mode = S_IFIFO;
76 if (argc != 2) {
77 show_usage();
78 } 60 }
79 break;
80 default:
81 show_usage();
82 }
83
84 if (mode == S_IFCHR || mode == S_IFBLK) {
85 dev = (dev_t) ((atoi(argv[2]) << 8) | atoi(argv[3]));
86 } 61 }
87 62 bb_show_usage();
88 mode |= perm;
89
90 if (mknod(argv[0], mode, dev) != 0)
91 perror_msg_and_die("%s", argv[0]);
92 return EXIT_SUCCESS;
93} 63}
diff --git a/coreutils/mv.c b/coreutils/mv.c
index 1c4a34788..ae0ee92e4 100644
--- a/coreutils/mv.c
+++ b/coreutils/mv.c
@@ -2,7 +2,6 @@
2/* 2/*
3 * Mini mv implementation for busybox 3 * Mini mv implementation for busybox
4 * 4 *
5 *
6 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> 5 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
@@ -21,148 +20,122 @@
21 * 20 *
22 */ 21 */
23 22
23/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
24 *
25 * Size reduction and improved error checking.
26 */
27
24#include <sys/types.h> 28#include <sys/types.h>
25#include <sys/stat.h> 29#include <sys/stat.h>
26#include <unistd.h> 30#include <unistd.h>
27#include <dirent.h> 31#include <dirent.h>
28#include <errno.h> 32#include <errno.h>
29#include <stdlib.h> 33#include <stdlib.h>
30
31#include "busybox.h" 34#include "busybox.h"
35#include "libcoreutils/coreutils.h"
32 36
33static int flags; 37static const char *fmt = "cannot overwrite %sdirectory with %sdirectory";
34 38
35static int manual_rename(const char *source, const char *dest) 39extern int mv_main(int argc, char **argv)
36{ 40{
37 struct stat source_stat; 41 struct stat source_stat;
38 struct stat dest_stat; 42 struct stat dest_stat;
39 int source_exists = 1; 43 const char *last;
40 int dest_exists = 1; 44 const char *dest;
45 int dest_exists;
46 int source_exists;
47 int opt;
48 int flags = 0;
49 int status = 0;
41 50
42 if (stat(source, &source_stat) < 0) { 51 while ((opt = getopt(argc, argv, "fi")) > 0) {
43 if (errno != ENOENT) { 52 flags &= ~(FILEUTILS_INTERACTIVE | FILEUTILS_FORCE);
44 perror_msg("unable to stat `%s'", source); 53 if (opt == 'i') {
45 return -1; 54 flags |= FILEUTILS_INTERACTIVE;
55 } else if (opt == 'f') {
56 flags |= FILEUTILS_FORCE;
57 } else {
58 bb_show_usage();
46 } 59 }
47 source_exists = 0;
48 } 60 }
49 61
50 if (stat(dest, &dest_stat) < 0) { 62 if (optind + 2 > argc)
51 if (errno != ENOENT) { 63 bb_show_usage();
52 perror_msg("unable to stat `%s'", dest);
53 return -1;
54 }
55 dest_exists = 0;
56 }
57 64
58 if (dest_exists) { 65 last = argv[argc - 1];
59 if (S_ISDIR(dest_stat.st_mode) && 66 argv += optind;
60 (!source_exists || !S_ISDIR(source_stat.st_mode))) {
61 error_msg("cannot overwrite directory with non-directory");
62 return -1;
63 }
64 67
65 if (!S_ISDIR(dest_stat.st_mode) && source_exists && 68 if (optind + 2 == argc) {
66 S_ISDIR(source_stat.st_mode)) { 69 if ((dest_exists = cp_mv_stat(last, &dest_stat)) < 0) {
67 error_msg("cannot overwrite non-directory with directory"); 70 return 1;
68 return -1;
69 } 71 }
70 72
71 if (unlink(dest) < 0) { 73 if (!(dest_exists & 2)) {
72 perror_msg("cannot remove `%s'", dest); 74 dest = last;
73 return -1; 75 goto DO_MOVE;
74 } 76 }
75 } 77 }
78
79 do {
80 dest = concat_path_file(last,
81 bb_get_last_path_component(*argv));
76 82
77 if (copy_file(source, dest, 83 if ((dest_exists = cp_mv_stat(dest, &dest_stat)) < 0) {
78 FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) < 0) 84 goto RET_1;
79 return -1;
80
81 if (remove_file(source, FILEUTILS_RECUR | FILEUTILS_FORCE) < 0)
82 return -1;
83
84 return 0;
85}
86
87static int move_file(const char *source, const char *dest)
88{
89 struct stat dest_stat;
90 int dest_exists = 1;
91
92 if (stat(dest, &dest_stat) < 0) {
93 if (errno != ENOENT) {
94 perror_msg("unable to stat `%s'", dest);
95 return -1;
96 } 85 }
97 dest_exists = 0;
98 }
99 86
100 if (dest_exists && !(flags & FILEUTILS_FORCE) && 87 DO_MOVE:
88
89 if (dest_exists && !(flags & FILEUTILS_FORCE) &&
101 ((access(dest, W_OK) < 0 && isatty(0)) || 90 ((access(dest, W_OK) < 0 && isatty(0)) ||
102 (flags & FILEUTILS_INTERACTIVE))) { 91 (flags & FILEUTILS_INTERACTIVE))) {
103 fprintf(stderr, "mv: overwrite `%s'? ", dest); 92 if (fprintf(stderr, "mv: overwrite `%s'? ", dest) < 0) {
104 if (!ask_confirmation()) 93 goto RET_1; /* Ouch! fprintf failed! */
105 return 0; 94 }
106 } 95 if (!bb_ask_confirmation())
107 96 goto RET_0;
108 if (rename(source, dest) < 0) { 97 }
109 if (errno == EXDEV) 98
110 return manual_rename(source, dest); 99 if (rename(*argv, dest) < 0) {
111 100 if (errno != EXDEV) {
112 perror_msg("unable to rename `%s'", source); 101 bb_perror_msg("unable to rename `%s'", *argv);
113 return -1; 102 } else if ((source_exists = cp_mv_stat(*argv, &source_stat)) >= 0) {
114 } 103 if (dest_exists) {
115 104 if (dest_exists & 2) {
116 return 0; 105 if (!(source_exists & 2)) {
117} 106 bb_error_msg(fmt, "", "non-");
118 107 goto RET_1;
119extern int mv_main(int argc, char **argv) 108 }
120{ 109 } else {
121 int status = 0; 110 if (source_exists & 2) {
122 int opt; 111 bb_error_msg(fmt, "non-", "");
123 int i; 112 goto RET_1;
124 113 }
125 while ((opt = getopt(argc, argv, "fi")) != -1) 114 }
126 switch (opt) { 115 if (unlink(dest) < 0) {
127 case 'f': 116 bb_perror_msg("cannot remove `%s'", dest);
128 flags &= ~FILEUTILS_INTERACTIVE; 117 goto RET_1;
129 flags |= FILEUTILS_FORCE; 118 }
130 break; 119 }
131 case 'i': 120
132 flags &= ~FILEUTILS_FORCE; 121 if ((copy_file(*argv, dest,
133 flags |= FILEUTILS_INTERACTIVE; 122 FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS) >= 0)
134 break; 123 && (remove_file(*argv, FILEUTILS_RECUR | FILEUTILS_FORCE) >= 0)
135 default: 124 ) {
136 show_usage(); 125 goto RET_0;
137 } 126 }
138 127
139 if (optind + 2 > argc) 128 }
140 show_usage(); 129 RET_1:
141 130 status = 1;
142 if (optind + 2 == argc) {
143 struct stat dest_stat;
144 int dest_exists = 1;
145
146 if (stat(argv[optind + 1], &dest_stat) < 0) {
147 if (errno != ENOENT)
148 perror_msg_and_die("unable to stat `%s'", argv[optind + 1]);
149 dest_exists = 0;
150 } 131 }
151 132
152 if (!dest_exists || !S_ISDIR(dest_stat.st_mode)) { 133 RET_0:
153 if (move_file(argv[optind], argv[optind + 1]) < 0) 134 if (dest != last) {
154 status = 1; 135 free((void *) dest);
155 return status;
156 } 136 }
157 } 137
158 138 } while (*++argv != last);
159 for (i = optind; i < argc - 1; i++) { 139
160 char *dest = concat_path_file(argv[argc - 1], 140 exit(status);
161 get_last_path_component(argv[i]));
162 if (move_file(argv[i], dest) < 0)
163 status = 1;
164 free(dest);
165 }
166
167 return status;
168} 141}
diff --git a/coreutils/od.c b/coreutils/od.c
index 6187cadf2..5eaaf5024 100644
--- a/coreutils/od.c
+++ b/coreutils/od.c
@@ -23,22 +23,18 @@
23 */ 23 */
24 24
25#include <ctype.h> 25#include <ctype.h>
26#include <string.h>
26#include <getopt.h> 27#include <getopt.h>
27#include <stdlib.h> 28#include <stdlib.h>
28#include "dump.h"
29#include "busybox.h" 29#include "busybox.h"
30#include "dump.h"
30 31
31extern FS *fshead; /* head of format strings */ 32#define isdecdigit(c) (isdigit)(c)
32extern int blocksize; /* data block size */ 33#define ishexdigit(c) (isxdigit)(c)
33extern int length; /* max bytes to read */
34
35#define ishexdigit(c) \
36 ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'))
37 34
38static void 35static void
39odoffset(int argc, char ***argvp) 36odoffset(int argc, char ***argvp)
40{ 37{
41 extern off_t skip;
42 register char *num, *p; 38 register char *num, *p;
43 int base; 39 int base;
44 char *end; 40 char *end;
@@ -62,13 +58,15 @@ odoffset(int argc, char ***argvp)
62 return; 58 return;
63 } 59 }
64 60
65 if (*p != '+' && (argc < 2 || 61 if ((*p != '+')
66 (!isdigit(p[0]) && (p[0] != 'x' || !ishexdigit(p[1]))))) 62 && (argc < 2
63 || (!isdecdigit(p[0])
64 && ((p[0] != 'x') || !ishexdigit(p[1])))))
67 return; 65 return;
68 66
69 base = 0; 67 base = 0;
70 /* 68 /*
71 * skip over leading '+', 'x[0-9a-fA-f]' or '0x', and 69 * bb_dump_skip over leading '+', 'x[0-9a-fA-f]' or '0x', and
72 * set base. 70 * set base.
73 */ 71 */
74 if (p[0] == '+') 72 if (p[0] == '+')
@@ -81,11 +79,11 @@ odoffset(int argc, char ***argvp)
81 base = 16; 79 base = 16;
82 } 80 }
83 81
84 /* skip over the number */ 82 /* bb_dump_skip over the number */
85 if (base == 16) 83 if (base == 16)
86 for (num = p; ishexdigit(*p); ++p); 84 for (num = p; ishexdigit(*p); ++p);
87 else 85 else
88 for (num = p; isdigit(*p); ++p); 86 for (num = p; isdecdigit(*p); ++p);
89 87
90 /* check for no number */ 88 /* check for no number */
91 if (num == p) 89 if (num == p)
@@ -98,21 +96,23 @@ odoffset(int argc, char ***argvp)
98 base = 10; 96 base = 10;
99 } 97 }
100 98
101 skip = strtol(num, &end, base ? base : 8); 99 bb_dump_skip = strtol(num, &end, base ? base : 8);
102 100
103 /* if end isn't the same as p, we got a non-octal digit */ 101 /* if end isn't the same as p, we got a non-octal digit */
104 if (end != p) 102 if (end != p)
105 skip = 0; 103 bb_dump_skip = 0;
106 else { 104 else {
107 if (*p) { 105 if (*p) {
108 if (*p == 'b') 106 if (*p == 'b') {
109 skip *= 512; 107 bb_dump_skip *= 512;
110 else if (*p == 'B') 108 ++p;
111 skip *= 1024; 109 } else if (*p == 'B') {
112 ++p; 110 bb_dump_skip *= 1024;
111 ++p;
112 }
113 } 113 }
114 if (*p) 114 if (*p)
115 skip = 0; 115 bb_dump_skip = 0;
116 else { 116 else {
117 ++*argvp; 117 ++*argvp;
118 /* 118 /*
@@ -121,117 +121,76 @@ odoffset(int argc, char ***argvp)
121 * but it's easy. 121 * but it's easy.
122 */ 122 */
123#define TYPE_OFFSET 7 123#define TYPE_OFFSET 7
124 if (base == 16) { 124 {
125 fshead->nextfu->fmt[TYPE_OFFSET] = 'x'; 125 char x_or_d;
126 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'x'; 126 if (base == 16) {
127 } else if (base == 10) { 127 x_or_d = 'x';
128 fshead->nextfu->fmt[TYPE_OFFSET] = 'd'; 128 goto DO_X_OR_D;
129 fshead->nextfs->nextfu->fmt[TYPE_OFFSET] = 'd'; 129 }
130 if (base == 10) {
131 x_or_d = 'd';
132 DO_X_OR_D:
133 bb_dump_fshead->nextfu->fmt[TYPE_OFFSET]
134 = bb_dump_fshead->nextfs->nextfu->fmt[TYPE_OFFSET]
135 = x_or_d;
136 }
130 } 137 }
131 } 138 }
132 } 139 }
133} 140}
134 141
135static void odprecede(void) 142static const char * const add_strings[] = {
136{ 143 "16/1 \"%3_u \" \"\\n\"", /* a */
137 static int first = 1; 144 "8/2 \" %06o \" \"\\n\"", /* B, o */
138 145 "16/1 \"%03o \" \"\\n\"", /* b */
139 if (first) { 146 "16/1 \"%3_c \" \"\\n\"", /* c */
140 first = 0; 147 "8/2 \" %05u \" \"\\n\"", /* d */
141 add("\"%07.7_Ao\n\""); 148 "4/4 \" %010u \" \"\\n\"", /* D */
142 add("\"%07.7_ao \""); 149 "2/8 \" %21.14e \" \"\\n\"", /* e (undocumented in od), F */
143 } else 150 "4/4 \" %14.7e \" \"\\n\"", /* f */
144 add("\" \""); 151 "4/4 \" %08x \" \"\\n\"", /* H, X */
145} 152 "8/2 \" %04x \" \"\\n\"", /* h, x */
153 "4/4 \" %11d \" \"\\n\"", /* I, L, l */
154 "8/2 \" %6d \" \"\\n\"", /* i */
155 "4/4 \" %011o \" \"\\n\"", /* O */
156};
157
158static const signed char od_opts[] = "aBbcDdeFfHhIiLlOovXx";
159
160static const signed char od_o2si[] = {
161 0, 1, 2, 3, 5,
162 4, 6, 6, 7, 8,
163 9, 0xa, 0xb, 0xa, 0xa,
164 0xb, 1, -1, 8, 9,
165};
146 166
147int od_main(int argc, char **argv) 167int od_main(int argc, char **argv)
148{ 168{
149 int ch; 169 int ch;
150 extern enum _vflag vflag; 170 bb_dump_vflag = FIRST;
151 vflag = FIRST; 171 bb_dump_length = -1;
152 length = -1; 172 int first = 1;
153 173 signed char *p;
154 while ((ch = getopt(argc, argv, "aBbcDdeFfHhIiLlOoPpswvXx")) != EOF) 174
155 switch (ch) { 175 while ((ch = getopt(argc, argv, od_opts)) > 0) {
156 case 'a': 176 if (((p = strchr(od_opts, ch)) != NULL) && (*p >= 0)) {
157 odprecede(); 177 if (first) {
158 add("16/1 \"%3_u \" \"\\n\""); 178 first = 0;
159 break; 179 bb_dump_add("\"%07.7_Ao\n\"");
160 case 'B': 180 bb_dump_add("\"%07.7_ao \"");
161 case 'o': 181 } else {
162 odprecede(); 182 bb_dump_add("\" \"");
163 add("8/2 \" %06o \" \"\\n\"");
164 break;
165 case 'b':
166 odprecede();
167 add("16/1 \"%03o \" \"\\n\"");
168 break;
169 case 'c':
170 odprecede();
171 add("16/1 \"%3_c \" \"\\n\"");
172 break;
173 case 'd':
174 odprecede();
175 add("8/2 \" %05u \" \"\\n\"");
176 break;
177 case 'D':
178 odprecede();
179 add("4/4 \" %010u \" \"\\n\"");
180 break;
181 case 'e': /* undocumented in od */
182 case 'F':
183 odprecede();
184 add("2/8 \" %21.14e \" \"\\n\"");
185 break;
186
187 case 'f':
188 odprecede();
189 add("4/4 \" %14.7e \" \"\\n\"");
190 break;
191 case 'H':
192 case 'X':
193 odprecede();
194 add("4/4 \" %08x \" \"\\n\"");
195 break;
196 case 'h':
197 case 'x':
198 odprecede();
199 add("8/2 \" %04x \" \"\\n\"");
200 break;
201 case 'I':
202 case 'L':
203 case 'l':
204 odprecede();
205 add("4/4 \" %11d \" \"\\n\"");
206 break;
207 case 'i':
208 odprecede();
209 add("8/2 \" %6d \" \"\\n\"");
210 break;
211 case 'O':
212 odprecede();
213 add("4/4 \" %011o \" \"\\n\"");
214 break;
215 case 'v':
216 vflag = ALL;
217 break;
218 case 'P':
219 case 'p':
220 case 's':
221 case 'w':
222 case '?':
223 default:
224 error_msg("od: od(1) has been deprecated for hexdump(1).\n");
225 if (ch != '?') {
226 error_msg("od: hexdump(1) compatibility doesn't support the -%c option%s\n",
227 ch, ch == 's' ? "; see strings(1)." : ".");
228 } 183 }
229 show_usage(); 184 bb_dump_add(add_strings[od_o2si[(int)(p-od_opts)]]);
185 } else if (ch == 'v') {
186 bb_dump_vflag = ALL;
187 } else { /* P, p, s, w, or other unhandled */
188 bb_show_usage();
230 } 189 }
231 190 }
232 if (!fshead) { 191 if (!bb_dump_fshead) {
233 add("\"%07.7_Ao\n\""); 192 bb_dump_add("\"%07.7_Ao\n\"");
234 add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\""); 193 bb_dump_add("\"%07.7_ao \" 8/2 \"%06o \" \"\\n\"");
235 } 194 }
236 195
237 argc -= optind; 196 argc -= optind;
@@ -239,7 +198,7 @@ int od_main(int argc, char **argv)
239 198
240 odoffset(argc, &argv); 199 odoffset(argc, &argv);
241 200
242 return(dump(argv)); 201 return(bb_dump_dump(argv));
243} 202}
244 203
245/*- 204/*-
diff --git a/coreutils/printf.c b/coreutils/printf.c
index d579a9b4e..9602788de 100644
--- a/coreutils/printf.c
+++ b/coreutils/printf.c
@@ -124,7 +124,7 @@ int printf_main(int argc, char **argv)
124 124
125 exit_status = 0; 125 exit_status = 0;
126 if (argc <= 1 || **(argv + 1) == '-') { 126 if (argc <= 1 || **(argv + 1) == '-') {
127 show_usage(); 127 bb_show_usage();
128 } 128 }
129 129
130 format = argv[1]; 130 format = argv[1];
diff --git a/coreutils/pwd.c b/coreutils/pwd.c
index 9c5d70cef..7e0dc056a 100644
--- a/coreutils/pwd.c
+++ b/coreutils/pwd.c
@@ -20,24 +20,18 @@
20 * 20 *
21 */ 21 */
22 22
23/* getopt not needed */
24
25#include <stdio.h> 23#include <stdio.h>
26#include <dirent.h>
27#include <errno.h>
28#include <unistd.h>
29#include <stdlib.h> 24#include <stdlib.h>
30#include "busybox.h" 25#include "busybox.h"
31 26
32extern int pwd_main(int argc, char **argv) 27extern int pwd_main(int argc, char **argv)
33{ 28{
34 static char *buf; 29 char *buf;
35 30
36 buf = xgetcwd(buf); 31 if ((buf = xgetcwd(NULL)) != NULL) {
37
38 if (buf != NULL) {
39 puts(buf); 32 puts(buf);
40 return EXIT_SUCCESS; 33 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
41 } 34 }
35
42 return EXIT_FAILURE; 36 return EXIT_FAILURE;
43} 37}
diff --git a/coreutils/realpath.c b/coreutils/realpath.c
index f89e0a274..ec98221ad 100644
--- a/coreutils/realpath.c
+++ b/coreutils/realpath.c
@@ -14,17 +14,26 @@
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */ 15 */
16 16
17/* BB_AUDIT SUSv3 N/A -- Apparently a busybox extension. */
18
19/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
20 *
21 * Now does proper error checking on output and returns a failure exit code
22 * if one or more paths can not be resolved.
23 */
24
17#include <limits.h> 25#include <limits.h>
18#include <stdlib.h> 26#include <stdlib.h>
19
20#include "busybox.h" 27#include "busybox.h"
21 28
22int realpath_main(int argc, char **argv) 29int realpath_main(int argc, char **argv)
23{ 30{
31 int retval = EXIT_SUCCESS;
32
24 RESERVE_CONFIG_BUFFER(resolved_path, PATH_MAX); 33 RESERVE_CONFIG_BUFFER(resolved_path, PATH_MAX);
25 34
26 if (--argc == 0) { 35 if (--argc == 0) {
27 show_usage(); 36 bb_show_usage();
28 } 37 }
29 38
30 do { 39 do {
@@ -32,11 +41,14 @@ int realpath_main(int argc, char **argv)
32 if (realpath(*argv, resolved_path) != NULL) { 41 if (realpath(*argv, resolved_path) != NULL) {
33 puts(resolved_path); 42 puts(resolved_path);
34 } else { 43 } else {
35 perror_msg("%s", *argv); 44 retval = EXIT_FAILURE;
45 bb_perror_msg("%s", *argv);
36 } 46 }
37 } while (--argc); 47 } while (--argc);
38 48
49#ifdef CONFIG_FEATURE_CLEAN_UP
39 RELEASE_CONFIG_BUFFER(resolved_path); 50 RELEASE_CONFIG_BUFFER(resolved_path);
51#endif
40 52
41 return(EXIT_SUCCESS); 53 bb_fflush_stdout_and_exit(retval);
42} 54}
diff --git a/coreutils/rm.c b/coreutils/rm.c
index 51c9f4ceb..5489350e5 100644
--- a/coreutils/rm.c
+++ b/coreutils/rm.c
@@ -2,7 +2,6 @@
2/* 2/*
3 * Mini rm implementation for busybox 3 * Mini rm implementation for busybox
4 * 4 *
5 *
6 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> 5 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 * 6 *
8 * 7 *
@@ -22,55 +21,51 @@
22 * 21 *
23 */ 22 */
24 23
25#include <stdio.h> 24/* BB_AUDIT SUSv3 compliant */
26#include <time.h> 25/* http://www.opengroup.org/onlinepubs/007904975/utilities/rm.html */
27#include <utime.h> 26
28#include <dirent.h> 27/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
29#include <errno.h> 28 *
29 * Size reduction.
30 */
31
30#include <unistd.h> 32#include <unistd.h>
31#include <stdlib.h>
32#include <string.h>
33#include <getopt.h>
34#include "busybox.h" 33#include "busybox.h"
35 34
36extern int rm_main(int argc, char **argv) 35extern int rm_main(int argc, char **argv)
37{ 36{
38 int status = 0; 37 int status = 0;
39 int opt;
40 int flags = 0; 38 int flags = 0;
41 int i; 39 int opt;
42 40
43 while ((opt = getopt(argc, argv, "fiRr")) != -1) { 41 while ((opt = getopt(argc, argv, "fiRr")) > 0) {
44 switch (opt) { 42 if ((opt == 'r') || (opt == 'R')) {
45 case 'f':
46 flags &= ~FILEUTILS_INTERACTIVE;
47 flags |= FILEUTILS_FORCE;
48 break;
49 case 'i':
50 flags &= ~FILEUTILS_FORCE;
51 flags |= FILEUTILS_INTERACTIVE;
52 break;
53 case 'R':
54 case 'r':
55 flags |= FILEUTILS_RECUR; 43 flags |= FILEUTILS_RECUR;
56 break; 44 } else {
45 flags &= ~(FILEUTILS_INTERACTIVE | FILEUTILS_FORCE);
46 if (opt == 'i') {
47 flags |= FILEUTILS_INTERACTIVE;
48 } else if (opt == 'f') {
49 flags |= FILEUTILS_FORCE;
50 } else {
51 bb_show_usage();
52 }
57 } 53 }
58 } 54 }
59 55
60 if (!(flags & FILEUTILS_FORCE) && optind == argc) 56 if (*(argv += optind) != NULL) {
61 show_usage(); 57 do {
62 58 const char *base = bb_get_last_path_component(*argv);
63 for (i = optind; i < argc; i++) {
64 char *base = get_last_path_component(argv[i]);
65
66 if (strcmp(base, ".") == 0 || strcmp(base, "..") == 0) {
67 error_msg("cannot remove `.' or `..'");
68 status = 1;
69 continue;
70 }
71 59
72 if (remove_file(argv[i], flags) < 0) 60 if ((base[0] == '.') && (!base[1] || ((base[1] == '.') && !base[2]))) {
61 bb_error_msg("cannot remove `.' or `..'");
62 } else if (remove_file(*argv, flags) >= 0) {
63 continue;
64 }
73 status = 1; 65 status = 1;
66 } while (*++argv);
67 } else if (!(flags & FILEUTILS_FORCE)) {
68 bb_show_usage();
74 } 69 }
75 70
76 return status; 71 return status;
diff --git a/coreutils/rmdir.c b/coreutils/rmdir.c
index 83b27c9bd..3f6037170 100644
--- a/coreutils/rmdir.c
+++ b/coreutils/rmdir.c
@@ -1,9 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini rmdir implementation for busybox 3 * rmdir implementation for busybox
4 * 4 *
5 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -21,76 +20,54 @@
21 * 20 *
22 */ 21 */
23 22
24#include <getopt.h> 23/* BB_AUDIT SUSv3 compliant */
25#include <unistd.h> 24/* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */
26#include <stdlib.h>
27 25
26#include <stdlib.h>
27#include <unistd.h>
28#include <libgen.h>
28#include "busybox.h" 29#include "busybox.h"
29 30
30 31extern int rmdir_main(int argc, char **argv)
31/* Return true if a path is composed of multiple components. */
32
33static int
34multiple_components_p (const char *path)
35{
36 const char *s = path;
37
38 while (s[0] != '\0' && s[0] != '/')
39 s++;
40
41 while (s[0] == '/')
42 s++;
43
44 return (s[0] != '\0');
45}
46
47
48/* Remove a directory. Returns 0 if successful, -1 on error. */
49
50static int
51remove_directory (char *path, int flags)
52{
53 if (!(flags & FILEUTILS_RECUR)) {
54 if (rmdir (path) < 0) {
55 perror_msg ("unable to remove `%s'", path);
56 return -1;
57 }
58 } else {
59 if (remove_directory (path, 0) < 0)
60 return -1;
61
62 if (multiple_components_p (path))
63 if (remove_directory (dirname (path), flags) < 0)
64 return -1;
65 }
66
67 return 0;
68}
69
70
71extern int
72rmdir_main (int argc, char **argv)
73{ 32{
74 int status = EXIT_SUCCESS; 33 int status = EXIT_SUCCESS;
75 int flags = 0; 34 int flags;
76 int i, opt; 35 int do_dot;
36 char *path;
77 37
78 while ((opt = getopt (argc, argv, "p")) != -1) 38 flags = bb_getopt_ulflags(argc, argv, "p");
79 switch (opt) {
80 case 'p':
81 flags |= FILEUTILS_RECUR;
82 break;
83 39
84 default: 40 argv += optind;
85 show_usage ();
86 }
87 41
88 if (optind == argc) 42 if (!*argv) {
89 show_usage(); 43 bb_show_usage();
44 }
90 45
91 for (i = optind; i < argc; i++) 46 do {
92 if (remove_directory (argv[i], flags) < 0) 47 path = *argv;
93 status = EXIT_FAILURE; 48
49 /* Record if the first char was a '.' so we can use dirname later. */
50 do_dot = (*path == '.');
51
52 do {
53 if (rmdir(path) < 0) {
54 bb_perror_msg("`%s'", path); /* Match gnu rmdir msg. */
55 status = EXIT_FAILURE;
56 } else if (flags) {
57 /* Note: path was not empty or null since rmdir succeeded. */
58 path = dirname(path);
59 /* Path is now just the parent component. Note that dirname
60 * returns "." if there are no parents. We must distinguish
61 * this from the case of the original path starting with '.'.
62 */
63 if (do_dot || (*path != '.') || path[1]) {
64 continue;
65 }
66 }
67 break;
68 } while (1);
69
70 } while (*++argv);
94 71
95 return status; 72 return status;
96} 73}
diff --git a/coreutils/sha1sum.c b/coreutils/sha1sum.c
index 03009f76f..d94cb4b53 100644
--- a/coreutils/sha1sum.c
+++ b/coreutils/sha1sum.c
@@ -187,7 +187,7 @@ extern int authenticate(const int argc, char **argv, void (*hash_ptr)(FILE *stre
187 break; 187 break;
188#endif 188#endif
189 default: 189 default:
190 show_usage(); 190 bb_show_usage();
191 } 191 }
192 } 192 }
193 193
@@ -204,7 +204,7 @@ extern int authenticate(const int argc, char **argv, void (*hash_ptr)(FILE *stre
204 hash_ptr(stdin, hashval); 204 hash_ptr(stdin, hashval);
205 print_hash(hash_length, hashval, NULL); 205 print_hash(hash_length, hashval, NULL);
206 } else { 206 } else {
207 FILE *stream = xfopen(argv[i], "r"); 207 FILE *stream = bb_xfopen(argv[i], "r");
208 hash_ptr(stream, hashval); 208 hash_ptr(stream, hashval);
209 fclose(stream); 209 fclose(stream);
210 print_hash(hash_length, hashval, argv[i]); 210 print_hash(hash_length, hashval, argv[i]);
diff --git a/coreutils/sleep.c b/coreutils/sleep.c
index 7bc98d8e8..506192dd3 100644
--- a/coreutils/sleep.c
+++ b/coreutils/sleep.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini sleep implementation for busybox 3 * sleep implementation for busybox
4 * 4 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -20,18 +20,67 @@
20 * 20 *
21 */ 21 */
22 22
23#include <stdio.h> 23/* BB_AUDIT SUSv3 compliant */
24#include <unistd.h> 24/* BB_AUDIT GNU issues -- fancy version matches except args must be ints. */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/sleep.html */
26
27/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
28 *
29 * Rewritten to do proper arg and error checking.
30 * Also, added a 'fancy' configuration to accept multiple args with
31 * time suffixes for seconds, minutes, hours, and days.
32 */
33
25#include <stdlib.h> 34#include <stdlib.h>
35#include <limits.h>
36#include <unistd.h>
26#include "busybox.h" 37#include "busybox.h"
27 38
39#ifdef CONFIG_FEATURE_FANCY_SLEEP
40static const struct suffix_mult sleep_suffixes[] = {
41 { "s", 1 },
42 { "m", 60 },
43 { "h", 60*60 },
44 { "d", 24*60*60 },
45 { NULL, 0 }
46};
47#endif
48
28extern int sleep_main(int argc, char **argv) 49extern int sleep_main(int argc, char **argv)
29{ 50{
30 if ((argc < 2) || (**(argv + 1) == '-')) { 51 unsigned int duration;
31 show_usage(); 52
53#ifdef CONFIG_FEATURE_FANCY_SLEEP
54
55 if (argc < 2) {
56 bb_show_usage();
57 }
58
59 ++argv;
60 duration = 0;
61 do {
62 duration += bb_xgetularg_bnd_sfx(*argv, 10,
63 0, UINT_MAX-duration,
64 sleep_suffixes);
65 } while (*++argv);
66
67#else /* CONFIG_FEATURE_FANCY_SLEEP */
68
69 if (argc != 2) {
70 bb_show_usage();
71 }
72
73#if UINT_MAX == ULONG_MAX
74 duration = bb_xgetularg10(argv[1]);
75#else
76 duration = bb_xgetularg10_bnd(argv[1], 0, UINT_MAX);
77#endif
78
79#endif /* CONFIG_FEATURE_FANCY_SLEEP */
80
81 if (sleep(duration)) {
82 bb_perror_nomsg_and_die();
32 } 83 }
33 84
34 if (sleep(atoi(*(++argv))) != 0)
35 perror_msg_and_die("sleep");
36 return EXIT_SUCCESS; 85 return EXIT_SUCCESS;
37} 86}
diff --git a/coreutils/sort.c b/coreutils/sort.c
index fc12dfb01..8cc4d8886 100644
--- a/coreutils/sort.c
+++ b/coreutils/sort.c
@@ -2,7 +2,6 @@
2/* 2/*
3 * Mini sort implementation for busybox 3 * Mini sort implementation for busybox
4 * 4 *
5 *
6 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> 5 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
@@ -21,10 +20,20 @@
21 * 20 *
22 */ 21 */
23 22
24#include <getopt.h> 23/* BB_AUDIT SUSv3 _NOT_ compliant -- a number of options are not supported. */
25#include <string.h> 24/* http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html */
25
26/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
27 *
28 * Now does proper error checking on i/o. Plus some space savings.
29 */
30
31#include <stdio.h>
26#include <stdlib.h> 32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
27#include "busybox.h" 35#include "busybox.h"
36#include "libcoreutils/coreutils.h"
28 37
29static int compare_ascii(const void *x, const void *y) 38static int compare_ascii(const void *x, const void *y)
30{ 39{
@@ -41,66 +50,51 @@ int sort_main(int argc, char **argv)
41{ 50{
42 FILE *fp; 51 FILE *fp;
43 char *line, **lines = NULL; 52 char *line, **lines = NULL;
44 int i, opt, nlines = 0; 53 int i, nlines = 0, inc;
45 int (*compare)(const void *, const void *) = compare_ascii; 54 int (*compare)(const void *, const void *) = compare_ascii;
46#ifdef CONFIG_FEATURE_SORT_REVERSE
47 int reverse = FALSE;
48#endif
49#ifdef CONFIG_FEATURE_SORT_UNIQUE
50 int unique = FALSE;
51#endif
52 55
53 while ((opt = getopt(argc, argv, "nru")) != -1) { 56 int flags;
54 switch (opt) { 57
55 case 'n': 58 bb_default_error_retval = 2;
56 compare = compare_numeric; 59
57 break; 60 flags = bb_getopt_ulflags(argc, argv, "nru");
58#ifdef CONFIG_FEATURE_SORT_REVERSE 61 if (flags & 1) {
59 case 'r': 62 compare = compare_numeric;
60 reverse = TRUE;
61 break;
62#endif
63#ifdef CONFIG_FEATURE_SORT_UNIQUE
64 case 'u':
65 unique = TRUE;
66 break;
67#endif
68 default:
69 show_usage();
70 }
71 } 63 }
72 64
73 /* read the input */ 65 argv += optind;
74 for (i = optind; i == optind || i < argc; i++) { 66 if (!*argv) {
75 if (argv[i] == NULL) 67 *--argv = "-";
76 fp = stdin; 68 }
77 else
78 fp = xfopen(argv[i], "r");
79 69
80 while ((line = get_line_from_file(fp)) != NULL) { 70 do {
71 fp = xgetoptfile_sort_uniq(argv, "r");
72 while ((line = bb_get_chomped_line_from_file(fp)) != NULL) {
81 lines = xrealloc(lines, sizeof(char *) * (nlines + 1)); 73 lines = xrealloc(lines, sizeof(char *) * (nlines + 1));
82 chomp(line);
83 lines[nlines++] = line; 74 lines[nlines++] = line;
84 } 75 }
85 } 76 bb_xferror(fp, *argv);
77 bb_fclose_nonstdin(fp);
78 } while (*++argv);
86 79
87 /* sort it */ 80 /* sort it */
88 qsort(lines, nlines, sizeof(char *), compare); 81 qsort(lines, nlines, sizeof(char *), compare);
89 82
90 /* print it */ 83 /* print it */
91#ifdef CONFIG_FEATURE_SORT_REVERSE 84 i = 0;
92 if (reverse) { 85 --nlines;
93 for (i = --nlines; 0 <= i; i--) 86 if ((inc = 1 - (flags & 2)) < 0) { /* reverse */
94#ifdef CONFIG_FEATURE_SORT_UNIQUE 87 i = nlines;
95 if((!unique) || (i == nlines) || (strcmp(lines[i + 1], lines[i]))) 88 }
96#endif 89 flags &= 4;
97 puts(lines[i]); 90
98 } else 91 while (nlines >= 0) {
99#endif 92 if (!flags || !nlines || strcmp(lines[i+inc], lines[i])) {
100 for (i = 0; i < nlines; i++) 93 puts(lines[i]);
101#ifdef CONFIG_FEATURE_SORT_UNIQUE 94 }
102 if((!unique) || (!i) || (strcmp(lines[i - 1], lines[i]))) 95 i += inc;
103#endif 96 --nlines;
104 puts(lines[i]); 97 }
105 return EXIT_SUCCESS; 98
99 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
106} 100}
diff --git a/coreutils/stty.c b/coreutils/stty.c
index a67a17c0f..a3a98d9ef 100644
--- a/coreutils/stty.c
+++ b/coreutils/stty.c
@@ -30,9 +30,9 @@
30 30
31//#define TEST 31//#define TEST
32 32
33#include <stddef.h>
33#include <termios.h> 34#include <termios.h>
34#include <sys/ioctl.h> 35#include <sys/ioctl.h>
35#include <getopt.h>
36 36
37#include <sys/param.h> 37#include <sys/param.h>
38#include <unistd.h> 38#include <unistd.h>
@@ -155,13 +155,10 @@ enum speed_setting {
155 input_speed, output_speed, both_speeds 155 input_speed, output_speed, both_speeds
156}; 156};
157 157
158/* What to output and how. */
159enum output_type {
160 changed, all, recoverable /* Default, -a, -g. */
161};
162
163/* Which member(s) of `struct termios' a mode uses. */ 158/* Which member(s) of `struct termios' a mode uses. */
164enum mode_type { 159enum mode_type {
160 /* Do NOT change the order or values, as mode_type_flag()
161 * depends on them. */
165 control, input, output, local, combination 162 control, input, output, local, combination
166}; 163};
167 164
@@ -199,156 +196,159 @@ static const char stty_dec [] = "dec";
199/* Each mode. */ 196/* Each mode. */
200struct mode_info { 197struct mode_info {
201 const char *name; /* Name given on command line. */ 198 const char *name; /* Name given on command line. */
202 enum mode_type type; /* Which structure element to change. */ 199 /* enum mode_type type; */
200 char type; /* Which structure element to change. */
203 char flags; /* Setting and display options. */ 201 char flags; /* Setting and display options. */
202 unsigned short mask; /* Other bits to turn off for this mode. */
204 unsigned long bits; /* Bits to set for this mode. */ 203 unsigned long bits; /* Bits to set for this mode. */
205 unsigned long mask; /* Other bits to turn off for this mode. */
206}; 204};
207 205
206#define MI_ENTRY(N,T,F,B,M) { N, T, F, M, B }
207
208static const struct mode_info mode_info[] = { 208static const struct mode_info mode_info[] = {
209 {"parenb", control, REV, PARENB, 0 }, 209 MI_ENTRY("parenb", control, REV, PARENB, 0 ),
210 {"parodd", control, REV, PARODD, 0 }, 210 MI_ENTRY("parodd", control, REV, PARODD, 0 ),
211 {"cs5", control, 0, CS5, CSIZE}, 211 MI_ENTRY("cs5", control, 0, CS5, CSIZE),
212 {"cs6", control, 0, CS6, CSIZE}, 212 MI_ENTRY("cs6", control, 0, CS6, CSIZE),
213 {"cs7", control, 0, CS7, CSIZE}, 213 MI_ENTRY("cs7", control, 0, CS7, CSIZE),
214 {"cs8", control, 0, CS8, CSIZE}, 214 MI_ENTRY("cs8", control, 0, CS8, CSIZE),
215 {"hupcl", control, REV, HUPCL, 0 }, 215 MI_ENTRY("hupcl", control, REV, HUPCL, 0 ),
216 {"hup", control, REV | OMIT, HUPCL, 0 }, 216 MI_ENTRY("hup", control, REV | OMIT, HUPCL, 0 ),
217 {"cstopb", control, REV, CSTOPB, 0 }, 217 MI_ENTRY("cstopb", control, REV, CSTOPB, 0 ),
218 {"cread", control, SANE_SET | REV, CREAD, 0 }, 218 MI_ENTRY("cread", control, SANE_SET | REV, CREAD, 0 ),
219 {"clocal", control, REV, CLOCAL, 0 }, 219 MI_ENTRY("clocal", control, REV, CLOCAL, 0 ),
220#ifdef CRTSCTS 220#ifdef CRTSCTS
221 {"crtscts", control, REV, CRTSCTS, 0 }, 221 MI_ENTRY("crtscts", control, REV, CRTSCTS, 0 ),
222#endif 222#endif
223 {"ignbrk", input, SANE_UNSET | REV, IGNBRK, 0 }, 223 MI_ENTRY("ignbrk", input, SANE_UNSET | REV, IGNBRK, 0 ),
224 {"brkint", input, SANE_SET | REV, BRKINT, 0 }, 224 MI_ENTRY("brkint", input, SANE_SET | REV, BRKINT, 0 ),
225 {"ignpar", input, REV, IGNPAR, 0 }, 225 MI_ENTRY("ignpar", input, REV, IGNPAR, 0 ),
226 {"parmrk", input, REV, PARMRK, 0 }, 226 MI_ENTRY("parmrk", input, REV, PARMRK, 0 ),
227 {"inpck", input, REV, INPCK, 0 }, 227 MI_ENTRY("inpck", input, REV, INPCK, 0 ),
228 {"istrip", input, REV, ISTRIP, 0 }, 228 MI_ENTRY("istrip", input, REV, ISTRIP, 0 ),
229 {"inlcr", input, SANE_UNSET | REV, INLCR, 0 }, 229 MI_ENTRY("inlcr", input, SANE_UNSET | REV, INLCR, 0 ),
230 {"igncr", input, SANE_UNSET | REV, IGNCR, 0 }, 230 MI_ENTRY("igncr", input, SANE_UNSET | REV, IGNCR, 0 ),
231 {"icrnl", input, SANE_SET | REV, ICRNL, 0 }, 231 MI_ENTRY("icrnl", input, SANE_SET | REV, ICRNL, 0 ),
232 {"ixon", input, REV, IXON, 0 }, 232 MI_ENTRY("ixon", input, REV, IXON, 0 ),
233 {"ixoff", input, SANE_UNSET | REV, IXOFF, 0 }, 233 MI_ENTRY("ixoff", input, SANE_UNSET | REV, IXOFF, 0 ),
234 {"tandem", input, REV | OMIT, IXOFF, 0 }, 234 MI_ENTRY("tandem", input, REV | OMIT, IXOFF, 0 ),
235#ifdef IUCLC 235#ifdef IUCLC
236 {"iuclc", input, SANE_UNSET | REV, IUCLC, 0 }, 236 MI_ENTRY("iuclc", input, SANE_UNSET | REV, IUCLC, 0 ),
237#endif 237#endif
238#ifdef IXANY 238#ifdef IXANY
239 {"ixany", input, SANE_UNSET | REV, IXANY, 0 }, 239 MI_ENTRY("ixany", input, SANE_UNSET | REV, IXANY, 0 ),
240#endif 240#endif
241#ifdef IMAXBEL 241#ifdef IMAXBEL
242 {"imaxbel", input, SANE_SET | REV, IMAXBEL, 0 }, 242 MI_ENTRY("imaxbel", input, SANE_SET | REV, IMAXBEL, 0 ),
243#endif 243#endif
244 {"opost", output, SANE_SET | REV, OPOST, 0 }, 244 MI_ENTRY("opost", output, SANE_SET | REV, OPOST, 0 ),
245#ifdef OLCUC 245#ifdef OLCUC
246 {"olcuc", output, SANE_UNSET | REV, OLCUC, 0 }, 246 MI_ENTRY("olcuc", output, SANE_UNSET | REV, OLCUC, 0 ),
247#endif 247#endif
248#ifdef OCRNL 248#ifdef OCRNL
249 {"ocrnl", output, SANE_UNSET | REV, OCRNL, 0 }, 249 MI_ENTRY("ocrnl", output, SANE_UNSET | REV, OCRNL, 0 ),
250#endif 250#endif
251#ifdef ONLCR 251#ifdef ONLCR
252 {"onlcr", output, SANE_SET | REV, ONLCR, 0 }, 252 MI_ENTRY("onlcr", output, SANE_SET | REV, ONLCR, 0 ),
253#endif 253#endif
254#ifdef ONOCR 254#ifdef ONOCR
255 {"onocr", output, SANE_UNSET | REV, ONOCR, 0 }, 255 MI_ENTRY("onocr", output, SANE_UNSET | REV, ONOCR, 0 ),
256#endif 256#endif
257#ifdef ONLRET 257#ifdef ONLRET
258 {"onlret", output, SANE_UNSET | REV, ONLRET, 0 }, 258 MI_ENTRY("onlret", output, SANE_UNSET | REV, ONLRET, 0 ),
259#endif 259#endif
260#ifdef OFILL 260#ifdef OFILL
261 {"ofill", output, SANE_UNSET | REV, OFILL, 0 }, 261 MI_ENTRY("ofill", output, SANE_UNSET | REV, OFILL, 0 ),
262#endif 262#endif
263#ifdef OFDEL 263#ifdef OFDEL
264 {"ofdel", output, SANE_UNSET | REV, OFDEL, 0 }, 264 MI_ENTRY("ofdel", output, SANE_UNSET | REV, OFDEL, 0 ),
265#endif 265#endif
266#ifdef NLDLY 266#ifdef NLDLY
267 {"nl1", output, SANE_UNSET, NL1, NLDLY}, 267 MI_ENTRY("nl1", output, SANE_UNSET, NL1, NLDLY),
268 {"nl0", output, SANE_SET, NL0, NLDLY}, 268 MI_ENTRY("nl0", output, SANE_SET, NL0, NLDLY),
269#endif 269#endif
270#ifdef CRDLY 270#ifdef CRDLY
271 {"cr3", output, SANE_UNSET, CR3, CRDLY}, 271 MI_ENTRY("cr3", output, SANE_UNSET, CR3, CRDLY),
272 {"cr2", output, SANE_UNSET, CR2, CRDLY}, 272 MI_ENTRY("cr2", output, SANE_UNSET, CR2, CRDLY),
273 {"cr1", output, SANE_UNSET, CR1, CRDLY}, 273 MI_ENTRY("cr1", output, SANE_UNSET, CR1, CRDLY),
274 {"cr0", output, SANE_SET, CR0, CRDLY}, 274 MI_ENTRY("cr0", output, SANE_SET, CR0, CRDLY),
275#endif 275#endif
276 276
277#ifdef TABDLY 277#ifdef TABDLY
278 {"tab3", output, SANE_UNSET, TAB3, TABDLY}, 278 MI_ENTRY("tab3", output, SANE_UNSET, TAB3, TABDLY),
279 {"tab2", output, SANE_UNSET, TAB2, TABDLY}, 279 MI_ENTRY("tab2", output, SANE_UNSET, TAB2, TABDLY),
280 {"tab1", output, SANE_UNSET, TAB1, TABDLY}, 280 MI_ENTRY("tab1", output, SANE_UNSET, TAB1, TABDLY),
281 {"tab0", output, SANE_SET, TAB0, TABDLY}, 281 MI_ENTRY("tab0", output, SANE_SET, TAB0, TABDLY),
282#else 282#else
283# ifdef OXTABS 283# ifdef OXTABS
284 {"tab3", output, SANE_UNSET, OXTABS, 0 }, 284 MI_ENTRY("tab3", output, SANE_UNSET, OXTABS, 0 ),
285# endif 285# endif
286#endif 286#endif
287 287
288#ifdef BSDLY 288#ifdef BSDLY
289 {"bs1", output, SANE_UNSET, BS1, BSDLY}, 289 MI_ENTRY("bs1", output, SANE_UNSET, BS1, BSDLY),
290 {"bs0", output, SANE_SET, BS0, BSDLY}, 290 MI_ENTRY("bs0", output, SANE_SET, BS0, BSDLY),
291#endif 291#endif
292#ifdef VTDLY 292#ifdef VTDLY
293 {"vt1", output, SANE_UNSET, VT1, VTDLY}, 293 MI_ENTRY("vt1", output, SANE_UNSET, VT1, VTDLY),
294 {"vt0", output, SANE_SET, VT0, VTDLY}, 294 MI_ENTRY("vt0", output, SANE_SET, VT0, VTDLY),
295#endif 295#endif
296#ifdef FFDLY 296#ifdef FFDLY
297 {"ff1", output, SANE_UNSET, FF1, FFDLY}, 297 MI_ENTRY("ff1", output, SANE_UNSET, FF1, FFDLY),
298 {"ff0", output, SANE_SET, FF0, FFDLY}, 298 MI_ENTRY("ff0", output, SANE_SET, FF0, FFDLY),
299#endif 299#endif
300 {"isig", local, SANE_SET | REV, ISIG, 0 }, 300 MI_ENTRY("isig", local, SANE_SET | REV, ISIG, 0 ),
301 {"icanon", local, SANE_SET | REV, ICANON, 0 }, 301 MI_ENTRY("icanon", local, SANE_SET | REV, ICANON, 0 ),
302#ifdef IEXTEN 302#ifdef IEXTEN
303 {"iexten", local, SANE_SET | REV, IEXTEN, 0 }, 303 MI_ENTRY("iexten", local, SANE_SET | REV, IEXTEN, 0 ),
304#endif 304#endif
305 {"echo", local, SANE_SET | REV, ECHO, 0 }, 305 MI_ENTRY("echo", local, SANE_SET | REV, ECHO, 0 ),
306 {"echoe", local, SANE_SET | REV, ECHOE, 0 }, 306 MI_ENTRY("echoe", local, SANE_SET | REV, ECHOE, 0 ),
307 {"crterase", local, REV | OMIT, ECHOE, 0 }, 307 MI_ENTRY("crterase", local, REV | OMIT, ECHOE, 0 ),
308 {"echok", local, SANE_SET | REV, ECHOK, 0 }, 308 MI_ENTRY("echok", local, SANE_SET | REV, ECHOK, 0 ),
309 {"echonl", local, SANE_UNSET | REV, ECHONL, 0 }, 309 MI_ENTRY("echonl", local, SANE_UNSET | REV, ECHONL, 0 ),
310 {"noflsh", local, SANE_UNSET | REV, NOFLSH, 0 }, 310 MI_ENTRY("noflsh", local, SANE_UNSET | REV, NOFLSH, 0 ),
311#ifdef XCASE 311#ifdef XCASE
312 {"xcase", local, SANE_UNSET | REV, XCASE, 0 }, 312 MI_ENTRY("xcase", local, SANE_UNSET | REV, XCASE, 0 ),
313#endif 313#endif
314#ifdef TOSTOP 314#ifdef TOSTOP
315 {"tostop", local, SANE_UNSET | REV, TOSTOP, 0 }, 315 MI_ENTRY("tostop", local, SANE_UNSET | REV, TOSTOP, 0 ),
316#endif 316#endif
317#ifdef ECHOPRT 317#ifdef ECHOPRT
318 {"echoprt", local, SANE_UNSET | REV, ECHOPRT, 0 }, 318 MI_ENTRY("echoprt", local, SANE_UNSET | REV, ECHOPRT, 0 ),
319 {"prterase", local, REV | OMIT, ECHOPRT, 0 }, 319 MI_ENTRY("prterase", local, REV | OMIT, ECHOPRT, 0 ),
320#endif 320#endif
321#ifdef ECHOCTL 321#ifdef ECHOCTL
322 {"echoctl", local, SANE_SET | REV, ECHOCTL, 0 }, 322 MI_ENTRY("echoctl", local, SANE_SET | REV, ECHOCTL, 0 ),
323 {"ctlecho", local, REV | OMIT, ECHOCTL, 0 }, 323 MI_ENTRY("ctlecho", local, REV | OMIT, ECHOCTL, 0 ),
324#endif 324#endif
325#ifdef ECHOKE 325#ifdef ECHOKE
326 {"echoke", local, SANE_SET | REV, ECHOKE, 0 }, 326 MI_ENTRY("echoke", local, SANE_SET | REV, ECHOKE, 0 ),
327 {"crtkill", local, REV | OMIT, ECHOKE, 0 }, 327 MI_ENTRY("crtkill", local, REV | OMIT, ECHOKE, 0 ),
328#endif 328#endif
329 {evenp, combination, REV | OMIT, 0, 0 }, 329 MI_ENTRY(evenp, combination, REV | OMIT, 0, 0 ),
330 {parity, combination, REV | OMIT, 0, 0 }, 330 MI_ENTRY(parity, combination, REV | OMIT, 0, 0 ),
331 {stty_oddp, combination, REV | OMIT, 0, 0 }, 331 MI_ENTRY(stty_oddp, combination, REV | OMIT, 0, 0 ),
332 {stty_nl, combination, REV | OMIT, 0, 0 }, 332 MI_ENTRY(stty_nl, combination, REV | OMIT, 0, 0 ),
333 {stty_ek, combination, OMIT, 0, 0 }, 333 MI_ENTRY(stty_ek, combination, OMIT, 0, 0 ),
334 {stty_sane, combination, OMIT, 0, 0 }, 334 MI_ENTRY(stty_sane, combination, OMIT, 0, 0 ),
335 {cooked, combination, REV | OMIT, 0, 0 }, 335 MI_ENTRY(cooked, combination, REV | OMIT, 0, 0 ),
336 {raw, combination, REV | OMIT, 0, 0 }, 336 MI_ENTRY(raw, combination, REV | OMIT, 0, 0 ),
337 {stty_pass8, combination, REV | OMIT, 0, 0 }, 337 MI_ENTRY(stty_pass8, combination, REV | OMIT, 0, 0 ),
338 {litout, combination, REV | OMIT, 0, 0 }, 338 MI_ENTRY(litout, combination, REV | OMIT, 0, 0 ),
339 {cbreak, combination, REV | OMIT, 0, 0 }, 339 MI_ENTRY(cbreak, combination, REV | OMIT, 0, 0 ),
340#ifdef IXANY 340#ifdef IXANY
341 {decctlq, combination, REV | OMIT, 0, 0 }, 341 MI_ENTRY(decctlq, combination, REV | OMIT, 0, 0 ),
342#endif 342#endif
343#if defined (TABDLY) || defined (OXTABS) 343#if defined (TABDLY) || defined (OXTABS)
344 {stty_tabs, combination, REV | OMIT, 0, 0 }, 344 MI_ENTRY(stty_tabs, combination, REV | OMIT, 0, 0 ),
345#endif 345#endif
346#if defined(XCASE) && defined(IUCLC) && defined(OLCUC) 346#if defined(XCASE) && defined(IUCLC) && defined(OLCUC)
347 {stty_lcase, combination, REV | OMIT, 0, 0 }, 347 MI_ENTRY(stty_lcase, combination, REV | OMIT, 0, 0 ),
348 {stty_LCASE, combination, REV | OMIT, 0, 0 }, 348 MI_ENTRY(stty_LCASE, combination, REV | OMIT, 0, 0 ),
349#endif 349#endif
350 {stty_crt, combination, OMIT, 0, 0 }, 350 MI_ENTRY(stty_crt, combination, OMIT, 0, 0 ),
351 {stty_dec, combination, OMIT, 0, 0 }, 351 MI_ENTRY(stty_dec, combination, OMIT, 0, 0 ),
352}; 352};
353 353
354static const int NUM_mode_info = 354static const int NUM_mode_info =
@@ -359,7 +359,7 @@ static const int NUM_mode_info =
359struct control_info { 359struct control_info {
360 const char *name; /* Name given on command line. */ 360 const char *name; /* Name given on command line. */
361 unsigned char saneval; /* Value to set for `stty sane'. */ 361 unsigned char saneval; /* Value to set for `stty sane'. */
362 int offset; /* Offset in c_cc. */ 362 unsigned char offset; /* Offset in c_cc. */
363}; 363};
364 364
365/* Control characters. */ 365/* Control characters. */
@@ -408,7 +408,6 @@ static const int NUM_control_info =
408 408
409 409
410static const char * visible(unsigned int ch); 410static const char * visible(unsigned int ch);
411static unsigned long baud_to_value(speed_t speed);
412static int recover_mode(char *arg, struct termios *mode); 411static int recover_mode(char *arg, struct termios *mode);
413static int screen_columns(void); 412static int screen_columns(void);
414static int set_mode(const struct mode_info *info, 413static int set_mode(const struct mode_info *info,
@@ -416,12 +415,11 @@ static int set_mode(const struct mode_info *info,
416static speed_t string_to_baud(const char *arg); 415static speed_t string_to_baud(const char *arg);
417static tcflag_t* mode_type_flag(enum mode_type type, struct termios *mode); 416static tcflag_t* mode_type_flag(enum mode_type type, struct termios *mode);
418static void display_all(struct termios *mode, int fd, 417static void display_all(struct termios *mode, int fd,
419 const char *device_name); 418 const char *device_name);
420static void display_changed(struct termios *mode); 419static void display_changed(struct termios *mode, int fd,
421static void display_recoverable(struct termios *mode); 420 const char *device_name);
422static void display_settings(enum output_type output_type, 421static void display_recoverable(struct termios *mode, int fd,
423 struct termios *mode, int fd, 422 const char *device_name);
424 const char *device_name);
425static void display_speed(struct termios *mode, int fancy); 423static void display_speed(struct termios *mode, int fancy);
426static void display_window_size(int fancy, int fd, 424static void display_window_size(int fancy, int fd,
427 const char *device_name); 425 const char *device_name);
@@ -479,7 +477,7 @@ extern int main(int argc, char **argv)
479#endif 477#endif
480{ 478{
481 struct termios mode; 479 struct termios mode;
482 enum output_type output_type; 480 void (*output_func)(struct termios *, int, const char *);
483 int optc; 481 int optc;
484 int require_set_attr; 482 int require_set_attr;
485 int speed_was_set; 483 int speed_was_set;
@@ -491,7 +489,7 @@ extern int main(int argc, char **argv)
491 int fd; 489 int fd;
492 const char *device_name; 490 const char *device_name;
493 491
494 output_type = changed; 492 output_func = display_changed;
495 verbose_output = 0; 493 verbose_output = 0;
496 recoverable_output = 0; 494 recoverable_output = 0;
497 495
@@ -502,17 +500,17 @@ extern int main(int argc, char **argv)
502 switch (optc) { 500 switch (optc) {
503 case 'a': 501 case 'a':
504 verbose_output = 1; 502 verbose_output = 1;
505 output_type = all; 503 output_func = display_all;
506 break; 504 break;
507 505
508 case 'g': 506 case 'g':
509 recoverable_output = 1; 507 recoverable_output = 1;
510 output_type = recoverable; 508 output_func = display_recoverable;
511 break; 509 break;
512 510
513 case 'F': 511 case 'F':
514 if (file_name) 512 if (file_name)
515 error_msg_and_die("only one device may be specified"); 513 bb_error_msg_and_die("only one device may be specified");
516 file_name = optarg; 514 file_name = optarg;
517 break; 515 break;
518 516
@@ -529,12 +527,12 @@ extern int main(int argc, char **argv)
529 noargs = 0; 527 noargs = 0;
530 528
531 /* Specifying both -a and -g gets an error. */ 529 /* Specifying both -a and -g gets an error. */
532 if (verbose_output && recoverable_output) 530 if (verbose_output & recoverable_output)
533 error_msg_and_die ("verbose and stty-readable output styles are mutually exclusive"); 531 bb_error_msg_and_die ("verbose and stty-readable output styles are mutually exclusive");
534 532
535 /* Specifying any other arguments with -a or -g gets an error. */ 533 /* Specifying any other arguments with -a or -g gets an error. */
536 if (!noargs && (verbose_output || recoverable_output)) 534 if (~noargs & (verbose_output | recoverable_output))
537 error_msg_and_die ("modes may not be set when specifying an output style"); 535 bb_error_msg_and_die ("modes may not be set when specifying an output style");
538 536
539 /* FIXME: it'd be better not to open the file until we've verified 537 /* FIXME: it'd be better not to open the file until we've verified
540 that all arguments are valid. Otherwise, we could end up doing 538 that all arguments are valid. Otherwise, we could end up doing
@@ -547,26 +545,26 @@ extern int main(int argc, char **argv)
547 device_name = file_name; 545 device_name = file_name;
548 fd = open(device_name, O_RDONLY | O_NONBLOCK); 546 fd = open(device_name, O_RDONLY | O_NONBLOCK);
549 if (fd < 0) 547 if (fd < 0)
550 perror_msg_and_die("%s", device_name); 548 bb_perror_msg_and_die("%s", device_name);
551 if ((fdflags = fcntl(fd, F_GETFL)) == -1 549 if ((fdflags = fcntl(fd, F_GETFL)) == -1
552 || fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) 550 || fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
553 perror_msg_and_die("%s: couldn't reset non-blocking mode", 551 bb_perror_msg_and_die("%s: couldn't reset non-blocking mode",
554 device_name); 552 device_name);
555 } else { 553 } else {
556 fd = 0; 554 fd = 0;
557 device_name = "standard input"; 555 device_name = bb_msg_standard_input;
558 } 556 }
559 557
560 /* Initialize to all zeroes so there is no risk memcmp will report a 558 /* Initialize to all zeroes so there is no risk memcmp will report a
561 spurious difference in an uninitialized portion of the structure. */ 559 spurious difference in an uninitialized portion of the structure. */
562 memset(&mode, 0, sizeof(mode)); 560 memset(&mode, 0, sizeof(mode));
563 if (tcgetattr(fd, &mode)) 561 if (tcgetattr(fd, &mode))
564 perror_msg_and_die("%s", device_name); 562 bb_perror_msg_and_die("%s", device_name);
565 563
566 if (verbose_output || recoverable_output || noargs) { 564 if (verbose_output | recoverable_output | noargs) {
567 max_col = screen_columns(); 565 max_col = screen_columns();
568 current_col = 0; 566 current_col = 0;
569 display_settings(output_type, &mode, fd, device_name); 567 output_func(&mode, fd, device_name);
570 return EXIT_SUCCESS; 568 return EXIT_SUCCESS;
571 } 569 }
572 570
@@ -610,13 +608,13 @@ extern int main(int argc, char **argv)
610 } 608 }
611 609
612 if (match_found == 0 && reversed) 610 if (match_found == 0 && reversed)
613 error_msg_and_die("invalid argument `%s'", --argv[k]); 611 bb_error_msg_and_die("invalid argument `%s'", --argv[k]);
614 612
615 if (match_found == 0) 613 if (match_found == 0)
616 for (i = 0; i < NUM_control_info; ++i) 614 for (i = 0; i < NUM_control_info; ++i)
617 if (STREQ(argv[k], control_info[i].name)) { 615 if (STREQ(argv[k], control_info[i].name)) {
618 if (k == argc - 1) 616 if (k == argc - 1)
619 error_msg_and_die("missing argument to `%s'", argv[k]); 617 bb_error_msg_and_die("missing argument to `%s'", argv[k]);
620 match_found = 1; 618 match_found = 1;
621 ++k; 619 ++k;
622 set_control_char(&control_info[i], argv[k], &mode); 620 set_control_char(&control_info[i], argv[k], &mode);
@@ -627,14 +625,14 @@ extern int main(int argc, char **argv)
627 if (match_found == 0) { 625 if (match_found == 0) {
628 if (STREQ(argv[k], "ispeed")) { 626 if (STREQ(argv[k], "ispeed")) {
629 if (k == argc - 1) 627 if (k == argc - 1)
630 error_msg_and_die("missing argument to `%s'", argv[k]); 628 bb_error_msg_and_die("missing argument to `%s'", argv[k]);
631 ++k; 629 ++k;
632 set_speed(input_speed, argv[k], &mode); 630 set_speed(input_speed, argv[k], &mode);
633 speed_was_set = 1; 631 speed_was_set = 1;
634 require_set_attr = 1; 632 require_set_attr = 1;
635 } else if (STREQ(argv[k], "ospeed")) { 633 } else if (STREQ(argv[k], "ospeed")) {
636 if (k == argc - 1) 634 if (k == argc - 1)
637 error_msg_and_die("missing argument to `%s'", argv[k]); 635 bb_error_msg_and_die("missing argument to `%s'", argv[k]);
638 ++k; 636 ++k;
639 set_speed(output_speed, argv[k], &mode); 637 set_speed(output_speed, argv[k], &mode);
640 speed_was_set = 1; 638 speed_was_set = 1;
@@ -643,16 +641,16 @@ extern int main(int argc, char **argv)
643#ifdef TIOCGWINSZ 641#ifdef TIOCGWINSZ
644 else if (STREQ(argv[k], "rows")) { 642 else if (STREQ(argv[k], "rows")) {
645 if (k == argc - 1) 643 if (k == argc - 1)
646 error_msg_and_die("missing argument to `%s'", argv[k]); 644 bb_error_msg_and_die("missing argument to `%s'", argv[k]);
647 ++k; 645 ++k;
648 set_window_size((int) parse_number(argv[k], stty_suffixes), 646 set_window_size((int) bb_xparse_number(argv[k], stty_suffixes),
649 -1, fd, device_name); 647 -1, fd, device_name);
650 } else if (STREQ(argv[k], "cols") || STREQ(argv[k], "columns")) { 648 } else if (STREQ(argv[k], "cols") || STREQ(argv[k], "columns")) {
651 if (k == argc - 1) 649 if (k == argc - 1)
652 error_msg_and_die("missing argument to `%s'", argv[k]); 650 bb_error_msg_and_die("missing argument to `%s'", argv[k]);
653 ++k; 651 ++k;
654 set_window_size(-1, 652 set_window_size(-1,
655 (int) parse_number(argv[k], stty_suffixes), 653 (int) bb_xparse_number(argv[k], stty_suffixes),
656 fd, device_name); 654 fd, device_name);
657 } else if (STREQ(argv[k], "size")) { 655 } else if (STREQ(argv[k], "size")) {
658 max_col = screen_columns(); 656 max_col = screen_columns();
@@ -663,9 +661,9 @@ extern int main(int argc, char **argv)
663#ifdef HAVE_C_LINE 661#ifdef HAVE_C_LINE
664 else if (STREQ(argv[k], "line")) { 662 else if (STREQ(argv[k], "line")) {
665 if (k == argc - 1) 663 if (k == argc - 1)
666 error_msg_and_die("missing argument to `%s'", argv[k]); 664 bb_error_msg_and_die("missing argument to `%s'", argv[k]);
667 ++k; 665 ++k;
668 mode.c_line = parse_number(argv[k], stty_suffixes); 666 mode.c_line = bb_xparse_number(argv[k], stty_suffixes);
669 require_set_attr = 1; 667 require_set_attr = 1;
670 } 668 }
671#endif 669#endif
@@ -679,7 +677,7 @@ extern int main(int argc, char **argv)
679 speed_was_set = 1; 677 speed_was_set = 1;
680 require_set_attr = 1; 678 require_set_attr = 1;
681 } else 679 } else
682 error_msg_and_die("invalid argument `%s'", argv[k]); 680 bb_error_msg_and_die("invalid argument `%s'", argv[k]);
683 } 681 }
684 } 682 }
685 683
@@ -687,7 +685,7 @@ extern int main(int argc, char **argv)
687 struct termios new_mode; 685 struct termios new_mode;
688 686
689 if (tcsetattr(fd, TCSADRAIN, &mode)) 687 if (tcsetattr(fd, TCSADRAIN, &mode))
690 perror_msg_and_die("%s", device_name); 688 bb_perror_msg_and_die("%s", device_name);
691 689
692 /* POSIX (according to Zlotnick's book) tcsetattr returns zero if 690 /* POSIX (according to Zlotnick's book) tcsetattr returns zero if
693 it performs *any* of the requested operations. This means it 691 it performs *any* of the requested operations. This means it
@@ -700,7 +698,7 @@ extern int main(int argc, char **argv)
700 spurious difference in an uninitialized portion of the structure. */ 698 spurious difference in an uninitialized portion of the structure. */
701 memset(&new_mode, 0, sizeof(new_mode)); 699 memset(&new_mode, 0, sizeof(new_mode));
702 if (tcgetattr(fd, &new_mode)) 700 if (tcgetattr(fd, &new_mode))
703 perror_msg_and_die("%s", device_name); 701 bb_perror_msg_and_die("%s", device_name);
704 702
705 /* Normally, one shouldn't use memcmp to compare structures that 703 /* Normally, one shouldn't use memcmp to compare structures that
706 may have `holes' containing uninitialized data, but we have been 704 may have `holes' containing uninitialized data, but we have been
@@ -723,7 +721,7 @@ extern int main(int argc, char **argv)
723 new_mode.c_cflag &= (~CIBAUD); 721 new_mode.c_cflag &= (~CIBAUD);
724 if (speed_was_set || memcmp(&mode, &new_mode, sizeof(mode)) != 0) 722 if (speed_was_set || memcmp(&mode, &new_mode, sizeof(mode)) != 0)
725#endif 723#endif
726 error_msg_and_die ("%s: unable to perform all requested operations", 724 bb_error_msg_and_die ("%s: unable to perform all requested operations",
727 device_name); 725 device_name);
728 } 726 }
729 } 727 }
@@ -896,9 +894,9 @@ set_mode(const struct mode_info *info, int reversed, struct termios *mode)
896#endif 894#endif
897 } 895 }
898 } else if (reversed) 896 } else if (reversed)
899 *bitsp = *bitsp & ~info->mask & ~info->bits; 897 *bitsp = *bitsp & ~((unsigned long)info->mask) & ~info->bits;
900 else 898 else
901 *bitsp = (*bitsp & ~info->mask) | info->bits; 899 *bitsp = (*bitsp & ~((unsigned long)info->mask)) | info->bits;
902 900
903 return 1; 901 return 1;
904} 902}
@@ -910,7 +908,7 @@ set_control_char(const struct control_info *info, const char *arg,
910 unsigned char value; 908 unsigned char value;
911 909
912 if (info->name == stty_min || info->name == stty_time) 910 if (info->name == stty_min || info->name == stty_time)
913 value = parse_number(arg, stty_suffixes); 911 value = bb_xparse_number(arg, stty_suffixes);
914 else if (arg[0] == '\0' || arg[1] == '\0') 912 else if (arg[0] == '\0' || arg[1] == '\0')
915 value = arg[0]; 913 value = arg[0];
916 else if (STREQ(arg, "^-") || STREQ(arg, "undef")) 914 else if (STREQ(arg, "^-") || STREQ(arg, "undef"))
@@ -921,7 +919,7 @@ set_control_char(const struct control_info *info, const char *arg,
921 else 919 else
922 value = arg[1] & ~0140; /* Non-letters get weird results. */ 920 value = arg[1] & ~0140; /* Non-letters get weird results. */
923 } else 921 } else
924 value = parse_number(arg, stty_suffixes); 922 value = bb_xparse_number(arg, stty_suffixes);
925 mode->c_cc[info->offset] = value; 923 mode->c_cc[info->offset] = value;
926} 924}
927 925
@@ -931,10 +929,13 @@ set_speed(enum speed_setting type, const char *arg, struct termios *mode)
931 speed_t baud; 929 speed_t baud;
932 930
933 baud = string_to_baud(arg); 931 baud = string_to_baud(arg);
934 if (type == input_speed || type == both_speeds) 932
933 if (type != output_speed) { /* either input or both */
935 cfsetispeed(mode, baud); 934 cfsetispeed(mode, baud);
936 if (type == output_speed || type == both_speeds) 935 }
936 if (type != input_speed) { /* either output or both */
937 cfsetospeed(mode, baud); 937 cfsetospeed(mode, baud);
938 }
938} 939}
939 940
940#ifdef TIOCGWINSZ 941#ifdef TIOCGWINSZ
@@ -953,7 +954,7 @@ set_window_size(int rows, int cols, int fd, const char *device_name)
953 954
954 if (get_win_size(fd, &win)) { 955 if (get_win_size(fd, &win)) {
955 if (errno != EINVAL) 956 if (errno != EINVAL)
956 perror_msg_and_die("%s", device_name); 957 bb_perror_msg_and_die("%s", device_name);
957 memset(&win, 0, sizeof(win)); 958 memset(&win, 0, sizeof(win));
958 } 959 }
959 960
@@ -975,32 +976,28 @@ set_window_size(int rows, int cols, int fd, const char *device_name)
975 ttysz.ts_lines = win.ws_row; 976 ttysz.ts_lines = win.ws_row;
976 ttysz.ts_cols = win.ws_col; 977 ttysz.ts_cols = win.ws_col;
977 978
978 win.ws_row = 1; 979 win.ws_row = win.ws_col = 1;
979 win.ws_col = 1;
980
981 if (ioctl(fd, TIOCSWINSZ, (char *) &win))
982 perror_msg_and_die("%s", device_name);
983 980
984 if (ioctl(fd, TIOCSSIZE, (char *) &ttysz)) 981 if ((ioctl(fd, TIOCSWINSZ, (char *) &win) != 0)
985 perror_msg_and_die("%s", device_name); 982 || (ioctl(fd, TIOCSSIZE, (char *) &ttysz) != 0)) {
983 bb_perror_msg_and_die("%s", device_name);
986 return; 984 return;
987 } 985 }
988# endif 986# endif
989 987
990 if (ioctl(fd, TIOCSWINSZ, (char *) &win)) 988 if (ioctl(fd, TIOCSWINSZ, (char *) &win))
991 perror_msg_and_die("%s", device_name); 989 bb_perror_msg_and_die("%s", device_name);
992} 990}
993 991
994static void display_window_size(int fancy, int fd, const char *device_name) 992static void display_window_size(int fancy, int fd, const char *device_name)
995{ 993{
994 const char *fmt_str = "%s" "\0" "%s: no size information for this device";
996 struct winsize win; 995 struct winsize win;
997 996
998 if (get_win_size(fd, &win)) { 997 if (get_win_size(fd, &win)) {
999 if (errno != EINVAL) 998 if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
1000 perror_msg_and_die("%s", device_name); 999 bb_perror_msg_and_die(fmt_str, device_name);
1001 if (!fancy) 1000 }
1002 perror_msg_and_die("%s: no size information for this device",
1003 device_name);
1004 } else { 1001 } else {
1005 wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n", 1002 wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n",
1006 win.ws_row, win.ws_col); 1003 win.ws_row, win.ws_col);
@@ -1012,6 +1009,9 @@ static void display_window_size(int fancy, int fd, const char *device_name)
1012 1009
1013static int screen_columns(void) 1010static int screen_columns(void)
1014{ 1011{
1012 int columns;
1013 const char *s;
1014
1015#ifdef TIOCGWINSZ 1015#ifdef TIOCGWINSZ
1016 struct winsize win; 1016 struct winsize win;
1017 1017
@@ -1025,51 +1025,29 @@ static int screen_columns(void)
1025 return win.ws_col; 1025 return win.ws_col;
1026#endif 1026#endif
1027 1027
1028 if (getenv("COLUMNS")) 1028 columns = 80;
1029 return atoi(getenv("COLUMNS")); 1029 if ((s = getenv("COLUMNS"))) {
1030 return 80; 1030 columns = atoi(s);
1031}
1032
1033static tcflag_t *mode_type_flag(enum mode_type type, struct termios *mode)
1034{
1035 switch (type) {
1036 case control:
1037 return &mode->c_cflag;
1038
1039 case input:
1040 return &mode->c_iflag;
1041
1042 case output:
1043 return &mode->c_oflag;
1044
1045 case local:
1046 return &mode->c_lflag;
1047
1048 default: /* combination: */
1049 return NULL;
1050 } 1031 }
1032 return columns;
1051} 1033}
1052 1034
1053static void 1035static tcflag_t *mode_type_flag(enum mode_type type, struct termios *mode)
1054display_settings(enum output_type output_type, struct termios *mode,
1055 int fd, const char *device_name)
1056{ 1036{
1057 switch (output_type) { 1037 static const unsigned char tcflag_offsets[] = {
1058 case changed: 1038 offsetof(struct termios, c_cflag), /* control */
1059 display_changed(mode); 1039 offsetof(struct termios, c_iflag), /* input */
1060 break; 1040 offsetof(struct termios, c_oflag), /* output */
1061 1041 offsetof(struct termios, c_lflag) /* local */
1062 case all: 1042 };
1063 display_all(mode, fd, device_name); 1043
1064 break; 1044 if (((unsigned int) type) <= local) {
1065 1045 return (tcflag_t *)(((char *) mode) + tcflag_offsets[(int)type]);
1066 case recoverable:
1067 display_recoverable(mode);
1068 break;
1069 } 1046 }
1047 return NULL;
1070} 1048}
1071 1049
1072static void display_changed(struct termios *mode) 1050static void display_changed(struct termios *mode, int fd, const char *device_name)
1073{ 1051{
1074 int i; 1052 int i;
1075 int empty_line; 1053 int empty_line;
@@ -1206,18 +1184,25 @@ display_all(struct termios *mode, int fd, const char *device_name)
1206 1184
1207static void display_speed(struct termios *mode, int fancy) 1185static void display_speed(struct termios *mode, int fancy)
1208{ 1186{
1209 if (cfgetispeed(mode) == 0 || cfgetispeed(mode) == cfgetospeed(mode)) 1187 unsigned long ispeed, ospeed;
1210 wrapf(fancy ? "speed %lu baud;" : "%lu\n", 1188 const char *fmt_str =
1211 baud_to_value(cfgetospeed(mode))); 1189 "%lu %lu\n\0" "ispeed %lu baud; ospeed %lu baud;\0"
1212 else 1190 "%lu\n\0" "\0\0\0\0" "speed %lu baud;";
1213 wrapf(fancy ? "ispeed %lu baud; ospeed %lu baud;" : "%lu %lu\n", 1191
1214 baud_to_value(cfgetispeed(mode)), 1192 ospeed = ispeed = cfgetispeed(mode);
1215 baud_to_value(cfgetospeed(mode))); 1193 if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) {
1194 ispeed = ospeed; /* in case ispeed was 0 */
1195 fmt_str += 43;
1196 }
1197 if (fancy) {
1198 fmt_str += 9;
1199 }
1200 wrapf(fmt_str, bb_baud_to_value(ispeed), bb_baud_to_value(ospeed));
1216 if (!fancy) 1201 if (!fancy)
1217 current_col = 0; 1202 current_col = 0;
1218} 1203}
1219 1204
1220static void display_recoverable(struct termios *mode) 1205static void display_recoverable(struct termios *mode, int fd, const char *device_name)
1221{ 1206{
1222 int i; 1207 int i;
1223 1208
@@ -1259,62 +1244,9 @@ static int recover_mode(char *arg, struct termios *mode)
1259 return 1; 1244 return 1;
1260} 1245}
1261 1246
1262struct speed_map {
1263 speed_t speed; /* Internal form. */
1264 unsigned long value; /* Numeric value. */
1265};
1266
1267static const struct speed_map speeds[] = {
1268 {B0, 0},
1269 {B50, 50},
1270 {B75, 75},
1271 {B110, 110},
1272 {B134, 134},
1273 {B150, 150},
1274 {B200, 200},
1275 {B300, 300},
1276 {B600, 600},
1277 {B1200, 1200},
1278 {B1800, 1800},
1279 {B2400, 2400},
1280 {B4800, 4800},
1281 {B9600, 9600},
1282 {B19200, 19200},
1283 {B38400, 38400},
1284#ifdef B57600
1285 {B57600, 57600},
1286#endif
1287#ifdef B115200
1288 {B115200, 115200},
1289#endif
1290#ifdef B230400
1291 {B230400, 230400},
1292#endif
1293#ifdef B460800
1294 {B460800, 460800},
1295#endif
1296};
1297
1298static const int NUM_SPEEDS = (sizeof(speeds) / sizeof(struct speed_map));
1299
1300static speed_t string_to_baud(const char *arg) 1247static speed_t string_to_baud(const char *arg)
1301{ 1248{
1302 int i; 1249 return bb_value_to_baud(bb_xparse_number(arg, 0));
1303
1304 for (i = 0; i < NUM_SPEEDS; ++i)
1305 if (parse_number(arg, 0) == speeds[i].value)
1306 return speeds[i].speed;
1307 return (speed_t) - 1;
1308}
1309
1310static unsigned long baud_to_value(speed_t speed)
1311{
1312 int i;
1313
1314 for (i = 0; i < NUM_SPEEDS; ++i)
1315 if (speed == speeds[i].speed)
1316 return speeds[i].value;
1317 return 0;
1318} 1250}
1319 1251
1320static void sane_mode(struct termios *mode) 1252static void sane_mode(struct termios *mode)
@@ -1333,10 +1265,12 @@ static void sane_mode(struct termios *mode)
1333 for (i = 0; i < NUM_mode_info; ++i) { 1265 for (i = 0; i < NUM_mode_info; ++i) {
1334 if (mode_info[i].flags & SANE_SET) { 1266 if (mode_info[i].flags & SANE_SET) {
1335 bitsp = mode_type_flag(mode_info[i].type, mode); 1267 bitsp = mode_type_flag(mode_info[i].type, mode);
1336 *bitsp = (*bitsp & ~mode_info[i].mask) | mode_info[i].bits; 1268 *bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask))
1269 | mode_info[i].bits;
1337 } else if (mode_info[i].flags & SANE_UNSET) { 1270 } else if (mode_info[i].flags & SANE_UNSET) {
1338 bitsp = mode_type_flag(mode_info[i].type, mode); 1271 bitsp = mode_type_flag(mode_info[i].type, mode);
1339 *bitsp = *bitsp & ~mode_info[i].mask & ~mode_info[i].bits; 1272 *bitsp = *bitsp & ~((unsigned long)mode_info[i].mask)
1273 & ~mode_info[i].bits;
1340 } 1274 }
1341 } 1275 }
1342} 1276}
@@ -1349,47 +1283,32 @@ static const char *visible(unsigned int ch)
1349 static char buf[10]; 1283 static char buf[10];
1350 char *bpout = buf; 1284 char *bpout = buf;
1351 1285
1352 if (ch == _POSIX_VDISABLE) 1286 if (ch == _POSIX_VDISABLE) {
1353 return "<undef>"; 1287 return "<undef>";
1288 }
1354 1289
1355 if (ch >= 32) { 1290 if (ch >= 128) {
1356 if (ch < 127) 1291 ch -= 128;
1357 *bpout++ = ch; 1292 *bpout++ = 'M';
1358 else if (ch == 127) { 1293 *bpout++ = '-';
1359 *bpout++ = '^'; 1294 }
1360 *bpout++ = '?'; 1295
1361 } else { 1296 if (ch < 32) {
1362 *bpout++ = 'M', *bpout++ = '-';
1363 if (ch >= 128 + 32) {
1364 if (ch < 128 + 127)
1365 *bpout++ = ch - 128;
1366 else {
1367 *bpout++ = '^';
1368 *bpout++ = '?';
1369 }
1370 } else {
1371 *bpout++ = '^';
1372 *bpout++ = ch - 128 + 64;
1373 }
1374 }
1375 } else {
1376 *bpout++ = '^'; 1297 *bpout++ = '^';
1377 *bpout++ = ch + 64; 1298 *bpout++ = ch + 64;
1299 } else if (ch < 127) {
1300 *bpout++ = ch;
1301 } else {
1302 *bpout++ = '^';
1303 *bpout++ = '?';
1378 } 1304 }
1305
1379 *bpout = '\0'; 1306 *bpout = '\0';
1380 return (const char *) buf; 1307 return (const char *) buf;
1381} 1308}
1382 1309
1383#ifdef TEST 1310#ifdef TEST
1384 1311
1385const char *applet_name = "stty"; 1312const char *bb_applet_name = "stty";
1386 1313
1387#endif 1314#endif
1388
1389/*
1390Local Variables:
1391c-file-style: "linux"
1392c-basic-offset: 4
1393tab-width: 4
1394End:
1395*/
diff --git a/coreutils/sync.c b/coreutils/sync.c
index d58168065..84746311f 100644
--- a/coreutils/sync.c
+++ b/coreutils/sync.c
@@ -20,15 +20,17 @@
20 * 20 *
21 */ 21 */
22 22
23#include <stdio.h> 23/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
24
24#include <stdlib.h> 25#include <stdlib.h>
25#include <unistd.h> 26#include <unistd.h>
26#include "busybox.h" 27#include "busybox.h"
27 28
28extern int sync_main(int argc, char **argv) 29extern int sync_main(int argc, char **argv)
29{ 30{
30 if (argc > 1 && **(argv + 1) == '-') 31 bb_warn_ignoring_args(argc - 1);
31 show_usage(); 32
32 sync(); 33 sync();
34
33 return(EXIT_SUCCESS); 35 return(EXIT_SUCCESS);
34} 36}
diff --git a/coreutils/tail.c b/coreutils/tail.c
index 1703eefc6..8e0adf5b5 100644
--- a/coreutils/tail.c
+++ b/coreutils/tail.c
@@ -2,7 +2,6 @@
2/* 2/*
3 * Mini tail implementation for busybox 3 * Mini tail implementation for busybox
4 * 4 *
5 *
6 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu> 5 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
@@ -21,14 +20,30 @@
21 * 20 *
22 */ 21 */
23 22
23/* BB_AUDIT SUSv3 compliant (need fancy for -c) */
24/* BB_AUDIT GNU compatible -c, -q, and -v options in 'fancy' configuration. */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/tail.html */
24 26
25#include <fcntl.h> 27/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
26#include <getopt.h> 28 *
29 * Pretty much rewritten to fix numerous bugs and reduce realloc() calls.
30 * Bugs fixed (although I may have forgotten one or two... it was pretty bad)
31 * 1) mixing printf/write without fflush()ing stdout
32 * 2) no check that any open files are present
33 * 3) optstring had -q taking an arg
34 * 4) no error checking on write in some cases, and a warning even then
35 * 5) q and s interaction bug
36 * 6) no check for lseek error
37 * 7) lseek attempted when count==0 even if arg was +0 (from top)
38 */
39
40#include <stdio.h>
41#include <stdlib.h>
27#include <string.h> 42#include <string.h>
28#include <ctype.h> 43#include <ctype.h>
29#include <stdlib.h>
30#include <unistd.h> 44#include <unistd.h>
31#include <sys/types.h> 45#include <fcntl.h>
46#include <sys/stat.h>
32#include "busybox.h" 47#include "busybox.h"
33 48
34static const struct suffix_mult tail_suffixes[] = { 49static const struct suffix_mult tail_suffixes[] = {
@@ -38,234 +53,287 @@ static const struct suffix_mult tail_suffixes[] = {
38 { NULL, 0 } 53 { NULL, 0 }
39}; 54};
40 55
41static const int BYTES = 0; 56static int status
42static const int LINES = 1; 57#if EXIT_SUCCESS != 0
43 58 = EXIT_SUCCESS /* If it is 0 (paranoid check), let bss initialize it. */
44static char *tailbuf; 59#endif
45static int taillen; 60 ;
46static int newline;
47 61
48static void tailbuf_append(char *buf, int len) 62static void tail_xprint_header(const char *fmt, const char *filename)
49{ 63{
50 tailbuf = xrealloc(tailbuf, taillen + len); 64 /* If we get an output error, there is really no sense in continuing. */
51 memcpy(tailbuf + taillen, buf, len); 65 if (dprintf(STDOUT_FILENO, fmt, filename) < 0) {
52 taillen += len; 66 bb_perror_nomsg_and_die();
67 }
53} 68}
54 69
55static void tailbuf_trunc(void) 70/* len should probably be size_t */
71static void tail_xbb_full_write(const char *buf, size_t len)
56{ 72{
57 char *s; 73 /* If we get a write error, there is really no sense in continuing. */
58 s = memchr(tailbuf, '\n', taillen); 74 if (bb_full_write(STDOUT_FILENO, buf, len) < 0) {
59 memmove(tailbuf, s + 1, taillen - ((s + 1) - tailbuf)); 75 bb_perror_nomsg_and_die();
60 taillen -= (s + 1) - tailbuf; 76 }
61 newline = 0;
62} 77}
63 78
64int tail_main(int argc, char **argv) 79static ssize_t tail_read(int fd, char *buf, size_t count)
65{ 80{
66 int from_top = 0, units = LINES, count = 10, sleep_period = 1; 81 ssize_t r;
67 int show_headers = 0, hide_headers = 0, follow = 0; 82
68 int *fds, nfiles = 0, status = EXIT_SUCCESS, nread, nwrite, seen = 0; 83 if ((r = safe_read(fd, buf, count)) < 0) {
69 char *s, *start, *end, buf[BUFSIZ]; 84 bb_perror_msg("read");
70 int i, opt; 85 status = EXIT_FAILURE;
71
72 if (argc >= 2) {
73 int line_num;
74 switch (argv[1][0]) {
75 case '+':
76 from_top = 1;
77 /* FALLS THROUGH */
78 case '-':
79 line_num = atoi(&argv[1][1]);
80 if (line_num != 0) {
81 optind = 2;
82 count = line_num;
83 }
84 break;
85 }
86 } 86 }
87 87
88 return r;
89}
90
91static const char tail_opts[] =
92 "fn:"
93#ifdef CONFIG_FEATURE_FANCY_TAIL
94 "c:qs:v"
95#endif
96 ;
97
98static const char header_fmt[] = "\n==> %s <==\n";
99
100int tail_main(int argc, char **argv)
101{
102 long count = 10;
103 unsigned int sleep_period = 1;
104 int from_top = 0;
105 int follow = 0;
106 int header_threshhold = 1;
88#ifdef CONFIG_FEATURE_FANCY_TAIL 107#ifdef CONFIG_FEATURE_FANCY_TAIL
89 while ((opt = getopt(argc, argv, "c:fn:q:s:v")) > 0) { 108 int count_bytes = 0;
90#else
91 while ((opt = getopt(argc, argv, "fn:")) > 0) {
92#endif 109#endif
110
111 char *tailbuf;
112 size_t tailbufsize;
113 int taillen = 0;
114 int newline = 0;
115
116 int *fds, nfiles, nread, nwrite, seen, i, opt;
117 char *s, *buf;
118 const char *fmt;
119
120 /* Allow legacy syntax of an initial numeric option without -n. */
121 if ((argv[1][0] == '+')
122 || ((argv[1][0] == '-')
123 /* && (isdigit)(argv[1][1]) */
124 && (((unsigned int)(argv[1][1] - '0')) <= 9))
125 ) {
126 optind = 2;
127 optarg = argv[1];
128 goto GET_COUNT;
129 }
130
131 while ((opt = getopt(argc, argv, tail_opts)) > 0) {
93 switch (opt) { 132 switch (opt) {
94 case 'f': 133 case 'f':
95 follow = 1; 134 follow = 1;
96 break; 135 break;
97#ifdef CONFIG_FEATURE_FANCY_TAIL 136#ifdef CONFIG_FEATURE_FANCY_TAIL
98 case 'c': 137 case 'c':
99 units = BYTES; 138 count_bytes = 1;
100 /* FALLS THROUGH */ 139 /* FALLS THROUGH */
101#endif 140#endif
102 case 'n': 141 case 'n':
103 count = parse_number(optarg, tail_suffixes); 142 GET_COUNT:
104 if (count < 0) 143 count = bb_xgetlarg10_sfx(optarg, tail_suffixes);
105 count = -count; 144 /* Note: Leading whitespace is an error trapped above. */
106 if (optarg[0] == '+') 145 if (*optarg == '+') {
107 from_top = 1; 146 from_top = 1;
147 } else {
148 from_top = 0;
149 }
150 if (count < 0) {
151 count = -count;
152 }
108 break; 153 break;
109#ifdef CONFIG_FEATURE_FANCY_TAIL 154#ifdef CONFIG_FEATURE_FANCY_TAIL
110 case 'q': 155 case 'q':
111 hide_headers = 1; 156 header_threshhold = INT_MAX;
112 break; 157 break;
113 case 's': 158 case 's':
114 sleep_period = parse_number(optarg, 0); 159 sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX);
115 break; 160 break;
116 case 'v': 161 case 'v':
117 show_headers = 1; 162 header_threshhold = 0;
118 break; 163 break;
119#endif 164#endif
120 default: 165 default:
121 show_usage(); 166 bb_show_usage();
122 } 167 }
123 } 168 }
124 169
125 /* open all the files */ 170 /* open all the files */
126 fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1)); 171 fds = (int *)xmalloc(sizeof(int) * (argc - optind + 1));
127 if (argc == optind) { 172
128 fds[nfiles++] = STDIN_FILENO; 173 argv += optind;
129 argv[optind] = "standard input"; 174 nfiles = i = 0;
130 } else { 175
131 for (i = optind; i < argc; i++) { 176 if ((argc -= optind) == 0) {
132 if (strcmp(argv[i], "-") == 0) { 177 struct stat statbuf;
133 fds[nfiles++] = STDIN_FILENO; 178
134 argv[i] = "standard input"; 179 if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
135 } else if ((fds[nfiles++] = open(argv[i], O_RDONLY)) < 0) { 180 follow = 0;
136 perror_msg("%s", argv[i]);
137 status = EXIT_FAILURE;
138 }
139 } 181 }
182 /* --argv; */
183 *argv = (char *) bb_msg_standard_input;
184 goto DO_STDIN;
185 }
186
187 do {
188 if ((argv[i][0] == '-') && !argv[i][1]) {
189 DO_STDIN:
190 fds[nfiles] = STDIN_FILENO;
191 } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) {
192 bb_perror_msg("%s", argv[i]);
193 status = EXIT_FAILURE;
194 continue;
195 }
196 argv[nfiles] = argv[i];
197 ++nfiles;
198 } while (++i < argc);
199
200 if (!nfiles) {
201 bb_error_msg_and_die("no files");
140 } 202 }
141 203
204 tailbufsize = BUFSIZ;
142#ifdef CONFIG_FEATURE_FANCY_TAIL 205#ifdef CONFIG_FEATURE_FANCY_TAIL
143 /* tail the files */ 206 /* tail the files */
144 if (!from_top && units == BYTES) 207 if (from_top < count_bytes) { /* Each is 0 or 1, so true iff 0 < 1. */
145 tailbuf = xmalloc(count); 208 /* Hence, !from_top && count_bytes */
209 if (tailbufsize < count) {
210 tailbufsize = count + BUFSIZ;
211 }
212 }
146#endif 213#endif
214 buf = tailbuf = xmalloc(tailbufsize);
147 215
148 for (i = 0; i < nfiles; i++) { 216 fmt = header_fmt + 1; /* Skip header leading newline on first output. */
149 if (fds[i] == -1) 217 i = 0;
150 continue; 218 do {
151 if (!count) { 219 /* Be careful. It would be possible to optimize the count-bytes
152 lseek(fds[i], 0, SEEK_END); 220 * case if the file is seekable. If you do though, remember that
221 * starting file position may not be the beginning of the file.
222 * Beware of backing up too far. See example in wc.c.
223 */
224 if ((!(count|from_top)) && (lseek(fds[i], 0, SEEK_END) >= 0)) {
153 continue; 225 continue;
154 } 226 }
155 seen = 0; 227
156 if (show_headers || (!hide_headers && nfiles > 1)) 228 if (nfiles > header_threshhold) {
157 printf("%s==> %s <==\n", i == 0 ? "" : "\n", argv[optind + i]); 229 tail_xprint_header(fmt, argv[i]);
158 while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) { 230 fmt = header_fmt;
231 }
232
233 buf = tailbuf;
234 taillen = 0;
235 seen = 1;
236
237 while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
159 if (from_top) { 238 if (from_top) {
239 nwrite = nread;
240 if (seen < count) {
160#ifdef CONFIG_FEATURE_FANCY_TAIL 241#ifdef CONFIG_FEATURE_FANCY_TAIL
161 if (units == BYTES) { 242 if (count_bytes) {
162 if (count - 1 <= seen) 243 nwrite -= (count - seen);
163 nwrite = nread; 244 seen = count;
164 else if (count - 1 <= seen + nread) 245 } else
165 nwrite = nread + seen - (count - 1);
166 else
167 nwrite = 0;
168 seen += nread;
169 } else {
170#else
171 {
172#endif 246#endif
173 if (count - 1 <= seen) 247 {
174 nwrite = nread; 248 s = buf;
175 else { 249 do {
176 nwrite = 0; 250 --nwrite;
177 for (s = memchr(buf, '\n', nread); s != NULL; 251 if ((*s++ == '\n') && (++seen == count)) {
178 s = memchr(s+1, '\n', nread - (s + 1 - buf))) {
179 if (count - 1 <= ++seen) {
180 nwrite = nread - (s + 1 - buf);
181 break; 252 break;
182 } 253 }
183 } 254 } while (nwrite);
184 } 255 }
185 } 256 }
186 if (full_write(STDOUT_FILENO, buf + nread - nwrite, 257 tail_xbb_full_write(buf + nread - nwrite, nwrite);
187 nwrite) < 0) { 258 } else if (count) {
188 perror_msg("write");
189 status = EXIT_FAILURE;
190 break;
191 }
192 } else {
193#ifdef CONFIG_FEATURE_FANCY_TAIL 259#ifdef CONFIG_FEATURE_FANCY_TAIL
194 if (units == BYTES) { 260 if (count_bytes) {
195 if (nread < count) { 261 taillen += nread;
196 memmove(tailbuf, tailbuf + nread, count - nread); 262 if (taillen > count) {
197 memcpy(tailbuf + count - nread, buf, nread); 263 memmove(tailbuf, tailbuf + taillen - count, count);
198 } else { 264 taillen = count;
199 memcpy(tailbuf, buf + nread - count, count);
200 } 265 }
201 seen += nread; 266 } else
202 } else {
203#else
204 {
205#endif 267#endif
206 for (start = buf, end = memchr(buf, '\n', nread); 268 {
207 end != NULL; start = end+1, 269 int k = nread;
208 end = memchr(start, '\n', nread - (start - buf))) { 270 int nbuf = 0;
209 if (newline && count <= seen) 271
210 tailbuf_trunc(); 272 while (k) {
211 tailbuf_append(start, end - start + 1); 273 --k;
212 seen++; 274 if (buf[k] == '\n') {
213 newline = 1; 275 ++nbuf;
276 }
214 } 277 }
215 if (newline && count <= seen && nread - (start - buf) > 0)
216 tailbuf_trunc();
217 tailbuf_append(start, nread - (start - buf));
218 }
219 }
220 }
221 278
222 if (nread < 0) { 279 if (newline + nbuf < count) {
223 perror_msg("read"); 280 newline += nbuf;
224 status = EXIT_FAILURE; 281 taillen += nread;
225 }
226 282
227#ifdef CONFIG_FEATURE_FANCY_TAIL 283 } else {
228 if (!from_top && units == BYTES) { 284 int extra = 0;
229 if (count < seen) 285 if (buf[nread-1] != '\n') {
230 seen = count; 286 extra = 1;
231 if (full_write(STDOUT_FILENO, tailbuf + count - seen, seen) < 0) { 287 }
232 perror_msg("write"); 288
233 status = EXIT_FAILURE; 289 k = newline + nbuf + extra - count;
290 s = tailbuf;
291 while (k) {
292 if (*s == '\n') {
293 --k;
294 }
295 ++s;
296 }
297
298 taillen += nread - (s - tailbuf);
299 memmove(tailbuf, s, taillen);
300 newline = count - extra;
301 }
302 if (tailbufsize < taillen + BUFSIZ) {
303 tailbufsize = taillen + BUFSIZ;
304 tailbuf = xrealloc(tailbuf, tailbufsize);
305 }
306 }
307 buf = tailbuf + taillen;
234 } 308 }
235 } 309 }
236#endif
237 310
238 if (!from_top && units == LINES) { 311 if (!from_top) {
239 if (full_write(STDOUT_FILENO, tailbuf, taillen) < 0) { 312 tail_xbb_full_write(tailbuf, taillen);
240 perror_msg("write");
241 status = EXIT_FAILURE;
242 }
243 } 313 }
244 314
245 taillen = 0; 315 taillen = 0;
246 } 316 } while (++i < nfiles);
247
248 while (follow) {
249 sleep(sleep_period);
250 317
251 for (i = 0; i < nfiles; i++) { 318 buf = xrealloc(tailbuf, BUFSIZ);
252 if (fds[i] == -1)
253 continue;
254 319
255 if ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) { 320 fmt = NULL;
256 if (show_headers || (!hide_headers && nfiles > 1))
257 printf("\n==> %s <==\n", argv[optind + i]);
258 321
259 do { 322 while (follow) {
260 full_write(STDOUT_FILENO, buf, nread); 323 sleep(sleep_period);
261 } while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0); 324 i = 0;
325 do {
326 if (nfiles > header_threshhold) {
327 fmt = header_fmt;
262 } 328 }
263 329 while ((nread = tail_read(fds[i], buf, sizeof(buf))) > 0) {
264 if (nread < 0) { 330 if (fmt) {
265 perror_msg("read"); 331 tail_xprint_header(fmt, argv[i]);
266 status = EXIT_FAILURE; 332 fmt = NULL;
333 }
334 tail_xbb_full_write(buf, nread);
267 } 335 }
268 } 336 } while (++i < nfiles);
269 } 337 }
270 338
271 return status; 339 return status;
diff --git a/coreutils/tee.c b/coreutils/tee.c
index 1c145426a..7e86f2e29 100644
--- a/coreutils/tee.c
+++ b/coreutils/tee.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini tee implementation for busybox 3 * tee implementation for busybox
4 * 4 *
5 * Copyright (C) 2000,2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu> 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -20,47 +20,96 @@
20 * 20 *
21 */ 21 */
22 22
23#include "busybox.h" 23/* BB_AUDIT SUSv3 compliant */
24#include <getopt.h> 24/* http://www.opengroup.org/onlinepubs/007904975/utilities/tee.html */
25
25#include <stdio.h> 26#include <stdio.h>
27#include <stdlib.h>
28#include <signal.h>
29#include <unistd.h>
30#include "busybox.h"
26 31
27int 32int tee_main(int argc, char **argv)
28tee_main(int argc, char **argv)
29{ 33{
30 char *mode = "w"; 34 const char *mode = "w\0a";
31 int c, i, status = 0, nfiles = 0;
32 FILE **files; 35 FILE **files;
36 FILE **p;
37 char **filenames;
38 int flags;
39 int retval = EXIT_SUCCESS;
40#ifdef CONFIG_FEATURE_TEE_USE_BLOCK_IO
41 size_t c;
42 RESERVE_CONFIG_BUFFER(buf, BUFSIZ);
43#else
44 int c;
45#endif
46
47 flags = bb_getopt_ulflags(argc, argv, "ia"); /* 'a' must be 2nd */
48
49 mode += (flags & 2); /* Since 'a' is the 2nd option... */
50
51 if (flags & 1) {
52 signal(SIGINT, SIG_IGN); /* TODO - switch to sigaction.*/
53 }
54
55 /* gnu tee ignores SIGPIPE in case one of the output files is a pipe
56 * that doesn't consume all its input. Good idea... */
57 signal(SIGPIPE, SIG_IGN); /* TODO - switch to sigaction.*/
33 58
34 while ((c = getopt(argc, argv, "a")) != EOF) { 59 /* Allocate an array of FILE *'s, with one extra for a sentinal. */
35 switch (c) { 60 p = files = (FILE **)xmalloc(sizeof(FILE *) * (argc - optind + 2));
36 case 'a': 61 *p = stdout;
37 mode = "a"; 62 argv += optind - 1;
38 break; 63 filenames = argv - 1;
39 default: 64 *filenames = (char *) bb_msg_standard_input; /* for later */
40 show_usage(); 65 goto GOT_NEW_FILE;
66
67 do {
68 if ((*p = bb_wfopen(*argv, mode)) == NULL) {
69 retval = EXIT_FAILURE;
70 continue;
71 }
72 filenames[(int)(p - files)] = *argv;
73 GOT_NEW_FILE:
74 setbuf(*p, NULL); /* tee must not buffer output. */
75 ++p;
76 } while (*++argv);
77
78 *p = NULL; /* Store the sentinal value. */
79
80#ifdef CONFIG_FEATURE_TEE_USE_BLOCK_IO
81 while ((c = fread(buf, 1, BUFSIZ, stdin)) != 0) {
82 for (p=files ; *p ; p++) {
83 fwrite(buf, 1, c, *p);
41 } 84 }
42 } 85 }
43 86
44 files = (FILE **)xmalloc(sizeof(FILE *) * (argc - optind + 1)); 87#ifdef CONFIG_FEATURE_CLEAN_UP
45 files[nfiles++] = stdout; 88 RELEASE_CONFIG_BUFFER(buf);
46 while (optind < argc) { 89#endif
47 if ((files[nfiles++] = wfopen(argv[optind++], mode)) == NULL) { 90
48 nfiles--; 91#else
49 status = 1; 92 while ((c = getchar()) != EOF) {
93 for (p=files ; *p ; p++) {
94 putc(c, *p);
50 } 95 }
51 } 96 }
97#endif
52 98
53 while ((c = getchar()) != EOF) 99 /* Now we need to check for i/o errors on stdin and the various
54 for (i = 0; i < nfiles; i++) 100 * output files. Since we know that the first entry in the output
55 putc(c, files[i]); 101 * file table is stdout, we can save one "if ferror" test by
102 * setting the first entry to stdin and checking stdout error
103 * status with bb_fflush_stdout_and_exit()... although fflush()ing
104 * is unnecessary here. */
56 105
57 return status; 106 p = files;
58} 107 *p = stdin;
108 do { /* Now check for (input and) output errors. */
109 /* Checking ferror should be sufficient, but we may want to fclose.
110 * If we do, remember not to close stdin! */
111 bb_xferror(*p, filenames[(int)(p - files)]);
112 } while (*++p);
59 113
60/* 114 bb_fflush_stdout_and_exit(retval);
61Local Variables: 115}
62c-file-style: "linux"
63c-basic-offset: 4
64tab-width: 4
65End:
66*/
diff --git a/coreutils/test.c b/coreutils/test.c
index 83e31ea5c..0bce66e6f 100644
--- a/coreutils/test.c
+++ b/coreutils/test.c
@@ -180,9 +180,9 @@ extern int test_main(int argc, char **argv)
180{ 180{
181 int res; 181 int res;
182 182
183 if (strcmp(applet_name, "[") == 0) { 183 if (strcmp(bb_applet_name, "[") == 0) {
184 if (strcmp(argv[--argc], "]")) 184 if (strcmp(argv[--argc], "]"))
185 error_msg_and_die("missing ]"); 185 bb_error_msg_and_die("missing ]");
186 argv[argc] = NULL; 186 argv[argc] = NULL;
187 } 187 }
188 /* Implement special cases from POSIX.2, section 4.62.4 */ 188 /* Implement special cases from POSIX.2, section 4.62.4 */
@@ -226,9 +226,9 @@ extern int test_main(int argc, char **argv)
226static void syntax(const char *op, const char *msg) 226static void syntax(const char *op, const char *msg)
227{ 227{
228 if (op && *op) { 228 if (op && *op) {
229 error_msg_and_die("%s: %s", op, msg); 229 bb_error_msg_and_die("%s: %s", op, msg);
230 } else { 230 } else {
231 error_msg_and_die("%s", msg); 231 bb_error_msg_and_die("%s", msg);
232 } 232 }
233} 233}
234 234
@@ -450,13 +450,13 @@ static int getn(const char *s)
450 r = strtol(s, &p, 10); 450 r = strtol(s, &p, 10);
451 451
452 if (errno != 0) 452 if (errno != 0)
453 error_msg_and_die("%s: out of range", s); 453 bb_error_msg_and_die("%s: out of range", s);
454 454
455 while (isspace(*p)) 455 while (isspace(*p))
456 p++; 456 p++;
457 457
458 if (*p) 458 if (*p)
459 error_msg_and_die("%s: bad number", s); 459 bb_error_msg_and_die("%s: bad number", s);
460 460
461 return (int) r; 461 return (int) r;
462} 462}
diff --git a/coreutils/touch.c b/coreutils/touch.c
index f1c6dc484..c66f26e0d 100644
--- a/coreutils/touch.c
+++ b/coreutils/touch.c
@@ -21,6 +21,16 @@
21 * 21 *
22 */ 22 */
23 23
24/* BB_AUDIT SUSv3 _NOT_ compliant -- options -a, -m, -r, -t not supported. */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/touch.html */
26
27/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
28 *
29 * Previous version called open() and then utime(). While this will be
30 * be necessary to implement -r and -t, it currently only makes things bigger.
31 * Also, exiting on a failure was a bug. All args should be processed.
32 */
33
24#include <stdio.h> 34#include <stdio.h>
25#include <sys/types.h> 35#include <sys/types.h>
26#include <fcntl.h> 36#include <fcntl.h>
@@ -33,44 +43,35 @@
33extern int touch_main(int argc, char **argv) 43extern int touch_main(int argc, char **argv)
34{ 44{
35 int fd; 45 int fd;
36 int create = TRUE; 46 int flags;
47 int status = EXIT_SUCCESS;
37 48
38 /* Parse options */ 49 flags = bb_getopt_ulflags(argc, argv, "c");
39 while (--argc > 0 && **(++argv) == '-') { 50
40 while (*(++(*argv))) { 51 argv += optind;
41 switch (**argv) {
42 case 'c':
43 create = FALSE;
44 break;
45 default:
46 show_usage();
47 }
48 }
49 }
50 52
51 if (argc < 1) { 53 if (!*argv) {
52 show_usage(); 54 bb_show_usage();
53 } 55 }
54 56
55 while (argc > 0) { 57 do {
56 fd = open(*argv, create ? O_RDWR | O_CREAT : O_RDWR,
57 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
58 if (fd < 0) {
59 if (! create && errno == ENOENT) {
60 argc--;
61 argv++;
62 continue;
63 } else {
64 perror_msg_and_die("%s", *argv);
65 }
66 }
67 close(fd);
68 if (utime(*argv, NULL)) { 58 if (utime(*argv, NULL)) {
69 perror_msg_and_die("%s", *argv); 59 if (errno == ENOENT) { /* no such file*/
60 if (flags & 1) { /* Creation is disabled, so ignore. */
61 continue;
62 }
63 /* Try to create the file. */
64 fd = open(*argv, O_RDWR | O_CREAT,
65 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
66 );
67 if ((fd >= 0) && !close(fd)) {
68 continue;
69 }
70 }
71 status = EXIT_FAILURE;
72 bb_perror_msg("%s", *argv);
70 } 73 }
71 argc--; 74 } while (*++argv);
72 argv++;
73 }
74 75
75 return EXIT_SUCCESS; 76 return status;
76} 77}
diff --git a/coreutils/tr.c b/coreutils/tr.c
index e2a4ef6c5..a00e36134 100644
--- a/coreutils/tr.c
+++ b/coreutils/tr.c
@@ -54,7 +54,7 @@ static void convert(void)
54 if (in_index == read_chars) { 54 if (in_index == read_chars) {
55 if ((read_chars = read(0, (char *) pinput, BUFSIZ)) <= 0) { 55 if ((read_chars = read(0, (char *) pinput, BUFSIZ)) <= 0) {
56 if (write(1, (char *) poutput, out_index) != out_index) 56 if (write(1, (char *) poutput, out_index) != out_index)
57 error_msg("%s", write_error); 57 bb_error_msg(bb_msg_write_error);
58 exit(0); 58 exit(0);
59 } 59 }
60 in_index = 0; 60 in_index = 0;
@@ -68,7 +68,7 @@ static void convert(void)
68 poutput[out_index++] = last = coded; 68 poutput[out_index++] = last = coded;
69 if (out_index == BUFSIZ) { 69 if (out_index == BUFSIZ) {
70 if (write(1, (char *) poutput, out_index) != out_index) 70 if (write(1, (char *) poutput, out_index) != out_index)
71 error_msg_and_die("%s", write_error); 71 bb_error_msg_and_die(bb_msg_write_error);
72 out_index = 0; 72 out_index = 0;
73 } 73 }
74 } 74 }
@@ -102,7 +102,7 @@ static unsigned int expand(const char *arg, register unsigned char *buffer)
102 while (*arg) { 102 while (*arg) {
103 if (*arg == '\\') { 103 if (*arg == '\\') {
104 arg++; 104 arg++;
105 *buffer++ = process_escape_sequence(&arg); 105 *buffer++ = bb_process_escape_sequence(&arg);
106 } else if (*(arg+1) == '-') { 106 } else if (*(arg+1) == '-') {
107 ac = *(arg+2); 107 ac = *(arg+2);
108 if(ac == 0) { 108 if(ac == 0) {
@@ -181,7 +181,7 @@ extern int tr_main(int argc, char **argv)
181 sq_fl = TRUE; 181 sq_fl = TRUE;
182 break; 182 break;
183 default: 183 default:
184 show_usage(); 184 bb_show_usage();
185 } 185 }
186 } 186 }
187 idx++; 187 idx++;
@@ -197,7 +197,7 @@ extern int tr_main(int argc, char **argv)
197 input_length = complement(input, input_length); 197 input_length = complement(input, input_length);
198 if (argv[idx] != NULL) { 198 if (argv[idx] != NULL) {
199 if (*argv[idx] == '\0') 199 if (*argv[idx] == '\0')
200 error_msg_and_die("STRING2 cannot be empty"); 200 bb_error_msg_and_die("STRING2 cannot be empty");
201 output_length = expand(argv[idx], output); 201 output_length = expand(argv[idx], output);
202 map(input, input_length, output, output_length); 202 map(input, input_length, output, output_length);
203 } 203 }
diff --git a/coreutils/true.c b/coreutils/true.c
index 9644aad4a..0fbe1023d 100644
--- a/coreutils/true.c
+++ b/coreutils/true.c
@@ -21,7 +21,8 @@
21 * 21 *
22 */ 22 */
23 23
24/* getopt not needed */ 24/* BB_AUDIT SUSv3 compliant */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/true.html */
25 26
26#include <stdlib.h> 27#include <stdlib.h>
27#include "busybox.h" 28#include "busybox.h"
diff --git a/coreutils/tty.c b/coreutils/tty.c
index 4510c2996..cd2c784fd 100644
--- a/coreutils/tty.c
+++ b/coreutils/tty.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini tty implementation for busybox 3 * tty implementation for busybox
4 * 4 *
5 * Copyright (C) 2000 Edward Betts <edward@debian.org>. 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -20,25 +20,39 @@
20 * 20 *
21 */ 21 */
22 22
23/* BB_AUDIT SUSv3 compliant */
24/* http://www.opengroup.org/onlinepubs/007904975/utilities/tty.html */
25
23#include <stdio.h> 26#include <stdio.h>
24#include <stdlib.h> 27#include <stdlib.h>
25#include <unistd.h> 28#include <unistd.h>
26#include <sys/types.h>
27#include "busybox.h" 29#include "busybox.h"
28 30
29extern int tty_main(int argc, char **argv) 31extern int tty_main(int argc, char **argv)
30{ 32{
31 char *tty; 33 const char *s;
32 34 int silent; /* Note: No longer relevant in SUSv3. */
33 if (argc > 1) { 35 int retval;
34 if (argv[1][0] != '-' || argv[1][1] != 's') 36
35 show_usage(); 37 bb_default_error_retval = 2; /* SUSv3 requires > 1 for error. */
36 } else { 38
37 tty = ttyname(0); 39 silent = bb_getopt_ulflags(argc, argv, "s");
38 if (tty) 40
39 puts(tty); 41 /* gnu tty outputs a warning that it is ignoring all args. */
40 else 42 bb_warn_ignoring_args(argc - optind);
41 puts("not a tty"); 43
44 retval = 0;
45
46 if ((s = ttyname(0)) == NULL) {
47 /* According to SUSv3, ttyname can on fail with EBADF or ENOTTY.
48 * We know the file descriptor is good, so failure means not a tty. */
49 s = "not a tty";
50 retval = 1;
51 }
52
53 if (!silent) {
54 puts(s);
42 } 55 }
43 return(isatty(0) ? EXIT_SUCCESS : EXIT_FAILURE); 56
57 bb_fflush_stdout_and_exit(retval);
44} 58}
diff --git a/coreutils/uname.c b/coreutils/uname.c
index df4f14ea0..a3e52e39f 100644
--- a/coreutils/uname.c
+++ b/coreutils/uname.c
@@ -16,6 +16,9 @@
16 along with this program; if not, write to the Free Software Foundation, 16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 18
19/* BB_AUDIT SUSv3 compliant */
20/* http://www.opengroup.org/onlinepubs/007904975/utilities/uname.html */
21
19/* Option Example 22/* Option Example
20 23
21 -s, --sysname SunOS 24 -s, --sysname SunOS
@@ -33,13 +36,18 @@
33 36
34/* Further size reductions by Glenn McGrath and Manuel Novoa III. */ 37/* Further size reductions by Glenn McGrath and Manuel Novoa III. */
35 38
39/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
40 *
41 * Now does proper error checking on i/o. Plus some further space savings.
42 */
43
36#include <stdio.h> 44#include <stdio.h>
37#include <stdlib.h> 45#include <stdlib.h>
38#include <stddef.h> 46#include <stddef.h>
39#include <string.h> 47#include <string.h>
48#include <unistd.h>
40#include <sys/types.h> 49#include <sys/types.h>
41#include <sys/utsname.h> 50#include <sys/utsname.h>
42#include <getopt.h>
43#include "busybox.h" 51#include "busybox.h"
44 52
45typedef struct { 53typedef struct {
@@ -48,7 +56,6 @@ typedef struct {
48} uname_info_t; 56} uname_info_t;
49 57
50static const char options[] = "snrvmpa"; 58static const char options[] = "snrvmpa";
51static const char flags[] = "\x01\x02\x04\x08\x10\x20\x3f";
52static const unsigned short int utsname_offset[] = { 59static const unsigned short int utsname_offset[] = {
53 offsetof(uname_info_t,name.sysname), 60 offsetof(uname_info_t,name.sysname),
54 offsetof(uname_info_t,name.nodename), 61 offsetof(uname_info_t,name.nodename),
@@ -61,29 +68,28 @@ static const unsigned short int utsname_offset[] = {
61int uname_main(int argc, char **argv) 68int uname_main(int argc, char **argv)
62{ 69{
63 uname_info_t uname_info; 70 uname_info_t uname_info;
64
65#if defined(__sparc__) && defined(__linux__) 71#if defined(__sparc__) && defined(__linux__)
66 char *fake_sparc = getenv("FAKE_SPARC"); 72 char *fake_sparc = getenv("FAKE_SPARC");
67#endif 73#endif
68
69 const unsigned short int *delta; 74 const unsigned short int *delta;
70 int opt; 75 char toprint;
71 char toprint = 0;
72 76
73 while ((opt = getopt(argc, argv, options)) != -1) { 77 toprint = bb_getopt_ulflags(argc, argv, options);
74 const char *p = strchr(options,opt); 78
75 if (p == NULL) { 79 if (argc != optind) {
76 show_usage(); 80 bb_show_usage();
77 } 81 }
78 toprint |= flags[(int)(p-options)]; 82
83 if (toprint & (1 << 6)) {
84 toprint = 0x3f;
79 } 85 }
80 86
81 if (toprint == 0) { 87 if (toprint == 0) {
82 toprint = flags[0]; /* sysname */ 88 toprint = 1; /* sysname */
83 } 89 }
84 90
85 if (uname(&uname_info.name) == -1) { 91 if (uname(&uname_info.name) == -1) {
86 error_msg_and_die("cannot get system name"); 92 bb_error_msg_and_die("cannot get system name");
87 } 93 }
88 94
89#if defined(__sparc__) && defined(__linux__) 95#if defined(__sparc__) && defined(__linux__)
@@ -99,7 +105,7 @@ int uname_main(int argc, char **argv)
99 delta=utsname_offset; 105 delta=utsname_offset;
100 do { 106 do {
101 if (toprint & 1) { 107 if (toprint & 1) {
102 printf(((char *)(&uname_info)) + *delta); 108 bb_printf(((char *)(&uname_info)) + *delta);
103 if (toprint > 1) { 109 if (toprint > 1) {
104 putchar(' '); 110 putchar(' ');
105 } 111 }
@@ -108,5 +114,5 @@ int uname_main(int argc, char **argv)
108 } while (toprint >>= 1); 114 } while (toprint >>= 1);
109 putchar('\n'); 115 putchar('\n');
110 116
111 return EXIT_SUCCESS; 117 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
112} 118}
diff --git a/coreutils/uniq.c b/coreutils/uniq.c
index cb63c4277..90686c9af 100644
--- a/coreutils/uniq.c
+++ b/coreutils/uniq.c
@@ -1,10 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini uniq implementation for busybox 3 * uniq implementation for busybox
4 * 4 *
5 * Copyright (C) 1999 by Lineo, inc. and John Beppu 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7 * Rewritten by Matt Kraai <kraai@alumni.carnegiemellon.edu>
8 * 6 *
9 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -22,67 +20,93 @@
22 * 20 *
23 */ 21 */
24 22
23/* BB_AUDIT SUSv3 compliant */
24/* http://www.opengroup.org/onlinepubs/007904975/utilities/uniq.html */
25
25#include <stdio.h> 26#include <stdio.h>
26#include <string.h>
27#include <getopt.h>
28#include <errno.h>
29#include <stdlib.h> 27#include <stdlib.h>
28#include <string.h>
29#include <ctype.h>
30#include <unistd.h>
30#include "busybox.h" 31#include "busybox.h"
32#include "libcoreutils/coreutils.h"
31 33
32static int print_count; 34static const char uniq_opts[] = "f:s:cdu\0\7\3\5\1\2\4";
33static int print_uniq = 1;
34static int print_duplicates = 1;
35
36static void print_line(char *line, int count, FILE *fp)
37{
38 if ((print_duplicates && count > 1) || (print_uniq && count == 1)) {
39 if (print_count)
40 fprintf(fp, "%7d\t%s", count, line);
41 else
42 fputs(line, fp);
43 }
44}
45 35
46int uniq_main(int argc, char **argv) 36int uniq_main(int argc, char **argv)
47{ 37{
48 FILE *in = stdin, *out = stdout; 38 FILE *in, *out;
49 char *lastline = NULL, *input; 39 /* Note: Ignore the warning about dups and e0 possibly being uninitialized.
50 int opt, count = 0; 40 * They will be initialized on the fist pass of the loop (since s0 is NULL). */
41 unsigned long dups, skip_fields, skip_chars, i;
42 const char *s0, *e0, *s1, *e1, *input_filename;
43 int opt;
44 int uniq_flags = 6; /* -u */
51 45
52 /* parse argv[] */ 46 skip_fields = skip_chars = 0;
53 while ((opt = getopt(argc, argv, "cdu")) > 0) { 47
54 switch (opt) { 48 while ((opt = getopt(argc, argv, uniq_opts)) > 0) {
55 case 'c': 49 if (opt == 'f') {
56 print_count = 1; 50 skip_fields = bb_xgetularg10(optarg);
57 break; 51 } else if (opt == 's') {
58 case 'd': 52 skip_chars = bb_xgetularg10(optarg);
59 print_duplicates = 1; 53 } else if ((s0 = strchr(uniq_opts, opt)) != NULL) {
60 print_uniq = 0; 54 uniq_flags &= s0[4];
61 break; 55 uniq_flags |= s0[7];
62 case 'u': 56 } else {
63 print_duplicates = 0; 57 bb_show_usage();
64 print_uniq = 1;
65 break;
66 } 58 }
67 } 59 }
68 60
69 if (argv[optind] != NULL) { 61 input_filename = *(argv += optind);
70 in = xfopen(argv[optind], "r"); 62
71 if (argv[optind+1] != NULL) 63 in = xgetoptfile_sort_uniq(argv, "r");
72 out = xfopen(argv[optind+1], "w"); 64 if (*argv) {
65 ++argv;
73 } 66 }
67 out = xgetoptfile_sort_uniq(argv, "w");
68 if (*argv && argv[1]) {
69 bb_show_usage();
70 }
71
72 s0 = NULL;
74 73
75 while ((input = get_line_from_file(in)) != NULL) { 74 /* gnu uniq ignores newlines */
76 if (lastline == NULL || strcmp(input, lastline) != 0) { 75 while ((s1 = bb_get_chomped_line_from_file(in)) != NULL) {
77 print_line(lastline, count, out); 76 e1 = s1;
78 free(lastline); 77 for (i=skip_fields ; i ; i--) {
79 lastline = input; 78 e1 = bb_skip_whitespace(e1);
80 count = 0; 79 while (*e1 && !isspace(*e1)) {
80 ++e1;
81 }
82 }
83 for (i = skip_chars ; *e1 && i ; i--) {
84 ++e1;
85 }
86 if (s0) {
87 if (strcmp(e0, e1) == 0) {
88 ++dups; /* Note: Testing for overflow seems excessive. */
89 continue;
90 }
91 DO_LAST:
92 if ((dups && (uniq_flags & 2)) || (!dups && (uniq_flags & 4))) {
93 bb_fprintf(out, "\0%7d\t" + (uniq_flags & 1), dups + 1);
94 bb_fprintf(out, "%s\n", s0);
95 }
96 free((void *)s0);
81 } 97 }
82 count++; 98
99 s0 = s1;
100 e0 = e1;
101 dups = 0;
102 }
103
104 if (s0) {
105 e1 = NULL;
106 goto DO_LAST;
83 } 107 }
84 print_line(lastline, count, out);
85 free(lastline);
86 108
87 return EXIT_SUCCESS; 109 bb_xferror(in, input_filename);
110
111 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
88} 112}
diff --git a/coreutils/usleep.c b/coreutils/usleep.c
index ac268b485..f570f2734 100644
--- a/coreutils/usleep.c
+++ b/coreutils/usleep.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini usleep implementation for busybox 3 * usleep implementation for busybox
4 * 4 *
5 * Copyright (C) 1995, 1996 by Bruce Perens <bruce@pixar.com>. 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -20,18 +20,22 @@
20 * 20 *
21 */ 21 */
22 22
23/* getopt not needed */ 23/* BB_AUDIT SUSv3 N/A -- Apparently a busybox extension. */
24 24
25#include <stdlib.h> 25#include <stdlib.h>
26#include <limits.h>
26#include <unistd.h> 27#include <unistd.h>
27#include "busybox.h" 28#include "busybox.h"
28 29
29extern int usleep_main(int argc, char **argv) 30extern int usleep_main(int argc, char **argv)
30{ 31{
31 if ((argc < 2) || (**(argv + 1) == '-')) { 32 if (argc != 2) {
32 show_usage(); 33 bb_show_usage();
34 }
35
36 if (usleep(bb_xgetularg10_bnd(argv[1], 0, UINT_MAX))) {
37 bb_perror_nomsg_and_die();
33 } 38 }
34 39
35 usleep(atoi(*(++argv))); /* return void */
36 return EXIT_SUCCESS; 40 return EXIT_SUCCESS;
37} 41}
diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c
index a5ac002e9..4f9270c18 100644
--- a/coreutils/uudecode.c
+++ b/coreutils/uudecode.c
@@ -46,7 +46,7 @@ static int read_stduu (const char *inname)
46 char *p; 46 char *p;
47 47
48 if (fgets (buf, sizeof(buf), stdin) == NULL) { 48 if (fgets (buf, sizeof(buf), stdin) == NULL) {
49 error_msg("%s: Short file", inname); 49 bb_error_msg("%s: Short file", inname);
50 return FALSE; 50 return FALSE;
51 } 51 }
52 p = buf; 52 p = buf;
@@ -81,7 +81,7 @@ static int read_stduu (const char *inname)
81 81
82 if (fgets (buf, sizeof(buf), stdin) == NULL 82 if (fgets (buf, sizeof(buf), stdin) == NULL
83 || strcmp (buf, "end\n")) { 83 || strcmp (buf, "end\n")) {
84 error_msg("%s: No `end' line", inname); 84 bb_error_msg("%s: No `end' line", inname);
85 return FALSE; 85 return FALSE;
86 } 86 }
87 87
@@ -131,7 +131,7 @@ static int read_base64 (const char *inname)
131 unsigned char *p; 131 unsigned char *p;
132 132
133 if (fgets (buf, sizeof(buf), stdin) == NULL) { 133 if (fgets (buf, sizeof(buf), stdin) == NULL) {
134 error_msg("%s: Short file", inname); 134 bb_error_msg("%s: Short file", inname);
135 return FALSE; 135 return FALSE;
136 } 136 }
137 p = buf; 137 p = buf;
@@ -139,7 +139,7 @@ static int read_base64 (const char *inname)
139 if (memcmp (buf, "====", 4) == 0) 139 if (memcmp (buf, "====", 4) == 0)
140 break; 140 break;
141 if (last_data != 0) { 141 if (last_data != 0) {
142 error_msg("%s: data following `=' padding character", inname); 142 bb_error_msg("%s: data following `=' padding character", inname);
143 return FALSE; 143 return FALSE;
144 } 144 }
145 145
@@ -161,14 +161,14 @@ static int read_base64 (const char *inname)
161 161
162 while ((b64_tab[*p] & '\100') != 0) 162 while ((b64_tab[*p] & '\100') != 0)
163 if (*p == '\n' || *p++ == '=') { 163 if (*p == '\n' || *p++ == '=') {
164 error_msg("%s: illegal line", inname); 164 bb_error_msg("%s: illegal line", inname);
165 return FALSE; 165 return FALSE;
166 } 166 }
167 c2 = b64_tab[*p++]; 167 c2 = b64_tab[*p++];
168 168
169 while (b64_tab[*p] == '\177') 169 while (b64_tab[*p] == '\177')
170 if (*p++ == '\n') { 170 if (*p++ == '\n') {
171 error_msg("%s: illegal line", inname); 171 bb_error_msg("%s: illegal line", inname);
172 return FALSE; 172 return FALSE;
173 } 173 }
174 if (*p == '=') { 174 if (*p == '=') {
@@ -180,7 +180,7 @@ static int read_base64 (const char *inname)
180 180
181 while (b64_tab[*p] == '\177') 181 while (b64_tab[*p] == '\177')
182 if (*p++ == '\n') { 182 if (*p++ == '\n') {
183 error_msg("%s: illegal line", inname); 183 bb_error_msg("%s: illegal line", inname);
184 return FALSE; 184 return FALSE;
185 } 185 }
186 putchar (c1 << 2 | c2 >> 4); 186 putchar (c1 << 2 | c2 >> 4);
@@ -213,7 +213,7 @@ static int decode (const char *inname,
213 213
214 while (1) { 214 while (1) {
215 if (fgets (buf, sizeof (buf), stdin) == NULL) { 215 if (fgets (buf, sizeof (buf), stdin) == NULL) {
216 error_msg("%s: No `begin' line", inname); 216 bb_error_msg("%s: No `begin' line", inname);
217 return FALSE; 217 return FALSE;
218 } 218 }
219 219
@@ -239,13 +239,13 @@ static int decode (const char *inname,
239 while (*p != '/') 239 while (*p != '/')
240 ++p; 240 ++p;
241 if (*p == '\0') { 241 if (*p == '\0') {
242 error_msg("%s: Illegal ~user", inname); 242 bb_error_msg("%s: Illegal ~user", inname);
243 return FALSE; 243 return FALSE;
244 } 244 }
245 *p++ = '\0'; 245 *p++ = '\0';
246 pw = getpwnam (buf + 1); 246 pw = getpwnam (buf + 1);
247 if (pw == NULL) { 247 if (pw == NULL) {
248 error_msg("%s: No user `%s'", inname, buf + 1); 248 bb_error_msg("%s: No user `%s'", inname, buf + 1);
249 return FALSE; 249 return FALSE;
250 } 250 }
251 outname = concat_path_file(pw->pw_dir, p); 251 outname = concat_path_file(pw->pw_dir, p);
@@ -258,7 +258,7 @@ static int decode (const char *inname,
258 && (freopen (outname, "w", stdout) == NULL 258 && (freopen (outname, "w", stdout) == NULL
259 || chmod (outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)) 259 || chmod (outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO))
260 )) { 260 )) {
261 perror_msg("%s", outname); /* */ 261 bb_perror_msg("%s", outname); /* */
262 if (dofre) 262 if (dofre)
263 free(outname); 263 free(outname);
264 return FALSE; 264 return FALSE;
@@ -295,7 +295,7 @@ int uudecode_main (int argc,
295 break; 295 break;
296 296
297 default: 297 default:
298 show_usage(); 298 bb_show_usage();
299 } 299 }
300 } 300 }
301 301
@@ -308,7 +308,7 @@ int uudecode_main (int argc,
308 if (decode (argv[optind], outname) != 0) 308 if (decode (argv[optind], outname) != 0)
309 exit_status = FALSE; 309 exit_status = FALSE;
310 } else { 310 } else {
311 perror_msg("%s", argv[optind]); 311 bb_perror_msg("%s", argv[optind]);
312 exit_status = EXIT_FAILURE; 312 exit_status = EXIT_FAILURE;
313 } 313 }
314 optind++; 314 optind++;
diff --git a/coreutils/uuencode.c b/coreutils/uuencode.c
index 49b2d9189..fd3326d80 100644
--- a/coreutils/uuencode.c
+++ b/coreutils/uuencode.c
@@ -29,7 +29,7 @@
29#include "busybox.h" 29#include "busybox.h"
30 30
31/* Conversion table. for base 64 */ 31/* Conversion table. for base 64 */
32static char tbl_base64[65] = { 32static const char tbl_base64[65] = {
33 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 33 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
34 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 34 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
35 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 35 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
@@ -41,7 +41,7 @@ static char tbl_base64[65] = {
41 '=' /* termination character */ 41 '=' /* termination character */
42}; 42};
43 43
44static char tbl_std[65] = { 44static const char tbl_std[65] = {
45 '`', '!', '"', '#', '$', '%', '&', '\'', 45 '`', '!', '"', '#', '$', '%', '&', '\'',
46 '(', ')', '*', '+', ',', '-', '.', '/', 46 '(', ')', '*', '+', ',', '-', '.', '/',
47 '0', '1', '2', '3', '4', '5', '6', '7', 47 '0', '1', '2', '3', '4', '5', '6', '7',
@@ -92,40 +92,36 @@ int uuencode_main(int argc, char **argv)
92 int write_size = dst_buf_size; 92 int write_size = dst_buf_size;
93 struct stat stat_buf; 93 struct stat stat_buf;
94 FILE *src_stream = stdin; 94 FILE *src_stream = stdin;
95 char *tbl = tbl_std; 95 const char *tbl;
96 size_t size; 96 size_t size;
97 mode_t mode; 97 mode_t mode;
98 int opt;
99 RESERVE_CONFIG_BUFFER(src_buf, SRC_BUF_SIZE + 1); 98 RESERVE_CONFIG_BUFFER(src_buf, SRC_BUF_SIZE + 1);
100 RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1); 99 RESERVE_CONFIG_BUFFER(dst_buf, DST_BUF_SIZE + 1);
101 100
102 while ((opt = getopt(argc, argv, "m")) != -1) { 101 tbl = tbl_std;
103 switch (opt) { 102 if (bb_getopt_ulflags(argc, argv, "m") & 1) {
104 case 'm': 103 tbl = tbl_base64;
105 tbl = tbl_base64;
106 break;
107 default:
108 show_usage();
109 }
110 } 104 }
111 105
112 switch (argc - optind) { 106 switch (argc - optind) {
113 case 2: 107 case 2:
114 src_stream = xfopen(argv[optind], "r"); 108 src_stream = bb_xfopen(argv[optind], "r");
115 stat(argv[optind], &stat_buf); 109 if (stat(argv[optind], &stat_buf) < 0) {
110 bb_perror_msg_and_die("stat");
111 }
116 mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 112 mode = stat_buf.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
117 if (src_stream == stdout) { 113 if (src_stream == stdout) {
118 printf("NULL\n"); 114 puts("NULL");
119 } 115 }
120 break; 116 break;
121 case 1: 117 case 1:
122 mode = 0666 & ~umask(0666); 118 mode = 0666 & ~umask(0666);
123 break; 119 break;
124 default: 120 default:
125 show_usage(); 121 bb_show_usage();
126 } 122 }
127 123
128 printf("begin%s %o %s", tbl == tbl_std ? "" : "-base64", mode, argv[argc - 1]); 124 bb_printf("begin%s %o %s", tbl == tbl_std ? "" : "-base64", mode, argv[argc - 1]);
129 125
130 while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) { 126 while ((size = fread(src_buf, 1, src_buf_size, src_stream)) > 0) {
131 if (size != src_buf_size) { 127 if (size != src_buf_size) {
@@ -142,10 +138,12 @@ int uuencode_main(int argc, char **argv)
142 putchar(tbl[size]); 138 putchar(tbl[size]);
143 } 139 }
144 if (fwrite(dst_buf, 1, write_size, stdout) != write_size) { 140 if (fwrite(dst_buf, 1, write_size, stdout) != write_size) {
145 perror("Couldnt finish writing"); 141 bb_perror_msg_and_die(bb_msg_write_error);
146 } 142 }
147 } 143 }
148 printf(tbl == tbl_std ? "\n`\nend\n" : "\n====\n"); 144 bb_printf(tbl == tbl_std ? "\n`\nend\n" : "\n====\n");
145
146 bb_xferror(src_stream, "source"); /* TODO - Fix this! */
149 147
150 return(EXIT_SUCCESS); 148 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
151} 149}
diff --git a/coreutils/watch.c b/coreutils/watch.c
index c275f3bb3..f9f40189e 100644
--- a/coreutils/watch.c
+++ b/coreutils/watch.c
@@ -20,67 +20,76 @@
20 * 20 *
21 */ 21 */
22 22
23/* getopt not needed */ 23/* BB_AUDIT SUSv3 N/A */
24/* BB_AUDIT GNU defects -- only option -n is supported. */
25
26/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
27 *
28 * Removed dependency on date_main(), added proper error checking, and
29 * reduced size.
30 */
24 31
25#include <stdio.h> 32#include <stdio.h>
26#include <errno.h>
27#include <unistd.h>
28#include <stdlib.h> 33#include <stdlib.h>
29#include <string.h> 34#include <string.h>
35#include <limits.h>
36#include <time.h>
37#include <assert.h>
38#include <unistd.h>
30#include <sys/wait.h> 39#include <sys/wait.h>
31#include "busybox.h" 40#include "busybox.h"
32 41
33extern int watch_main(int argc, char **argv) 42extern int watch_main(int argc, char **argv)
34{ 43{
35 const char date_argv[2][10] = { "date", "" };
36 const int header_len = 40; 44 const int header_len = 40;
37 char header[header_len + 1]; 45 time_t t;
38 int period = 2;
39 char **cargv;
40 int cargc;
41 pid_t pid; 46 pid_t pid;
47 unsigned period = 2;
42 int old_stdout; 48 int old_stdout;
43 int i; 49 int len, len2;
50 char **watched_argv;
51 char header[header_len + 1];
44 52
45 if (argc < 2) { 53 if (argc < 2) {
46 show_usage(); 54 bb_show_usage();
47 } else {
48 cargv = argv + 1;
49 cargc = argc - 1;
50
51 /* don't use getopt, because it permutes the arguments */
52 if (argc >= 3 && !strcmp(argv[1], "-n")) {
53 period = strtol(argv[2], NULL, 10);
54 if (period < 1)
55 show_usage();
56 cargv += 2;
57 cargc -= 2;
58 }
59 } 55 }
60 56
61 57 /* don't use getopt, because it permutes the arguments */
62 /* create header */ 58 ++argv;
63 snprintf(header, header_len, "Every %ds: ", period); 59 if ((argc > 3) && !strcmp(*argv, "-n")
64 for (i = 0; i < cargc && (strlen(header) + strlen(cargv[i]) < header_len); 60 ) {
65 i++) { 61 period = bb_xgetularg10_bnd(argv[1], 1, UINT_MAX);
66 strcat(header, cargv[i]); 62 argv += 2;
67 strcat(header, " ");
68 } 63 }
64 watched_argv = argv;
69 65
70 /* fill with blanks */ 66 /* create header */
71 for (i = strlen(header); i < header_len; i++)
72 header[i] = ' ';
73 67
74 header[header_len - 1] = '\0'; 68 len = snprintf(header, header_len, "Every %ds:", period);
69 /* Don't bother checking for len < 0, as it should never happen.
70 * But, just to be prepared... */
71 assert(len >= 0);
72 do {
73 len2 = strlen(*argv);
74 if (len + len2 >= header_len-1) {
75 break;
76 }
77 header[len] = ' ';
78 memcpy(header+len+1, *argv, len2);
79 len += len2+1;
80 } while (*++argv);
75 81
82 header[len] = 0;
76 83
77 /* thanks to lye, who showed me how to redirect stdin/stdout */ 84 /* thanks to lye, who showed me how to redirect stdin/stdout */
78 old_stdout = dup(1); 85 old_stdout = dup(1);
79 86
80 while (1) { 87 while (1) {
81 printf("\033[H\033[J%s", header); 88 time(&t);
82 date_main(1, (char **) date_argv); 89 /* Use dprintf to avoid fflush()ing stdout. */
83 printf("\n"); 90 if (dprintf(1, "\033[H\033[J%-*s%s\n", header_len, header, ctime(&t)) < 0) {
91 bb_perror_msg_and_die("printf");
92 }
84 93
85 pid = vfork(); /* vfork, because of ucLinux */ 94 pid = vfork(); /* vfork, because of ucLinux */
86 if (pid > 0) { 95 if (pid > 0) {
@@ -91,13 +100,11 @@ extern int watch_main(int argc, char **argv)
91 //child 100 //child
92 close(1); 101 close(1);
93 dup(old_stdout); 102 dup(old_stdout);
94 if (execvp(*cargv, cargv)) 103 if (execvp(*watched_argv, watched_argv)) {
95 error_msg_and_die("Couldn't run command\n"); 104 bb_error_msg_and_die("Couldn't run command\n");
105 }
96 } else { 106 } else {
97 error_msg_and_die("Couldn't vfork\n"); 107 bb_error_msg_and_die("Couldn't vfork\n");
98 } 108 }
99 } 109 }
100
101
102 return EXIT_SUCCESS;
103} 110}
diff --git a/coreutils/wc.c b/coreutils/wc.c
index 8e3b5bbf4..779901524 100644
--- a/coreutils/wc.c
+++ b/coreutils/wc.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini wc implementation for busybox 3 * wc implementation for busybox
4 * 4 *
5 * Copyright (C) 2000 Edward Betts <edward@debian.org> 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -20,159 +20,208 @@
20 * 20 *
21 */ 21 */
22 22
23/* BB_AUDIT SUSv3 _NOT_ compliant -- option -m is not currently supported. */
24/* http://www.opengroup.org/onlinepubs/007904975/utilities/wc.html */
25
26/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
27 *
28 * Rewritten to fix a number of problems and do some size optimizations.
29 * Problems in the previous busybox implementation (besides bloat) included:
30 * 1) broken 'wc -c' optimization (read note below)
31 * 2) broken handling of '-' args
32 * 3) no checking of ferror on EOF returns
33 * 4) isprint() wasn't considered when word counting.
34 *
35 * TODO:
36 *
37 * When locale support is enabled, count multibyte chars in the '-m' case.
38 *
39 * NOTES:
40 *
41 * The previous busybox wc attempted an optimization using stat for the
42 * case of counting chars only. I omitted that because it was broken.
43 * It didn't take into account the possibility of input coming from a
44 * pipe, or input from a file with file pointer not at the beginning.
45 *
46 * To implement such a speed optimization correctly, not only do you
47 * need the size, but also the file position. Note also that the
48 * file position may be past the end of file. Consider the example
49 * (adapted from example in gnu wc.c)
50 *
51 * echo hello > /tmp/testfile &&
52 * (dd ibs=1k skip=1 count=0 &> /dev/null ; wc -c) < /tmp/testfile
53 *
54 * for which 'wc -c' should output '0'.
55 */
56
23#include <stdio.h> 57#include <stdio.h>
24#include <getopt.h>
25#include <string.h>
26#include <stdlib.h> 58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
27#include "busybox.h" 61#include "busybox.h"
28 62
29enum print_e { 63#ifdef CONFIG_LOCALE_SUPPORT
30 print_lines = 1, 64#include <locale.h>
31 print_words = 2, 65#include <ctype.h>
32 print_chars = 4, 66#define isspace_given_isprint(c) isspace(c)
33 print_length = 8 67#else
68#undef isspace
69#undef isprint
70#define isspace(c) ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9))))
71#define isprint(c) (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20))
72#define isspace_given_isprint(c) ((c) == ' ')
73#endif
74
75enum {
76 WC_LINES = 0,
77 WC_WORDS = 1,
78 WC_CHARS = 2,
79 WC_LENGTH = 3
34}; 80};
35 81
36static unsigned int total_lines = 0; 82/* Note: If this changes, remember to change the initialization of
37static unsigned int total_words = 0; 83 * 'name' in wc_main. It needs to point to the terminating nul. */
38static unsigned int total_chars = 0; 84static const char wc_opts[] = "lwcL"; /* READ THE WARNING ABOVE! */
39static unsigned int max_length = 0;
40static char print_type = 0;
41 85
42static void print_counts(const unsigned int lines, const unsigned int words, 86enum {
43 const unsigned int chars, const unsigned int length, const char *name) 87 OP_INC_LINE = 1, /* OP_INC_LINE must be 1. */
44{ 88 OP_SPACE = 2,
45 int output = 0; 89 OP_NEWLINE = 4,
90 OP_TAB = 8,
91 OP_NUL = 16,
92};
46 93
47 if (print_type & print_lines) { 94/* Note: If fmt_str changes, the offsets to 's' in the OUTPUT section
48 printf("%7d", lines); 95 * will need to be updated. */
49 output++; 96static const char fmt_str[] = " %7u\0 %s\n";
50 } 97static const char total_str[] = "total";
51 if (print_type & print_words) {
52 if (output++)
53 putchar(' ');
54 printf("%7d", words);
55 }
56 if (print_type & print_chars) {
57 if (output++)
58 putchar(' ');
59 printf("%7d", chars);
60 }
61 if (print_type & print_length) {
62 if (output++)
63 putchar(' ');
64 printf("%7d", length);
65 }
66 if (*name) {
67 printf(" %s", name);
68 }
69 putchar('\n');
70}
71 98
72static void wc_file(FILE * file, const char *name) 99int wc_main(int argc, char **argv)
73{ 100{
74 unsigned int lines = 0; 101 FILE *fp;
75 unsigned int words = 0; 102 const char *s;
76 unsigned int chars = 0; 103 unsigned int *pcounts;
77 unsigned int length = 0; 104 unsigned int counts[4];
78 unsigned int linepos = 0; 105 unsigned int totals[4];
79 char in_word = 0; 106 unsigned int linepos;
107 unsigned int u;
108 int num_files = 0;
80 int c; 109 int c;
81 110 char status = EXIT_SUCCESS;
82 while ((c = getc(file)) != EOF) { 111 char in_word;
83 chars++; 112 char print_type;
84 switch (c) { 113
85 case '\n': 114 print_type = bb_getopt_ulflags(argc, argv, wc_opts);
86 lines++; 115
87 case '\r': 116 if (print_type == 0) {
88 case '\f': 117 print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
89 if (linepos > length)
90 length = linepos;
91 linepos = 0;
92 goto word_separator;
93 case '\t':
94 linepos += 8 - (linepos % 8);
95 goto word_separator;
96 case ' ':
97 linepos++;
98 case '\v':
99word_separator:
100 if (in_word) {
101 in_word = 0;
102 words++;
103 }
104 break;
105 default:
106 linepos++;
107 in_word = 1;
108 break;
109 }
110 }
111 if (linepos > length) {
112 length = linepos;
113 }
114 if (in_word) {
115 words++;
116 } 118 }
117 print_counts(lines, words, chars, length, name); 119
118 total_lines += lines; 120 argv += optind;
119 total_words += words; 121 if (!*argv) {
120 total_chars += chars; 122 *--argv = (char *) bb_msg_standard_input;
121 if (length > max_length) {
122 max_length = length;
123 } 123 }
124} 124
125 125 memset(totals, 0, sizeof(totals));
126int wc_main(int argc, char **argv) 126
127{ 127 pcounts = counts;
128 int opt; 128
129 129 do {
130 while ((opt = getopt(argc, argv, "clLw")) > 0) { 130 ++num_files;
131 switch (opt) { 131 if (!(fp = bb_wfopen_input(*argv))) {
132 case 'c': 132 status = EXIT_FAILURE;
133 print_type |= print_chars; 133 continue;
134 break;
135 case 'l':
136 print_type |= print_lines;
137 break;
138 case 'L':
139 print_type |= print_length;
140 break;
141 case 'w':
142 print_type |= print_words;
143 break;
144 default:
145 show_usage();
146 } 134 }
147 } 135
148 136 memset(counts, 0, sizeof(counts));
149 if (print_type == 0) { 137 linepos = 0;
150 print_type = print_lines | print_words | print_chars; 138 in_word = 0;
151 } 139
152 140 do {
153 if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { 141 ++counts[WC_CHARS];
154 wc_file(stdin, ""); 142 c = getc(fp);
155 } else { 143 if (isprint(c)) {
156 unsigned short num_files_counted = 0; 144 ++linepos;
157 while (optind < argc) { 145 if (!isspace_given_isprint(c)) {
158 if (print_type == print_chars) { 146 in_word = 1;
159 struct stat statbuf; 147 continue;
160 stat(argv[optind], &statbuf); 148 }
161 print_counts(0, 0, statbuf.st_size, 0, argv[optind]); 149 } else if (((unsigned int)(c - 9)) <= 4) {
162 total_chars += statbuf.st_size; 150 /* \t 9
151 * \n 10
152 * \v 11
153 * \f 12
154 * \r 13
155 */
156 if (c == '\t') {
157 linepos = (linepos | 7) + 1;
158 } else { /* '\n', '\r', '\f', or '\v' */
159 DO_EOF:
160 if (linepos > counts[WC_LENGTH]) {
161 counts[WC_LENGTH] = linepos;
162 }
163 if (c == '\n') {
164 ++counts[WC_LINES];
165 }
166 if (c != '\v') {
167 linepos = 0;
168 }
169 }
170 } else if (c == EOF) {
171 if (ferror(fp)) {
172 bb_perror_msg("%s", *argv);
173 status = EXIT_FAILURE;
174 }
175 --counts[WC_CHARS];
176 goto DO_EOF; /* Treat an EOF as '\r'. */
163 } else { 177 } else {
164 FILE *file; 178 continue;
165 file = xfopen(argv[optind], "r"); 179 }
166 wc_file(file, argv[optind]); 180
167 fclose(file); 181 counts[WC_WORDS] += in_word;
182 in_word = 0;
183 if (c == EOF) {
184 break;
168 } 185 }
169 optind++; 186 } while (1);
170 num_files_counted++; 187
188 if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
189 totals[WC_LENGTH] = counts[WC_LENGTH];
171 } 190 }
172 if (num_files_counted > 1) { 191 totals[WC_LENGTH] -= counts[WC_LENGTH];
173 print_counts(total_lines, total_words, total_chars, max_length, "total"); 192
193 bb_fclose_nonstdin(fp);
194
195 OUTPUT:
196 s = fmt_str + 1; /* Skip the leading space on 1st pass. */
197 u = 0;
198 do {
199 if (print_type & (1 << u)) {
200 bb_printf(s, pcounts[u]);
201 s = fmt_str; /* Ok... restore the leading space. */
202 }
203 totals[u] += pcounts[u];
204 } while (++u < 4);
205
206 s += 8; /* Set the format to the empty string. */
207
208 if (*argv != bb_msg_standard_input) {
209 s -= 3; /* We have a name, so do %s conversion. */
174 } 210 }
211 bb_printf(s, *argv);
212
213 } while (*++argv);
214
215 /* If more than one file was processed, we want the totals. To save some
216 * space, we set the pcounts ptr to the totals array. This has the side
217 * effect of trashing the totals array after outputting it, but that's
218 * irrelavent since we no longer need it. */
219 if (num_files > 1) {
220 num_files = 0; /* Make sure we don't get here again. */
221 *--argv = (char *) total_str;
222 pcounts = totals;
223 goto OUTPUT;
175 } 224 }
176 225
177 return(EXIT_SUCCESS); 226 bb_fflush_stdout_and_exit(status);
178} 227}
diff --git a/coreutils/who.c b/coreutils/who.c
index 3da7ed1c7..1bf552056 100644
--- a/coreutils/who.c
+++ b/coreutils/who.c
@@ -23,7 +23,7 @@
23#include <errno.h> 23#include <errno.h>
24#include <string.h> 24#include <string.h>
25#include <time.h> 25#include <time.h>
26#include <busybox.h> 26#include "busybox.h"
27 27
28extern int who_main(int argc, char **argv) 28extern int who_main(int argc, char **argv)
29{ 29{
@@ -33,7 +33,7 @@ extern int who_main(int argc, char **argv)
33 time_t now, idle; 33 time_t now, idle;
34 34
35 if (argc > 1) 35 if (argc > 1)
36 show_usage(); 36 bb_show_usage();
37 37
38 setutent(); 38 setutent();
39 devlen = sizeof("/dev/") - 1; 39 devlen = sizeof("/dev/") - 1;
@@ -81,5 +81,3 @@ extern int who_main(int argc, char **argv)
81 81
82 return 0; 82 return 0;
83} 83}
84
85
diff --git a/coreutils/whoami.c b/coreutils/whoami.c
index a9d6ecf26..f93034d3a 100644
--- a/coreutils/whoami.c
+++ b/coreutils/whoami.c
@@ -20,7 +20,7 @@
20 * 20 *
21 */ 21 */
22 22
23/* getopt not needed */ 23/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
24 24
25#include <stdio.h> 25#include <stdio.h>
26#include <stdlib.h> 26#include <stdlib.h>
@@ -30,14 +30,15 @@
30extern int whoami_main(int argc, char **argv) 30extern int whoami_main(int argc, char **argv)
31{ 31{
32 char user[9]; 32 char user[9];
33 uid_t uid = geteuid(); 33 uid_t uid;
34 34
35 if (argc > 1) 35 if (argc > 1)
36 show_usage(); 36 bb_show_usage();
37 37
38 uid = geteuid();
38 if (my_getpwuid(user, uid)) { 39 if (my_getpwuid(user, uid)) {
39 puts(user); 40 puts(user);
40 return EXIT_SUCCESS; 41 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
41 } 42 }
42 error_msg_and_die("cannot find username for UID %u", (unsigned) uid); 43 bb_error_msg_and_die("cannot find username for UID %u", (unsigned) uid);
43} 44}
diff --git a/coreutils/yes.c b/coreutils/yes.c
index 7d9596d0b..74f7571cf 100644
--- a/coreutils/yes.c
+++ b/coreutils/yes.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini yes implementation for busybox 3 * yes implementation for busybox
4 * 4 *
5 * Copyright (C) 2000 Edward Betts <edward@debian.org>. 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -20,7 +20,12 @@
20 * 20 *
21 */ 21 */
22 22
23/* getopt not needed */ 23/* BB_AUDIT SUSv3 N/A -- Matches GNU behavior. */
24
25/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
26 *
27 * Size reductions and removed redundant applet name prefix from error messages.
28 */
24 29
25#include <stdio.h> 30#include <stdio.h>
26#include <stdlib.h> 31#include <stdlib.h>
@@ -28,26 +33,24 @@
28 33
29extern int yes_main(int argc, char **argv) 34extern int yes_main(int argc, char **argv)
30{ 35{
31 int i; 36 static const char fmt_str[] = " %s";
32 37 const char *fmt;
33 if (argc >= 2 && *argv[1] == '-') 38 char **first_arg;
34 show_usage();
35 39
36 if (argc == 1) { 40 *argv = "y";
37 while (1) 41 if (argc != 1) {
38 if (puts("y") == EOF) { 42 ++argv;
39 perror("yes");
40 return EXIT_FAILURE;
41 }
42 } 43 }
43 44
44 while (1) 45 first_arg = argv;
45 for (i = 1; i < argc; i++) 46 do {
46 if (fputs(argv[i], stdout) == EOF 47 fmt = fmt_str + 1;
47 || putchar(i == argc - 1 ? '\n' : ' ') == EOF) { 48 do {
48 perror("yes"); 49 bb_printf(fmt, *argv);
49 return EXIT_FAILURE; 50 fmt = fmt_str;
50 } 51 } while (*++argv);
52 argv = first_arg;
53 } while (putchar('\n') != EOF);
51 54
52 return EXIT_SUCCESS; 55 bb_perror_nomsg_and_die();
53} 56}
diff --git a/debianutils/mktemp.c b/debianutils/mktemp.c
index bc47d0af0..9d8de2174 100644
--- a/debianutils/mktemp.c
+++ b/debianutils/mktemp.c
@@ -32,7 +32,7 @@
32extern int mktemp_main(int argc, char **argv) 32extern int mktemp_main(int argc, char **argv)
33{ 33{
34 if (argc != 2 && (argc != 3 || strcmp(argv[1], "-q"))) 34 if (argc != 2 && (argc != 3 || strcmp(argv[1], "-q")))
35 show_usage(); 35 bb_show_usage();
36 if(mkstemp(argv[argc-1]) < 0) 36 if(mkstemp(argv[argc-1]) < 0)
37 return EXIT_FAILURE; 37 return EXIT_FAILURE;
38 (void) puts(argv[argc-1]); 38 (void) puts(argv[argc-1]);
diff --git a/debianutils/readlink.c b/debianutils/readlink.c
index da5259038..d8d7e8c2d 100644
--- a/debianutils/readlink.c
+++ b/debianutils/readlink.c
@@ -32,7 +32,7 @@ int readlink_main(int argc, char **argv)
32 /* no options, no getopt */ 32 /* no options, no getopt */
33 33
34 if (argc != 2) 34 if (argc != 2)
35 show_usage(); 35 bb_show_usage();
36 36
37 buf = xreadlink(argv[1]); 37 buf = xreadlink(argv[1]);
38 if (!buf) 38 if (!buf)
diff --git a/debianutils/run_parts.c b/debianutils/run_parts.c
index a6ad81096..a941e1fc8 100644
--- a/debianutils/run_parts.c
+++ b/debianutils/run_parts.c
@@ -78,7 +78,7 @@ int run_parts_main(int argc, char **argv)
78 { 78 {
79 const unsigned int mask = (unsigned int) strtol(optarg, NULL, 8); 79 const unsigned int mask = (unsigned int) strtol(optarg, NULL, 8);
80 if (mask > 07777) { 80 if (mask > 07777) {
81 perror_msg_and_die("bad umask value"); 81 bb_perror_msg_and_die("bad umask value");
82 } 82 }
83 umask(mask); 83 umask(mask);
84 } 84 }
@@ -90,13 +90,13 @@ int run_parts_main(int argc, char **argv)
90 args[argcount++] = optarg; 90 args[argcount++] = optarg;
91 break; 91 break;
92 default: 92 default:
93 show_usage(); 93 bb_show_usage();
94 } 94 }
95 } 95 }
96 96
97 /* We require exactly one argument: the directory name */ 97 /* We require exactly one argument: the directory name */
98 if (optind != (argc - 1)) { 98 if (optind != (argc - 1)) {
99 show_usage(); 99 bb_show_usage();
100 } 100 }
101 101
102 args[0] = argv[optind]; 102 args[0] = argv[optind];
diff --git a/debianutils/start_stop_daemon.c b/debianutils/start_stop_daemon.c
index 576526183..a1c2c21c2 100644
--- a/debianutils/start_stop_daemon.c
+++ b/debianutils/start_stop_daemon.c
@@ -74,7 +74,7 @@ parse_options(int argc, char * const *argv)
74 break; 74 break;
75 case 's': 75 case 's':
76 if (sscanf(optarg, "%d", &signal_nr) != 1) 76 if (sscanf(optarg, "%d", &signal_nr) != 1)
77 error_msg_and_die ("-s takes a numeric argument"); 77 bb_error_msg_and_die ("-s takes a numeric argument");
78 break; 78 break;
79 case 'u': 79 case 'u':
80 userspec = optarg; 80 userspec = optarg;
@@ -86,21 +86,21 @@ parse_options(int argc, char * const *argv)
86 fork_before_exec = 1; 86 fork_before_exec = 1;
87 break; 87 break;
88 default: 88 default:
89 show_usage(); 89 bb_show_usage();
90 } 90 }
91 } 91 }
92 92
93 if (start == stop) 93 if (start == stop)
94 error_msg_and_die ("need one of -S or -K"); 94 bb_error_msg_and_die ("need one of -S or -K");
95 95
96 if (!execname && !userspec) 96 if (!execname && !userspec)
97 error_msg_and_die ("need at least one of -x or -u"); 97 bb_error_msg_and_die ("need at least one of -x or -u");
98 98
99 if (!startas) 99 if (!startas)
100 startas = execname; 100 startas = execname;
101 101
102 if (start && !startas) 102 if (start && !startas)
103 error_msg_and_die ("-S needs -x or -a"); 103 bb_error_msg_and_die ("-S needs -x or -a");
104} 104}
105 105
106 106
@@ -185,7 +185,7 @@ do_procfs(void)
185 185
186 procdir = opendir("/proc"); 186 procdir = opendir("/proc");
187 if (!procdir) 187 if (!procdir)
188 perror_msg_and_die ("opendir /proc"); 188 bb_perror_msg_and_die ("opendir /proc");
189 189
190 foundany = 0; 190 foundany = 0;
191 while ((entry = readdir(procdir)) != NULL) { 191 while ((entry = readdir(procdir)) != NULL) {
@@ -196,7 +196,7 @@ do_procfs(void)
196 } 196 }
197 closedir(procdir); 197 closedir(procdir);
198 if (!foundany) 198 if (!foundany)
199 error_msg_and_die ("nothing in /proc - not mounted?"); 199 bb_error_msg_and_die ("nothing in /proc - not mounted?");
200} 200}
201 201
202 202
@@ -214,7 +214,7 @@ do_stop(void)
214 else if (userspec) 214 else if (userspec)
215 sprintf(what, "process(es) owned by `%s'", userspec); 215 sprintf(what, "process(es) owned by `%s'", userspec);
216 else 216 else
217 error_msg_and_die ("internal error, please report"); 217 bb_error_msg_and_die ("internal error, please report");
218 218
219 if (!found) { 219 if (!found) {
220 printf("no %s found; none killed.\n", what); 220 printf("no %s found; none killed.\n", what);
@@ -225,7 +225,7 @@ do_stop(void)
225 p->pid = -p->pid; 225 p->pid = -p->pid;
226 killed++; 226 killed++;
227 } else { 227 } else {
228 perror_msg("warning: failed to kill %d:", p->pid); 228 bb_perror_msg("warning: failed to kill %d:", p->pid);
229 } 229 }
230 } 230 }
231 if (killed) { 231 if (killed) {
@@ -262,10 +262,10 @@ start_stop_daemon_main(int argc, char **argv)
262 *--argv = startas; 262 *--argv = startas;
263 if (fork_before_exec) { 263 if (fork_before_exec) {
264 if (daemon(0, 0) == -1) 264 if (daemon(0, 0) == -1)
265 perror_msg_and_die ("unable to fork"); 265 bb_perror_msg_and_die ("unable to fork");
266 } 266 }
267 setsid(); 267 setsid();
268 execv(startas, argv); 268 execv(startas, argv);
269 perror_msg_and_die ("unable to start %s", startas); 269 bb_perror_msg_and_die ("unable to start %s", startas);
270} 270}
271 271
diff --git a/debianutils/which.c b/debianutils/which.c
index b2af5a8ea..fbcfb371b 100644
--- a/debianutils/which.c
+++ b/debianutils/which.c
@@ -34,7 +34,7 @@ extern int which_main(int argc, char **argv)
34 int i, count=1, found, status = EXIT_SUCCESS; 34 int i, count=1, found, status = EXIT_SUCCESS;
35 35
36 if (argc <= 1 || **(argv + 1) == '-') 36 if (argc <= 1 || **(argv + 1) == '-')
37 show_usage(); 37 bb_show_usage();
38 argc--; 38 argc--;
39 39
40 path_list = getenv("PATH"); 40 path_list = getenv("PATH");
diff --git a/editors/awk.c b/editors/awk.c
index 44c2f45b2..0f8cf94f4 100644
--- a/editors/awk.c
+++ b/editors/awk.c
@@ -463,7 +463,7 @@ static const char EMSG_NO_MATH[] = "Math support is not compiled in";
463 463
464static void syntax_error(const char * const message) 464static void syntax_error(const char * const message)
465{ 465{
466 error_msg("%s:%i: %s", programname, lineno, message); 466 bb_error_msg("%s:%i: %s", programname, lineno, message);
467 awk_exit(1); 467 awk_exit(1);
468} 468}
469 469
@@ -546,7 +546,7 @@ static void *hash_find(xhash *hash, char *name) {
546 if (++hash->nel / hash->csize > 10) 546 if (++hash->nel / hash->csize > 10)
547 hash_rebuild(hash); 547 hash_rebuild(hash);
548 548
549 l = xstrlen(name) + 1; 549 l = bb_strlen(name) + 1;
550 hi = xcalloc(sizeof(hash_item) + l, 1); 550 hi = xcalloc(sizeof(hash_item) + l, 1);
551 memcpy(hi->name, name, l); 551 memcpy(hi->name, name, l);
552 552
@@ -571,7 +571,7 @@ static void hash_remove(xhash *hash, char *name) {
571 while (*phi) { 571 while (*phi) {
572 hi = *phi; 572 hi = *phi;
573 if (strcmp(hi->name, name) == 0) { 573 if (strcmp(hi->name, name) == 0) {
574 hash->glen -= (xstrlen(name) + 1); 574 hash->glen -= (bb_strlen(name) + 1);
575 hash->nel--; 575 hash->nel--;
576 *phi = hi->next; 576 *phi = hi->next;
577 free(hi); 577 free(hi);
@@ -609,7 +609,7 @@ static char nextchar(char **s) {
609 609
610 c = *((*s)++); 610 c = *((*s)++);
611 pps = *s; 611 pps = *s;
612 if (c == '\\') c = process_escape_sequence((const char**)s); 612 if (c == '\\') c = bb_process_escape_sequence((const char**)s);
613 if (c == '\\' && *s == pps) c = *((*s)++); 613 if (c == '\\' && *s == pps) c = *((*s)++);
614 return c; 614 return c;
615} 615}
@@ -621,7 +621,7 @@ static inline int isalnum_(int c) {
621 621
622static FILE *afopen(const char *path, const char *mode) { 622static FILE *afopen(const char *path, const char *mode) {
623 623
624 return (*path == '-' && *(path+1) == '\0') ? stdin : xfopen(path, mode); 624 return (*path == '-' && *(path+1) == '\0') ? stdin : bb_xfopen(path, mode);
625} 625}
626 626
627/* -------- working with variables (set/get/copy/etc) -------- */ 627/* -------- working with variables (set/get/copy/etc) -------- */
@@ -683,7 +683,7 @@ static var *setvar_p(var *v, char *value) {
683/* same as setvar_p but make a copy of string */ 683/* same as setvar_p but make a copy of string */
684static var *setvar_s(var *v, char *value) { 684static var *setvar_s(var *v, char *value) {
685 685
686 return setvar_p(v, (value && *value) ? xstrdup(value) : NULL); 686 return setvar_p(v, (value && *value) ? bb_xstrdup(value) : NULL);
687} 687}
688 688
689/* same as setvar_s but set USER flag */ 689/* same as setvar_s but set USER flag */
@@ -720,7 +720,7 @@ static char *getvar_s(var *v) {
720 /* if v is numeric and has no cached string, convert it to string */ 720 /* if v is numeric and has no cached string, convert it to string */
721 if ((v->type & (VF_NUMBER | VF_CACHED)) == VF_NUMBER) { 721 if ((v->type & (VF_NUMBER | VF_CACHED)) == VF_NUMBER) {
722 fmt_num(buf, MAXVARFMT, getvar_s(V[CONVFMT]), v->number, TRUE); 722 fmt_num(buf, MAXVARFMT, getvar_s(V[CONVFMT]), v->number, TRUE);
723 v->string = xstrdup(buf); 723 v->string = bb_xstrdup(buf);
724 v->type |= VF_CACHED; 724 v->type |= VF_CACHED;
725 } 725 }
726 return (v->string == NULL) ? "" : v->string; 726 return (v->string == NULL) ? "" : v->string;
@@ -755,7 +755,7 @@ static var *copyvar(var *dest, var *src) {
755 dest->type |= (src->type & ~VF_DONTTOUCH); 755 dest->type |= (src->type & ~VF_DONTTOUCH);
756 dest->number = src->number; 756 dest->number = src->number;
757 if (src->string) 757 if (src->string)
758 dest->string = xstrdup(src->string); 758 dest->string = bb_xstrdup(src->string);
759 } 759 }
760 handle_special(dest); 760 handle_special(dest);
761 return dest; 761 return dest;
@@ -903,7 +903,7 @@ static unsigned long next_token(unsigned long expected) {
903 syntax_error(EMSG_UNEXP_EOS); 903 syntax_error(EMSG_UNEXP_EOS);
904 if ((*s++ = *p++) == '\\') { 904 if ((*s++ = *p++) == '\\') {
905 pp = p; 905 pp = p;
906 *(s-1) = process_escape_sequence((const char **)&p); 906 *(s-1) = bb_process_escape_sequence((const char **)&p);
907 if (*pp == '\\') *s++ = '\\'; 907 if (*pp == '\\') *s++ = '\\';
908 if (p == pp) *s++ = *p++; 908 if (p == pp) *s++ = *p++;
909 } 909 }
@@ -1153,7 +1153,7 @@ static node *chain_node(unsigned long info) {
1153 if (seq->programname != programname) { 1153 if (seq->programname != programname) {
1154 seq->programname = programname; 1154 seq->programname = programname;
1155 n = chain_node(OC_NEWSOURCE); 1155 n = chain_node(OC_NEWSOURCE);
1156 n->l.s = xstrdup(programname); 1156 n->l.s = bb_xstrdup(programname);
1157 } 1157 }
1158 1158
1159 n = seq->last; 1159 n = seq->last;
@@ -1373,7 +1373,7 @@ static node *mk_splitter(char *s, tsplitter *spl) {
1373 regfree(re); 1373 regfree(re);
1374 regfree(ire); 1374 regfree(ire);
1375 } 1375 }
1376 if (xstrlen(s) > 1) { 1376 if (bb_strlen(s) > 1) {
1377 mk_re_node(s, n, re); 1377 mk_re_node(s, n, re);
1378 } else { 1378 } else {
1379 n->info = (unsigned long) *s; 1379 n->info = (unsigned long) *s;
@@ -1441,7 +1441,7 @@ static int awk_split(char *s, node *spl, char **slist) {
1441 regmatch_t pmatch[2]; 1441 regmatch_t pmatch[2];
1442 1442
1443 /* in worst case, each char would be a separate field */ 1443 /* in worst case, each char would be a separate field */
1444 *slist = s1 = xstrndup(s, xstrlen(s) * 2 + 3); 1444 *slist = s1 = bb_xstrndup(s, bb_strlen(s) * 2 + 3);
1445 1445
1446 c[0] = c[1] = (char)spl->info; 1446 c[0] = c[1] = (char)spl->info;
1447 c[2] = c[3] = '\0'; 1447 c[2] = c[3] = '\0';
@@ -1536,12 +1536,12 @@ static void handle_special(var *v) {
1536 1536
1537 /* recalculate $0 */ 1537 /* recalculate $0 */
1538 sep = getvar_s(V[OFS]); 1538 sep = getvar_s(V[OFS]);
1539 sl = xstrlen(sep); 1539 sl = bb_strlen(sep);
1540 b = NULL; 1540 b = NULL;
1541 len = 0; 1541 len = 0;
1542 for (i=0; i<n; i++) { 1542 for (i=0; i<n; i++) {
1543 s = getvar_s(&Fields[i]); 1543 s = getvar_s(&Fields[i]);
1544 l = xstrlen(s); 1544 l = bb_strlen(s);
1545 if (b) { 1545 if (b) {
1546 memcpy(b+len, sep, sl); 1546 memcpy(b+len, sep, sl);
1547 len += sl; 1547 len += sl;
@@ -1744,7 +1744,7 @@ static char *awk_printf(node *n) {
1744 var *v, *arg; 1744 var *v, *arg;
1745 1745
1746 v = nvalloc(1); 1746 v = nvalloc(1);
1747 fmt = f = xstrdup(getvar_s(evaluate(nextarg(&n), v))); 1747 fmt = f = bb_xstrdup(getvar_s(evaluate(nextarg(&n), v)));
1748 1748
1749 i = 0; 1749 i = 0;
1750 while (*f) { 1750 while (*f) {
@@ -1767,7 +1767,7 @@ static char *awk_printf(node *n) {
1767 1767
1768 } else if (c == 's') { 1768 } else if (c == 's') {
1769 s1 = getvar_s(arg); 1769 s1 = getvar_s(arg);
1770 qrealloc(&b, incr+i+xstrlen(s1), &bsize); 1770 qrealloc(&b, incr+i+bb_strlen(s1), &bsize);
1771 i += sprintf(b+i, s, s1); 1771 i += sprintf(b+i, s, s1);
1772 1772
1773 } else { 1773 } else {
@@ -1807,7 +1807,7 @@ static int awk_sub(node *rn, char *repl, int nm, var *src, var *dest, int ex) {
1807 1807
1808 i = di = 0; 1808 i = di = 0;
1809 sp = getvar_s(src); 1809 sp = getvar_s(src);
1810 rl = xstrlen(repl); 1810 rl = bb_strlen(repl);
1811 while (regexec(re, sp, 10, pmatch, sp==getvar_s(src) ? 0:REG_NOTBOL) == 0) { 1811 while (regexec(re, sp, 10, pmatch, sp==getvar_s(src) ? 0:REG_NOTBOL) == 0) {
1812 so = pmatch[0].rm_so; 1812 so = pmatch[0].rm_so;
1813 eo = pmatch[0].rm_eo; 1813 eo = pmatch[0].rm_eo;
@@ -1920,7 +1920,7 @@ static var *exec_builtin(node *op, var *res) {
1920 break; 1920 break;
1921 1921
1922 case B_ss: 1922 case B_ss:
1923 l = xstrlen(as[0]); 1923 l = bb_strlen(as[0]);
1924 i = getvar_i(av[1]) - 1; 1924 i = getvar_i(av[1]) - 1;
1925 if (i>l) i=l; if (i<0) i=0; 1925 if (i>l) i=l; if (i<0) i=0;
1926 n = (nargs > 2) ? getvar_i(av[2]) : l-i; 1926 n = (nargs > 2) ? getvar_i(av[2]) : l-i;
@@ -1938,7 +1938,7 @@ static var *exec_builtin(node *op, var *res) {
1938 case B_up: 1938 case B_up:
1939 to_xxx = toupper; 1939 to_xxx = toupper;
1940lo_cont: 1940lo_cont:
1941 s1 = s = xstrdup(as[0]); 1941 s1 = s = bb_xstrdup(as[0]);
1942 while (*s1) { 1942 while (*s1) {
1943 *s1 = (*to_xxx)(*s1); 1943 *s1 = (*to_xxx)(*s1);
1944 s1++; 1944 s1++;
@@ -1948,8 +1948,8 @@ lo_cont:
1948 1948
1949 case B_ix: 1949 case B_ix:
1950 n = 0; 1950 n = 0;
1951 ll = xstrlen(as[1]); 1951 ll = bb_strlen(as[1]);
1952 l = xstrlen(as[0]) - ll; 1952 l = bb_strlen(as[0]) - ll;
1953 if (ll > 0 && l >= 0) { 1953 if (ll > 0 && l >= 0) {
1954 if (! icase) { 1954 if (! icase) {
1955 s = strstr(as[0], as[1]); 1955 s = strstr(as[0], as[1]);
@@ -2112,10 +2112,10 @@ static var *evaluate(node *op, var *res) {
2112 if (! X.rsm->F) { 2112 if (! X.rsm->F) {
2113 if (opn == '|') { 2113 if (opn == '|') {
2114 if((X.rsm->F = popen(R.s, "w")) == NULL) 2114 if((X.rsm->F = popen(R.s, "w")) == NULL)
2115 perror_msg_and_die("popen"); 2115 bb_perror_msg_and_die("popen");
2116 X.rsm->is_pipe = 1; 2116 X.rsm->is_pipe = 1;
2117 } else { 2117 } else {
2118 X.rsm->F = xfopen(R.s, opn=='w' ? "w" : "a"); 2118 X.rsm->F = bb_xfopen(R.s, opn=='w' ? "w" : "a");
2119 } 2119 }
2120 } 2120 }
2121 X.F = X.rsm->F; 2121 X.F = X.rsm->F;
@@ -2269,7 +2269,7 @@ re_cont:
2269 X.rsm->F = popen(L.s, "r"); 2269 X.rsm->F = popen(L.s, "r");
2270 X.rsm->is_pipe = TRUE; 2270 X.rsm->is_pipe = TRUE;
2271 } else { 2271 } else {
2272 X.rsm->F = fopen(L.s, "r"); /* not xfopen! */ 2272 X.rsm->F = fopen(L.s, "r"); /* not bb_xfopen! */
2273 } 2273 }
2274 } 2274 }
2275 } else { 2275 } else {
@@ -2351,7 +2351,7 @@ re_cont:
2351 case F_le: 2351 case F_le:
2352 if (! op1) 2352 if (! op1)
2353 L.s = getvar_s(V[F0]); 2353 L.s = getvar_s(V[F0]);
2354 R.d = xstrlen(L.s); 2354 R.d = bb_strlen(L.s);
2355 break; 2355 break;
2356 2356
2357 case F_sy: 2357 case F_sy:
@@ -2439,12 +2439,12 @@ re_cont:
2439 /* concatenation (" ") and index joining (",") */ 2439 /* concatenation (" ") and index joining (",") */
2440 case XC( OC_CONCAT ): 2440 case XC( OC_CONCAT ):
2441 case XC( OC_COMMA ): 2441 case XC( OC_COMMA ):
2442 opn = xstrlen(L.s) + xstrlen(R.s) + 2; 2442 opn = bb_strlen(L.s) + bb_strlen(R.s) + 2;
2443 X.s = (char *)xmalloc(opn); 2443 X.s = (char *)xmalloc(opn);
2444 strcpy(X.s, L.s); 2444 strcpy(X.s, L.s);
2445 if ((opinfo & OPCLSMASK) == OC_COMMA) { 2445 if ((opinfo & OPCLSMASK) == OC_COMMA) {
2446 L.s = getvar_s(V[SUBSEP]); 2446 L.s = getvar_s(V[SUBSEP]);
2447 X.s = (char *)xrealloc(X.s, opn + xstrlen(L.s)); 2447 X.s = (char *)xrealloc(X.s, opn + bb_strlen(L.s));
2448 strcat(X.s, L.s); 2448 strcat(X.s, L.s);
2449 } 2449 }
2450 strcat(X.s, R.s); 2450 strcat(X.s, R.s);
@@ -2554,7 +2554,7 @@ static int is_assignment(char *expr) {
2554 2554
2555 char *exprc, *s, *s0, *s1; 2555 char *exprc, *s, *s0, *s1;
2556 2556
2557 exprc = xstrdup(expr); 2557 exprc = bb_xstrdup(expr);
2558 if (!isalnum_(*exprc) || (s = strchr(exprc, '=')) == NULL) { 2558 if (!isalnum_(*exprc) || (s = strchr(exprc, '=')) == NULL) {
2559 free(exprc); 2559 free(exprc);
2560 return FALSE; 2560 return FALSE;
@@ -2649,7 +2649,7 @@ extern int awk_main(int argc, char **argv) {
2649 } 2649 }
2650 2650
2651 for (envp=environ; *envp; envp++) { 2651 for (envp=environ; *envp; envp++) {
2652 s = xstrdup(*envp); 2652 s = bb_xstrdup(*envp);
2653 s1 = strchr(s, '='); 2653 s1 = strchr(s, '=');
2654 *(s1++) = '\0'; 2654 *(s1++) = '\0';
2655 setvar_u(findvar(iamarray(V[ENVIRON]), s), s1); 2655 setvar_u(findvar(iamarray(V[ENVIRON]), s), s1);
@@ -2663,7 +2663,7 @@ extern int awk_main(int argc, char **argv) {
2663 break; 2663 break;
2664 case 'v': 2664 case 'v':
2665 if (! is_assignment(optarg)) 2665 if (! is_assignment(optarg))
2666 show_usage(); 2666 bb_show_usage();
2667 break; 2667 break;
2668 case 'f': 2668 case 'f':
2669 from_file = TRUE; 2669 from_file = TRUE;
@@ -2680,17 +2680,17 @@ extern int awk_main(int argc, char **argv) {
2680 free(s); 2680 free(s);
2681 break; 2681 break;
2682 case 'W': 2682 case 'W':
2683 error_msg("Warning: unrecognized option '-W %s' ignored\n", optarg); 2683 bb_error_msg("Warning: unrecognized option '-W %s' ignored\n", optarg);
2684 break; 2684 break;
2685 2685
2686 default: 2686 default:
2687 show_usage(); 2687 bb_show_usage();
2688 } 2688 }
2689 } 2689 }
2690 2690
2691 if (!from_file) { 2691 if (!from_file) {
2692 if (argc == optind) 2692 if (argc == optind)
2693 show_usage(); 2693 bb_show_usage();
2694 programname="cmd. line"; 2694 programname="cmd. line";
2695 parse_program(argv[optind++]); 2695 parse_program(argv[optind++]);
2696 2696
diff --git a/editors/sed.c b/editors/sed.c
index 8bd627a9c..2ff028498 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -200,7 +200,7 @@ static int get_address(char *delimiter, char *my_str, int *linenum, regex_t **re
200 } 200 }
201 idx = index_of_next_unescaped_regexp_delim(*delimiter, my_str, ++idx); 201 idx = index_of_next_unescaped_regexp_delim(*delimiter, my_str, ++idx);
202 if (idx == -1) { 202 if (idx == -1) {
203 error_msg_and_die("unterminated match expression"); 203 bb_error_msg_and_die("unterminated match expression");
204 } 204 }
205 my_str[idx] = '\0'; 205 my_str[idx] = '\0';
206 *regex = (regex_t *)xmalloc(sizeof(regex_t)); 206 *regex = (regex_t *)xmalloc(sizeof(regex_t));
@@ -230,7 +230,7 @@ static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
230 /* verify that the 's' is followed by something. That something 230 /* verify that the 's' is followed by something. That something
231 * (typically a 'slash') is now our regexp delimiter... */ 231 * (typically a 'slash') is now our regexp delimiter... */
232 if (substr[idx] == '\0') 232 if (substr[idx] == '\0')
233 error_msg_and_die("bad format in substitution expression"); 233 bb_error_msg_and_die("bad format in substitution expression");
234 else 234 else
235 sed_cmd->delimiter=substr[idx]; 235 sed_cmd->delimiter=substr[idx];
236 236
@@ -238,8 +238,8 @@ static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
238 oldidx = idx+1; 238 oldidx = idx+1;
239 idx = index_of_next_unescaped_regexp_delim(sed_cmd->delimiter, substr, ++idx); 239 idx = index_of_next_unescaped_regexp_delim(sed_cmd->delimiter, substr, ++idx);
240 if (idx == -1) 240 if (idx == -1)
241 error_msg_and_die("bad format in substitution expression"); 241 bb_error_msg_and_die("bad format in substitution expression");
242 match = xstrndup(substr + oldidx, idx - oldidx); 242 match = bb_xstrndup(substr + oldidx, idx - oldidx);
243 243
244 /* determine the number of back references in the match string */ 244 /* determine the number of back references in the match string */
245 /* Note: we compute this here rather than in the do_subst_command() 245 /* Note: we compute this here rather than in the do_subst_command()
@@ -257,8 +257,8 @@ static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
257 oldidx = idx+1; 257 oldidx = idx+1;
258 idx = index_of_next_unescaped_regexp_delim(sed_cmd->delimiter, substr, ++idx); 258 idx = index_of_next_unescaped_regexp_delim(sed_cmd->delimiter, substr, ++idx);
259 if (idx == -1) 259 if (idx == -1)
260 error_msg_and_die("bad format in substitution expression"); 260 bb_error_msg_and_die("bad format in substitution expression");
261 sed_cmd->replace = xstrndup(substr + oldidx, idx - oldidx); 261 sed_cmd->replace = bb_xstrndup(substr + oldidx, idx - oldidx);
262 262
263 /* process the flags */ 263 /* process the flags */
264 while (substr[++idx]) { 264 while (substr[++idx]) {
@@ -278,7 +278,7 @@ static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
278 if (strchr(semicolon_whitespace, substr[idx])) 278 if (strchr(semicolon_whitespace, substr[idx]))
279 goto out; 279 goto out;
280 /* else */ 280 /* else */
281 error_msg_and_die("bad option in substitution expression"); 281 bb_error_msg_and_die("bad option in substitution expression");
282 } 282 }
283 } 283 }
284 284
@@ -317,7 +317,7 @@ static int parse_edit_cmd(sed_cmd_t *sed_cmd, const char *editstr)
317 * 317 *
318 */ 318 */
319 if ((*editstr != '\\') || ((editstr[1] != '\n') && (editstr[1] != '\r'))) { 319 if ((*editstr != '\\') || ((editstr[1] != '\n') && (editstr[1] != '\r'))) {
320 error_msg_and_die("bad format in edit expression"); 320 bb_error_msg_and_die("bad format in edit expression");
321 } 321 }
322 322
323 /* store the edit line text */ 323 /* store the edit line text */
@@ -390,20 +390,20 @@ static char *parse_cmd_str(sed_cmd_t * const sed_cmd, char *cmdstr)
390 /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */ 390 /* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
391 else if (strchr("aic", sed_cmd->cmd)) { 391 else if (strchr("aic", sed_cmd->cmd)) {
392 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c') 392 if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
393 error_msg_and_die("only a beginning address can be specified for edit commands"); 393 bb_error_msg_and_die("only a beginning address can be specified for edit commands");
394 cmdstr += parse_edit_cmd(sed_cmd, cmdstr); 394 cmdstr += parse_edit_cmd(sed_cmd, cmdstr);
395 } 395 }
396 /* handle file cmds: (r)ead */ 396 /* handle file cmds: (r)ead */
397 else if (sed_cmd->cmd == 'r') { 397 else if (sed_cmd->cmd == 'r') {
398 if (sed_cmd->end_line || sed_cmd->end_match) 398 if (sed_cmd->end_line || sed_cmd->end_match)
399 error_msg_and_die("Command only uses one address"); 399 bb_error_msg_and_die("Command only uses one address");
400 cmdstr += parse_file_cmd(sed_cmd, cmdstr); 400 cmdstr += parse_file_cmd(sed_cmd, cmdstr);
401 } 401 }
402 /* if it wasnt a single-letter command that takes no arguments 402 /* if it wasnt a single-letter command that takes no arguments
403 * then it must be an invalid command. 403 * then it must be an invalid command.
404 */ 404 */
405 else if (strchr("nNpPqd=", sed_cmd->cmd) == 0) { 405 else if (strchr("nNpPqd=", sed_cmd->cmd) == 0) {
406 error_msg_and_die("Unsupported command %c", sed_cmd->cmd); 406 bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd);
407 } 407 }
408 408
409 /* give back whatever's left over */ 409 /* give back whatever's left over */
@@ -442,7 +442,7 @@ static char *add_cmd(sed_cmd_t *sed_cmd, char *cmdstr)
442 cmdstr++; 442 cmdstr++;
443 idx = get_address(&(sed_cmd->delimiter), cmdstr, &sed_cmd->end_line, &sed_cmd->end_match); 443 idx = get_address(&(sed_cmd->delimiter), cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
444 if (idx == 0) { 444 if (idx == 0) {
445 error_msg_and_die("get_address: no address found in string\n" 445 bb_error_msg_and_die("get_address: no address found in string\n"
446 "\t(you probably didn't check the string you passed me)"); 446 "\t(you probably didn't check the string you passed me)");
447 } 447 }
448 cmdstr += idx; 448 cmdstr += idx;
@@ -465,7 +465,7 @@ static char *add_cmd(sed_cmd_t *sed_cmd, char *cmdstr)
465 * with <blank>s. 465 * with <blank>s.
466 */ 466 */
467 if (isblank(cmdstr[idx]) { 467 if (isblank(cmdstr[idx]) {
468 error_msg_and_die("blank follows '!'"); 468 bb_error_msg_and_die("blank follows '!'");
469 } 469 }
470#else 470#else
471 /* skip whitespace before the command */ 471 /* skip whitespace before the command */
@@ -478,7 +478,7 @@ static char *add_cmd(sed_cmd_t *sed_cmd, char *cmdstr)
478 478
479 /* last part (mandatory) will be a command */ 479 /* last part (mandatory) will be a command */
480 if (*cmdstr == '\0') 480 if (*cmdstr == '\0')
481 error_msg_and_die("missing command"); 481 bb_error_msg_and_die("missing command");
482 482
483 sed_cmd->cmd = *cmdstr; 483 sed_cmd->cmd = *cmdstr;
484 cmdstr++; 484 cmdstr++;
@@ -517,14 +517,16 @@ static void load_cmd_file(char *filename)
517 FILE *cmdfile; 517 FILE *cmdfile;
518 char *line; 518 char *line;
519 char *nextline; 519 char *nextline;
520 char *e;
520 521
521 cmdfile = xfopen(filename, "r"); 522 cmdfile = bb_xfopen(filename, "r");
522 523
523 while ((line = get_line_from_file(cmdfile)) != NULL) { 524 while ((line = bb_get_line_from_file(cmdfile)) != NULL) {
524 /* if a line ends with '\' it needs the next line appended to it */ 525 /* if a line ends with '\' it needs the next line appended to it */
525 while (line[strlen(line)-2] == '\\' && 526 while (((e = last_char_is(line, '\n')) != NULL)
526 (nextline = get_line_from_file(cmdfile)) != NULL) { 527 && (e > line) && (e[-1] == '\\')
527 line = xrealloc(line, strlen(line) + strlen(nextline) + 1); 528 && ((nextline = bb_get_line_from_file(cmdfile)) != NULL)) {
529 line = xrealloc(line, (e - line) + 1 + strlen(nextline) + 1);
528 strcat(line, nextline); 530 strcat(line, nextline);
529 free(nextline); 531 free(nextline);
530 } 532 }
@@ -677,20 +679,18 @@ static void process_file(FILE *file)
677 int altered; 679 int altered;
678 int i; 680 int i;
679 681
680 line = get_line_from_file(file); 682 line = bb_get_chomped_line_from_file(file);
681 if (line == NULL) { 683 if (line == NULL) {
682 return; 684 return;
683 } 685 }
684 chomp(line);
685 686
686 /* go through every line in the file */ 687 /* go through every line in the file */
687 do { 688 do {
688 char *next_line; 689 char *next_line;
689 690
690 /* Read one line in advance so we can act on the last line, the '$' address */ 691 /* Read one line in advance so we can act on the last line, the '$' address */
691 next_line = get_line_from_file(file); 692 next_line = bb_get_chomped_line_from_file(file);
692 693
693 chomp(next_line);
694 linenum++; 694 linenum++;
695 altered = 0; 695 altered = 0;
696 696
@@ -805,7 +805,7 @@ static void process_file(FILE *file)
805 puts(line); 805 puts(line);
806 outfile = fopen(sed_cmd->filename, "r"); 806 outfile = fopen(sed_cmd->filename, "r");
807 if (outfile) 807 if (outfile)
808 print_file(outfile); 808 bb_xprint_and_close_file(outfile);
809 /* else if we couldn't open the output file, 809 /* else if we couldn't open the output file,
810 * no biggie, just don't print anything */ 810 * no biggie, just don't print anything */
811 altered++; 811 altered++;
@@ -817,16 +817,14 @@ static void process_file(FILE *file)
817 case 'n': /* Read next line from input */ 817 case 'n': /* Read next line from input */
818 free(line); 818 free(line);
819 line = next_line; 819 line = next_line;
820 next_line = get_line_from_file(file); 820 next_line = bb_get_chomped_line_from_file(file);
821 chomp(next_line);
822 linenum++; 821 linenum++;
823 break; 822 break;
824 case 'N': /* Append the next line to the current line */ 823 case 'N': /* Append the next line to the current line */
825 line = realloc(line, strlen(line) + strlen(next_line) + 2); 824 line = realloc(line, strlen(line) + strlen(next_line) + 2);
826 strcat(line, "\n"); 825 strcat(line, "\n");
827 strcat(line, next_line); 826 strcat(line, next_line);
828 next_line = get_line_from_file(file); 827 next_line = bb_get_chomped_line_from_file(file);
829 chomp(next_line);
830 linenum++; 828 linenum++;
831 } 829 }
832 } 830 }
@@ -880,7 +878,7 @@ extern int sed_main(int argc, char **argv)
880#ifdef CONFIG_FEATURE_CLEAN_UP 878#ifdef CONFIG_FEATURE_CLEAN_UP
881 /* destroy command strings on exit */ 879 /* destroy command strings on exit */
882 if (atexit(destroy_cmd_strs) == -1) 880 if (atexit(destroy_cmd_strs) == -1)
883 perror_msg_and_die("atexit"); 881 bb_perror_msg_and_die("atexit");
884#endif 882#endif
885 883
886 /* do normal option parsing */ 884 /* do normal option parsing */
@@ -896,7 +894,7 @@ extern int sed_main(int argc, char **argv)
896 load_cmd_file(optarg); 894 load_cmd_file(optarg);
897 break; 895 break;
898 default: 896 default:
899 show_usage(); 897 bb_show_usage();
900 } 898 }
901 } 899 }
902 900
@@ -904,7 +902,7 @@ extern int sed_main(int argc, char **argv)
904 * argv[optind] should be the pattern. no pattern, no worky */ 902 * argv[optind] should be the pattern. no pattern, no worky */
905 if (ncmds == 0) { 903 if (ncmds == 0) {
906 if (argv[optind] == NULL) 904 if (argv[optind] == NULL)
907 show_usage(); 905 bb_show_usage();
908 else { 906 else {
909 add_cmd_str(argv[optind]); 907 add_cmd_str(argv[optind]);
910 optind++; 908 optind++;
@@ -921,7 +919,7 @@ extern int sed_main(int argc, char **argv)
921 int i; 919 int i;
922 FILE *file; 920 FILE *file;
923 for (i = optind; i < argc; i++) { 921 for (i = optind; i < argc; i++) {
924 file = wfopen(argv[i], "r"); 922 file = bb_wfopen(argv[i], "r");
925 if (file) { 923 if (file) {
926 process_file(file); 924 process_file(file);
927 fclose(file); 925 fclose(file);
diff --git a/editors/vi.c b/editors/vi.c
index cda17db1a..144e9d760 100644
--- a/editors/vi.c
+++ b/editors/vi.c
@@ -19,7 +19,7 @@
19 */ 19 */
20 20
21static const char vi_Version[] = 21static const char vi_Version[] =
22 "$Id: vi.c,v 1.27 2002/12/03 21:48:15 bug1 Exp $"; 22 "$Id: vi.c,v 1.28 2003/03/19 09:11:45 mjn3 Exp $";
23 23
24/* 24/*
25 * To compile for standalone use: 25 * To compile for standalone use:
@@ -403,7 +403,7 @@ extern int vi_main(int argc, char **argv)
403 for (; optind < argc; optind++) { 403 for (; optind < argc; optind++) {
404 editing = 1; // 0=exit, 1=one file, 2+ =many files 404 editing = 1; // 0=exit, 1=one file, 2+ =many files
405 free(cfn); 405 free(cfn);
406 cfn = (Byte *) xstrdup(argv[optind]); 406 cfn = (Byte *) bb_xstrdup(argv[optind]);
407 edit_file(cfn); 407 edit_file(cfn);
408 } 408 }
409 } 409 }
@@ -597,7 +597,7 @@ static Byte *get_one_address(Byte * p, int *addr) // get colon addr, if present
597 *q++ = *p; 597 *q++ = *p;
598 *q = '\0'; 598 *q = '\0';
599 } 599 }
600 pat = (Byte *) xstrdup((char *) buf); // save copy of pattern 600 pat = (Byte *) bb_xstrdup((char *) buf); // save copy of pattern
601 if (*p == '/') 601 if (*p == '/')
602 p++; 602 p++;
603 q = char_search(dot, pat, FORWARD, FULL); 603 q = char_search(dot, pat, FORWARD, FULL);
@@ -811,7 +811,7 @@ static void colon(Byte * buf)
811 811
812 // There is a read-able regular file 812 // There is a read-able regular file
813 // make this the current file 813 // make this the current file
814 q = (Byte *) xstrdup((char *) fn); // save the cfn 814 q = (Byte *) bb_xstrdup((char *) fn); // save the cfn
815 free(cfn); // free the old name 815 free(cfn); // free the old name
816 cfn = q; // remember new cfn 816 cfn = q; // remember new cfn
817 817
@@ -862,7 +862,7 @@ static void colon(Byte * buf)
862 if (strlen((char *) args) > 0) { 862 if (strlen((char *) args) > 0) {
863 // user wants a new filename 863 // user wants a new filename
864 free(cfn); 864 free(cfn);
865 cfn = (Byte *) xstrdup((char *) args); 865 cfn = (Byte *) bb_xstrdup((char *) args);
866 } else { 866 } else {
867 // user wants file status info 867 // user wants file status info
868 edit_status(); 868 edit_status();
@@ -2432,7 +2432,7 @@ static Byte *get_input_line(Byte * prompt) // get input line- use "status line"
2432 } 2432 }
2433 refresh(FALSE); 2433 refresh(FALSE);
2434 free(obufp); 2434 free(obufp);
2435 obufp = (Byte *) xstrdup((char *) buf); 2435 obufp = (Byte *) bb_xstrdup((char *) buf);
2436 return (obufp); 2436 return (obufp);
2437} 2437}
2438 2438
@@ -3263,7 +3263,7 @@ key_cmd_mode:
3263 // Stuff the last_modifying_cmd back into stdin 3263 // Stuff the last_modifying_cmd back into stdin
3264 // and let it be re-executed. 3264 // and let it be re-executed.
3265 if (last_modifying_cmd != 0) { 3265 if (last_modifying_cmd != 0) {
3266 ioq = ioq_start = (Byte *) xstrdup((char *) last_modifying_cmd); 3266 ioq = ioq_start = (Byte *) bb_xstrdup((char *) last_modifying_cmd);
3267 } 3267 }
3268 break; 3268 break;
3269#endif /* CONFIG_FEATURE_VI_DOT_CMD */ 3269#endif /* CONFIG_FEATURE_VI_DOT_CMD */
@@ -3278,7 +3278,7 @@ key_cmd_mode:
3278 if (strlen((char *) q) > 1) { // new pat- save it and find 3278 if (strlen((char *) q) > 1) { // new pat- save it and find
3279 // there is a new pat 3279 // there is a new pat
3280 free(last_search_pattern); 3280 free(last_search_pattern);
3281 last_search_pattern = (Byte *) xstrdup((char *) q); 3281 last_search_pattern = (Byte *) bb_xstrdup((char *) q);
3282 goto dc3; // now find the pattern 3282 goto dc3; // now find the pattern
3283 } 3283 }
3284 // user changed mind and erased the "/"- do nothing 3284 // user changed mind and erased the "/"- do nothing
diff --git a/findutils/find.c b/findutils/find.c
index 048aac503..58bc8bf9c 100644
--- a/findutils/find.c
+++ b/findutils/find.c
@@ -167,7 +167,7 @@ static int find_type(char *type)
167 } 167 }
168 168
169 if (mask == 0 || type[1] != '\0') 169 if (mask == 0 || type[1] != '\0')
170 error_msg_and_die(msg_invalid_arg, type, "-type"); 170 bb_error_msg_and_die(msg_invalid_arg, type, "-type");
171 171
172 return mask; 172 return mask;
173} 173}
@@ -192,22 +192,22 @@ int find_main(int argc, char **argv)
192 } 192 }
193 else if (strcmp(argv[i], "-name") == 0) { 193 else if (strcmp(argv[i], "-name") == 0) {
194 if (++i == argc) 194 if (++i == argc)
195 error_msg_and_die(msg_req_arg, "-name"); 195 bb_error_msg_and_die(msg_req_arg, "-name");
196 pattern = argv[i]; 196 pattern = argv[i];
197#ifdef CONFIG_FEATURE_FIND_TYPE 197#ifdef CONFIG_FEATURE_FIND_TYPE
198 } else if (strcmp(argv[i], "-type") == 0) { 198 } else if (strcmp(argv[i], "-type") == 0) {
199 if (++i == argc) 199 if (++i == argc)
200 error_msg_and_die(msg_req_arg, "-type"); 200 bb_error_msg_and_die(msg_req_arg, "-type");
201 type_mask = find_type(argv[i]); 201 type_mask = find_type(argv[i]);
202#endif 202#endif
203#ifdef CONFIG_FEATURE_FIND_PERM 203#ifdef CONFIG_FEATURE_FIND_PERM
204 } else if (strcmp(argv[i], "-perm") == 0) { 204 } else if (strcmp(argv[i], "-perm") == 0) {
205 char *end; 205 char *end;
206 if (++i == argc) 206 if (++i == argc)
207 error_msg_and_die(msg_req_arg, "-perm"); 207 bb_error_msg_and_die(msg_req_arg, "-perm");
208 perm_mask = strtol(argv[i], &end, 8); 208 perm_mask = strtol(argv[i], &end, 8);
209 if ((end[0] != '\0') || (perm_mask > 07777)) 209 if ((end[0] != '\0') || (perm_mask > 07777))
210 error_msg_and_die(msg_invalid_arg, argv[i], "-perm"); 210 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm");
211 if ((perm_char = argv[i][0]) == '-') 211 if ((perm_char = argv[i][0]) == '-')
212 perm_mask = -perm_mask; 212 perm_mask = -perm_mask;
213#endif 213#endif
@@ -215,10 +215,10 @@ int find_main(int argc, char **argv)
215 } else if (strcmp(argv[i], "-mtime") == 0) { 215 } else if (strcmp(argv[i], "-mtime") == 0) {
216 char *end; 216 char *end;
217 if (++i == argc) 217 if (++i == argc)
218 error_msg_and_die(msg_req_arg, "-mtime"); 218 bb_error_msg_and_die(msg_req_arg, "-mtime");
219 mtime_days = strtol(argv[i], &end, 10); 219 mtime_days = strtol(argv[i], &end, 10);
220 if (end[0] != '\0') 220 if (end[0] != '\0')
221 error_msg_and_die(msg_invalid_arg, argv[i], "-mtime"); 221 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime");
222 if ((mtime_char = argv[i][0]) == '-') 222 if ((mtime_char = argv[i][0]) == '-')
223 mtime_days = -mtime_days; 223 mtime_days = -mtime_days;
224#endif 224#endif
@@ -231,14 +231,14 @@ int find_main(int argc, char **argv)
231 231
232 if ( firstopt == 1 ) { 232 if ( firstopt == 1 ) {
233 if ( stat ( ".", &stbuf ) < 0 ) 233 if ( stat ( ".", &stbuf ) < 0 )
234 error_msg_and_die("could not stat '.'" ); 234 bb_error_msg_and_die("could not stat '.'" );
235 xdev_dev [0] = stbuf. st_dev; 235 xdev_dev [0] = stbuf. st_dev;
236 } 236 }
237 else { 237 else {
238 238
239 for (i = 1; i < firstopt; i++) { 239 for (i = 1; i < firstopt; i++) {
240 if ( stat ( argv [i], &stbuf ) < 0 ) 240 if ( stat ( argv [i], &stbuf ) < 0 )
241 error_msg_and_die("could not stat '%s'", argv [i] ); 241 bb_error_msg_and_die("could not stat '%s'", argv [i] );
242 xdev_dev [i-1] = stbuf. st_dev; 242 xdev_dev [i-1] = stbuf. st_dev;
243 } 243 }
244 } 244 }
@@ -247,22 +247,22 @@ int find_main(int argc, char **argv)
247 } else if (strcmp(argv[i], "-newer") == 0) { 247 } else if (strcmp(argv[i], "-newer") == 0) {
248 struct stat stat_newer; 248 struct stat stat_newer;
249 if (++i == argc) 249 if (++i == argc)
250 error_msg_and_die(msg_req_arg, "-newer"); 250 bb_error_msg_and_die(msg_req_arg, "-newer");
251 if (stat (argv[i], &stat_newer) != 0) 251 if (stat (argv[i], &stat_newer) != 0)
252 error_msg_and_die("file %s not found", argv[i]); 252 bb_error_msg_and_die("file %s not found", argv[i]);
253 newer_mtime = stat_newer.st_mtime; 253 newer_mtime = stat_newer.st_mtime;
254#endif 254#endif
255#ifdef CONFIG_FEATURE_FIND_INUM 255#ifdef CONFIG_FEATURE_FIND_INUM
256 } else if (strcmp(argv[i], "-inum") == 0) { 256 } else if (strcmp(argv[i], "-inum") == 0) {
257 char *end; 257 char *end;
258 if (++i == argc) 258 if (++i == argc)
259 error_msg_and_die(msg_req_arg, "-inum"); 259 bb_error_msg_and_die(msg_req_arg, "-inum");
260 inode_num = strtol(argv[i], &end, 10); 260 inode_num = strtol(argv[i], &end, 10);
261 if (end[0] != '\0') 261 if (end[0] != '\0')
262 error_msg_and_die(msg_invalid_arg, argv[i], "-inum"); 262 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum");
263#endif 263#endif
264 } else 264 } else
265 show_usage(); 265 bb_show_usage();
266 } 266 }
267 267
268 if (firstopt == 1) { 268 if (firstopt == 1) {
diff --git a/findutils/grep.c b/findutils/grep.c
index cc2b697e8..b4a25923e 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -88,8 +88,7 @@ static void grep_file(FILE *file)
88 int idx = 0; /* used for iteration through the circular buffer */ 88 int idx = 0; /* used for iteration through the circular buffer */
89#endif /* CONFIG_FEATURE_GREP_CONTEXT */ 89#endif /* CONFIG_FEATURE_GREP_CONTEXT */
90 90
91 while ((line = get_line_from_file(file)) != NULL) { 91 while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
92 chomp(line);
93 linenum++; 92 linenum++;
94 93
95 for (i = 0; i < nregexes; i++) { 94 for (i = 0; i < nregexes; i++) {
@@ -154,7 +153,7 @@ static void grep_file(FILE *file)
154 /* Add the line to the circular 'before' buffer */ 153 /* Add the line to the circular 'before' buffer */
155 if(lines_before) { 154 if(lines_before) {
156 free(before_buf[curpos]); 155 free(before_buf[curpos]);
157 before_buf[curpos] = xstrdup(line); 156 before_buf[curpos] = bb_xstrdup(line);
158 curpos = (curpos + 1) % lines_before; 157 curpos = (curpos + 1) % lines_before;
159 } 158 }
160 } 159 }
@@ -205,9 +204,8 @@ static void add_regex(const char *restr)
205static void load_regexes_from_file(const char *filename) 204static void load_regexes_from_file(const char *filename)
206{ 205{
207 char *line; 206 char *line;
208 FILE *f = xfopen(filename, "r"); 207 FILE *f = bb_xfopen(filename, "r");
209 while ((line = get_line_from_file(f)) != NULL) { 208 while ((line = bb_get_chomped_line_from_file(f)) != NULL) {
210 chomp(line);
211 add_regex(line); 209 add_regex(line);
212 free(line); 210 free(line);
213 } 211 }
@@ -242,7 +240,7 @@ extern int grep_main(int argc, char **argv)
242#endif 240#endif
243 241
244#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS 242#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
245 if (strcmp(get_last_path_component(argv[0]), "egrep") == 0) 243 if (strcmp(bb_get_last_path_component(argv[0]), "egrep") == 0)
246 reflags |= REG_EXTENDED; 244 reflags |= REG_EXTENDED;
247#endif 245#endif
248 246
@@ -298,23 +296,23 @@ extern int grep_main(int argc, char **argv)
298 case 'A': 296 case 'A':
299 lines_after = strtoul(optarg, &junk, 10); 297 lines_after = strtoul(optarg, &junk, 10);
300 if(*junk != '\0') 298 if(*junk != '\0')
301 error_msg_and_die("invalid context length argument"); 299 bb_error_msg_and_die("invalid context length argument");
302 break; 300 break;
303 case 'B': 301 case 'B':
304 lines_before = strtoul(optarg, &junk, 10); 302 lines_before = strtoul(optarg, &junk, 10);
305 if(*junk != '\0') 303 if(*junk != '\0')
306 error_msg_and_die("invalid context length argument"); 304 bb_error_msg_and_die("invalid context length argument");
307 before_buf = (char **)xcalloc(lines_before, sizeof(char *)); 305 before_buf = (char **)xcalloc(lines_before, sizeof(char *));
308 break; 306 break;
309 case 'C': 307 case 'C':
310 lines_after = lines_before = strtoul(optarg, &junk, 10); 308 lines_after = lines_before = strtoul(optarg, &junk, 10);
311 if(*junk != '\0') 309 if(*junk != '\0')
312 error_msg_and_die("invalid context length argument"); 310 bb_error_msg_and_die("invalid context length argument");
313 before_buf = (char **)xcalloc(lines_before, sizeof(char *)); 311 before_buf = (char **)xcalloc(lines_before, sizeof(char *));
314 break; 312 break;
315#endif /* CONFIG_FEATURE_GREP_CONTEXT */ 313#endif /* CONFIG_FEATURE_GREP_CONTEXT */
316 default: 314 default:
317 show_usage(); 315 bb_show_usage();
318 } 316 }
319 } 317 }
320 318
@@ -322,7 +320,7 @@ extern int grep_main(int argc, char **argv)
322 * argv[optind] should be the pattern. no pattern, no worky */ 320 * argv[optind] should be the pattern. no pattern, no worky */
323 if (nregexes == 0) { 321 if (nregexes == 0) {
324 if (argv[optind] == NULL) 322 if (argv[optind] == NULL)
325 show_usage(); 323 bb_show_usage();
326 else { 324 else {
327 add_regex(argv[optind]); 325 add_regex(argv[optind]);
328 optind++; 326 optind++;
@@ -356,7 +354,7 @@ extern int grep_main(int argc, char **argv)
356 file = fopen(cur_file, "r"); 354 file = fopen(cur_file, "r");
357 if (file == NULL) { 355 if (file == NULL) {
358 if (!suppress_err_msgs) 356 if (!suppress_err_msgs)
359 perror_msg("%s", cur_file); 357 bb_perror_msg("%s", cur_file);
360 } 358 }
361 else { 359 else {
362 grep_file(file); 360 grep_file(file);
diff --git a/findutils/xargs.c b/findutils/xargs.c
index 677618dc0..d4bed57f0 100644
--- a/findutils/xargs.c
+++ b/findutils/xargs.c
@@ -53,11 +53,11 @@ static void xargs_exec(char * const * args)
53 wait(NULL); 53 wait(NULL);
54 if(common[0]) { 54 if(common[0]) {
55 errno = common[0]; 55 errno = common[0];
56 perror_msg_and_die("%s", args[0]); 56 bb_perror_msg_and_die("%s", args[0]);
57 } 57 }
58 } 58 }
59 } else { 59 } else {
60 perror_msg_and_die("vfork"); 60 bb_perror_msg_and_die("vfork");
61 } 61 }
62} 62}
63 63
@@ -81,7 +81,7 @@ int xargs_main(int argc, char **argv)
81 flg_no_empty = 1; 81 flg_no_empty = 1;
82 break; 82 break;
83 default: 83 default:
84 show_usage(); 84 bb_show_usage();
85 } 85 }
86 } 86 }
87 87
@@ -101,9 +101,7 @@ int xargs_main(int argc, char **argv)
101 101
102 /* Now, read in one line at a time from stdin, and store this 102 /* Now, read in one line at a time from stdin, and store this
103 * line to be used later as an argument to the command */ 103 * line to be used later as an argument to the command */
104 while ((file_to_act_on = get_line_from_file(stdin)) != NULL) { 104 while ((file_to_act_on = bb_get_chomped_line_from_file(stdin)) != NULL) {
105 /* eat the newline off the filename. */
106 chomp(file_to_act_on);
107 if(file_to_act_on[0] != 0 || flg_no_empty == 0) { 105 if(file_to_act_on[0] != 0 || flg_no_empty == 0) {
108 args[a] = file_to_act_on[0] ? file_to_act_on : NULL; 106 args[a] = file_to_act_on[0] ? file_to_act_on : NULL;
109 if(flg_vi) { 107 if(flg_vi) {
@@ -114,7 +112,7 @@ int xargs_main(int argc, char **argv)
114 } 112 }
115 fprintf(stderr, "%s", ((flg_vi & 2) ? " ?..." : "\n")); 113 fprintf(stderr, "%s", ((flg_vi & 2) ? " ?..." : "\n"));
116 } 114 }
117 if((flg_vi & 2) == 0 || ask_confirmation() != 0 ) { 115 if((flg_vi & 2) == 0 || bb_ask_confirmation() != 0 ) {
118 xargs_exec(args); 116 xargs_exec(args);
119 } 117 }
120 } 118 }
diff --git a/include/dump.h b/include/dump.h
index 0e8dbe07f..3f4b480b2 100644
--- a/include/dump.h
+++ b/include/dump.h
@@ -38,5 +38,12 @@ typedef struct _fs { /* format strings */
38 int bcnt; 38 int bcnt;
39} FS; 39} FS;
40 40
41void add(char *fmt); 41extern void bb_dump_add(const char *fmt);
42int dump (char **argv); 42extern int bb_dump_dump (char **argv);
43extern int bb_dump_size(FS * fs);
44
45extern FS *bb_dump_fshead; /* head of format strings */
46extern int bb_dump_blocksize; /* data block size */
47extern int bb_dump_length; /* max bytes to read */
48extern enum _vflag bb_dump_vflag;
49extern off_t bb_dump_skip; /* bytes to skip */
diff --git a/include/libbb.h b/include/libbb.h
index 8cb72de8b..f79def321 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -28,6 +28,7 @@
28#include <stdarg.h> 28#include <stdarg.h>
29#include <sys/stat.h> 29#include <sys/stat.h>
30#include <sys/types.h> 30#include <sys/types.h>
31#include <termios.h>
31 32
32#include <netdb.h> 33#include <netdb.h>
33 34
@@ -86,84 +87,132 @@ char *strtok_r(char *s, const char *delim, char **ptrptr);
86#define MAX(a,b) (((a)>(b))?(a):(b)) 87#define MAX(a,b) (((a)>(b))?(a):(b))
87#endif 88#endif
88 89
89extern void show_usage(void) __attribute__ ((noreturn)); 90extern void bb_show_usage(void) __attribute__ ((noreturn));
90extern void error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); 91extern void bb_error_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
91extern void error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))); 92extern void bb_error_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
92extern void perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); 93extern void bb_perror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
93extern void perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))); 94extern void bb_perror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
94extern void vherror_msg(const char *s, va_list p); 95extern void bb_vherror_msg(const char *s, va_list p);
95extern void herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2))); 96extern void bb_herror_msg(const char *s, ...) __attribute__ ((format (printf, 1, 2)));
96extern void herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2))); 97extern void bb_herror_msg_and_die(const char *s, ...) __attribute__ ((noreturn, format (printf, 1, 2)));
98
99extern void bb_perror_nomsg_and_die(void) __attribute__ ((noreturn));
100extern void bb_perror_nomsg(void);
97 101
98/* These two are used internally -- you shouldn't need to use them */ 102/* These two are used internally -- you shouldn't need to use them */
99extern void verror_msg(const char *s, va_list p); 103extern void bb_verror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0)));
100extern void vperror_msg(const char *s, va_list p); 104extern void bb_vperror_msg(const char *s, va_list p) __attribute__ ((format (printf, 1, 0)));
101 105
102const char *mode_string(int mode); 106extern const char *bb_mode_string(int mode);
103const char *time_string(time_t timeVal); 107extern int is_directory(const char *name, int followLinks, struct stat *statBuf);
104int is_directory(const char *name, int followLinks, struct stat *statBuf); 108
105int isDevice(const char *name); 109extern int remove_file(const char *path, int flags);
106 110extern int copy_file(const char *source, const char *dest, int flags);
107int remove_file(const char *path, int flags); 111extern ssize_t safe_read(int fd, void *buf, size_t count);
108int copy_file(const char *source, const char *dest, int flags); 112extern ssize_t bb_full_write(int fd, const void *buf, size_t len);
109int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); 113extern ssize_t bb_full_read(int fd, void *buf, size_t len);
110char *buildName(const char *dirName, const char *fileName); 114extern int recursive_action(const char *fileName, int recurse,
111int makeString(int argc, const char **argv, char *buf, int bufLen); 115 int followLinks, int depthFirst,
112char *getChunk(int size);
113char *chunkstrdup(const char *str);
114void freeChunks(void);
115ssize_t safe_read(int fd, void *buf, size_t count);
116int full_write(int fd, const char *buf, int len);
117int full_read(int fd, char *buf, int len);
118int recursive_action(const char *fileName, int recurse, int followLinks, int depthFirst,
119 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData), 116 int (*fileAction) (const char *fileName, struct stat* statbuf, void* userData),
120 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData), 117 int (*dirAction) (const char *fileName, struct stat* statbuf, void* userData),
121 void* userData); 118 void* userData);
122 119
123extern int parse_mode( const char* s, mode_t* theMode); 120extern int bb_parse_mode( const char* s, mode_t* theMode);
124extern long bb_xgetlarg(char *arg, int base, long lower, long upper); 121extern long bb_xgetlarg(char *arg, int base, long lower, long upper);
125 122
123extern unsigned long bb_baud_to_value(speed_t speed);
124extern speed_t bb_value_to_baud(unsigned long value);
125
126extern int get_kernel_revision(void); 126extern int get_kernel_revision(void);
127 127
128extern int get_console_fd(void); 128extern int get_console_fd(void);
129extern struct mntent *find_mount_point(const char *name, const char *table); 129extern struct mntent *find_mount_point(const char *name, const char *table);
130extern void write_mtab(char* blockDevice, char* directory, 130extern void write_mtab(char* blockDevice, char* directory,
131 char* filesystemType, long flags, char* string_flags); 131 char* filesystemType, long flags, char* string_flags);
132extern void erase_mtab(const char * name); 132extern void erase_mtab(const char * name);
133extern long atoi_w_units (const char *cp); 133extern long *find_pid_by_name( const char* pidName);
134extern long* find_pid_by_name( const char* pidName);
135extern char *find_real_root_device_name(const char* name); 134extern char *find_real_root_device_name(const char* name);
136extern char *get_line_from_file(FILE *file); 135extern char *bb_get_line_from_file(FILE *file);
137extern void print_file(FILE *file); 136extern char *bb_get_chomped_line_from_file(FILE *file);
138extern int copyfd(int fd1, int fd2, const off_t chunksize); 137extern int bb_copyfd(int fd1, int fd2, const off_t chunksize);
139extern int print_file_by_name(char *filename); 138extern void bb_xprint_and_close_file(FILE *file);
140extern char process_escape_sequence(const char **ptr); 139extern int bb_xprint_file_by_name(const char *filename);
141extern char *get_last_path_component(char *path); 140extern char bb_process_escape_sequence(const char **ptr);
142extern FILE *wfopen(const char *path, const char *mode); 141extern char *bb_get_last_path_component(char *path);
143extern FILE *xfopen(const char *path, const char *mode); 142extern FILE *bb_wfopen(const char *path, const char *mode);
143extern FILE *bb_xfopen(const char *path, const char *mode);
144
145//#warning rename?
146extern int bb_fclose_nonstdin(FILE *f);
147extern void bb_fflush_stdout_and_exit(int retval) __attribute__ ((noreturn));
148extern unsigned long bb_getopt_ulflags(int argc, char **argv, const char *applet_opts);
149//#warning rename?
150extern FILE *bb_wfopen_input(const char *filename);
151
152extern int bb_vfprintf(FILE * __restrict stream, const char * __restrict format,
153 va_list arg) __attribute__ ((format (printf, 2, 0)));
154extern int bb_vprintf(const char * __restrict format, va_list arg)
155 __attribute__ ((format (printf, 1, 0)));
156extern int bb_fprintf(FILE * __restrict stream, const char * __restrict format, ...)
157 __attribute__ ((format (printf, 2, 3)));
158extern int bb_printf(const char * __restrict format, ...)
159 __attribute__ ((format (printf, 1, 2)));
160
161//#warning rename to xferror_filename?
162extern void bb_xferror(FILE *fp, const char *fn);
163extern void bb_xferror_stdout(void);
164extern void bb_xfflush_stdout(void);
165
166extern void bb_warn_ignoring_args(int n);
167
144extern void chomp(char *s); 168extern void chomp(char *s);
145extern void trim(char *s); 169extern void trim(char *s);
170extern const char *bb_skip_whitespace(const char *);
171
146extern struct BB_applet *find_applet_by_name(const char *name); 172extern struct BB_applet *find_applet_by_name(const char *name);
147void run_applet_by_name(const char *name, int argc, char **argv); 173void run_applet_by_name(const char *name, int argc, char **argv);
148 174
175//#warning is this needed anymore?
149#ifndef DMALLOC 176#ifndef DMALLOC
150extern void *xmalloc (size_t size); 177extern void *xmalloc (size_t size);
151extern void *xrealloc(void *old, size_t size); 178extern void *xrealloc(void *old, size_t size);
152extern void *xcalloc(size_t nmemb, size_t size); 179extern void *xcalloc(size_t nmemb, size_t size);
153extern char *xstrdup (const char *s); 180extern char *bb_xstrdup (const char *s);
154#endif 181#endif
155extern char *xstrndup (const char *s, int n); 182extern char *bb_xstrndup (const char *s, int n);
156extern char * safe_strncpy(char *dst, const char *src, size_t size); 183extern char * safe_strncpy(char *dst, const char *src, size_t size);
157 184
158struct suffix_mult { 185struct suffix_mult {
159 const char *suffix; 186 const char *suffix;
160 int mult; 187 unsigned int mult;
161}; 188};
162 189
163extern unsigned long parse_number(const char *numstr, 190extern unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
191 unsigned long lower,
192 unsigned long upper,
193 const struct suffix_mult *suffixes);
194extern unsigned long bb_xgetularg_bnd(const char *arg, int base,
195 unsigned long lower,
196 unsigned long upper);
197extern unsigned long bb_xgetularg10_bnd(const char *arg,
198 unsigned long lower,
199 unsigned long upper);
200extern unsigned long bb_xgetularg10(const char *arg);
201
202extern long bb_xgetlarg_bnd_sfx(const char *arg, int base,
203 long lower,
204 long upper,
205 const struct suffix_mult *suffixes);
206extern long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes);
207
208
209//#warning pitchable now?
210extern unsigned long bb_xparse_number(const char *numstr,
164 const struct suffix_mult *suffixes); 211 const struct suffix_mult *suffixes);
165 212
166 213
214//#warning change names?
215
167/* These parse entries in /etc/passwd and /etc/group. This is desirable 216/* These parse entries in /etc/passwd and /etc/group. This is desirable
168 * for BusyBox since we want to avoid using the glibc NSS stuff, which 217 * for BusyBox since we want to avoid using the glibc NSS stuff, which
169 * increases target size and is often not needed embedded systems. */ 218 * increases target size and is often not needed embedded systems. */
@@ -217,7 +266,7 @@ enum {
217}; 266};
218const char *make_human_readable_str(unsigned long size, unsigned long block_size, unsigned long display_unit); 267const char *make_human_readable_str(unsigned long size, unsigned long block_size, unsigned long display_unit);
219 268
220int ask_confirmation(void); 269int bb_ask_confirmation(void);
221int klogctl(int type, char * b, int len); 270int klogctl(int type, char * b, int len);
222 271
223char *xgetcwd(char *cwd); 272char *xgetcwd(char *cwd);
@@ -228,6 +277,7 @@ char *last_char_is(const char *s, int c);
228extern long arith (const char *startbuf, int *errcode); 277extern long arith (const char *startbuf, int *errcode);
229 278
230int read_package_field(const char *package_buffer, char **field_name, char **field_value); 279int read_package_field(const char *package_buffer, char **field_name, char **field_value);
280//#warning yuk!
231char *fgets_str(FILE *file, const char *terminating_string); 281char *fgets_str(FILE *file, const char *terminating_string);
232 282
233extern int uncompress(int fd_in, int fd_out); 283extern int uncompress(int fd_in, int fd_out);
@@ -239,19 +289,15 @@ extern int create_icmp_socket(void);
239extern int create_icmp6_socket(void); 289extern int create_icmp6_socket(void);
240extern int xconnect(const char *host, const char *port); 290extern int xconnect(const char *host, const char *port);
241 291
292//#warning wrap this?
242char *dirname (char *path); 293char *dirname (char *path);
243 294
244int make_directory (char *path, long mode, int flags); 295int bb_make_directory (char *path, long mode, int flags);
245 296
246const char *u_signal_names(const char *str_sig, int *signo, int startnum); 297const char *u_signal_names(const char *str_sig, int *signo, int startnum);
247char *simplify_path(const char *path); 298char *bb_simplify_path(const char *path);
248
249#define CT_AUTO 0
250#define CT_UNIX2DOS 1
251#define CT_DOS2UNIX 2
252/* extern int convert(char *fn, int ConvType); */
253 299
254enum { 300enum { /* DO NOT CHANGE THESE VALUES! cp.c depends on them. */
255 FILEUTILS_PRESERVE_STATUS = 1, 301 FILEUTILS_PRESERVE_STATUS = 1,
256 FILEUTILS_DEREFERENCE = 2, 302 FILEUTILS_DEREFERENCE = 2,
257 FILEUTILS_RECUR = 4, 303 FILEUTILS_RECUR = 4,
@@ -259,29 +305,31 @@ enum {
259 FILEUTILS_INTERACTIVE = 16 305 FILEUTILS_INTERACTIVE = 16
260}; 306};
261 307
262extern const char *applet_name; 308extern const char *bb_applet_name;
263extern const char * const full_version; 309
264extern const char * const name_too_long; 310extern const char * const bb_msg_full_version;
265extern const char * const omitting_directory; 311extern const char * const bb_msg_memory_exhausted;
266extern const char * const not_a_directory; 312extern const char * const bb_msg_invalid_date;
267extern const char * const memory_exhausted; 313extern const char * const bb_msg_io_error;
268extern const char * const invalid_date; 314extern const char * const bb_msg_write_error;
269extern const char * const invalid_option; 315extern const char * const bb_msg_name_longer_than_foo;
270extern const char * const io_error; 316extern const char * const bb_msg_unknown;
271extern const char * const dash_dash_help; 317extern const char * const bb_msg_can_not_create_raw_socket;
272extern const char * const write_error; 318extern const char * const bb_msg_perm_denied_are_you_root;
273extern const char * const too_few_args; 319extern const char * const bb_msg_standard_input;
274extern const char * const name_longer_than_foo; 320extern const char * const bb_msg_standard_output;
275extern const char * const unknown; 321
276extern const char * const can_not_create_raw_socket; 322extern const char * const bb_path_nologin_file;
277extern const char * const nologin_file; 323extern const char * const bb_path_passwd_file;
278extern const char * const passwd_file; 324extern const char * const bb_path_shadow_file;
279extern const char * const shadow_file; 325extern const char * const bb_path_gshadow_file;
280extern const char * const gshadow_file; 326extern const char * const bb_path_group_file;
281extern const char * const group_file; 327extern const char * const bb_path_securetty_file;
282extern const char * const securetty_file; 328extern const char * const bb_path_motd_file;
283extern const char * const motd_file; 329
284extern const char * const _path_login; 330extern const char bb_path_mtab_file[];
331
332extern int bb_default_error_retval;
285 333
286#ifdef CONFIG_FEATURE_DEVFS 334#ifdef CONFIG_FEATURE_DEVFS
287# define CURRENT_VC "/dev/vc/0" 335# define CURRENT_VC "/dev/vc/0"
@@ -309,6 +357,8 @@ extern const char * const _path_login;
309# define LOOP_FORMAT "/dev/loop%d" 357# define LOOP_FORMAT "/dev/loop%d"
310#endif 358#endif
311 359
360//#warning put these in .o files
361
312/* The following devices are the same on devfs and non-devfs systems. */ 362/* The following devices are the same on devfs and non-devfs systems. */
313#define CURRENT_TTY "/dev/tty" 363#define CURRENT_TTY "/dev/tty"
314#define CONSOLE_DEV "/dev/console" 364#define CONSOLE_DEV "/dev/console"
@@ -318,10 +368,10 @@ void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
318void reset_ino_dev_hashtable(void); 368void reset_ino_dev_hashtable(void);
319 369
320/* Stupid gcc always includes its own builtin strlen()... */ 370/* Stupid gcc always includes its own builtin strlen()... */
321extern size_t xstrlen(const char *string); 371extern size_t bb_strlen(const char *string);
322#define strlen(x) xstrlen(x) 372#define strlen(x) bb_strlen(x)
323 373
324void bb_asprintf(char **string_ptr, const char *format, ...) __attribute__ ((format (printf, 2, 3))); 374void bb_xasprintf(char **string_ptr, const char *format, ...) __attribute__ ((format (printf, 2, 3)));
325 375
326 376
327#define FAIL_DELAY 3 377#define FAIL_DELAY 3
@@ -335,10 +385,10 @@ extern char *pw_encrypt(const char *clear, const char *salt);
335extern struct spwd *pwd_to_spwd(const struct passwd *pw); 385extern struct spwd *pwd_to_spwd(const struct passwd *pw);
336extern int obscure(const char *old, const char *newval, const struct passwd *pwdp); 386extern int obscure(const char *old, const char *newval, const struct passwd *pwdp);
337 387
338extern int xopen(const char *pathname, int flags); 388extern int bb_xopen(const char *pathname, int flags);
339extern ssize_t xread(int fd, void *buf, size_t count); 389extern ssize_t bb_xread(int fd, void *buf, size_t count);
340extern void xread_all(int fd, void *buf, size_t count); 390extern void bb_xread_all(int fd, void *buf, size_t count);
341extern unsigned char xread_char(int fd); 391extern unsigned char bb_xread_char(int fd);
342 392
343typedef struct { 393typedef struct {
344 int pid; 394 int pid;
diff --git a/include/usage.h b/include/usage.h
index 8fc12f151..bbc0babfc 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -98,9 +98,11 @@
98 "\t-y\tDisplay the entire year." 98 "\t-y\tDisplay the entire year."
99 99
100#define cat_trivial_usage \ 100#define cat_trivial_usage \
101 "[FILE]..." 101 "[-u] [FILE]..."
102#define cat_full_usage \ 102#define cat_full_usage \
103 "Concatenates FILE(s) and prints them to stdout." 103 "Concatenates FILE(s) and prints them to stdout.\n\n" \
104 "Options:\n" \
105 "\t-u\tignored since unbuffered i/o is always used"
104#define cat_example_usage \ 106#define cat_example_usage \
105 "$ cat /proc/uptime\n" \ 107 "$ cat /proc/uptime\n" \
106 "110716.72 17.67" 108 "110716.72 17.67"
@@ -397,20 +399,34 @@
397#define dpkg_deb_example_usage \ 399#define dpkg_deb_example_usage \
398 "$ dpkg-deb -X ./busybox_0.48-1_i386.deb /tmp\n" 400 "$ dpkg-deb -X ./busybox_0.48-1_i386.deb /tmp\n"
399 401
402#ifdef CONFIG_FEATURE_DU_DEFALT_BLOCKSIZE_1K
403#define USAGE_DU_DEFALT_BLOCKSIZE_1k(a) a
404#define USAGE_NOT_DU_DEFALT_BLOCKSIZE_1k(a)
405#else
406#define USAGE_DU_DEFALT_BLOCKSIZE_1k(a)
407#define USAGE_NOT_DU_DEFALT_BLOCKSIZE_1k(a) a
408#endif
409
400#define du_trivial_usage \ 410#define du_trivial_usage \
401 "[-lsx" USAGE_HUMAN_READABLE("hm") USAGE_NOT_HUMAN_READABLE("") "k] [FILE]..." 411 "[-aHLdclsx" USAGE_HUMAN_READABLE("hm") "k] [FILE]..."
402#define du_full_usage \ 412#define du_full_usage \
403 "Summarizes disk space used for each FILE and/or directory.\n" \ 413 "Summarizes disk space used for each FILE and/or directory.\n" \
404 "Disk space is printed in units of 1024 bytes.\n\n" \ 414 "Disk space is printed in units of " \
405 "Options:\n" \ 415 USAGE_DU_DEFALT_BLOCKSIZE_1k("1024") USAGE_NOT_DU_DEFALT_BLOCKSIZE_1k("512") \
416 " bytes.\n\n" \
417 "Options:\n" \
418 "\t-a\tshow sizes of files in addition to directories\n" \
419 "\t-H\tfollow symbolic links that are FILE command line args\n" \
420 "\t-L\tfollow all symbolic links encountered\n" \
421 "\t-d N\tlimit output to directories (and files with -a) of depth < N\n" \
422 "\t-c\toutput a grand total\n" \
406 "\t-l\tcount sizes many times if hard linked\n" \ 423 "\t-l\tcount sizes many times if hard linked\n" \
407 "\t-s\tdisplay only a total for each argument" \ 424 "\t-s\tdisplay only a total for each argument\n" \
408 USAGE_HUMAN_READABLE( \
409 "\n\t-h\tprint sizes in human readable format (e.g., 1K 243M 2G )\n" \
410 "\t-m\tprint sizes in megabytes\n" \
411 "\t-x\tskip directories on different filesystems\n" \ 425 "\t-x\tskip directories on different filesystems\n" \
412 "\t-k\tprint sizes in kilobytes(default)") USAGE_NOT_HUMAN_READABLE( \ 426 USAGE_HUMAN_READABLE( \
413 "\n\t-k\tprint sizes in kilobytes(compatibility)") 427 "\t-h\tprint sizes in human readable format (e.g., 1K 243M 2G )\n" \
428 "\t-m\tprint sizes in megabytes\n" ) \
429 "\t-k\tprint sizes in kilobytes" USAGE_DU_DEFALT_BLOCKSIZE_1k("(default)")
414#define du_example_usage \ 430#define du_example_usage \
415 "$ du\n" \ 431 "$ du\n" \
416 "16 ./CVS\n" \ 432 "16 ./CVS\n" \
@@ -451,25 +467,31 @@
451 "8|125||l4|||0|0|0|955637629|998367|0\n" \ 467 "8|125||l4|||0|0|0|955637629|998367|0\n" \
452 "6|245|tty1|1|LOGIN||0|0|0|955637630|998974|0\n" \ 468 "6|245|tty1|1|LOGIN||0|0|0|955637630|998974|0\n" \
453 "6|246|tty2|2|LOGIN||0|0|0|955637630|999498|0\n" \ 469 "6|246|tty2|2|LOGIN||0|0|0|955637630|999498|0\n" \
454 "7|336|pts/0|vt00andersen|andersen|:0.0|0|0|0|955637763|0|0\n" 470 "7|336|pts/0|vt00|andersen|:0.0|0|0|0|955637763|0|0\n"
471
472#ifdef CONFIG_FEATURE_FANCY_ECHO
473 #define USAGE_FANCY_ECHO(a) a
474#else
475 #define USAGE_FANCY_ECHO(a)
476#endif
455 477
456#define echo_trivial_usage \ 478#define echo_trivial_usage \
457 "[-neE] [ARG ...]" 479 USAGE_FANCY_ECHO("[-neE] ") "[ARG ...]"
458#define echo_full_usage \ 480#define echo_full_usage \
459 "Prints the specified ARGs to stdout\n\n" \ 481 "Prints the specified ARGs to stdout\n\n" \
460 "Options:\n" \ 482 USAGE_FANCY_ECHO("Options:\n" \
461 "\t-n\tsuppress trailing newline\n" \ 483 "\t-n\tsuppress trailing newline\n" \
462 "\t-e\tinterpret backslash-escaped characters (i.e., \\t=tab)\n" \ 484 "\t-e\tinterpret backslash-escaped characters (i.e., \\t=tab)\n" \
463 "\t-E\tdisable interpretation of backslash-escaped characters" 485 "\t-E\tdisable interpretation of backslash-escaped characters")
464#define echo_example_usage \ 486#define echo_example_usage \
465 "$ echo "Erik is cool"\n" \ 487 "$ echo "Erik is cool"\n" \
466 "Erik is cool\n" \ 488 "Erik is cool\n" \
467 "$ echo -e "Erik\\nis\\ncool"\n" \ 489 USAGE_FANCY_ECHO("$ echo -e "Erik\\nis\\ncool"\n" \
468 "Erik\n" \ 490 "Erik\n" \
469 "is\n" \ 491 "is\n" \
470 "cool\n" \ 492 "cool\n" \
471 "$ echo "Erik\\nis\\ncool"\n" \ 493 "$ echo "Erik\\nis\\ncool"\n" \
472 "Erik\\nis\\ncool\n" 494 "Erik\\nis\\ncool\n")
473 495
474#define env_trivial_usage \ 496#define env_trivial_usage \
475 "[-iu] [-] [name=value]... [command]" 497 "[-iu] [-] [name=value]... [command]"
@@ -757,6 +779,12 @@
757#define halt_full_usage \ 779#define halt_full_usage \
758 "Halt the system." 780 "Halt the system."
759 781
782#ifdef CONFIG_FEATURE_FANCY_HEAD
783#define USAGE_FANCY_HEAD(a) a
784#else
785#define USAGE_FANCY_HEAD(a)
786#endif
787
760#define head_trivial_usage \ 788#define head_trivial_usage \
761 "[OPTION] [FILE]..." 789 "[OPTION] [FILE]..."
762#define head_full_usage \ 790#define head_full_usage \
@@ -764,7 +792,11 @@
764 "With more than one FILE, precede each with a header giving the\n" \ 792 "With more than one FILE, precede each with a header giving the\n" \
765 "file name. With no FILE, or when FILE is -, read standard input.\n\n" \ 793 "file name. With no FILE, or when FILE is -, read standard input.\n\n" \
766 "Options:\n" \ 794 "Options:\n" \
767 "\t-n NUM\t\tPrint first NUM lines instead of first 10" 795 "\t-n NUM\t\tPrint first NUM lines instead of first 10" \
796 USAGE_FANCY_HEAD( \
797 "\n\t-c NUM\t\toutput the first NUM bytes\n" \
798 "\t-q\t\tnever output headers giving file names\n" \
799 "\t-v\t\talways output headers giving file names" )
768#define head_example_usage \ 800#define head_example_usage \
769 "$ head -n 2 /etc/passwd\n" \ 801 "$ head -n 2 /etc/passwd\n" \
770 "root:x:0:0:root:/root:/bin/bash\n" \ 802 "root:x:0:0:root:/root:/bin/bash\n" \
@@ -1762,7 +1794,7 @@
1762 "Remove (unlink) the FILE(s). You may use '--' to\n" \ 1794 "Remove (unlink) the FILE(s). You may use '--' to\n" \
1763 "indicate that all following arguments are non-options.\n\n" \ 1795 "indicate that all following arguments are non-options.\n\n" \
1764 "Options:\n" \ 1796 "Options:\n" \
1765 "\t-i\t\talways prompt before removing each destination" \ 1797 "\t-i\t\talways prompt before removing each destination\n" \
1766 "\t-f\t\tremove existing destinations, never prompt\n" \ 1798 "\t-f\t\tremove existing destinations, never prompt\n" \
1767 "\t-r or -R\tremove the contents of directories recursively" 1799 "\t-r or -R\tremove the contents of directories recursively"
1768#define rm_example_usage \ 1800#define rm_example_usage \
@@ -1864,14 +1896,26 @@
1864#define sha1sum_full_usage \ 1896#define sha1sum_full_usage \
1865 "[OPTION] [FILE]" 1897 "[OPTION] [FILE]"
1866 1898
1899#ifdef CONFIG_FEATURE_FANCY_SLEEP
1900 #define USAGE_FANCY_SLEEP(a) a
1901 #define USAGE_NOT_FANCY_SLEEP(a)
1902#else
1903 #define USAGE_FANCY_SLEEP(a)
1904 #define USAGE_NOT_FANCY_SLEEP(a) a
1905#endif
1906
1867#define sleep_trivial_usage \ 1907#define sleep_trivial_usage \
1868 "N" 1908 USAGE_FANCY_SLEEP("[") "N" USAGE_FANCY_SLEEP("]...")
1869#define sleep_full_usage \ 1909#define sleep_full_usage \
1870 "Pause for N seconds." 1910 USAGE_NOT_FANCY_SLEEP("Pause for N seconds.") \
1911 USAGE_FANCY_SLEEP( \
1912"Pause for a time equal to the total of the args given, where each arg can\n" \
1913"have an optional suffix of (s)econds, (m)inutes, (h)ours, or (d)ays.")
1871#define sleep_example_usage \ 1914#define sleep_example_usage \
1872 "$ sleep 2\n" \ 1915 "$ sleep 2\n" \
1873 "[2 second delay results]\n" 1916 "[2 second delay results]\n" \
1874 1917 USAGE_FANCY_SLEEP("$ sleep 1d 3h 22m 8s\n" \
1918 "[98528 second delay results]\n")
1875 1919
1876#ifdef CONFIG_FEATURE_SORT_UNIQUE 1920#ifdef CONFIG_FEATURE_SORT_UNIQUE
1877 #define USAGE_SORT_UNIQUE(a) a 1921 #define USAGE_SORT_UNIQUE(a) a
@@ -2077,7 +2121,8 @@
2077#define tee_full_usage \ 2121#define tee_full_usage \
2078 "Copy standard input to each FILE, and also to standard output.\n\n" \ 2122 "Copy standard input to each FILE, and also to standard output.\n\n" \
2079 "Options:\n" \ 2123 "Options:\n" \
2080 "\t-a\tappend to the given FILEs, do not overwrite" 2124 "\t-a\tappend to the given FILEs, do not overwrite\n" \
2125 "\t-i\tignore interrupt signals (SIGINT)"
2081#define tee_example_usage \ 2126#define tee_example_usage \
2082 "$ echo "Hello" | tee /tmp/foo\n" \ 2127 "$ echo "Hello" | tee /tmp/foo\n" \
2083 "$ cat /tmp/foo\n" \ 2128 "$ cat /tmp/foo\n" \
@@ -2312,7 +2357,9 @@
2312 "Options:\n" \ 2357 "Options:\n" \
2313 "\t-c\tprefix lines by the number of occurrences\n" \ 2358 "\t-c\tprefix lines by the number of occurrences\n" \
2314 "\t-d\tonly print duplicate lines\n" \ 2359 "\t-d\tonly print duplicate lines\n" \
2315 "\t-u\tonly print unique lines" 2360 "\t-u\tonly print unique lines\n" \
2361 "\t-f N\tskip the first N fields\n" \
2362 "\t-s N\tskip the first N chars (after any skipped fields)"
2316#define uniq_example_usage \ 2363#define uniq_example_usage \
2317 "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n" \ 2364 "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n" \
2318 "a\n" \ 2365 "a\n" \
diff --git a/init/halt.c b/init/halt.c
index 3d4725f09..7e663227c 100644
--- a/init/halt.c
+++ b/init/halt.c
@@ -32,7 +32,7 @@ extern int halt_main(int argc, char **argv)
32 if (!pid || *pid<=0) { 32 if (!pid || *pid<=0) {
33 pid = find_pid_by_name("linuxrc"); 33 pid = find_pid_by_name("linuxrc");
34 if (!pid || *pid<=0) 34 if (!pid || *pid<=0)
35 error_msg_and_die("no process killed"); 35 bb_error_msg_and_die("no process killed");
36 } 36 }
37 return(kill(*pid, SIGUSR1)); 37 return(kill(*pid, SIGUSR1));
38#else 38#else
diff --git a/init/init.c b/init/init.c
index 258a7fa6b..be91d6a8f 100644
--- a/init/init.c
+++ b/init/init.c
@@ -247,14 +247,14 @@ static void message(int device, const char *fmt, ...)
247 if (log_fd < 0) { 247 if (log_fd < 0) {
248 if ((log_fd = device_open(log, O_RDWR | O_NDELAY | O_NOCTTY)) < 0) { 248 if ((log_fd = device_open(log, O_RDWR | O_NDELAY | O_NOCTTY)) < 0) {
249 log_fd = -2; 249 log_fd = -2;
250 error_msg("Bummer, can't write to log on %s!", log); 250 bb_error_msg("Bummer, can't write to log on %s!", log);
251 device = CONSOLE; 251 device = CONSOLE;
252 } else { 252 } else {
253 fcntl(log_fd, F_SETFD, FD_CLOEXEC); 253 fcntl(log_fd, F_SETFD, FD_CLOEXEC);
254 } 254 }
255 } 255 }
256 if ((device & LOG) && (log_fd >= 0)) { 256 if ((device & LOG) && (log_fd >= 0)) {
257 full_write(log_fd, msg, l); 257 bb_full_write(log_fd, msg, l);
258 } 258 }
259#endif 259#endif
260 260
@@ -263,12 +263,12 @@ static void message(int device, const char *fmt, ...)
263 O_WRONLY | O_NOCTTY | O_NDELAY); 263 O_WRONLY | O_NOCTTY | O_NDELAY);
264 /* Always send console messages to /dev/console so people will see them. */ 264 /* Always send console messages to /dev/console so people will see them. */
265 if (fd >= 0) { 265 if (fd >= 0) {
266 full_write(fd, msg, l); 266 bb_full_write(fd, msg, l);
267 close(fd); 267 close(fd);
268#ifdef DEBUG_INIT 268#ifdef DEBUG_INIT
269 /* all descriptors may be closed */ 269 /* all descriptors may be closed */
270 } else { 270 } else {
271 error_msg("Bummer, can't print: "); 271 bb_error_msg("Bummer, can't print: ");
272 va_start(arguments, fmt); 272 va_start(arguments, fmt);
273 vfprintf(stderr, fmt, arguments); 273 vfprintf(stderr, fmt, arguments);
274 va_end(arguments); 274 va_end(arguments);
@@ -323,7 +323,7 @@ static int check_free_memory(void)
323 unsigned int result, u, s = 10; 323 unsigned int result, u, s = 10;
324 324
325 if (sysinfo(&info) != 0) { 325 if (sysinfo(&info) != 0) {
326 perror_msg("Error checking free memory"); 326 bb_perror_msg("Error checking free memory");
327 return -1; 327 return -1;
328 } 328 }
329 329
@@ -564,11 +564,11 @@ static pid_t run(const struct init_action *a)
564 ++cmdpath; 564 ++cmdpath;
565 565
566 /* find the last component in the command pathname */ 566 /* find the last component in the command pathname */
567 s = get_last_path_component(cmdpath); 567 s = bb_get_last_path_component(cmdpath);
568 568
569 /* make a new argv[0] */ 569 /* make a new argv[0] */
570 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) { 570 if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) {
571 message(LOG | CONSOLE, memory_exhausted); 571 message(LOG | CONSOLE, bb_msg_memory_exhausted);
572 cmd[0] = cmdpath; 572 cmd[0] = cmdpath;
573 } else { 573 } else {
574 cmd[0][0] = '-'; 574 cmd[0][0] = '-';
@@ -589,7 +589,7 @@ static pid_t run(const struct init_action *a)
589 messageD(LOG, "Waiting for enter to start '%s'" 589 messageD(LOG, "Waiting for enter to start '%s'"
590 "(pid %d, terminal %s)\n", 590 "(pid %d, terminal %s)\n",
591 cmdpath, getpid(), a->terminal); 591 cmdpath, getpid(), a->terminal);
592 full_write(1, press_enter, sizeof(press_enter) - 1); 592 bb_full_write(1, press_enter, sizeof(press_enter) - 1);
593 while(read(0, &c, 1) == 1 && c != '\n') 593 while(read(0, &c, 1) == 1 && c != '\n')
594 ; 594 ;
595 } 595 }
@@ -1017,7 +1017,7 @@ extern int init_main(int argc, char **argv)
1017 if (!pid || *pid <= 0) { 1017 if (!pid || *pid <= 0) {
1018 pid = find_pid_by_name("linuxrc"); 1018 pid = find_pid_by_name("linuxrc");
1019 if (!pid || *pid <= 0) 1019 if (!pid || *pid <= 0)
1020 error_msg_and_die("no process killed"); 1020 bb_error_msg_and_die("no process killed");
1021 } 1021 }
1022 return kill(*pid, SIGHUP); 1022 return kill(*pid, SIGHUP);
1023 } 1023 }
@@ -1025,10 +1025,10 @@ extern int init_main(int argc, char **argv)
1025 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */ 1025 /* Expect to be invoked as init with PID=1 or be invoked as linuxrc */
1026 if (getpid() != 1 1026 if (getpid() != 1
1027#ifdef CONFIG_FEATURE_INITRD 1027#ifdef CONFIG_FEATURE_INITRD
1028 && strstr(applet_name, "linuxrc") == NULL 1028 && strstr(bb_applet_name, "linuxrc") == NULL
1029#endif 1029#endif
1030 ) { 1030 ) {
1031 show_usage(); 1031 bb_show_usage();
1032 } 1032 }
1033 /* Set up sig handlers -- be sure to 1033 /* Set up sig handlers -- be sure to
1034 * clear all of these in run() */ 1034 * clear all of these in run() */
@@ -1068,10 +1068,10 @@ extern int init_main(int argc, char **argv)
1068 const char * const *e; 1068 const char * const *e;
1069 /* Make sure environs is set to something sane */ 1069 /* Make sure environs is set to something sane */
1070 for(e = environment; *e; e++) 1070 for(e = environment; *e; e++)
1071 putenv(*e); 1071 putenv((char *) *e);
1072 } 1072 }
1073 /* Hello world */ 1073 /* Hello world */
1074 message(MAYBE_CONSOLE | LOG, "init started: %s", full_version); 1074 message(MAYBE_CONSOLE | LOG, "init started: %s", bb_msg_full_version);
1075 1075
1076 /* Make sure there is enough memory to do something useful. */ 1076 /* Make sure there is enough memory to do something useful. */
1077 check_memory(); 1077 check_memory();
diff --git a/init/mesg.c b/init/mesg.c
index f9b46e029..7b8acf657 100644
--- a/init/mesg.c
+++ b/init/mesg.c
@@ -52,7 +52,7 @@ extern int mesg_main(int argc, char *argv[])
52 return EXIT_SUCCESS; 52 return EXIT_SUCCESS;
53 } 53 }
54 } 54 }
55 perror_msg_and_die("%s", tty); 55 bb_perror_msg_and_die("%s", tty);
56 } 56 }
57 show_usage(); 57 bb_show_usage();
58} 58}
diff --git a/init/poweroff.c b/init/poweroff.c
index 27468bac0..aca6e2f25 100644
--- a/init/poweroff.c
+++ b/init/poweroff.c
@@ -32,7 +32,7 @@ extern int poweroff_main(int argc, char **argv)
32 if (!pid || *pid<=0) { 32 if (!pid || *pid<=0) {
33 pid = find_pid_by_name("linuxrc"); 33 pid = find_pid_by_name("linuxrc");
34 if (!pid || *pid<=0) 34 if (!pid || *pid<=0)
35 error_msg_and_die("no process killed"); 35 bb_error_msg_and_die("no process killed");
36 } 36 }
37 return(kill(*pid, SIGUSR2)); 37 return(kill(*pid, SIGUSR2));
38#else 38#else
diff --git a/init/reboot.c b/init/reboot.c
index 872d9e741..8c380fa6a 100644
--- a/init/reboot.c
+++ b/init/reboot.c
@@ -52,7 +52,7 @@ extern int reboot_main(int argc, char **argv)
52 break; 52 break;
53 53
54 default: 54 default:
55 show_usage(); 55 bb_show_usage();
56 break; 56 break;
57 } 57 }
58 } 58 }
@@ -99,7 +99,7 @@ extern int reboot_main(int argc, char **argv)
99 if (!pid || *pid<=0) 99 if (!pid || *pid<=0)
100 pid = find_pid_by_name("linuxrc"); 100 pid = find_pid_by_name("linuxrc");
101 if (!pid || *pid<=0) 101 if (!pid || *pid<=0)
102 error_msg_and_die("no process killed"); 102 bb_error_msg_and_die("no process killed");
103 fflush(stdout); 103 fflush(stdout);
104 return(kill(*pid, SIGTERM)); 104 return(kill(*pid, SIGTERM));
105 } 105 }
diff --git a/init/start_stop_daemon.c b/init/start_stop_daemon.c
index 576526183..a1c2c21c2 100644
--- a/init/start_stop_daemon.c
+++ b/init/start_stop_daemon.c
@@ -74,7 +74,7 @@ parse_options(int argc, char * const *argv)
74 break; 74 break;
75 case 's': 75 case 's':
76 if (sscanf(optarg, "%d", &signal_nr) != 1) 76 if (sscanf(optarg, "%d", &signal_nr) != 1)
77 error_msg_and_die ("-s takes a numeric argument"); 77 bb_error_msg_and_die ("-s takes a numeric argument");
78 break; 78 break;
79 case 'u': 79 case 'u':
80 userspec = optarg; 80 userspec = optarg;
@@ -86,21 +86,21 @@ parse_options(int argc, char * const *argv)
86 fork_before_exec = 1; 86 fork_before_exec = 1;
87 break; 87 break;
88 default: 88 default:
89 show_usage(); 89 bb_show_usage();
90 } 90 }
91 } 91 }
92 92
93 if (start == stop) 93 if (start == stop)
94 error_msg_and_die ("need one of -S or -K"); 94 bb_error_msg_and_die ("need one of -S or -K");
95 95
96 if (!execname && !userspec) 96 if (!execname && !userspec)
97 error_msg_and_die ("need at least one of -x or -u"); 97 bb_error_msg_and_die ("need at least one of -x or -u");
98 98
99 if (!startas) 99 if (!startas)
100 startas = execname; 100 startas = execname;
101 101
102 if (start && !startas) 102 if (start && !startas)
103 error_msg_and_die ("-S needs -x or -a"); 103 bb_error_msg_and_die ("-S needs -x or -a");
104} 104}
105 105
106 106
@@ -185,7 +185,7 @@ do_procfs(void)
185 185
186 procdir = opendir("/proc"); 186 procdir = opendir("/proc");
187 if (!procdir) 187 if (!procdir)
188 perror_msg_and_die ("opendir /proc"); 188 bb_perror_msg_and_die ("opendir /proc");
189 189
190 foundany = 0; 190 foundany = 0;
191 while ((entry = readdir(procdir)) != NULL) { 191 while ((entry = readdir(procdir)) != NULL) {
@@ -196,7 +196,7 @@ do_procfs(void)
196 } 196 }
197 closedir(procdir); 197 closedir(procdir);
198 if (!foundany) 198 if (!foundany)
199 error_msg_and_die ("nothing in /proc - not mounted?"); 199 bb_error_msg_and_die ("nothing in /proc - not mounted?");
200} 200}
201 201
202 202
@@ -214,7 +214,7 @@ do_stop(void)
214 else if (userspec) 214 else if (userspec)
215 sprintf(what, "process(es) owned by `%s'", userspec); 215 sprintf(what, "process(es) owned by `%s'", userspec);
216 else 216 else
217 error_msg_and_die ("internal error, please report"); 217 bb_error_msg_and_die ("internal error, please report");
218 218
219 if (!found) { 219 if (!found) {
220 printf("no %s found; none killed.\n", what); 220 printf("no %s found; none killed.\n", what);
@@ -225,7 +225,7 @@ do_stop(void)
225 p->pid = -p->pid; 225 p->pid = -p->pid;
226 killed++; 226 killed++;
227 } else { 227 } else {
228 perror_msg("warning: failed to kill %d:", p->pid); 228 bb_perror_msg("warning: failed to kill %d:", p->pid);
229 } 229 }
230 } 230 }
231 if (killed) { 231 if (killed) {
@@ -262,10 +262,10 @@ start_stop_daemon_main(int argc, char **argv)
262 *--argv = startas; 262 *--argv = startas;
263 if (fork_before_exec) { 263 if (fork_before_exec) {
264 if (daemon(0, 0) == -1) 264 if (daemon(0, 0) == -1)
265 perror_msg_and_die ("unable to fork"); 265 bb_perror_msg_and_die ("unable to fork");
266 } 266 }
267 setsid(); 267 setsid();
268 execv(startas, argv); 268 execv(startas, argv);
269 perror_msg_and_die ("unable to start %s", startas); 269 bb_perror_msg_and_die ("unable to start %s", startas);
270} 270}
271 271
diff --git a/libbb/Makefile.in b/libbb/Makefile.in
index 6d2475bcf..c7916f108 100644
--- a/libbb/Makefile.in
+++ b/libbb/Makefile.in
@@ -42,26 +42,59 @@ LIBBB_SRC:= \
42 restricted_shell.c run_parts.c run_shell.c safe_read.c safe_strncpy.c \ 42 restricted_shell.c run_parts.c run_shell.c safe_read.c safe_strncpy.c \
43 setup_environment.c simplify_path.c syscalls.c syslog_msg_with_name.c \ 43 setup_environment.c simplify_path.c syscalls.c syslog_msg_with_name.c \
44 time_string.c trim.c u_signal_names.c vdprintf.c verror_msg.c \ 44 time_string.c trim.c u_signal_names.c vdprintf.c verror_msg.c \
45 vherror_msg.c vperror_msg.c wfopen.c xconnect.c xgetcwd.c xfuncs.c \ 45 vherror_msg.c vperror_msg.c wfopen.c xconnect.c xgetcwd.c \
46 xgethostbyname.c xgethostbyname2.c xreadlink.c xregcomp.c xgetlarg.c 46 xgethostbyname.c xgethostbyname2.c xreadlink.c xregcomp.c xgetlarg.c \
47 47 \
48 fclose_nonstdin.c fflush_stdout_and_exit.c getopt_ulflags.c \
49 default_error_retval.c wfopen_input.c speed_table.c \
50 perror_nomsg_and_die.c perror_nomsg.c skip_whitespace.c \
51 warn_ignoring_args.c
48 52
49LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC)) 53LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC))
50 54
51LIBBB_MSRC:=$(LIBBB_DIR)messages.c 55LIBBB_MSRC0:=$(LIBBB_DIR)messages.c
52LIBBB_MOBJ:=full_version.o name_too_long.o omitting_directory.o not_a_directory.o \ 56LIBBB_MOBJ0:=full_version.o \
53 memory_exhausted.o invalid_date.o invalid_option.o io_error.o dash_dash_help.o \ 57 memory_exhausted.o invalid_date.o io_error.o \
54 write_error.o too_few_args.o name_longer_than_foo.o unknown.o can_not_create_raw_socket.o \ 58 write_error.o name_longer_than_foo.o unknown.o \
55 shadow_file.o passwd_file.o group_file.o gshadow_file.o nologin_file.o securetty_file.o \ 59 can_not_create_raw_socket.o perm_denied_are_you_root.o \
56 motd_file.o 60 shadow_file.o passwd_file.o group_file.o gshadow_file.o nologin_file.o \
57LIBBB_MOBJS=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ)) 61 securetty_file.o motd_file.o \
62 msg_standard_input.o msg_standard_output.o
63
64LIBBB_MSRC1:=$(LIBBB_DIR)xfuncs.c
65LIBBB_MOBJ1:=xmalloc.o xrealloc.o xcalloc.o xstrdup.o xstrndup.o \
66 xfopen.o xopen.o xread.o xread_all.o xread_char.o \
67 xferror.o xferror_stdout.o xfflush_stdout.o strlen.o
68
69LIBBB_MSRC2:=$(LIBBB_DIR)printf.c
70LIBBB_MOBJ2:=vfprintf.o vprintf.o fprintf.o printf.o
71
72LIBBB_MSRC3:=$(LIBBB_DIR)xgetularg.c
73LIBBB_MOBJ3:=xgetularg_bnd_sfx.o xgetlarg_bnd_sfx.o getlarg10_sfx.o \
74 xgetularg_bnd.o xgetularg10_bnd.o xgetularg10.o
75
76LIBBB_MOBJS0=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ0))
77LIBBB_MOBJS1=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ1))
78LIBBB_MOBJS2=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ2))
79LIBBB_MOBJS3=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ3))
58 80
59libraries-y+=$(LIBBB_DIR)$(LIBBB_AR) 81libraries-y+=$(LIBBB_DIR)$(LIBBB_AR)
60 82
61$(LIBBB_DIR)$(LIBBB_AR): $(LIBBB_OBJS) $(LIBBB_MOBJS) 83$(LIBBB_DIR)$(LIBBB_AR): $(LIBBB_OBJS) $(LIBBB_MOBJS0) $(LIBBB_MOBJS1) \
62 $(AR) -ro $@ $(LIBBB_OBJS) $(LIBBB_MOBJS) 84 $(LIBBB_MOBJS2) $(LIBBB_MOBJS3)
85 $(AR) -ro $@ $(LIBBB_OBJS) $(LIBBB_MOBJS0) $(LIBBB_MOBJS1) \
86 $(LIBBB_MOBJS2) $(LIBBB_MOBJS3)
87
88$(LIBBB_MOBJS0): $(LIBBB_MSRC0)
89 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@
90
91$(LIBBB_MOBJS1): $(LIBBB_MSRC1)
92 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@
93
94$(LIBBB_MOBJS2): $(LIBBB_MSRC2)
95 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@
63 96
64$(LIBBB_MOBJS): $(LIBBB_MSRC) 97$(LIBBB_MOBJS3): $(LIBBB_MSRC3)
65 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@ 98 $(CC) $(CFLAGS) $(EXTRA_CFLAGS) -DL_$(notdir $*) -c $< -o $@
66 99
67$(LIBBB_DIR)loop.o: $(LIBBB_DIR)loop.h 100$(LIBBB_DIR)loop.o: $(LIBBB_DIR)loop.h
diff --git a/libbb/ask_confirmation.c b/libbb/ask_confirmation.c
index d4d943ad7..a99a4e733 100644
--- a/libbb/ask_confirmation.c
+++ b/libbb/ask_confirmation.c
@@ -1,49 +1,49 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Utility routines. 3 * bb_ask_confirmation implementation for busybox
4 * 4 *
5 * Copyright (C) many different people. If you wrote this, please 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * acknowledge your work.
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 10 * (at your option) any later version.
12 * 11 *
13 * This program is distributed in the hope that it will be useful, but 12 * This program is distributed in the hope that it will be useful,
14 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details. 15 * General Public License for more details.
17 * 16 *
18 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * USA 20 *
21 */
22
23/* Read a line from stdin. If the first non-whitespace char is 'y' or 'Y',
24 * return 1. Otherwise return 0.
22 */ 25 */
23 26
24#include <stdio.h> 27#include <stdio.h>
28#include <ctype.h>
25#include "libbb.h" 29#include "libbb.h"
26 30
27 31int bb_ask_confirmation(void)
28int ask_confirmation()
29{ 32{
30 int c = '\0'; 33 int retval = 0;
31 int ret = 0; 34 int first = 1;
35 int c;
32 36
33 while (c != '\n') { 37 while (((c = getchar()) != EOF) && (c != '\n')) {
34 c = getchar(); 38 /* Make sure we get the actual function call for isspace,
35 if ( c != '\n' ) { 39 * as speed is not critical here. */
36 ret = ((c=='y')||(c=='Y')) ? 1 : 0; 40 if (first && !(isspace)(c)) {
41 --first;
42 if ((c == 'y') || (c == 'Y')) {
43 ++retval;
44 }
37 } 45 }
38 } 46 }
39 return ret;
40}
41 47
42/* END CODE */ 48 return retval;
43/* 49}
44Local Variables:
45c-file-style: "linux"
46c-basic-offset: 4
47tab-width: 4
48End:
49*/
diff --git a/libbb/bb_asprintf.c b/libbb/bb_asprintf.c
index 9a71be7f5..7075b46de 100644
--- a/libbb/bb_asprintf.c
+++ b/libbb/bb_asprintf.c
@@ -5,17 +5,18 @@
5#include <stdlib.h> 5#include <stdlib.h>
6#include <stdio.h> 6#include <stdio.h>
7#include <stdarg.h> 7#include <stdarg.h>
8
9
10#include "libbb.h" 8#include "libbb.h"
11 9
12 10void bb_xasprintf(char **string_ptr, const char *format, ...)
13void bb_asprintf(char **string_ptr, const char *format, ...)
14{ 11{
15 va_list p; 12 va_list p;
13 int r;
14
15 va_start(p, format);
16 r = vasprintf(string_ptr, format, p);
17 va_end(p);
16 18
17 va_start(p, format); 19 if (r < 0) {
18 if(vasprintf(string_ptr, format, p)<0) 20 bb_perror_msg_and_die("bb_xasprintf");
19 error_msg_and_die(memory_exhausted); 21 }
20 va_end(p);
21} 22}
diff --git a/libbb/change_identity.c b/libbb/change_identity.c
index 819b216e0..c2b73eeb8 100644
--- a/libbb/change_identity.c
+++ b/libbb/change_identity.c
@@ -43,12 +43,12 @@
43void change_identity ( const struct passwd *pw ) 43void change_identity ( const struct passwd *pw )
44{ 44{
45 if ( initgroups ( pw-> pw_name, pw-> pw_gid ) == -1 ) 45 if ( initgroups ( pw-> pw_name, pw-> pw_gid ) == -1 )
46 perror_msg_and_die ( "cannot set groups" ); 46 bb_perror_msg_and_die ( "cannot set groups" );
47 endgrent ( ); 47 endgrent ( );
48 48
49 if ( setgid ( pw-> pw_gid )) 49 if ( setgid ( pw-> pw_gid ))
50 perror_msg_and_die ( "cannot set group id" ); 50 bb_perror_msg_and_die ( "cannot set group id" );
51 if ( setuid ( pw->pw_uid )) 51 if ( setuid ( pw->pw_uid ))
52 perror_msg_and_die ( "cannot set user id" ); 52 bb_perror_msg_and_die ( "cannot set user id" );
53} 53}
54 54
diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c
index b158ae447..993b46266 100644
--- a/libbb/compare_string_array.c
+++ b/libbb/compare_string_array.c
@@ -27,4 +27,5 @@ extern unsigned short compare_string_array(const char *string_array[], const cha
27 } 27 }
28 } 28 }
29 return(i); 29 return(i);
30} \ No newline at end of file 30}
31
diff --git a/libbb/concat_path_file.c b/libbb/concat_path_file.c
index 0146606a1..b972ba6a3 100644
--- a/libbb/concat_path_file.c
+++ b/libbb/concat_path_file.c
@@ -38,7 +38,7 @@ extern char *concat_path_file(const char *path, const char *filename)
38 lc = last_char_is(path, '/'); 38 lc = last_char_is(path, '/');
39 while (*filename == '/') 39 while (*filename == '/')
40 filename++; 40 filename++;
41 bb_asprintf(&outbuf, "%s%s%s", path, (lc==NULL)? "/" : "", filename); 41 bb_xasprintf(&outbuf, "%s%s%s", path, (lc==NULL)? "/" : "", filename);
42 42
43 return outbuf; 43 return outbuf;
44} 44}
diff --git a/libbb/copy_file.c b/libbb/copy_file.c
index 23a2d75a3..81c547479 100644
--- a/libbb/copy_file.c
+++ b/libbb/copy_file.c
@@ -43,19 +43,19 @@ int copy_file(const char *source, const char *dest, int flags)
43 lstat(source, &source_stat) < 0) || 43 lstat(source, &source_stat) < 0) ||
44 ((flags & FILEUTILS_DEREFERENCE) && 44 ((flags & FILEUTILS_DEREFERENCE) &&
45 stat(source, &source_stat) < 0)) { 45 stat(source, &source_stat) < 0)) {
46 perror_msg("%s", source); 46 bb_perror_msg("%s", source);
47 return -1; 47 return -1;
48 } 48 }
49 49
50 if (lstat(dest, &dest_stat) < 0) { 50 if (lstat(dest, &dest_stat) < 0) {
51 if (errno != ENOENT) { 51 if (errno != ENOENT) {
52 perror_msg("unable to stat `%s'", dest); 52 bb_perror_msg("unable to stat `%s'", dest);
53 return -1; 53 return -1;
54 } 54 }
55 } else { 55 } else {
56 if (source_stat.st_dev == dest_stat.st_dev && 56 if (source_stat.st_dev == dest_stat.st_dev &&
57 source_stat.st_ino == dest_stat.st_ino) { 57 source_stat.st_ino == dest_stat.st_ino) {
58 error_msg("`%s' and `%s' are the same file", source, dest); 58 bb_error_msg("`%s' and `%s' are the same file", source, dest);
59 return -1; 59 return -1;
60 } 60 }
61 dest_exists = 1; 61 dest_exists = 1;
@@ -67,14 +67,14 @@ int copy_file(const char *source, const char *dest, int flags)
67 mode_t saved_umask = 0; 67 mode_t saved_umask = 0;
68 68
69 if (!(flags & FILEUTILS_RECUR)) { 69 if (!(flags & FILEUTILS_RECUR)) {
70 error_msg("%s: omitting directory", source); 70 bb_error_msg("%s: omitting directory", source);
71 return -1; 71 return -1;
72 } 72 }
73 73
74 /* Create DEST. */ 74 /* Create DEST. */
75 if (dest_exists) { 75 if (dest_exists) {
76 if (!S_ISDIR(dest_stat.st_mode)) { 76 if (!S_ISDIR(dest_stat.st_mode)) {
77 error_msg("`%s' is not a directory", dest); 77 bb_error_msg("`%s' is not a directory", dest);
78 return -1; 78 return -1;
79 } 79 }
80 } else { 80 } else {
@@ -88,7 +88,7 @@ int copy_file(const char *source, const char *dest, int flags)
88 88
89 if (mkdir(dest, mode) < 0) { 89 if (mkdir(dest, mode) < 0) {
90 umask(saved_umask); 90 umask(saved_umask);
91 perror_msg("cannot create directory `%s'", dest); 91 bb_perror_msg("cannot create directory `%s'", dest);
92 return -1; 92 return -1;
93 } 93 }
94 94
@@ -97,7 +97,7 @@ int copy_file(const char *source, const char *dest, int flags)
97 97
98 /* Recursively copy files in SOURCE. */ 98 /* Recursively copy files in SOURCE. */
99 if ((dp = opendir(source)) == NULL) { 99 if ((dp = opendir(source)) == NULL) {
100 perror_msg("unable to open directory `%s'", source); 100 bb_perror_msg("unable to open directory `%s'", source);
101 status = -1; 101 status = -1;
102 goto end; 102 goto end;
103 } 103 }
@@ -121,7 +121,7 @@ int copy_file(const char *source, const char *dest, int flags)
121 121
122 if (!dest_exists && 122 if (!dest_exists &&
123 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) { 123 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
124 perror_msg("unable to change permissions of `%s'", dest); 124 bb_perror_msg("unable to change permissions of `%s'", dest);
125 status = -1; 125 status = -1;
126 } 126 }
127 } else if (S_ISREG(source_stat.st_mode)) { 127 } else if (S_ISREG(source_stat.st_mode)) {
@@ -132,7 +132,7 @@ int copy_file(const char *source, const char *dest, int flags)
132 if (!(flags & FILEUTILS_DEREFERENCE) && 132 if (!(flags & FILEUTILS_DEREFERENCE) &&
133 is_in_ino_dev_hashtable(&source_stat, &link_name)) { 133 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
134 if (link(link_name, dest) < 0) { 134 if (link(link_name, dest) < 0) {
135 perror_msg("unable to link `%s'", dest); 135 bb_perror_msg("unable to link `%s'", dest);
136 return -1; 136 return -1;
137 } 137 }
138 138
@@ -140,14 +140,14 @@ int copy_file(const char *source, const char *dest, int flags)
140 } 140 }
141#endif 141#endif
142 142
143 if ((sfp = wfopen(source, "r")) == NULL) { 143 if ((sfp = bb_wfopen(source, "r")) == NULL) {
144 return -1; 144 return -1;
145 } 145 }
146 146
147 if (dest_exists) { 147 if (dest_exists) {
148 if (flags & FILEUTILS_INTERACTIVE) { 148 if (flags & FILEUTILS_INTERACTIVE) {
149 fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest); 149 fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest);
150 if (!ask_confirmation()) { 150 if (!bb_ask_confirmation()) {
151 fclose (sfp); 151 fclose (sfp);
152 return 0; 152 return 0;
153 } 153 }
@@ -155,13 +155,13 @@ int copy_file(const char *source, const char *dest, int flags)
155 155
156 if ((dfp = fopen(dest, "w")) == NULL) { 156 if ((dfp = fopen(dest, "w")) == NULL) {
157 if (!(flags & FILEUTILS_FORCE)) { 157 if (!(flags & FILEUTILS_FORCE)) {
158 perror_msg("unable to open `%s'", dest); 158 bb_perror_msg("unable to open `%s'", dest);
159 fclose (sfp); 159 fclose (sfp);
160 return -1; 160 return -1;
161 } 161 }
162 162
163 if (unlink(dest) < 0) { 163 if (unlink(dest) < 0) {
164 perror_msg("unable to remove `%s'", dest); 164 bb_perror_msg("unable to remove `%s'", dest);
165 fclose (sfp); 165 fclose (sfp);
166 return -1; 166 return -1;
167 } 167 }
@@ -177,22 +177,22 @@ int copy_file(const char *source, const char *dest, int flags)
177 (dfp = fdopen(fd, "w")) == NULL) { 177 (dfp = fdopen(fd, "w")) == NULL) {
178 if (fd >= 0) 178 if (fd >= 0)
179 close(fd); 179 close(fd);
180 perror_msg("unable to open `%s'", dest); 180 bb_perror_msg("unable to open `%s'", dest);
181 fclose (sfp); 181 fclose (sfp);
182 return -1; 182 return -1;
183 } 183 }
184 } 184 }
185 185
186 if (copyfd(fileno(sfp), fileno(dfp), 0) == -1) 186 if (bb_copyfd(fileno(sfp), fileno(dfp), 0) == -1)
187 status = -1; 187 status = -1;
188 188
189 if (fclose(dfp) < 0) { 189 if (fclose(dfp) < 0) {
190 perror_msg("unable to close `%s'", dest); 190 bb_perror_msg("unable to close `%s'", dest);
191 status = -1; 191 status = -1;
192 } 192 }
193 193
194 if (fclose(sfp) < 0) { 194 if (fclose(sfp) < 0) {
195 perror_msg("unable to close `%s'", source); 195 bb_perror_msg("unable to close `%s'", source);
196 status = -1; 196 status = -1;
197 } 197 }
198 } 198 }
@@ -202,23 +202,23 @@ int copy_file(const char *source, const char *dest, int flags)
202 202
203 if (dest_exists && 203 if (dest_exists &&
204 ((flags & FILEUTILS_FORCE) == 0 || unlink(dest) < 0)) { 204 ((flags & FILEUTILS_FORCE) == 0 || unlink(dest) < 0)) {
205 perror_msg("unable to remove `%s'", dest); 205 bb_perror_msg("unable to remove `%s'", dest);
206 return -1; 206 return -1;
207 207
208 } 208 }
209 } else { 209 } else {
210 error_msg("internal error: unrecognized file type"); 210 bb_error_msg("internal error: unrecognized file type");
211 return -1; 211 return -1;
212 } 212 }
213 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || 213 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
214 S_ISSOCK(source_stat.st_mode)) { 214 S_ISSOCK(source_stat.st_mode)) {
215 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { 215 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
216 perror_msg("unable to create `%s'", dest); 216 bb_perror_msg("unable to create `%s'", dest);
217 return -1; 217 return -1;
218 } 218 }
219 } else if (S_ISFIFO(source_stat.st_mode)) { 219 } else if (S_ISFIFO(source_stat.st_mode)) {
220 if (mkfifo(dest, source_stat.st_mode) < 0) { 220 if (mkfifo(dest, source_stat.st_mode) < 0) {
221 perror_msg("cannot create fifo `%s'", dest); 221 bb_perror_msg("cannot create fifo `%s'", dest);
222 return -1; 222 return -1;
223 } 223 }
224 } else if (S_ISLNK(source_stat.st_mode)) { 224 } else if (S_ISLNK(source_stat.st_mode)) {
@@ -226,7 +226,7 @@ int copy_file(const char *source, const char *dest, int flags)
226 226
227 lpath = xreadlink(source); 227 lpath = xreadlink(source);
228 if (symlink(lpath, dest) < 0) { 228 if (symlink(lpath, dest) < 0) {
229 perror_msg("cannot create symlink `%s'", dest); 229 bb_perror_msg("cannot create symlink `%s'", dest);
230 return -1; 230 return -1;
231 } 231 }
232 free(lpath); 232 free(lpath);
@@ -234,7 +234,7 @@ int copy_file(const char *source, const char *dest, int flags)
234#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) 234#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
235 if (flags & FILEUTILS_PRESERVE_STATUS) 235 if (flags & FILEUTILS_PRESERVE_STATUS)
236 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) 236 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
237 perror_msg("unable to preserve ownership of `%s'", dest); 237 bb_perror_msg("unable to preserve ownership of `%s'", dest);
238#endif 238#endif
239 239
240#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS 240#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
@@ -256,13 +256,13 @@ end:
256 times.actime = source_stat.st_atime; 256 times.actime = source_stat.st_atime;
257 times.modtime = source_stat.st_mtime; 257 times.modtime = source_stat.st_mtime;
258 if (utime(dest, &times) < 0) 258 if (utime(dest, &times) < 0)
259 perror_msg("unable to preserve times of `%s'", dest); 259 bb_perror_msg("unable to preserve times of `%s'", dest);
260 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { 260 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
261 source_stat.st_mode &= ~(S_ISUID | S_ISGID); 261 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
262 perror_msg("unable to preserve ownership of `%s'", dest); 262 bb_perror_msg("unable to preserve ownership of `%s'", dest);
263 } 263 }
264 if (chmod(dest, source_stat.st_mode) < 0) 264 if (chmod(dest, source_stat.st_mode) < 0)
265 perror_msg("unable to preserve permissions of `%s'", dest); 265 bb_perror_msg("unable to preserve permissions of `%s'", dest);
266 } 266 }
267 267
268 return status; 268 return status;
diff --git a/libbb/copy_file_chunk.c b/libbb/copy_file_chunk.c
deleted file mode 100644
index 63d2ab173..000000000
--- a/libbb/copy_file_chunk.c
+++ /dev/null
@@ -1,70 +0,0 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
5 * Copyright (C) many different people. If you wrote this, please
6 * acknowledge your work.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 */
23
24#include <stdio.h>
25#include <sys/stat.h>
26#include "libbb.h"
27
28/* Copy CHUNKSIZE bytes (or until EOF if CHUNKSIZE equals -1) from SRC_FILE
29 * to DST_FILE. */
30extern int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize)
31{
32 size_t nread, nwritten, size;
33 char buffer[BUFSIZ];
34
35 while (chunksize != 0) {
36 if (chunksize > BUFSIZ)
37 size = BUFSIZ;
38 else
39 size = chunksize;
40
41 nread = fread (buffer, 1, size, src_file);
42
43 if (nread != size && ferror (src_file)) {
44 perror_msg ("read");
45 return -1;
46 } else if (nread == 0) {
47 if (chunksize != -1) {
48 error_msg ("Unable to read all data");
49 return -1;
50 }
51
52 return 0;
53 }
54
55 nwritten = fwrite (buffer, 1, nread, dst_file);
56
57 if (nwritten != nread) {
58 if (ferror (dst_file))
59 perror_msg ("write");
60 else
61 error_msg ("Unable to write all data");
62 return -1;
63 }
64
65 if (chunksize != -1)
66 chunksize -= nwritten;
67 }
68
69 return 0;
70}
diff --git a/libbb/copyfd.c b/libbb/copyfd.c
index 4df1fd084..41b78c7d6 100644
--- a/libbb/copyfd.c
+++ b/libbb/copyfd.c
@@ -22,65 +22,51 @@
22#include <unistd.h> 22#include <unistd.h>
23#include <string.h> 23#include <string.h>
24#include <errno.h> 24#include <errno.h>
25#include "libbb.h" 25#include "busybox.h"
26 26
27/* If chunksize is 0 copy untill EOF */ 27#if BUFSIZ < 4096
28extern int copyfd(int fd1, int fd2, const off_t chunksize) 28#undef BUFSIZ
29#define BUFSIZ 4096
30#endif
31
32/* If chunksize is 0 copy until EOF */
33extern int bb_copyfd(int fd1, int fd2, const off_t chunksize)
29{ 34{
30 size_t nread; 35 ssize_t nread;
31 size_t nwritten;
32 size_t size; 36 size_t size;
33 size_t remaining; 37 off_t remaining;
34 char buffer[BUFSIZ]; 38 RESERVE_CONFIG_BUFFER(buffer,BUFSIZ);
35 39
40 remaining = size = BUFSIZ;
36 if (chunksize) { 41 if (chunksize) {
37 remaining = chunksize; 42 remaining = chunksize;
38 } else {
39 remaining = -1;
40 } 43 }
41 44
42 do { 45 do {
43 if ((chunksize > BUFSIZ) || (chunksize == 0)) { 46 if (size > remaining) {
44 size = BUFSIZ; 47 size = remaining;
45 } else {
46 size = chunksize;
47 } 48 }
48 49
49 nread = safe_read(fd1, buffer, size); 50 if ((nread = safe_read(fd1, buffer, size)) > 0) {
50 51 if (bb_full_write(fd2, buffer, nread) < 0) {
51 if (nread == -1) { 52 bb_perror_msg(bb_msg_write_error); /* match Read error below */
52 perror_msg("read failure"); 53 break;
53 return(-1); 54 }
54 } 55 if (chunksize && ((remaining -= nread) == 0)) {
55 else if (nread == 0) { 56 return 0;
57 }
58 } else if (!nread) {
56 if (chunksize) { 59 if (chunksize) {
57 error_msg("Unable to read all data"); 60 bb_error_msg("Unable to read all data");
58 return(-1); 61 break;
59 } else {
60 return(0);
61 } 62 }
63 return 0;
64 } else { /* nread < 0 */
65 bb_perror_msg("Read error"); /* match bb_msg_write_error above */
66 break;
62 } 67 }
63 68
64 nwritten = full_write(fd2, buffer, nread); 69 } while (1);
65 70
66 if (nwritten != nread) { 71 return -1;
67 error_msg("Unable to write all data");
68 return(-1);
69 }
70
71 if (chunksize) {
72 remaining -= nwritten;
73 }
74 } while (remaining != 0);
75
76 return 0;
77} 72}
78
79/* END CODE */
80/*
81Local Variables:
82c-file-style: "linux"
83c-basic-offset: 4
84tab-width: 4
85End:
86*/
diff --git a/libbb/correct_password.c b/libbb/correct_password.c
index 758b89eed..396253614 100644
--- a/libbb/correct_password.c
+++ b/libbb/correct_password.c
@@ -55,7 +55,7 @@ int correct_password ( const struct passwd *pw )
55 struct spwd *sp = getspnam ( pw-> pw_name ); 55 struct spwd *sp = getspnam ( pw-> pw_name );
56 56
57 if ( !sp ) 57 if ( !sp )
58 error_msg_and_die ( "no valid shadow password" ); 58 bb_error_msg_and_die ( "no valid shadow password" );
59 59
60 correct = sp-> sp_pwdp; 60 correct = sp-> sp_pwdp;
61 } 61 }
@@ -73,6 +73,6 @@ int correct_password ( const struct passwd *pw )
73 return 0; 73 return 0;
74 } 74 }
75 encrypted = crypt ( unencrypted, correct ); 75 encrypted = crypt ( unencrypted, correct );
76 memset ( unencrypted, 0, xstrlen ( unencrypted )); 76 memset ( unencrypted, 0, bb_strlen ( unencrypted ));
77 return ( strcmp ( encrypted, correct ) == 0 ) ? 1 : 0; 77 return ( strcmp ( encrypted, correct ) == 0 ) ? 1 : 0;
78} 78}
diff --git a/libbb/create_icmp6_socket.c b/libbb/create_icmp6_socket.c
index 1d0b6b6bf..596610449 100644
--- a/libbb/create_icmp6_socket.c
+++ b/libbb/create_icmp6_socket.c
@@ -26,9 +26,9 @@ int create_icmp6_socket(void)
26 if ((sock = socket(AF_INET6, SOCK_RAW, 26 if ((sock = socket(AF_INET6, SOCK_RAW,
27 (proto ? proto->p_proto : IPPROTO_ICMPV6))) < 0) { 27 (proto ? proto->p_proto : IPPROTO_ICMPV6))) < 0) {
28 if (errno == EPERM) 28 if (errno == EPERM)
29 error_msg_and_die("permission denied. (are you root?)"); 29 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
30 else 30 else
31 perror_msg_and_die(can_not_create_raw_socket); 31 bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
32 } 32 }
33 33
34 /* drop root privs if running setuid */ 34 /* drop root privs if running setuid */
diff --git a/libbb/create_icmp_socket.c b/libbb/create_icmp_socket.c
index d804b3987..58d792b1b 100644
--- a/libbb/create_icmp_socket.c
+++ b/libbb/create_icmp_socket.c
@@ -25,9 +25,9 @@ int create_icmp_socket(void)
25 if ((sock = socket(AF_INET, SOCK_RAW, 25 if ((sock = socket(AF_INET, SOCK_RAW,
26 (proto ? proto->p_proto : 1))) < 0) { /* 1 == ICMP */ 26 (proto ? proto->p_proto : 1))) < 0) { /* 1 == ICMP */
27 if (errno == EPERM) 27 if (errno == EPERM)
28 error_msg_and_die("permission denied. (are you root?)"); 28 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
29 else 29 else
30 perror_msg_and_die(can_not_create_raw_socket); 30 bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
31 } 31 }
32 32
33 /* drop root privs if running setuid */ 33 /* drop root privs if running setuid */
diff --git a/libbb/default_error_retval.c b/libbb/default_error_retval.c
new file mode 100644
index 000000000..7d2d89bb5
--- /dev/null
+++ b/libbb/default_error_retval.c
@@ -0,0 +1,32 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
20
21/* Seems silly to copyright a global variable. ;-) Oh well.
22 *
23 * At least one applet (cmp) returns a value different from the typical
24 * EXIT_FAILURE values (1) when an error occurs. So, make it configureable
25 * by the applet. I suppose we could use a wrapper function to set it, but
26 * that too seems silly.
27 */
28
29#include <stdlib.h>
30#include "libbb.h"
31
32int bb_default_error_retval = EXIT_FAILURE;
diff --git a/libbb/dirname.c b/libbb/dirname.c
index df9a49daa..81298730b 100644
--- a/libbb/dirname.c
+++ b/libbb/dirname.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini dirname function. 3 * dirname implementation for busybox (for libc's missing one)
4 * 4 *
5 * Copyright (C) 2001 Matt Kraai. 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -17,39 +17,53 @@
17 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23/* Note: The previous busybox implementation did not handle NULL path
24 * and also moved a pointer before path, which is not portable in C.
25 * So I replaced it with my uClibc version.
20 */ 26 */
21 27
22#include <string.h> 28#include <string.h>
23#include "libbb.h" 29#include "libbb.h"
24 30
25#if defined __UCLIBC__ || __GNU_LIBRARY___ < 5 31#if __GNU_LIBRARY__ < 5
26
27/* Return a string containing the path name of the parent
28 * directory of PATH. */
29 32
33extern
30char *dirname(char *path) 34char *dirname(char *path)
31{ 35{
32 char *s; 36 static const char null_or_empty_or_noslash[] = ".";
33 37 register char *s;
34 /* Go to the end of the string. */ 38 register char *last;
35 s = path + strlen(path) - 1; 39 char *first;
36
37 /* Strip off trailing /s (unless it is also the leading /). */
38 while (path < s && s[0] == '/')
39 s--;
40 40
41 /* Strip the last component. */ 41 last = s = path;
42 while (path <= s && s[0] != '/')
43 s--;
44 42
45 while (path < s && s[0] == '/') 43 if (s != NULL) {
46 s--;
47 44
48 if (s < path) 45 LOOP:
49 return "."; 46 while (*s && (*s != '/')) ++s;
47 first = s;
48 while (*s == '/') ++s;
49 if (*s) {
50 last = first;
51 goto LOOP;
52 }
50 53
51 s[1] = '\0'; 54 if (last == path) {
52 return path; 55 if (*last != '/') {
56 goto DOT;
57 }
58 if ((*++last == '/') && (last[1] == 0)) {
59 ++last;
60 }
61 }
62 *last = 0;
63 return path;
64 }
65 DOT:
66 return (char *) null_or_empty_or_noslash;
53} 67}
54 68
55#endif 69#endif
diff --git a/libbb/dump.c b/libbb/dump.c
index 1afad83fd..26dabe57f 100644
--- a/libbb/dump.c
+++ b/libbb/dump.c
@@ -25,94 +25,80 @@
25#include <stdlib.h> 25#include <stdlib.h>
26#include <string.h> 26#include <string.h>
27#include <ctype.h> /* for isdigit() */ 27#include <ctype.h> /* for isdigit() */
28#include "dump.h"
29#include "libbb.h" 28#include "libbb.h"
29#include "dump.h"
30 30
31enum _vflag vflag = FIRST; 31enum _vflag bb_dump_vflag = FIRST;
32FS *fshead; /* head of format strings */ 32FS *bb_dump_fshead; /* head of format strings */
33extern FS *fshead; /* head of format strings */
34extern int blocksize;
35static FU *endfu; 33static FU *endfu;
36static char **_argv; 34static char **_argv;
37static off_t savaddress; /* saved address/offset in stream */ 35static off_t savaddress; /* saved address/offset in stream */
38static off_t eaddress; /* end address */ 36static off_t eaddress; /* end address */
39static off_t address; /* address/offset in stream */ 37static off_t address; /* address/offset in stream */
40off_t skip; /* bytes to skip */ 38off_t bb_dump_skip; /* bytes to skip */
41off_t saveaddress; 39static int exitval; /* final exit value */
42int exitval; /* final exit value */ 40int bb_dump_blocksize; /* data block size */
43int blocksize; /* data block size */ 41int bb_dump_length = -1; /* max bytes to read */
44int length = -1; /* max bytes to read */ 42
43static const char index_str[] = ".#-+ 0123456789";
45 44
45static const char size_conv_str[] =
46"\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG";
46 47
47int size(FS * fs) 48static const char lcc[] = "diouxX";
49
50int bb_dump_size(FS * fs)
48{ 51{
49 register FU *fu; 52 register FU *fu;
50 register int bcnt, cursize; 53 register int bcnt, cur_size;
51 register char *fmt; 54 register char *fmt;
55 const char *p;
52 int prec; 56 int prec;
53 57
54 /* figure out the data block size needed for each format unit */ 58 /* figure out the data block bb_dump_size needed for each format unit */
55 for (cursize = 0, fu = fs->nextfu; fu; fu = fu->nextfu) { 59 for (cur_size = 0, fu = fs->nextfu; fu; fu = fu->nextfu) {
56 if (fu->bcnt) { 60 if (fu->bcnt) {
57 cursize += fu->bcnt * fu->reps; 61 cur_size += fu->bcnt * fu->reps;
58 continue; 62 continue;
59 } 63 }
60 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) { 64 for (bcnt = prec = 0, fmt = fu->fmt; *fmt; ++fmt) {
61 if (*fmt != '%') 65 if (*fmt != '%')
62 continue; 66 continue;
63 /* 67 /*
64 * skip any special chars -- save precision in 68 * bb_dump_skip any special chars -- save precision in
65 * case it's a %s format. 69 * case it's a %s format.
66 */ 70 */
67 while (index(".#-+ 0123456789" + 1, *++fmt)); 71 while (strchr(index_str + 1, *++fmt));
68 if (*fmt == '.' && isdigit(*++fmt)) { 72 if (*fmt == '.' && isdigit(*++fmt)) {
69 prec = atoi(fmt); 73 prec = atoi(fmt);
70 while (isdigit(*++fmt)); 74 while (isdigit(*++fmt));
71 } 75 }
72 switch (*fmt) { 76 if (!(p = strchr(size_conv_str + 12, *fmt))) {
73 case 'c': 77 if (*fmt == 's') {
74 bcnt += 1; 78 bcnt += prec;
75 break; 79 } else if (*fmt == '_') {
76 case 'd': 80 ++fmt;
77 case 'i': 81 if ((*fmt == 'c') || (*fmt == 'p') || (*fmt == 'u')) {
78 case 'o': 82 bcnt += 1;
79 case 'u': 83 }
80 case 'x':
81 case 'X':
82 bcnt += 4;
83 break;
84 case 'e':
85 case 'E':
86 case 'f':
87 case 'g':
88 case 'G':
89 bcnt += 8;
90 break;
91 case 's':
92 bcnt += prec;
93 break;
94 case '_':
95 switch (*++fmt) {
96 case 'c':
97 case 'p':
98 case 'u':
99 bcnt += 1;
100 break;
101 } 84 }
85 } else {
86 bcnt += size_conv_str[p - (size_conv_str + 12)];
102 } 87 }
103 } 88 }
104 cursize += bcnt * fu->reps; 89 cur_size += bcnt * fu->reps;
105 } 90 }
106 return (cursize); 91 return (cur_size);
107} 92}
108 93
109void rewrite(FS * fs) 94static void rewrite(FS * fs)
110{ 95{
111 enum { NOTOKAY, USEBCNT, USEPREC } sokay; 96 enum { NOTOKAY, USEBCNT, USEPREC } sokay;
112 register PR *pr, **nextpr = NULL; 97 register PR *pr, **nextpr = NULL;
113 register FU *fu; 98 register FU *fu;
114 register char *p1, *p2; 99 register char *p1, *p2;
115 char savech, *fmtp; 100 char savech, *fmtp;
101 const char *byte_count_str;
116 int nconv, prec = 0; 102 int nconv, prec = 0;
117 103
118 for (fu = fs->nextfu; fu; fu = fu->nextfu) { 104 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
@@ -128,7 +114,7 @@ void rewrite(FS * fs)
128 else 114 else
129 *nextpr = pr; 115 *nextpr = pr;
130 116
131 /* skip preceding text and up to the next % sign */ 117 /* bb_dump_skip preceding text and up to the next % sign */
132 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1); 118 for (p1 = fmtp; *p1 && *p1 != '%'; ++p1);
133 119
134 /* only text in the string */ 120 /* only text in the string */
@@ -144,11 +130,11 @@ void rewrite(FS * fs)
144 */ 130 */
145 if (fu->bcnt) { 131 if (fu->bcnt) {
146 sokay = USEBCNT; 132 sokay = USEBCNT;
147 /* skip to conversion character */ 133 /* bb_dump_skip to conversion character */
148 for (++p1; index(".#-+ 0123456789", *p1); ++p1); 134 for (++p1; strchr(index_str, *p1); ++p1);
149 } else { 135 } else {
150 /* skip any special chars, field width */ 136 /* bb_dump_skip any special chars, field width */
151 while (index(".#-+ 0123456789" + 1, *++p1)); 137 while (strchr(index_str + 1, *++p1));
152 if (*p1 == '.' && isdigit(*++p1)) { 138 if (*p1 == '.' && isdigit(*++p1)) {
153 sokay = USEPREC; 139 sokay = USEPREC;
154 prec = atoi(p1); 140 prec = atoi(p1);
@@ -162,104 +148,59 @@ void rewrite(FS * fs)
162 /* 148 /*
163 * figure out the byte count for each conversion; 149 * figure out the byte count for each conversion;
164 * rewrite the format as necessary, set up blank- 150 * rewrite the format as necessary, set up blank-
165 * padding for end of data. 151 * pbb_dump_adding for end of data.
166 */ 152 */
167 switch (*p1) { 153
168 case 'c': 154 if (*p1 == 'c') {
169 pr->flags = F_CHAR; 155 pr->flags = F_CHAR;
170 switch (fu->bcnt) { 156 DO_BYTE_COUNT_1:
171 case 0: 157 byte_count_str = "\001";
172 case 1: 158 DO_BYTE_COUNT:
173 pr->bcnt = 1; 159 if (fu->bcnt) {
174 break; 160 do {
175 default: 161 if (fu->bcnt == *byte_count_str) {
176 p1[1] = '\0'; 162 break;
177 error_msg_and_die 163 }
178 ("bad byte count for conversion character %s.", p1); 164 } while (*++byte_count_str);
179 } 165 }
180 break; 166 /* Unlike the original, output the remainder of the format string. */
181 case 'd': 167 if (!*byte_count_str) {
182 case 'i': 168 bb_error_msg_and_die("bad byte count for conversion character %s.", p1);
183 pr->flags = F_INT; 169 }
184 goto sw1; 170 pr->bcnt = *byte_count_str;
185 case 'l': 171 } else if (*p1 == 'l') {
186 ++p2; 172 ++p2;
187 switch (p1[1]) { 173 ++p1;
188 case 'd': 174 DO_INT_CONV:
189 case 'i': 175 {
190 ++p1; 176 const char *e;
177 if (!(e = strchr(lcc, *p1))) {
178 goto DO_BAD_CONV_CHAR;
179 }
191 pr->flags = F_INT; 180 pr->flags = F_INT;
192 goto sw1; 181 if (e > lcc + 1) {
193 case 'o': 182 pr->flags = F_UINT;
194 case 'u': 183 }
195 case 'x': 184 byte_count_str = "\004\002\001";
196 case 'X': 185 goto DO_BYTE_COUNT;
197 ++p1;
198 pr->flags = F_UINT;
199 goto sw1;
200 default:
201 p1[2] = '\0';
202 error_msg_and_die
203 ("hexdump: bad conversion character %%%s.\n", p1);
204 } 186 }
205 /* NOTREACHED */ 187 /* NOTREACHED */
206 case 'o': 188 } else if (strchr(lcc, *p1)) {
207 case 'u': 189 goto DO_INT_CONV;
208 case 'x': 190 } else if (strchr("eEfgG", *p1)) {
209 case 'X':
210 pr->flags = F_UINT;
211 sw1:switch (fu->bcnt) {
212 case 0:
213 case 4:
214 pr->bcnt = 4;
215 break;
216 case 1:
217 pr->bcnt = 1;
218 break;
219 case 2:
220 pr->bcnt = 2;
221 break;
222 default:
223 p1[1] = '\0';
224 error_msg_and_die
225 ("bad byte count for conversion character %s.", p1);
226 }
227 break;
228 case 'e':
229 case 'E':
230 case 'f':
231 case 'g':
232 case 'G':
233 pr->flags = F_DBL; 191 pr->flags = F_DBL;
234 switch (fu->bcnt) { 192 byte_count_str = "\010\004";
235 case 0: 193 goto DO_BYTE_COUNT;
236 case 8: 194 } else if (*p1 == 's') {
237 pr->bcnt = 8;
238 break;
239 case 4:
240 pr->bcnt = 4;
241 break;
242 default:
243 p1[1] = '\0';
244 error_msg_and_die
245 ("bad byte count for conversion character %s.", p1);
246 }
247 break;
248 case 's':
249 pr->flags = F_STR; 195 pr->flags = F_STR;
250 switch (sokay) { 196 if (sokay == USEBCNT) {
251 case NOTOKAY:
252 error_msg_and_die
253 ("%%s requires a precision or a byte count.");
254 case USEBCNT:
255 pr->bcnt = fu->bcnt; 197 pr->bcnt = fu->bcnt;
256 break; 198 } else if (sokay == USEPREC) {
257 case USEPREC:
258 pr->bcnt = prec; 199 pr->bcnt = prec;
259 break; 200 } else { /* NOTOKAY */
201 bb_error_msg_and_die("%%s requires a precision or a byte count.");
260 } 202 }
261 break; 203 } else if (*p1 == '_') {
262 case '_':
263 ++p2; 204 ++p2;
264 switch (p1[1]) { 205 switch (p1[1]) {
265 case 'A': 206 case 'A':
@@ -269,51 +210,29 @@ void rewrite(FS * fs)
269 case 'a': 210 case 'a':
270 pr->flags = F_ADDRESS; 211 pr->flags = F_ADDRESS;
271 ++p2; 212 ++p2;
272 switch (p1[2]) { 213 if ((p1[2] != 'd') && (p1[2] != 'o') && (p1[2] != 'x')) {
273 case 'd': 214 goto DO_BAD_CONV_CHAR;
274 case 'o':
275 case 'x':
276 *p1 = p1[2];
277 break;
278 default:
279 p1[3] = '\0';
280 error_msg_and_die
281 ("hexdump: bad conversion character %%%s.\n", p1);
282 } 215 }
216 *p1 = p1[2];
283 break; 217 break;
284 case 'c': 218 case 'c':
285 pr->flags = F_C; 219 pr->flags = F_C;
286 /* *p1 = 'c'; set in conv_c */ 220 /* *p1 = 'c'; set in conv_c */
287 goto sw2; 221 goto DO_BYTE_COUNT_1;
288 case 'p': 222 case 'p':
289 pr->flags = F_P; 223 pr->flags = F_P;
290 *p1 = 'c'; 224 *p1 = 'c';
291 goto sw2; 225 goto DO_BYTE_COUNT_1;
292 case 'u': 226 case 'u':
293 pr->flags = F_U; 227 pr->flags = F_U;
294 /* *p1 = 'c'; set in conv_u */ 228 /* *p1 = 'c'; set in conv_u */
295 sw2:switch (fu->bcnt) { 229 goto DO_BYTE_COUNT_1;
296 case 0:
297 case 1:
298 pr->bcnt = 1;
299 break;
300 default:
301 p1[2] = '\0';
302 error_msg_and_die
303 ("bad byte count for conversion character %s.",
304 p1);
305 }
306 break;
307 default: 230 default:
308 p1[2] = '\0'; 231 goto DO_BAD_CONV_CHAR;
309 error_msg_and_die
310 ("hexdump: bad conversion character %%%s.\n", p1);
311 } 232 }
312 break; 233 } else {
313 default: 234 DO_BAD_CONV_CHAR:
314 p1[1] = '\0'; 235 bb_error_msg_and_die("bad conversion character %%%s.\n", p1);
315 error_msg_and_die("hexdump: bad conversion character %%%s.\n",
316 p1);
317 } 236 }
318 237
319 /* 238 /*
@@ -322,16 +241,14 @@ void rewrite(FS * fs)
322 */ 241 */
323 savech = *p2; 242 savech = *p2;
324 p1[1] = '\0'; 243 p1[1] = '\0';
325 if (!(pr->fmt = strdup(fmtp))) 244 pr->fmt = bb_xstrdup(fmtp);
326 perror_msg_and_die("hexdump");
327 *p2 = savech; 245 *p2 = savech;
328 pr->cchar = pr->fmt + (p1 - fmtp); 246 pr->cchar = pr->fmt + (p1 - fmtp);
329 fmtp = p2; 247 fmtp = p2;
330 248
331 /* only one conversion character if byte count */ 249 /* only one conversion character if byte count */
332 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) { 250 if (!(pr->flags & F_ADDRESS) && fu->bcnt && nconv++) {
333 error_msg_and_die 251 bb_error_msg_and_die("byte count with multiple conversion characters.\n");
334 ("hexdump: byte count with multiple conversion characters.\n");
335 } 252 }
336 } 253 }
337 /* 254 /*
@@ -344,7 +261,7 @@ void rewrite(FS * fs)
344 } 261 }
345 /* 262 /*
346 * if the format string interprets any data at all, and it's 263 * if the format string interprets any data at all, and it's
347 * not the same as the blocksize, and its last format unit 264 * not the same as the bb_dump_blocksize, and its last format unit
348 * interprets any data at all, and has no iteration count, 265 * interprets any data at all, and has no iteration count,
349 * repeat it as necessary. 266 * repeat it as necessary.
350 * 267 *
@@ -352,9 +269,9 @@ void rewrite(FS * fs)
352 * gets output from the last iteration of the format unit. 269 * gets output from the last iteration of the format unit.
353 */ 270 */
354 for (fu = fs->nextfu;; fu = fu->nextfu) { 271 for (fu = fs->nextfu;; fu = fu->nextfu) {
355 if (!fu->nextfu && fs->bcnt < blocksize && 272 if (!fu->nextfu && fs->bcnt < bb_dump_blocksize &&
356 !(fu->flags & F_SETREP) && fu->bcnt) 273 !(fu->flags & F_SETREP) && fu->bcnt)
357 fu->reps += (blocksize - fs->bcnt) / fu->bcnt; 274 fu->reps += (bb_dump_blocksize - fs->bcnt) / fu->bcnt;
358 if (fu->reps > 1) { 275 if (fu->reps > 1) {
359 for (pr = fu->nextpr;; pr = pr->nextpr) 276 for (pr = fu->nextpr;; pr = pr->nextpr)
360 if (!pr->nextpr) 277 if (!pr->nextpr)
@@ -369,31 +286,31 @@ void rewrite(FS * fs)
369 } 286 }
370} 287}
371 288
372static void doskip(char *fname, int statok) 289static void do_skip(char *fname, int statok)
373{ 290{
374 struct stat sbuf; 291 struct stat sbuf;
375 292
376 if (statok) { 293 if (statok) {
377 if (fstat(fileno(stdin), &sbuf)) { 294 if (fstat(fileno(stdin), &sbuf)) {
378 perror_msg_and_die("hexdump: %s", fname); 295 bb_perror_msg_and_die("%s", fname);
379 } 296 }
380 if ((!(S_ISCHR(sbuf.st_mode) || 297 if ((!(S_ISCHR(sbuf.st_mode) ||
381 S_ISBLK(sbuf.st_mode) || 298 S_ISBLK(sbuf.st_mode) ||
382 S_ISFIFO(sbuf.st_mode))) && skip >= sbuf.st_size) { 299 S_ISFIFO(sbuf.st_mode))) && bb_dump_skip >= sbuf.st_size) {
383 /* If size valid and skip >= size */ 300 /* If bb_dump_size valid and bb_dump_skip >= size */
384 skip -= sbuf.st_size; 301 bb_dump_skip -= sbuf.st_size;
385 address += sbuf.st_size; 302 address += sbuf.st_size;
386 return; 303 return;
387 } 304 }
388 } 305 }
389 if (fseek(stdin, skip, SEEK_SET)) { 306 if (fseek(stdin, bb_dump_skip, SEEK_SET)) {
390 perror_msg_and_die("hexdump: %s", fname); 307 bb_perror_msg_and_die("%s", fname);
391 } 308 }
392 savaddress = address += skip; 309 savaddress = address += bb_dump_skip;
393 skip = 0; 310 bb_dump_skip = 0;
394} 311}
395 312
396int next(char **argv) 313static int next(char **argv)
397{ 314{
398 static int done; 315 static int done;
399 int statok; 316 int statok;
@@ -405,7 +322,7 @@ int next(char **argv)
405 for (;;) { 322 for (;;) {
406 if (*_argv) { 323 if (*_argv) {
407 if (!(freopen(*_argv, "r", stdin))) { 324 if (!(freopen(*_argv, "r", stdin))) {
408 perror_msg("%s", *_argv); 325 bb_perror_msg("%s", *_argv);
409 exitval = 1; 326 exitval = 1;
410 ++_argv; 327 ++_argv;
411 continue; 328 continue;
@@ -416,11 +333,11 @@ int next(char **argv)
416 return (0); 333 return (0);
417 statok = 0; 334 statok = 0;
418 } 335 }
419 if (skip) 336 if (bb_dump_skip)
420 doskip(statok ? *_argv : "stdin", statok); 337 do_skip(statok ? *_argv : "stdin", statok);
421 if (*_argv) 338 if (*_argv)
422 ++_argv; 339 ++_argv;
423 if (!skip) 340 if (!bb_dump_skip)
424 return (1); 341 return (1);
425 } 342 }
426 /* NOTREACHED */ 343 /* NOTREACHED */
@@ -435,26 +352,26 @@ static u_char *get(void)
435 u_char *tmpp; 352 u_char *tmpp;
436 353
437 if (!curp) { 354 if (!curp) {
438 curp = (u_char *) xmalloc(blocksize); 355 curp = (u_char *) xmalloc(bb_dump_blocksize);
439 savp = (u_char *) xmalloc(blocksize); 356 savp = (u_char *) xmalloc(bb_dump_blocksize);
440 } else { 357 } else {
441 tmpp = curp; 358 tmpp = curp;
442 curp = savp; 359 curp = savp;
443 savp = tmpp; 360 savp = tmpp;
444 address = savaddress += blocksize; 361 address = savaddress += bb_dump_blocksize;
445 } 362 }
446 for (need = blocksize, nread = 0;;) { 363 for (need = bb_dump_blocksize, nread = 0;;) {
447 /* 364 /*
448 * if read the right number of bytes, or at EOF for one file, 365 * if read the right number of bytes, or at EOF for one file,
449 * and no other files are available, zero-pad the rest of the 366 * and no other files are available, zero-pad the rest of the
450 * block and set the end flag. 367 * block and set the end flag.
451 */ 368 */
452 if (!length || (ateof && !next((char **) NULL))) { 369 if (!bb_dump_length || (ateof && !next((char **) NULL))) {
453 if (need == blocksize) { 370 if (need == bb_dump_blocksize) {
454 return ((u_char *) NULL); 371 return ((u_char *) NULL);
455 } 372 }
456 if (vflag != ALL && !bcmp(curp, savp, nread)) { 373 if (bb_dump_vflag != ALL && !bcmp(curp, savp, nread)) {
457 if (vflag != DUP) { 374 if (bb_dump_vflag != DUP) {
458 printf("*\n"); 375 printf("*\n");
459 } 376 }
460 return ((u_char *) NULL); 377 return ((u_char *) NULL);
@@ -464,31 +381,32 @@ static u_char *get(void)
464 return (curp); 381 return (curp);
465 } 382 }
466 n = fread((char *) curp + nread, sizeof(u_char), 383 n = fread((char *) curp + nread, sizeof(u_char),
467 length == -1 ? need : MIN(length, need), stdin); 384 bb_dump_length == -1 ? need : MIN(bb_dump_length, need), stdin);
468 if (!n) { 385 if (!n) {
469 if (ferror(stdin)) { 386 if (ferror(stdin)) {
470 perror_msg("%s", _argv[-1]); 387 bb_perror_msg("%s", _argv[-1]);
471 } 388 }
472 ateof = 1; 389 ateof = 1;
473 continue; 390 continue;
474 } 391 }
475 ateof = 0; 392 ateof = 0;
476 if (length != -1) { 393 if (bb_dump_length != -1) {
477 length -= n; 394 bb_dump_length -= n;
478 } 395 }
479 if (!(need -= n)) { 396 if (!(need -= n)) {
480 if (vflag == ALL || vflag == FIRST || bcmp(curp, savp, blocksize)) { 397 if (bb_dump_vflag == ALL || bb_dump_vflag == FIRST
481 if (vflag == DUP || vflag == FIRST) { 398 || bcmp(curp, savp, bb_dump_blocksize)) {
482 vflag = WAIT; 399 if (bb_dump_vflag == DUP || bb_dump_vflag == FIRST) {
400 bb_dump_vflag = WAIT;
483 } 401 }
484 return (curp); 402 return (curp);
485 } 403 }
486 if (vflag == WAIT) { 404 if (bb_dump_vflag == WAIT) {
487 printf("*\n"); 405 printf("*\n");
488 } 406 }
489 vflag = DUP; 407 bb_dump_vflag = DUP;
490 address = savaddress += blocksize; 408 address = savaddress += bb_dump_blocksize;
491 need = blocksize; 409 need = bb_dump_blocksize;
492 nread = 0; 410 nread = 0;
493 } else { 411 } else {
494 nread += n; 412 nread += n;
@@ -507,67 +425,59 @@ static void bpad(PR * pr)
507 pr->flags = F_BPAD; 425 pr->flags = F_BPAD;
508 *pr->cchar = 's'; 426 *pr->cchar = 's';
509 for (p1 = pr->fmt; *p1 != '%'; ++p1); 427 for (p1 = pr->fmt; *p1 != '%'; ++p1);
510 for (p2 = ++p1; *p1 && index(" -0+#", *p1); ++p1); 428 for (p2 = ++p1; *p1 && strchr(" -0+#", *p1); ++p1);
511 while ((*p2++ = *p1++) != 0); 429 while ((*p2++ = *p1++) != 0);
512} 430}
513 431
514void conv_c(PR * pr, u_char * p) 432static const char conv_str[] =
433 "\0\\0\0"
434 "\007\\a\0" /* \a */
435 "\b\\b\0"
436 "\f\\b\0"
437 "\n\\n\0"
438 "\r\\r\0"
439 "\t\\t\0"
440 "\v\\v\0"
441 "\0";
442
443
444static void conv_c(PR * pr, u_char * p)
515{ 445{
516 char buf[10], *str; 446 const char *str = conv_str;
517 447 char buf[10];
518 switch (*p) { 448
519 case '\0': 449 do {
520 str = "\\0"; 450 if (*p == *str) {
521 goto strpr; 451 ++str;
522 /* case '\a': */ 452 goto strpr;
523 case '\007': 453 }
524 str = "\\a"; 454 str += 4;
525 goto strpr; 455 } while (*str);
526 case '\b': 456
527 str = "\\b";
528 goto strpr;
529 case '\f':
530 str = "\\f";
531 goto strpr;
532 case '\n':
533 str = "\\n";
534 goto strpr;
535 case '\r':
536 str = "\\r";
537 goto strpr;
538 case '\t':
539 str = "\\t";
540 goto strpr;
541 case '\v':
542 str = "\\v";
543 goto strpr;
544 default:
545 break;
546 }
547 if (isprint(*p)) { 457 if (isprint(*p)) {
548 *pr->cchar = 'c'; 458 *pr->cchar = 'c';
549 (void) printf(pr->fmt, *p); 459 (void) printf(pr->fmt, *p);
550 } else { 460 } else {
551 sprintf(str = buf, "%03o", (int) *p); 461 sprintf(buf, "%03o", (int) *p);
462 str = buf;
552 strpr: 463 strpr:
553 *pr->cchar = 's'; 464 *pr->cchar = 's';
554 printf(pr->fmt, str); 465 printf(pr->fmt, str);
555 } 466 }
556} 467}
557 468
558void conv_u(PR * pr, u_char * p) 469static void conv_u(PR * pr, u_char * p)
559{ 470{
560 static char *list[] = { 471 static const char list[] =
561 "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", 472 "nul\0soh\0stx\0etx\0eot\0enq\0ack\0bel\0"
562 "bs", "ht", "lf", "vt", "ff", "cr", "so", "si", 473 "bs\0_ht\0_lf\0_vt\0_ff\0_cr\0_so\0_si\0_"
563 "dle", "dcl", "dc2", "dc3", "dc4", "nak", "syn", "etb", 474 "dle\0dcl\0dc2\0dc3\0dc4\0nak\0syn\0etb\0"
564 "can", "em", "sub", "esc", "fs", "gs", "rs", "us", 475 "can\0em\0_sub\0esc\0fs\0_gs\0_rs\0_us";
565 };
566 476
567 /* od used nl, not lf */ 477 /* od used nl, not lf */
568 if (*p <= 0x1f) { 478 if (*p <= 0x1f) {
569 *pr->cchar = 's'; 479 *pr->cchar = 's';
570 printf(pr->fmt, list[*p]); 480 printf(pr->fmt, list[4 * (int)(*p)]);
571 } else if (*p == 0x7f) { 481 } else if (*p == 0x7f) {
572 *pr->cchar = 's'; 482 *pr->cchar = 's';
573 printf(pr->fmt, "del"); 483 printf(pr->fmt, "del");
@@ -580,7 +490,7 @@ void conv_u(PR * pr, u_char * p)
580 } 490 }
581} 491}
582 492
583void display(void) 493static void display(void)
584{ 494{
585/* extern FU *endfu; */ 495/* extern FU *endfu; */
586 register FS *fs; 496 register FS *fs;
@@ -589,11 +499,11 @@ void display(void)
589 register int cnt; 499 register int cnt;
590 register u_char *bp; 500 register u_char *bp;
591 501
592/* off_t saveaddress; */ 502 off_t saveaddress;
593 u_char savech = 0, *savebp; 503 u_char savech = 0, *savebp;
594 504
595 while ((bp = get()) != NULL) { 505 while ((bp = get()) != NULL) {
596 for (fs = fshead, savebp = bp, saveaddress = address; fs; 506 for (fs = bb_dump_fshead, savebp = bp, saveaddress = address; fs;
597 fs = fs->nextfs, bp = savebp, address = saveaddress) { 507 fs = fs->nextfs, bp = savebp, address = saveaddress) {
598 for (fu = fs->nextfu; fu; fu = fu->nextfu) { 508 for (fu = fs->nextfu; fu; fu = fu->nextfu) {
599 if (fu->flags & F_IGNORE) { 509 if (fu->flags & F_IGNORE) {
@@ -707,8 +617,8 @@ void display(void)
707 } 617 }
708 if (endfu) { 618 if (endfu) {
709 /* 619 /*
710 * if eaddress not set, error or file size was multiple of 620 * if eaddress not set, error or file bb_dump_size was multiple of
711 * blocksize, and no partial block ever found. 621 * bb_dump_blocksize, and no partial block ever found.
712 */ 622 */
713 if (!eaddress) { 623 if (!eaddress) {
714 if (!address) { 624 if (!address) {
@@ -729,19 +639,19 @@ void display(void)
729 } 639 }
730} 640}
731 641
732int dump(char **argv) 642int bb_dump_dump(char **argv)
733{ 643{
734 register FS *tfs; 644 register FS *tfs;
735 645
736 /* figure out the data block size */ 646 /* figure out the data block bb_dump_size */
737 for (blocksize = 0, tfs = fshead; tfs; tfs = tfs->nextfs) { 647 for (bb_dump_blocksize = 0, tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
738 tfs->bcnt = size(tfs); 648 tfs->bcnt = bb_dump_size(tfs);
739 if (blocksize < tfs->bcnt) { 649 if (bb_dump_blocksize < tfs->bcnt) {
740 blocksize = tfs->bcnt; 650 bb_dump_blocksize = tfs->bcnt;
741 } 651 }
742 } 652 }
743 /* rewrite the rules, do syntax checking */ 653 /* rewrite the rules, do syntax checking */
744 for (tfs = fshead; tfs; tfs = tfs->nextfs) { 654 for (tfs = bb_dump_fshead; tfs; tfs = tfs->nextfs) {
745 rewrite(tfs); 655 rewrite(tfs);
746 } 656 }
747 657
@@ -751,21 +661,21 @@ int dump(char **argv)
751 return (exitval); 661 return (exitval);
752} 662}
753 663
754void add(char *fmt) 664void bb_dump_add(const char *fmt)
755{ 665{
756 register char *p; 666 register const char *p;
757 register char *p1; 667 register char *p1;
758 register char *p2; 668 register char *p2;
759 static FS **nextfs; 669 static FS **nextfs;
760 FS *tfs; 670 FS *tfs;
761 FU *tfu, **nextfu; 671 FU *tfu, **nextfu;
762 char *savep; 672 const char *savep;
763 673
764 /* start new linked list of format units */ 674 /* start new linked list of format units */
765 /* NOSTRICT */ 675 /* NOSTRICT */
766 tfs = (FS *) xmalloc(sizeof(FS)); 676 tfs = (FS *) xmalloc(sizeof(FS));
767 if (!fshead) { 677 if (!bb_dump_fshead) {
768 fshead = tfs; 678 bb_dump_fshead = tfs;
769 } else { 679 } else {
770 *nextfs = tfs; 680 *nextfs = tfs;
771 } 681 }
@@ -774,8 +684,8 @@ void add(char *fmt)
774 684
775 /* take the format string and break it up into format units */ 685 /* take the format string and break it up into format units */
776 for (p = fmt;;) { 686 for (p = fmt;;) {
777 /* skip leading white space */ 687 /* bb_dump_skip leading white space */
778 for (; isspace(*p); ++p); 688 p = bb_skip_whitespace(p);
779 if (!*p) { 689 if (!*p) {
780 break; 690 break;
781 } 691 }
@@ -791,43 +701,41 @@ void add(char *fmt)
791 if (isdigit(*p)) { 701 if (isdigit(*p)) {
792 for (savep = p; isdigit(*p); ++p); 702 for (savep = p; isdigit(*p); ++p);
793 if (!isspace(*p) && *p != '/') { 703 if (!isspace(*p) && *p != '/') {
794 error_msg_and_die("hexdump: bad format {%s}", fmt); 704 bb_error_msg_and_die("bad format {%s}", fmt);
795 } 705 }
796 /* may overwrite either white space or slash */ 706 /* may overwrite either white space or slash */
797 tfu->reps = atoi(savep); 707 tfu->reps = atoi(savep);
798 tfu->flags = F_SETREP; 708 tfu->flags = F_SETREP;
799 /* skip trailing white space */ 709 /* bb_dump_skip trailing white space */
800 for (++p; isspace(*p); ++p); 710 p = bb_skip_whitespace(++p);
801 } 711 }
802 712
803 /* skip slash and trailing white space */ 713 /* bb_dump_skip slash and trailing white space */
804 if (*p == '/') { 714 if (*p == '/') {
805 while (isspace(*++p)); 715 p = bb_skip_whitespace(++p);
806 } 716 }
807 717
808 /* byte count */ 718 /* byte count */
809 if (isdigit(*p)) { 719 if (isdigit(*p)) {
810 for (savep = p; isdigit(*p); ++p); 720 for (savep = p; isdigit(*p); ++p);
811 if (!isspace(*p)) { 721 if (!isspace(*p)) {
812 error_msg_and_die("hexdump: bad format {%s}", fmt); 722 bb_error_msg_and_die("bad format {%s}", fmt);
813 } 723 }
814 tfu->bcnt = atoi(savep); 724 tfu->bcnt = atoi(savep);
815 /* skip trailing white space */ 725 /* bb_dump_skip trailing white space */
816 for (++p; isspace(*p); ++p); 726 p = bb_skip_whitespace(++p);
817 } 727 }
818 728
819 /* format */ 729 /* format */
820 if (*p != '"') { 730 if (*p != '"') {
821 error_msg_and_die("hexdump: bad format {%s}", fmt); 731 bb_error_msg_and_die("bad format {%s}", fmt);
822 } 732 }
823 for (savep = ++p; *p != '"';) { 733 for (savep = ++p; *p != '"';) {
824 if (*p++ == 0) { 734 if (*p++ == 0) {
825 error_msg_and_die("hexdump: bad format {%s}", fmt); 735 bb_error_msg_and_die("bad format {%s}", fmt);
826 } 736 }
827 } 737 }
828 if (!(tfu->fmt = malloc(p - savep + 1))) { 738 tfu->fmt = xmalloc(p - savep + 1);
829 perror_msg_and_die("hexdump");
830 }
831 strncpy(tfu->fmt, savep, p - savep); 739 strncpy(tfu->fmt, savep, p - savep);
832 tfu->fmt[p - savep] = '\0'; 740 tfu->fmt[p - savep] = '\0';
833/* escape(tfu->fmt); */ 741/* escape(tfu->fmt); */
@@ -841,33 +749,16 @@ void add(char *fmt)
841 break; 749 break;
842 } 750 }
843 if (*p1 == '\\') { 751 if (*p1 == '\\') {
844 switch (*++p1) { 752 const char *cs = conv_str + 4;
845 case 'a': 753 ++p1;
846 /* *p2 = '\a'; */ 754 *p2 = *p1;
847 *p2 = '\007'; 755 do {
848 break; 756 if (*p1 == cs[2]) {
849 case 'b': 757 *p2 = cs[0];
850 *p2 = '\b'; 758 break;
851 break; 759 }
852 case 'f': 760 cs += 4;
853 *p2 = '\f'; 761 } while (*cs);
854 break;
855 case 'n':
856 *p2 = '\n';
857 break;
858 case 'r':
859 *p2 = '\r';
860 break;
861 case 't':
862 *p2 = '\t';
863 break;
864 case 'v':
865 *p2 = '\v';
866 break;
867 default:
868 *p2 = *p1;
869 break;
870 }
871 } 762 }
872 } 763 }
873 764
diff --git a/libbb/error_msg.c b/libbb/error_msg.c
index 58308b6be..5456dd361 100644
--- a/libbb/error_msg.c
+++ b/libbb/error_msg.c
@@ -25,12 +25,12 @@
25#include <stdlib.h> 25#include <stdlib.h>
26#include "libbb.h" 26#include "libbb.h"
27 27
28extern void error_msg(const char *s, ...) 28extern void bb_error_msg(const char *s, ...)
29{ 29{
30 va_list p; 30 va_list p;
31 31
32 va_start(p, s); 32 va_start(p, s);
33 verror_msg(s, p); 33 bb_verror_msg(s, p);
34 va_end(p); 34 va_end(p);
35 putc('\n', stderr); 35 putc('\n', stderr);
36} 36}
diff --git a/libbb/error_msg_and_die.c b/libbb/error_msg_and_die.c
index 67a79c375..7e7393773 100644
--- a/libbb/error_msg_and_die.c
+++ b/libbb/error_msg_and_die.c
@@ -25,15 +25,15 @@
25#include <stdlib.h> 25#include <stdlib.h>
26#include "libbb.h" 26#include "libbb.h"
27 27
28extern void error_msg_and_die(const char *s, ...) 28extern void bb_error_msg_and_die(const char *s, ...)
29{ 29{
30 va_list p; 30 va_list p;
31 31
32 va_start(p, s); 32 va_start(p, s);
33 verror_msg(s, p); 33 bb_verror_msg(s, p);
34 va_end(p); 34 va_end(p);
35 putc('\n', stderr); 35 putc('\n', stderr);
36 exit(EXIT_FAILURE); 36 exit(bb_default_error_retval);
37} 37}
38 38
39 39
diff --git a/libbb/fclose_nonstdin.c b/libbb/fclose_nonstdin.c
new file mode 100644
index 000000000..97e303e9c
--- /dev/null
+++ b/libbb/fclose_nonstdin.c
@@ -0,0 +1,37 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * fclose_nonstdin implementation for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23/* A number of standard utilites can accept multiple command line args
24 * of '-' for stdin, according to SUSv3. So we encapsulate the check
25 * here to save a little space.
26 */
27
28#include <stdio.h>
29#include <libbb.h>
30
31int bb_fclose_nonstdin(FILE *f)
32{
33 if (f != stdin) {
34 return fclose(f);
35 }
36 return 0;
37}
diff --git a/libbb/fflush_stdout_and_exit.c b/libbb/fflush_stdout_and_exit.c
new file mode 100644
index 000000000..cbba04207
--- /dev/null
+++ b/libbb/fflush_stdout_and_exit.c
@@ -0,0 +1,37 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * fflush_stdout_and_exit implementation for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23/* Attempt to fflush(stdout), and exit with an error code if stdout is
24 * in an error state.
25 */
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <libbb.h>
30
31void bb_fflush_stdout_and_exit(int retval)
32{
33 if (fflush(stdout)) {
34 retval = bb_default_error_retval;
35 }
36 exit(retval);
37}
diff --git a/libbb/find_root_device.c b/libbb/find_root_device.c
index 0a3f1bc77..763ac7519 100644
--- a/libbb/find_root_device.c
+++ b/libbb/find_root_device.c
@@ -38,14 +38,14 @@ extern char *find_real_root_device_name(const char* name)
38 dev_t dev; 38 dev_t dev;
39 39
40 if (stat("/", &rootStat) != 0) 40 if (stat("/", &rootStat) != 0)
41 perror_msg("could not stat '/'"); 41 bb_perror_msg("could not stat '/'");
42 else { 42 else {
43 if ((dev = rootStat.st_rdev)==0) 43 if ((dev = rootStat.st_rdev)==0)
44 dev=rootStat.st_dev; 44 dev=rootStat.st_dev;
45 45
46 dir = opendir("/dev"); 46 dir = opendir("/dev");
47 if (!dir) 47 if (!dir)
48 perror_msg("could not open '/dev'"); 48 bb_perror_msg("could not open '/dev'");
49 else { 49 else {
50 while((entry = readdir(dir)) != NULL) { 50 while((entry = readdir(dir)) != NULL) {
51 51
@@ -69,7 +69,7 @@ extern char *find_real_root_device_name(const char* name)
69 } 69 }
70 } 70 }
71 if(fileName==NULL) 71 if(fileName==NULL)
72 fileName=xstrdup("/dev/root"); 72 fileName=bb_xstrdup("/dev/root");
73 return fileName; 73 return fileName;
74} 74}
75 75
diff --git a/libbb/full_read.c b/libbb/full_read.c
index ccf26fc3d..e75f967db 100644
--- a/libbb/full_read.c
+++ b/libbb/full_read.c
@@ -23,17 +23,16 @@
23#include <unistd.h> 23#include <unistd.h>
24#include "libbb.h" 24#include "libbb.h"
25 25
26
27/* 26/*
28 * Read all of the supplied buffer from a file. 27 * Read all of the supplied buffer from a file.
29 * This does multiple reads as necessary. 28 * This does multiple reads as necessary.
30 * Returns the amount read, or -1 on an error. 29 * Returns the amount read, or -1 on an error.
31 * A short read is returned on an end of file. 30 * A short read is returned on an end of file.
32 */ 31 */
33int full_read(int fd, char *buf, int len) 32ssize_t bb_full_read(int fd, void *buf, size_t len)
34{ 33{
35 int cc; 34 ssize_t cc;
36 int total; 35 ssize_t total;
37 36
38 total = 0; 37 total = 0;
39 38
@@ -41,12 +40,12 @@ int full_read(int fd, char *buf, int len)
41 cc = read(fd, buf, len); 40 cc = read(fd, buf, len);
42 41
43 if (cc < 0) 42 if (cc < 0)
44 return -1; 43 return cc; /* read() returns -1 on failure. */
45 44
46 if (cc == 0) 45 if (cc == 0)
47 break; 46 break;
48 47
49 buf += cc; 48 buf = ((char *)buf) + cc;
50 total += cc; 49 total += cc;
51 len -= cc; 50 len -= cc;
52 } 51 }
diff --git a/libbb/full_write.c b/libbb/full_write.c
index a2c07fbc9..1106a53b4 100644
--- a/libbb/full_write.c
+++ b/libbb/full_write.c
@@ -28,10 +28,10 @@
28 * This does multiple writes as necessary. 28 * This does multiple writes as necessary.
29 * Returns the amount written, or -1 on an error. 29 * Returns the amount written, or -1 on an error.
30 */ 30 */
31int full_write(int fd, const char *buf, int len) 31ssize_t bb_full_write(int fd, const void *buf, size_t len)
32{ 32{
33 int cc; 33 ssize_t cc;
34 int total; 34 ssize_t total;
35 35
36 total = 0; 36 total = 0;
37 37
@@ -39,10 +39,10 @@ int full_write(int fd, const char *buf, int len)
39 cc = write(fd, buf, len); 39 cc = write(fd, buf, len);
40 40
41 if (cc < 0) 41 if (cc < 0)
42 return -1; 42 return cc; /* write() returns -1 on failure. */
43 43
44 buf += cc;
45 total += cc; 44 total += cc;
45 buf = ((const char *)buf) + cc;
46 len -= cc; 46 len -= cc;
47 } 47 }
48 48
diff --git a/libbb/get_console.c b/libbb/get_console.c
index 794888fa7..562b57703 100644
--- a/libbb/get_console.c
+++ b/libbb/get_console.c
@@ -106,7 +106,7 @@ int get_console_fd(void)
106 if (is_a_console(fd)) 106 if (is_a_console(fd))
107 return fd; 107 return fd;
108 108
109 error_msg("Couldn't get a file descriptor referring to the console"); 109 bb_error_msg("Couldn't get a file descriptor referring to the console");
110 return -1; /* total failure */ 110 return -1; /* total failure */
111} 111}
112 112
diff --git a/libbb/get_last_path_component.c b/libbb/get_last_path_component.c
index 6af726c83..497d6ae4e 100644
--- a/libbb/get_last_path_component.c
+++ b/libbb/get_last_path_component.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * get_last_path_component implementation for busybox 3 * bb_get_last_path_component implementation for busybox
4 * 4 *
5 * Copyright (C) 2001 Manuel Novoa III <mjn3@opensource.lineo.com> 5 * Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -24,7 +24,7 @@
24/* WARNING!!! Doing so will break basename applet at least! */ 24/* WARNING!!! Doing so will break basename applet at least! */
25#define EMULATE_BASENAME 0 25#define EMULATE_BASENAME 0
26 26
27char *get_last_path_component(char *path) 27char *bb_get_last_path_component(char *path)
28{ 28{
29#if EMULATE_BASENAME 29#if EMULATE_BASENAME
30 static const char null_or_empty[] = "."; 30 static const char null_or_empty[] = ".";
diff --git a/libbb/get_line_from_file.c b/libbb/get_line_from_file.c
index 5e7062127..5af898934 100644
--- a/libbb/get_line_from_file.c
+++ b/libbb/get_line_from_file.c
@@ -21,41 +21,57 @@
21 */ 21 */
22 22
23#include <stdio.h> 23#include <stdio.h>
24#include <stdlib.h>
24#include "libbb.h" 25#include "libbb.h"
25 26
26 27/* get_line_from_file() - This function reads an entire line from a text file,
27
28/* get_line_from_file() - This function reads an entire line from a text file
29 * up to a newline. It returns a malloc'ed char * which must be stored and 28 * up to a newline. It returns a malloc'ed char * which must be stored and
30 * free'ed by the caller. */ 29 * free'ed by the caller. If 'c' is nonzero, the trailing '\n' (if any)
31extern char *get_line_from_file(FILE *file) 30 * is removed. In event of a read error or EOF, NULL is returned. */
31
32static char *private_get_line_from_file(FILE *file, int c)
32{ 33{
33 static const int GROWBY = 80; /* how large we will grow strings by */ 34#define GROWBY (80) /* how large we will grow strings by */
34 35
35 int ch; 36 int ch;
36 int idx = 0; 37 int idx = 0;
37 char *linebuf = NULL; 38 char *linebuf = NULL;
38 int linebufsz = 0; 39 int linebufsz = 0;
39 40
40 while (1) { 41 while ((ch = getc(file)) != EOF) {
41 ch = fgetc(file);
42 if (ch == EOF)
43 break;
44 /* grow the line buffer as necessary */ 42 /* grow the line buffer as necessary */
45 while (idx > linebufsz-2) 43 if (idx > linebufsz-2) {
46 linebuf = xrealloc(linebuf, linebufsz += GROWBY); 44 linebuf = xrealloc(linebuf, linebufsz += GROWBY);
45 }
47 linebuf[idx++] = (char)ch; 46 linebuf[idx++] = (char)ch;
48 if (ch == '\n' || ch == '\0') 47 if (ch == '\n' || ch == '\0') {
48 if (c) {
49 --idx;
50 }
49 break; 51 break;
52 }
50 } 53 }
51 54
52 if (idx == 0) 55 if (linebuf) {
53 return NULL; 56 if (ferror(file)) {
54 57 free(linebuf);
55 linebuf[idx] = 0; 58 return NULL;
59 }
60 linebuf[idx] = 0;
61 }
56 return linebuf; 62 return linebuf;
57} 63}
58 64
65extern char *bb_get_line_from_file(FILE *file)
66{
67 return private_get_line_from_file(file, 0);
68}
69
70extern char *bb_get_chomped_line_from_file(FILE *file)
71{
72 return private_get_line_from_file(file, 1);
73}
74
59 75
60/* END CODE */ 76/* END CODE */
61/* 77/*
diff --git a/libbb/time_string.c b/libbb/getopt_ulflags.c
index d103a02f8..91de392b6 100644
--- a/libbb/time_string.c
+++ b/libbb/getopt_ulflags.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Utility routines. 3 * getopt_ulflags implementation for busybox
4 * 4 *
5 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org> 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -17,46 +17,25 @@
17 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
20 */ 21 */
21 22
22#include <stdio.h> 23#include <getopt.h>
23#include <string.h> 24#include <string.h>
24#include <time.h>
25#include <utime.h>
26#include "libbb.h" 25#include "libbb.h"
27 26
28 27unsigned long bb_getopt_ulflags(int argc, char **argv, const char *applet_opts)
29/*
30 * Return the standard ls-like time string from a time_t
31 * This is static and so is overwritten on each call.
32 */
33const char *time_string(time_t timeVal)
34{ 28{
35 time_t now; 29 unsigned long flags = 0;
36 char *str; 30 const char *s;
37 static char buf[26]; 31 int c;
38 32
39 time(&now); 33 while ((c = getopt(argc, argv, applet_opts)) > 0) {
40 34 if (!(s = strchr(applet_opts, c))) {
41 str = ctime(&timeVal); 35 bb_show_usage();
42 36 }
43 strcpy(buf, &str[4]); 37 flags |= (1 << (s-applet_opts));
44 buf[12] = '\0';
45
46 if ((timeVal > now) || (timeVal < now - 365 * 24 * 60 * 60L)) {
47 strcpy(&buf[7], &str[20]);
48 buf[11] = '\0';
49 } 38 }
50 39
51 return buf; 40 return flags;
52} 41}
53
54
55/* END CODE */
56/*
57Local Variables:
58c-file-style: "linux"
59c-basic-offset: 4
60tab-width: 4
61End:
62*/
diff --git a/libbb/herror_msg.c b/libbb/herror_msg.c
index 1081a56b1..4fe921b29 100644
--- a/libbb/herror_msg.c
+++ b/libbb/herror_msg.c
@@ -24,12 +24,12 @@
24 24
25#include "libbb.h" 25#include "libbb.h"
26 26
27extern void herror_msg(const char *s, ...) 27extern void bb_herror_msg(const char *s, ...)
28{ 28{
29 va_list p; 29 va_list p;
30 30
31 va_start(p, s); 31 va_start(p, s);
32 vherror_msg(s, p); 32 bb_vherror_msg(s, p);
33 va_end(p); 33 va_end(p);
34} 34}
35 35
diff --git a/libbb/herror_msg_and_die.c b/libbb/herror_msg_and_die.c
index a47c7ff95..33a8c3e28 100644
--- a/libbb/herror_msg_and_die.c
+++ b/libbb/herror_msg_and_die.c
@@ -24,14 +24,14 @@
24 24
25#include "libbb.h" 25#include "libbb.h"
26 26
27extern void herror_msg_and_die(const char *s, ...) 27extern void bb_herror_msg_and_die(const char *s, ...)
28{ 28{
29 va_list p; 29 va_list p;
30 30
31 va_start(p, s); 31 va_start(p, s);
32 vherror_msg(s, p); 32 bb_vherror_msg(s, p);
33 va_end(p); 33 va_end(p);
34 exit(EXIT_FAILURE); 34 exit(bb_default_error_retval);
35} 35}
36 36
37 37
diff --git a/libbb/inet_common.c b/libbb/inet_common.c
index 16dd1db7c..5e2343bdd 100644
--- a/libbb/inet_common.c
+++ b/libbb/inet_common.c
@@ -4,7 +4,7 @@
4 * 4 *
5 * Heavily modified by Manuel Novoa III Mar 12, 2001 5 * Heavily modified by Manuel Novoa III Mar 12, 2001
6 * 6 *
7 * Version: $Id: inet_common.c,v 1.5 2002/11/28 09:52:23 bug1 Exp $ 7 * Version: $Id: inet_common.c,v 1.6 2003/03/19 09:12:07 mjn3 Exp $
8 * 8 *
9 */ 9 */
10 10
@@ -44,7 +44,7 @@ int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
44 /* If we expect this to be a hostname, try hostname database first */ 44 /* If we expect this to be a hostname, try hostname database first */
45#ifdef DEBUG 45#ifdef DEBUG
46 if (hostfirst) { 46 if (hostfirst) {
47 error_msg("gethostbyname (%s)", name); 47 bb_error_msg("gethostbyname (%s)", name);
48 } 48 }
49#endif 49#endif
50 if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) { 50 if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
@@ -54,7 +54,7 @@ int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
54 } 54 }
55 /* Try the NETWORKS database to see if this is a known network. */ 55 /* Try the NETWORKS database to see if this is a known network. */
56#ifdef DEBUG 56#ifdef DEBUG
57 error_msg("getnetbyname (%s)", name); 57 bb_error_msg("getnetbyname (%s)", name);
58#endif 58#endif
59 if ((np = getnetbyname(name)) != (struct netent *) NULL) { 59 if ((np = getnetbyname(name)) != (struct netent *) NULL) {
60 s_in->sin_addr.s_addr = htonl(np->n_net); 60 s_in->sin_addr.s_addr = htonl(np->n_net);
@@ -71,7 +71,7 @@ int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
71#endif 71#endif
72 72
73#ifdef DEBUG 73#ifdef DEBUG
74 error_msg("gethostbyname (%s)", name); 74 bb_error_msg("gethostbyname (%s)", name);
75#endif 75#endif
76 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) { 76 if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
77 errno = h_errno; 77 errno = h_errno;
@@ -109,7 +109,7 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
109 /* Grmpf. -FvK */ 109 /* Grmpf. -FvK */
110 if (s_in->sin_family != AF_INET) { 110 if (s_in->sin_family != AF_INET) {
111#ifdef DEBUG 111#ifdef DEBUG
112 error_msg("rresolve: unsupport address family %d !", 112 bb_error_msg("rresolve: unsupport address family %d !",
113 s_in->sin_family); 113 s_in->sin_family);
114#endif 114#endif
115 errno = EAFNOSUPPORT; 115 errno = EAFNOSUPPORT;
@@ -117,7 +117,7 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
117 } 117 }
118 ad = (unsigned long) s_in->sin_addr.s_addr; 118 ad = (unsigned long) s_in->sin_addr.s_addr;
119#ifdef DEBUG 119#ifdef DEBUG
120 error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric); 120 bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
121#endif 121#endif
122 if (ad == INADDR_ANY) { 122 if (ad == INADDR_ANY) {
123 if ((numeric & 0x0FFF) == 0) { 123 if ((numeric & 0x0FFF) == 0) {
@@ -143,7 +143,7 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
143 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) { 143 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
144 safe_strncpy(name, pn->name, len); 144 safe_strncpy(name, pn->name, len);
145#ifdef DEBUG 145#ifdef DEBUG
146 error_msg("rresolve: found %s %08lx in cache", 146 bb_error_msg("rresolve: found %s %08lx in cache",
147 (host ? "host" : "net"), ad); 147 (host ? "host" : "net"), ad);
148#endif 148#endif
149 return (0); 149 return (0);
@@ -156,7 +156,7 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
156 ent = NULL; 156 ent = NULL;
157 if (host) { 157 if (host) {
158#ifdef DEBUG 158#ifdef DEBUG
159 error_msg("gethostbyaddr (%08lx)", ad); 159 bb_error_msg("gethostbyaddr (%08lx)", ad);
160#endif 160#endif
161 ent = gethostbyaddr((char *) &ad, 4, AF_INET); 161 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
162 if (ent != NULL) { 162 if (ent != NULL) {
@@ -164,7 +164,7 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
164 } 164 }
165 } else { 165 } else {
166#ifdef DEBUG 166#ifdef DEBUG
167 error_msg("getnetbyaddr (%08lx)", host_ad); 167 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
168#endif 168#endif
169 np = getnetbyaddr(host_ad, AF_INET); 169 np = getnetbyaddr(host_ad, AF_INET);
170 if (np != NULL) { 170 if (np != NULL) {
@@ -178,7 +178,7 @@ int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
178 pn->addr = *s_in; 178 pn->addr = *s_in;
179 pn->next = INET_nn; 179 pn->next = INET_nn;
180 pn->host = host; 180 pn->host = host;
181 pn->name = xstrdup(name); 181 pn->name = bb_xstrdup(name);
182 INET_nn = pn; 182 INET_nn = pn;
183 183
184 return (0); 184 return (0);
@@ -194,7 +194,7 @@ int INET6_resolve(char *name, struct sockaddr_in6 *sin6)
194 memset(&req, '\0', sizeof req); 194 memset(&req, '\0', sizeof req);
195 req.ai_family = AF_INET6; 195 req.ai_family = AF_INET6;
196 if ((s = getaddrinfo(name, NULL, &req, &ai))) { 196 if ((s = getaddrinfo(name, NULL, &req, &ai))) {
197 error_msg("getaddrinfo: %s: %d", name, s); 197 bb_error_msg("getaddrinfo: %s: %d", name, s);
198 return -1; 198 return -1;
199 } 199 }
200 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6)); 200 memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
@@ -219,7 +219,7 @@ int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
219 /* Grmpf. -FvK */ 219 /* Grmpf. -FvK */
220 if (sin6->sin6_family != AF_INET6) { 220 if (sin6->sin6_family != AF_INET6) {
221#ifdef DEBUG 221#ifdef DEBUG
222 error_msg(_("rresolve: unsupport address family %d !\n"), 222 bb_error_msg(_("rresolve: unsupport address family %d !\n"),
223 sin6->sin6_family); 223 sin6->sin6_family);
224#endif 224#endif
225 errno = EAFNOSUPPORT; 225 errno = EAFNOSUPPORT;
@@ -240,7 +240,7 @@ int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
240 240
241 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0); 241 s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
242 if (s) { 242 if (s) {
243 error_msg("getnameinfo failed"); 243 bb_error_msg("getnameinfo failed");
244 return -1; 244 return -1;
245 } 245 }
246 return (0); 246 return (0);
diff --git a/libbb/interface.c b/libbb/interface.c
index fb794169d..531700f45 100644
--- a/libbb/interface.c
+++ b/libbb/interface.c
@@ -15,7 +15,7 @@
15 * that either displays or sets the characteristics of 15 * that either displays or sets the characteristics of
16 * one or more of the system's networking interfaces. 16 * one or more of the system's networking interfaces.
17 * 17 *
18 * Version: $Id: interface.c,v 1.14 2003/02/09 22:40:33 bug1 Exp $ 18 * Version: $Id: interface.c,v 1.15 2003/03/19 09:12:07 mjn3 Exp $
19 * 19 *
20 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org> 20 * Author: Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
21 * and others. Copyright 1993 MicroWalt Corporation 21 * and others. Copyright 1993 MicroWalt Corporation
@@ -619,7 +619,7 @@ static int aftrans_opt(const char *arg)
619 if (strcmp(tmp1, paft->alias)) 619 if (strcmp(tmp1, paft->alias))
620 continue; 620 continue;
621 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) { 621 if (strlen(paft->name) + strlen(afname) + 1 >= sizeof(afname)) {
622 error_msg(_("Too many address family arguments.")); 622 bb_error_msg(_("Too many address family arguments."));
623 return (0); 623 return (0);
624 } 624 }
625 if (paft->flag) 625 if (paft->flag)
@@ -630,7 +630,7 @@ static int aftrans_opt(const char *arg)
630 break; 630 break;
631 } 631 }
632 if (!paft->alias) { 632 if (!paft->alias) {
633 error_msg(_("Unknown address family `%s'."), tmp1); 633 bb_error_msg(_("Unknown address family `%s'."), tmp1);
634 return (1); 634 return (1);
635 } 635 }
636 tmp1 = tmp2; 636 tmp1 = tmp2;
@@ -693,7 +693,7 @@ static struct aftype *get_aftype(const char *name)
693 afp++; 693 afp++;
694 } 694 }
695 if (strchr(name, ',')) 695 if (strchr(name, ','))
696 error_msg(_("Please don't supply more than one address family.")); 696 bb_error_msg(_("Please don't supply more than one address family."));
697 return (NULL); 697 return (NULL);
698} 698}
699#endif /* KEEP_UNUSED */ 699#endif /* KEEP_UNUSED */
@@ -888,7 +888,7 @@ static int sockets_open(int family)
888 sfd = af->fd; 888 sfd = af->fd;
889 } 889 }
890 if (sfd < 0) { 890 if (sfd < 0) {
891 error_msg(_("No usable address families found.")); 891 bb_error_msg(_("No usable address families found."));
892 } 892 }
893 return sfd; 893 return sfd;
894} 894}
@@ -960,7 +960,7 @@ static int if_readconf(void)
960 (as of 2.1.128) */ 960 (as of 2.1.128) */
961 skfd2 = get_socket_for_af(AF_INET); 961 skfd2 = get_socket_for_af(AF_INET);
962 if (skfd2 < 0) { 962 if (skfd2 < 0) {
963 perror_msg(("warning: no inet socket available")); 963 bb_perror_msg(("warning: no inet socket available"));
964 /* Try to soldier on with whatever socket we can get hold of. */ 964 /* Try to soldier on with whatever socket we can get hold of. */
965 skfd2 = sockets_open(0); 965 skfd2 = sockets_open(0);
966 if (skfd2 < 0) 966 if (skfd2 < 0)
@@ -1106,7 +1106,7 @@ static int if_readlist_proc(char *target)
1106 1106
1107 fh = fopen(_PATH_PROCNET_DEV, "r"); 1107 fh = fopen(_PATH_PROCNET_DEV, "r");
1108 if (!fh) { 1108 if (!fh) {
1109 perror_msg(_("Warning: cannot open %s. Limited output."), _PATH_PROCNET_DEV); 1109 bb_perror_msg(_("Warning: cannot open %s. Limited output."), _PATH_PROCNET_DEV);
1110 return if_readconf(); 1110 return if_readconf();
1111 } 1111 }
1112 fgets(buf, sizeof buf, fh); /* eat line */ 1112 fgets(buf, sizeof buf, fh); /* eat line */
@@ -1366,7 +1366,7 @@ static int do_if_fetch(struct interface *ife)
1366 } else { 1366 } else {
1367 errmsg = strerror(errno); 1367 errmsg = strerror(errno);
1368 } 1368 }
1369 error_msg(_("%s: error fetching interface information: %s\n"), 1369 bb_error_msg(_("%s: error fetching interface information: %s\n"),
1370 ife->name, errmsg); 1370 ife->name, errmsg);
1371 return -1; 1371 return -1;
1372 } 1372 }
@@ -1441,7 +1441,7 @@ static int in_ether(char *bufp, struct sockaddr *sap)
1441 val = c - 'A' + 10; 1441 val = c - 'A' + 10;
1442 else { 1442 else {
1443#ifdef DEBUG 1443#ifdef DEBUG
1444 error_msg(_("in_ether(%s): invalid ether address!\n"), orig); 1444 bb_error_msg(_("in_ether(%s): invalid ether address!\n"), orig);
1445#endif 1445#endif
1446 errno = EINVAL; 1446 errno = EINVAL;
1447 return (-1); 1447 return (-1);
@@ -1458,7 +1458,7 @@ static int in_ether(char *bufp, struct sockaddr *sap)
1458 val >>= 4; 1458 val >>= 4;
1459 else { 1459 else {
1460#ifdef DEBUG 1460#ifdef DEBUG
1461 error_msg(_("in_ether(%s): invalid ether address!"), orig); 1461 bb_error_msg(_("in_ether(%s): invalid ether address!"), orig);
1462#endif 1462#endif
1463 errno = EINVAL; 1463 errno = EINVAL;
1464 return (-1); 1464 return (-1);
@@ -1472,7 +1472,7 @@ static int in_ether(char *bufp, struct sockaddr *sap)
1472 if (*bufp == ':') { 1472 if (*bufp == ':') {
1473#ifdef DEBUG 1473#ifdef DEBUG
1474 if (i == ETH_ALEN) { 1474 if (i == ETH_ALEN) {
1475 error_msg(_("in_ether(%s): trailing : ignored!"), orig); 1475 bb_error_msg(_("in_ether(%s): trailing : ignored!"), orig);
1476 } 1476 }
1477#endif 1477#endif
1478 bufp++; 1478 bufp++;
@@ -1482,11 +1482,11 @@ static int in_ether(char *bufp, struct sockaddr *sap)
1482#ifdef DEBUG 1482#ifdef DEBUG
1483 /* That's it. Any trailing junk? */ 1483 /* That's it. Any trailing junk? */
1484 if ((i == ETH_ALEN) && (*bufp != '\0')) { 1484 if ((i == ETH_ALEN) && (*bufp != '\0')) {
1485 error_msg(_("in_ether(%s): trailing junk!"), orig); 1485 bb_error_msg(_("in_ether(%s): trailing junk!"), orig);
1486 errno = EINVAL; 1486 errno = EINVAL;
1487 return (-1); 1487 return (-1);
1488 } 1488 }
1489 error_msg("in_ether(%s): %s", orig, pr_ether(sap->sa_data)); 1489 bb_error_msg("in_ether(%s): %s", orig, pr_ether(sap->sa_data));
1490#endif 1490#endif
1491 1491
1492 return (0); 1492 return (0);
@@ -1511,7 +1511,7 @@ static struct hwtype ether_hwtype = {
1511/* Start the PPP encapsulation on the file descriptor. */ 1511/* Start the PPP encapsulation on the file descriptor. */
1512static int do_ppp(int fd) 1512static int do_ppp(int fd)
1513{ 1513{
1514 error_msg(_("You cannot start PPP with this program.")); 1514 bb_error_msg(_("You cannot start PPP with this program."));
1515 return -1; 1515 return -1;
1516} 1516}
1517#endif /* KEEP_UNUSED */ 1517#endif /* KEEP_UNUSED */
@@ -2052,7 +2052,7 @@ int display_interfaces(char *ifname)
2052 2052
2053 /* Create a channel to the NET kernel. */ 2053 /* Create a channel to the NET kernel. */
2054 if ((skfd = sockets_open(0)) < 0) { 2054 if ((skfd = sockets_open(0)) < 0) {
2055 perror_msg_and_die("socket"); 2055 bb_perror_msg_and_die("socket");
2056 } 2056 }
2057 2057
2058 /* Do we have to show the current setup? */ 2058 /* Do we have to show the current setup? */
diff --git a/libbb/kernel_version.c b/libbb/kernel_version.c
index 694af8e2c..14b163654 100644
--- a/libbb/kernel_version.c
+++ b/libbb/kernel_version.c
@@ -37,7 +37,7 @@ extern int get_kernel_revision(void)
37 int i, r; 37 int i, r;
38 38
39 if (uname(&name) == -1) { 39 if (uname(&name) == -1) {
40 perror_msg("cannot get system information"); 40 bb_perror_msg("cannot get system information");
41 return (0); 41 return (0);
42 } 42 }
43 43
diff --git a/libbb/loop.c b/libbb/loop.c
index 7e58b2f85..29128abe4 100644
--- a/libbb/loop.c
+++ b/libbb/loop.c
@@ -33,11 +33,11 @@ extern int del_loop(const char *device)
33 int fd; 33 int fd;
34 34
35 if ((fd = open(device, O_RDONLY)) < 0) { 35 if ((fd = open(device, O_RDONLY)) < 0) {
36 perror_msg("%s", device); 36 bb_perror_msg("%s", device);
37 return (FALSE); 37 return (FALSE);
38 } 38 }
39 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) { 39 if (ioctl(fd, LOOP_CLR_FD, 0) < 0) {
40 perror_msg("ioctl: LOOP_CLR_FD"); 40 bb_perror_msg("ioctl: LOOP_CLR_FD");
41 return (FALSE); 41 return (FALSE);
42 } 42 }
43 close(fd); 43 close(fd);
@@ -53,12 +53,12 @@ extern int set_loop(const char *device, const char *file, int offset,
53 mode = *loopro ? O_RDONLY : O_RDWR; 53 mode = *loopro ? O_RDONLY : O_RDWR;
54 if ((ffd = open(file, mode)) < 0 && !*loopro 54 if ((ffd = open(file, mode)) < 0 && !*loopro
55 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) { 55 && (errno != EROFS || (ffd = open(file, mode = O_RDONLY)) < 0)) {
56 perror_msg("%s", file); 56 bb_perror_msg("%s", file);
57 return 1; 57 return 1;
58 } 58 }
59 if ((fd = open(device, mode)) < 0) { 59 if ((fd = open(device, mode)) < 0) {
60 close(ffd); 60 close(ffd);
61 perror_msg("%s", device); 61 bb_perror_msg("%s", device);
62 return 1; 62 return 1;
63 } 63 }
64 *loopro = (mode == O_RDONLY); 64 *loopro = (mode == O_RDONLY);
@@ -70,14 +70,14 @@ extern int set_loop(const char *device, const char *file, int offset,
70 70
71 loopinfo.lo_encrypt_key_size = 0; 71 loopinfo.lo_encrypt_key_size = 0;
72 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) { 72 if (ioctl(fd, LOOP_SET_FD, ffd) < 0) {
73 perror_msg("ioctl: LOOP_SET_FD"); 73 bb_perror_msg("ioctl: LOOP_SET_FD");
74 close(fd); 74 close(fd);
75 close(ffd); 75 close(ffd);
76 return 1; 76 return 1;
77 } 77 }
78 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) { 78 if (ioctl(fd, LOOP_SET_STATUS, &loopinfo) < 0) {
79 (void) ioctl(fd, LOOP_CLR_FD, 0); 79 (void) ioctl(fd, LOOP_CLR_FD, 0);
80 perror_msg("ioctl: LOOP_SET_STATUS"); 80 bb_perror_msg("ioctl: LOOP_SET_STATUS");
81 close(fd); 81 close(fd);
82 close(ffd); 82 close(ffd);
83 return 1; 83 return 1;
diff --git a/libbb/make_directory.c b/libbb/make_directory.c
index 65be397bf..e3d2c52b1 100644
--- a/libbb/make_directory.c
+++ b/libbb/make_directory.c
@@ -1,12 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Mini make_directory implementation for busybox 3 * parse_mode implementation for busybox
4 * 4 *
5 * Copyright (C) 2001 Matt Kraai. 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * Rewriten in 2002
8 * Copyright (C) 2002 Glenn McGrath
9 * Copyright (C) 2002 Vladimir N. Oleynik
10 * 6 *
11 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -24,57 +20,87 @@
24 * 20 *
25 */ 21 */
26 22
23/* Mar 5, 2003 Manuel Novoa III
24 *
25 * This is the main work function for the 'mkdir' applet. As such, it
26 * strives to be SUSv3 compliant in it's behaviour when recursively
27 * making missing parent dirs, and in it's mode setting of the final
28 * directory 'path'.
29 *
30 * To recursively build all missing intermediate directories, make
31 * sure that (flags & FILEUTILS_RECUR) is non-zero. Newly created
32 * intermediate directories will have at least u+wx perms.
33 *
34 * To set specific permisions on 'path', pass the appropriate 'mode'
35 * val. Otherwise, pass -1 to get default permisions.
36 */
37
27#include <errno.h> 38#include <errno.h>
28#include <fcntl.h>
29#include <string.h>
30#include <sys/stat.h>
31#include <sys/types.h>
32#include <unistd.h> 39#include <unistd.h>
33#include <stdlib.h>
34
35#include "libbb.h" 40#include "libbb.h"
36 41
37/* Create the directory PATH with mode MODE, or the default if MODE is -1. 42int bb_make_directory (char *path, long mode, int flags)
38 * Also create parent directories as necessary if flags contains
39 * FILEUTILS_RECUR. */
40
41int make_directory (char *path, long mode, int flags)
42{ 43{
43 int ret; 44 mode_t mask;
44 45 const char *fail_msg;
45 if (flags == FILEUTILS_RECUR) { 46 char *s = path;
46 char *pp = strrchr(path, '/'); 47 char c;
47 if ((pp) && (pp != path)) {
48 *pp = '\0';
49 make_directory(path, -1, flags);
50 *pp = '/';
51 }
52 }
53 48
54 if (mode == -1) { 49 mask = umask(0);
55 struct stat statbuf; 50 umask(mask & ~0300);
56 char *pp = strrchr(path, '/');
57 51
58 statbuf.st_mode = 0777; 52 do {
53 c = 0;
59 54
60 /* stat the directory */ 55 if (flags & FILEUTILS_RECUR) { /* Get the parent. */
61 if ((pp) && (pp != path)) { 56 /* Bypass leading non-'/'s and then subsequent '/'s. */
62 *pp = '\0'; 57 while (*s) {
63 stat(path, &statbuf); 58 if (*s == '/') {
64 *pp = '/'; 59 do {
60 ++s;
61 } while (*s == '/');
62 c = *s; /* Save the current char */
63 *s = 0; /* and replace it with nul. */
64 break;
65 }
66 ++s;
67 }
65 } 68 }
66 69
67 mode = statbuf.st_mode; 70 if (mkdir(path, 0777) < 0) {
68 } 71 /* If we failed for any other reason than the directory
72 * already exists, output a diagnostic and return -1.*/
73 if (errno != EEXIST) {
74 fail_msg = "create";
75 umask(mask);
76 break;
77 }
78 /* Since the directory exists, don't attempt to change
79 * permissions if it was the full target. Note that
80 * this is not an error conditon. */
81 if (!c) {
82 umask(mask);
83 return 0;
84 }
85 }
69 86
70 ret = mkdir(path, mode); 87 if (!c) {
71 if (ret == -1) { 88 /* Done. If necessary, updated perms on the newly
72 if ((flags == FILEUTILS_RECUR) && (errno == EEXIST)) { 89 * created directory. Failure to update here _is_
73 ret = 0; 90 * an error.*/
74 } else { 91 umask(mask);
75 perror_msg_and_die("Cannot create directory '%s'", path); 92 if ((mode != -1) && (chmod(path, mode) < 0)){
93 fail_msg = "set permissions of";
94 break;
95 }
96 return 0;
76 } 97 }
77 }
78 98
79 return(ret); 99 /* Remove any inserted nul from the path (recursive mode). */
100 *s = c;
101
102 } while (1);
103
104 bb_perror_msg ("Cannot %s directory `%s'", fail_msg, path);
105 return -1;
80} 106}
diff --git a/libbb/messages.c b/libbb/messages.c
index cc7e2146c..e5ffbf7f7 100644
--- a/libbb/messages.c
+++ b/libbb/messages.c
@@ -23,82 +23,71 @@
23#include "libbb.h" 23#include "libbb.h"
24 24
25#ifdef L_full_version 25#ifdef L_full_version
26 const char * const full_version = BB_BANNER " multi-call binary"; 26 const char * const bb_msg_full_version = BB_BANNER " multi-call binary";
27#endif
28#ifdef L_name_too_long
29 const char * const name_too_long = "file name too long";
30#endif
31
32#ifdef L_omitting_directory
33 const char * const omitting_directory = "%s: omitting directory";
34#endif
35#ifdef L_not_a_directory
36 const char * const not_a_directory = "%s: not a directory";
37#endif 27#endif
38#ifdef L_memory_exhausted 28#ifdef L_memory_exhausted
39 const char * const memory_exhausted = "memory exhausted"; 29 const char * const bb_msg_memory_exhausted = "memory exhausted";
40#endif 30#endif
41#ifdef L_invalid_date 31#ifdef L_invalid_date
42 const char * const invalid_date = "invalid date `%s'"; 32 const char * const bb_msg_invalid_date = "invalid date `%s'";
43#endif
44#ifdef L_invalid_option
45 const char * const invalid_option = "invalid option -- %c";
46#endif 33#endif
47#ifdef L_io_error 34#ifdef L_io_error
48 const char * const io_error = "%s: input/output error -- %m"; 35 const char * const bb_msg_io_error = "%s: input/output error -- %m";
49#endif
50#ifdef L_dash_dash_help
51 const char * const dash_dash_help = "--help";
52#endif 36#endif
53#ifdef L_write_error 37#ifdef L_write_error
54 const char * const write_error = "Write Error"; 38 const char * const bb_msg_write_error = "Write Error";
55#endif
56#ifdef L_too_few_args
57 const char * const too_few_args = "too few arguments";
58#endif 39#endif
59#ifdef L_name_longer_than_foo 40#ifdef L_name_longer_than_foo
60 const char * const name_longer_than_foo = "Names longer than %d chars not supported."; 41 const char * const bb_msg_name_longer_than_foo = "Names longer than %d chars not supported.";
61#endif 42#endif
62#ifdef L_unknown 43#ifdef L_unknown
63 const char * const unknown = "(unknown)"; 44 const char * const bb_msg_unknown = "(unknown)";
64#endif 45#endif
65
66#ifdef L_can_not_create_raw_socket 46#ifdef L_can_not_create_raw_socket
67 const char * const can_not_create_raw_socket = "can`t create raw socket"; 47 const char * const bb_msg_can_not_create_raw_socket = "can`t create raw socket";
48#endif
49#ifdef L_perm_denied_are_you_root
50 const char * const bb_msg_perm_denied_are_you_root = "permission denied. (are you root?)";
51#endif
52#ifdef L_msg_standard_input
53 const char * const bb_msg_standard_input = "standard input";
54#endif
55#ifdef L_msg_standard_output
56 const char * const bb_msg_standard_output = "standard output";
68#endif 57#endif
69 58
70#ifdef L_passwd_file 59#ifdef L_passwd_file
71#define PASSWD_FILE "/etc/passwd" 60#define PASSWD_FILE "/etc/passwd"
72const char * const passwd_file = PASSWD_FILE; 61const char * const bb_path_passwd_file = PASSWD_FILE;
73#endif 62#endif
74 63
75#ifdef L_shadow_file 64#ifdef L_shadow_file
76#define SHADOW_FILE "/etc/shadow" 65#define SHADOW_FILE "/etc/shadow"
77const char * const shadow_file = SHADOW_FILE; 66const char * const bb_path_shadow_file = SHADOW_FILE;
78#endif 67#endif
79 68
80#ifdef L_group_file 69#ifdef L_group_file
81#define GROUP_FILE "/etc/group" 70#define GROUP_FILE "/etc/group"
82const char * const group_file = GROUP_FILE; 71const char * const bb_path_group_file = GROUP_FILE;
83#endif 72#endif
84 73
85#ifdef L_gshadow_file 74#ifdef L_gshadow_file
86#define GSHADOW_FILE "/etc/gshadow" 75#define GSHADOW_FILE "/etc/gshadow"
87const char * const gshadow_file = GSHADOW_FILE; 76const char * const bb_path_gshadow_file = GSHADOW_FILE;
88#endif 77#endif
89 78
90#ifdef L_nologin_file 79#ifdef L_nologin_file
91#define NOLOGIN_FILE "/etc/nologin" 80#define NOLOGIN_FILE "/etc/nologin"
92const char * const nologin_file = NOLOGIN_FILE; 81const char * const bb_path_nologin_file = NOLOGIN_FILE;
93#endif 82#endif
94 83
95#ifdef L_securetty_file 84#ifdef L_securetty_file
96#define SECURETTY_FILE "/etc/securetty" 85#define SECURETTY_FILE "/etc/securetty"
97const char * const securetty_file = SECURETTY_FILE; 86const char * const bb_path_securetty_file = SECURETTY_FILE;
98#endif 87#endif
99 88
100#ifdef L_motd_file 89#ifdef L_motd_file
101#define MOTD_FILE "/etc/motd" 90#define MOTD_FILE "/etc/motd"
102const char * const motd_file = MOTD_FILE; 91const char * const bb_path_motd_file = MOTD_FILE;
103#endif 92#endif
104 93
diff --git a/libbb/mode_string.c b/libbb/mode_string.c
index 12dc17966..8d4d736ef 100644
--- a/libbb/mode_string.c
+++ b/libbb/mode_string.c
@@ -1,78 +1,134 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Utility routines. 3 * mode_string implementation for busybox
4 * 4 *
5 * Copyright (C) many different people. If you wrote this, please 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * acknowledge your work.
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 10 * (at your option) any later version.
12 * 11 *
13 * This program is distributed in the hope that it will be useful, but 12 * This program is distributed in the hope that it will be useful,
14 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details. 15 * General Public License for more details.
17 * 16 *
18 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * USA 20 *
22 */ 21 */
23 22
24#include <stdio.h> 23#include <assert.h>
25#include "libbb.h" 24#include <sys/stat.h>
26 25
26#if ( S_ISUID != 04000 ) || ( S_ISGID != 02000 ) || ( S_ISVTX != 01000 ) \
27 || ( S_IRUSR != 00400 ) || ( S_IWUSR != 00200 ) || ( S_IXUSR != 00100 ) \
28 || ( S_IRGRP != 00040 ) || ( S_IWGRP != 00020 ) || ( S_IXGRP != 00010 ) \
29 || ( S_IROTH != 00004 ) || ( S_IWOTH != 00002 ) || ( S_IXOTH != 00001 )
30#error permission bitflag value assumption(s) violated!
31#endif
27 32
33#if ( S_IFSOCK!= 0140000 ) || ( S_IFLNK != 0120000 ) \
34 || ( S_IFREG != 0100000 ) || ( S_IFBLK != 0060000 ) \
35 || ( S_IFDIR != 0040000 ) || ( S_IFCHR != 0020000 ) \
36 || ( S_IFIFO != 0010000 )
37#warning mode type bitflag value assumption(s) violated! falling back to larger version
28 38
29#define TYPEINDEX(mode) (((mode) >> 12) & 0x0f) 39#if (S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX) == 07777
30#define TYPECHAR(mode) ("0pcCd?bB-?l?s???" [TYPEINDEX(mode)]) 40#undef mode_t
41#define mode_t unsigned short
42#endif
31 43
32/* The special bits. If set, display SMODE0/1 instead of MODE0/1 */ 44static const mode_t mode_flags[] = {
33static const mode_t SBIT[] = { 45 S_IRUSR, S_IWUSR, S_IXUSR, S_ISUID,
34 0, 0, S_ISUID, 46 S_IRGRP, S_IWGRP, S_IXGRP, S_ISGID,
35 0, 0, S_ISGID, 47 S_IROTH, S_IWOTH, S_IXOTH, S_ISVTX
36 0, 0, S_ISVTX
37}; 48};
38 49
39/* The 9 mode bits to test */ 50/* The static const char arrays below are duplicated for the two cases
40static const mode_t MBIT[] = { 51 * because moving them ahead of the mode_flags declaration cause a text
41 S_IRUSR, S_IWUSR, S_IXUSR, 52 * size increase with the gcc version I'm using. */
42 S_IRGRP, S_IWGRP, S_IXGRP,
43 S_IROTH, S_IWOTH, S_IXOTH
44};
45 53
46static const char MODE1[] = "rwxrwxrwx"; 54/* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
47static const char MODE0[] = "---------"; 55 * and 'B' types don't appear to be available on linux. So I removed them. */
48static const char SMODE1[] = "..s..s..t"; 56static const char type_chars[16] = "?pc?d?b?-?l?s???";
49static const char SMODE0[] = "..S..S..T"; 57/* 0123456789abcdef */
58static const char mode_chars[7] = "rwxSTst";
50 59
51/* 60const char *bb_mode_string(int mode)
52 * Return the standard ls-like mode string from a file mode.
53 * This is static and so is overwritten on each call.
54 */
55const char *mode_string(int mode)
56{ 61{
57 static char buf[12]; 62 static char buf[12];
63 char *p = buf;
64
65 int i, j, k;
58 66
59 int i; 67 *p = type_chars[ (mode >> 12) & 0xf ];
68 i = 0;
69 do {
70 j = k = 0;
71 do {
72 *++p = '-';
73 if (mode & mode_flags[i+j]) {
74 *p = mode_chars[j];
75 k = j;
76 }
77 } while (++j < 3);
78 if (mode & mode_flags[i+j]) {
79 *p = mode_chars[3 + (k & 2) + ((i&8) >> 3)];
80 }
81 i += 4;
82 } while (i < 12);
83
84 /* Note: We don't bother with nul termination because bss initialization
85 * should have taken care of that for us. If the user scribbled in buf
86 * memory, they deserve whatever happens. But we'll at least assert. */
87 assert(buf[10] == 0);
60 88
61 buf[0] = TYPECHAR(mode);
62 for (i = 0; i < 9; i++) {
63 if (mode & SBIT[i])
64 buf[i + 1] = (mode & MBIT[i]) ? SMODE1[i] : SMODE0[i];
65 else
66 buf[i + 1] = (mode & MBIT[i]) ? MODE1[i] : MODE0[i];
67 }
68 return buf; 89 return buf;
69} 90}
70 91
71/* END CODE */ 92#else
72/* 93
73Local Variables: 94/* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C',
74c-file-style: "linux" 95 * and 'B' types don't appear to be available on linux. So I removed them. */
75c-basic-offset: 4 96static const char type_chars[16] = "?pc?d?b?-?l?s???";
76tab-width: 4 97/* 0123456789abcdef */
77End: 98static const char mode_chars[7] = "rwxSTst";
78*/ 99
100const char *bb_mode_string(int mode)
101{
102 static char buf[12];
103 char *p = buf;
104
105 int i, j, k, m;
106
107 *p = type_chars[ (mode >> 12) & 0xf ];
108 i = 0;
109 m = 0400;
110 do {
111 j = k = 0;
112 do {
113 *++p = '-';
114 if (mode & m) {
115 *p = mode_chars[j];
116 k = j;
117 }
118 m >>= 1;
119 } while (++j < 3);
120 ++i;
121 if (mode & (010000 >> i)) {
122 *p = mode_chars[3 + k + (i >> 1)];
123 }
124 } while (i < 3);
125
126 /* Note: We don't bother with nul termination because bss initialization
127 * should have taken care of that for us. If the user scribbled in buf
128 * memory, they deserve whatever happens. But we'll at least assert. */
129 assert(buf[10] == 0);
130
131 return buf;
132}
133
134#endif
diff --git a/libbb/module_syscalls.c b/libbb/module_syscalls.c
index 6bfff207f..8fe9e525c 100644
--- a/libbb/module_syscalls.c
+++ b/libbb/module_syscalls.c
@@ -51,7 +51,7 @@ _syscall5(int, init_module, void *, first, void *, second, void *, third,
51#warning -> The query_module system call is being stubbed out... 51#warning -> The query_module system call is being stubbed out...
52int query_module(const char *name, int which, void *buf, size_t bufsize, size_t *ret) 52int query_module(const char *name, int which, void *buf, size_t bufsize, size_t *ret)
53{ 53{
54 error_msg("\n\nTo make this application work, you will need to recompile\n" 54 bb_error_msg("\n\nTo make this application work, you will need to recompile\n"
55 "with a kernel supporting the query_module system call. -Erik\n"); 55 "with a kernel supporting the query_module system call. -Erik\n");
56 errno=ENOSYS; 56 errno=ENOSYS;
57 return -1; 57 return -1;
diff --git a/libbb/mtab.c b/libbb/mtab.c
index c521b1e05..54905c724 100644
--- a/libbb/mtab.c
+++ b/libbb/mtab.c
@@ -27,21 +27,20 @@
27#include <mntent.h> 27#include <mntent.h>
28#include "libbb.h" 28#include "libbb.h"
29 29
30extern const char mtab_file[]; /* Defined in utility.c */
31static const int MS_RDONLY = 1; /* Mount read-only. */ 30static const int MS_RDONLY = 1; /* Mount read-only. */
32 31
33void erase_mtab(const char *name) 32void erase_mtab(const char *name)
34{ 33{
35 struct mntent entries[20]; 34 struct mntent entries[20];
36 int count = 0; 35 int count = 0;
37 FILE *mountTable = setmntent(mtab_file, "r"); 36 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
38 struct mntent *m; 37 struct mntent *m;
39 38
40 /* Check if reading the mtab file failed */ 39 /* Check if reading the mtab file failed */
41 if (mountTable == 0 40 if (mountTable == 0
42 /* Bummer. fall back on trying the /proc filesystem */ 41 /* Bummer. fall back on trying the /proc filesystem */
43 && (mountTable = setmntent("/proc/mounts", "r")) == 0) { 42 && (mountTable = setmntent("/proc/mounts", "r")) == 0) {
44 perror_msg("%s", mtab_file); 43 bb_perror_msg(bb_path_mtab_file);
45 return; 44 return;
46 } 45 }
47 46
@@ -55,7 +54,7 @@ void erase_mtab(const char *name)
55 count++; 54 count++;
56 } 55 }
57 endmntent(mountTable); 56 endmntent(mountTable);
58 if ((mountTable = setmntent(mtab_file, "w"))) { 57 if ((mountTable = setmntent(bb_path_mtab_file, "w"))) {
59 int i; 58 int i;
60 59
61 for (i = 0; i < count; i++) { 60 for (i = 0; i < count; i++) {
@@ -69,17 +68,17 @@ void erase_mtab(const char *name)
69 } 68 }
70 endmntent(mountTable); 69 endmntent(mountTable);
71 } else if (errno != EROFS) 70 } else if (errno != EROFS)
72 perror_msg("%s", mtab_file); 71 bb_perror_msg(bb_path_mtab_file);
73} 72}
74 73
75void write_mtab(char *blockDevice, char *directory, 74void write_mtab(char *blockDevice, char *directory,
76 char *filesystemType, long flags, char *string_flags) 75 char *filesystemType, long flags, char *string_flags)
77{ 76{
78 FILE *mountTable = setmntent(mtab_file, "a+"); 77 FILE *mountTable = setmntent(bb_path_mtab_file, "a+");
79 struct mntent m; 78 struct mntent m;
80 79
81 if (mountTable == 0) { 80 if (mountTable == 0) {
82 perror_msg("%s", mtab_file); 81 bb_perror_msg(bb_path_mtab_file);
83 return; 82 return;
84 } 83 }
85 if (mountTable) { 84 if (mountTable) {
diff --git a/libbb/mtab_file.c b/libbb/mtab_file.c
index 2124e0144..898e2fa89 100644
--- a/libbb/mtab_file.c
+++ b/libbb/mtab_file.c
@@ -26,9 +26,9 @@
26/* Busybox mount uses either /proc/mounts or /etc/mtab to 26/* Busybox mount uses either /proc/mounts or /etc/mtab to
27 * get the list of currently mounted filesystems */ 27 * get the list of currently mounted filesystems */
28#if defined CONFIG_FEATURE_MTAB_SUPPORT 28#if defined CONFIG_FEATURE_MTAB_SUPPORT
29const char mtab_file[] = CONFIG_FEATURE_MTAB_FILENAME; 29const char bb_path_mtab_file[] = CONFIG_FEATURE_MTAB_FILENAME;
30#else 30#else
31const char mtab_file[] = "/proc/mounts"; 31const char bb_path_mtab_file[] = "/proc/mounts";
32#endif 32#endif
33 33
34 34
diff --git a/libbb/my_getgrnam.c b/libbb/my_getgrnam.c
index f80d3f824..eb5b58174 100644
--- a/libbb/my_getgrnam.c
+++ b/libbb/my_getgrnam.c
@@ -33,7 +33,7 @@ long my_getgrnam(const char *name)
33 33
34 mygroup = getgrnam(name); 34 mygroup = getgrnam(name);
35 if (mygroup==NULL) 35 if (mygroup==NULL)
36 error_msg_and_die("unknown group name: %s", name); 36 bb_error_msg_and_die("unknown group name: %s", name);
37 37
38 return (mygroup->gr_gid); 38 return (mygroup->gr_gid);
39} 39}
diff --git a/libbb/my_getpwnam.c b/libbb/my_getpwnam.c
index 04951a4d0..ada2ffb56 100644
--- a/libbb/my_getpwnam.c
+++ b/libbb/my_getpwnam.c
@@ -33,7 +33,7 @@ long my_getpwnam(const char *name)
33 33
34 myuser = getpwnam(name); 34 myuser = getpwnam(name);
35 if (myuser==NULL) 35 if (myuser==NULL)
36 error_msg_and_die("unknown user name: %s", name); 36 bb_error_msg_and_die("unknown user name: %s", name);
37 37
38 return myuser->pw_uid; 38 return myuser->pw_uid;
39} 39}
diff --git a/libbb/my_getpwnamegid.c b/libbb/my_getpwnamegid.c
index 07e02c1cf..06071c9f2 100644
--- a/libbb/my_getpwnamegid.c
+++ b/libbb/my_getpwnamegid.c
@@ -35,11 +35,11 @@ long my_getpwnamegid(const char *name)
35 35
36 myuser=getpwnam(name); 36 myuser=getpwnam(name);
37 if (myuser==NULL) 37 if (myuser==NULL)
38 error_msg_and_die("unknown user name: %s", name); 38 bb_error_msg_and_die("unknown user name: %s", name);
39 39
40 mygroup = getgrgid(myuser->pw_gid); 40 mygroup = getgrgid(myuser->pw_gid);
41 if (mygroup==NULL) 41 if (mygroup==NULL)
42 error_msg_and_die("unknown gid %ld", (long)myuser->pw_gid); 42 bb_error_msg_and_die("unknown gid %ld", (long)myuser->pw_gid);
43 43
44 return mygroup->gr_gid; 44 return mygroup->gr_gid;
45} 45}
diff --git a/libbb/obscure.c b/libbb/obscure.c
index 588ef5af6..1a99b7cf9 100644
--- a/libbb/obscure.c
+++ b/libbb/obscure.c
@@ -143,7 +143,7 @@ password_check(const char *old, const char *newval, const struct passwd *pwdp)
143 return "too simple"; 143 return "too simple";
144 144
145 msg = NULL; 145 msg = NULL;
146 newmono = str_lower(xstrdup(newval)); 146 newmono = str_lower(bb_xstrdup(newval));
147 lenwrap = strlen(old) * 2 + 1; 147 lenwrap = strlen(old) * 2 + 1;
148 wrapped = (char *) xmalloc(lenwrap); 148 wrapped = (char *) xmalloc(lenwrap);
149 str_lower(strcpy(wrapped, old)); 149 str_lower(strcpy(wrapped, old));
@@ -208,8 +208,8 @@ obscure_msg(const char *old, const char *newval, const struct passwd *pwdp)
208 if (oldlen <= maxlen && newlen <= maxlen) 208 if (oldlen <= maxlen && newlen <= maxlen)
209 return NULL; 209 return NULL;
210 210
211 new1 = (char *) xstrdup(newval); 211 new1 = (char *) bb_xstrdup(newval);
212 old1 = (char *) xstrdup(old); 212 old1 = (char *) bb_xstrdup(old);
213 if (newlen > maxlen) 213 if (newlen > maxlen)
214 new1[maxlen] = '\0'; 214 new1[maxlen] = '\0';
215 if (oldlen > maxlen) 215 if (oldlen > maxlen)
diff --git a/libbb/parse_mode.c b/libbb/parse_mode.c
index ba34ea929..49573dfbb 100644
--- a/libbb/parse_mode.c
+++ b/libbb/parse_mode.c
@@ -1,134 +1,177 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Utility routines. 3 * parse_mode implementation for busybox
4 * 4 *
5 * Copyright (C) many different people. If you wrote this, please 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * acknowledge your work.
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 10 * (at your option) any later version.
12 * 11 *
13 * This program is distributed in the hope that it will be useful, but 12 * This program is distributed in the hope that it will be useful,
14 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details. 15 * General Public License for more details.
17 * 16 *
18 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * USA 20 *
22 */ 21 */
23 22
24#include <stdio.h> 23/* http://www.opengroup.org/onlinepubs/007904975/utilities/chmod.html */
24
25#include <stdlib.h> 25#include <stdlib.h>
26#include <assert.h>
27#include <sys/stat.h>
26#include "libbb.h" 28#include "libbb.h"
27 29
30#define FILEMODEBITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO)
28 31
29/* This function parses the sort of string you might pass 32extern int bb_parse_mode(const char *s, mode_t *current_mode)
30 * to chmod (i.e., [ugoa]{+|-|=}[rwxst] ) and returns the
31 * correct mode described by the string. */
32extern int parse_mode(const char *s, mode_t * theMode)
33{ 33{
34 static const mode_t group_set[] = { 34 static const mode_t who_mask[] = {
35 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO, /* a */
35 S_ISUID | S_IRWXU, /* u */ 36 S_ISUID | S_IRWXU, /* u */
36 S_ISGID | S_IRWXG, /* g */ 37 S_ISGID | S_IRWXG, /* g */
37 S_IRWXO, /* o */ 38 S_IRWXO /* o */
38 S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO /* a */
39 }; 39 };
40 40
41 static const mode_t mode_set[] = { 41 static const mode_t perm_mask[] = {
42 S_IRUSR | S_IRGRP | S_IROTH, /* r */ 42 S_IRUSR | S_IRGRP | S_IROTH, /* r */
43 S_IWUSR | S_IWGRP | S_IWOTH, /* w */ 43 S_IWUSR | S_IWGRP | S_IWOTH, /* w */
44 S_IXUSR | S_IXGRP | S_IXOTH, /* x */ 44 S_IXUSR | S_IXGRP | S_IXOTH, /* x */
45 S_IXUSR | S_IXGRP | S_IXOTH, /* X -- special -- see below */
45 S_ISUID | S_ISGID, /* s */ 46 S_ISUID | S_ISGID, /* s */
46 S_ISVTX /* t */ 47 S_ISVTX /* t */
47 }; 48 };
48 49
49 static const char group_chars[] = "ugoa"; 50 static const char who_chars[] = "augo";
50 static const char mode_chars[] = "rwxst"; 51 static const char perm_chars[] = "rwxXst";
51 52
52 const char *p; 53 const char *p;
53 54
54 mode_t andMode = 55 mode_t wholist;
55 S_ISVTX | S_ISUID | S_ISGID | S_IRWXU | S_IRWXG | S_IRWXO; 56 mode_t permlist;
56 mode_t orMode = 0; 57 mode_t mask;
57 mode_t mode; 58 mode_t new_mode;
58 mode_t groups; 59 char op;
59 char type; 60
60 char c; 61 assert(s);
62
63 if (((unsigned int)(*s - '0')) < 8) {
64 unsigned long tmp;
65 char *e;
61 66
62 if (s==NULL) { 67 tmp = strtol(s, &e, 8);
63 return (FALSE); 68 if (*e || (tmp > 0xffffU)) { /* Check range and trailing chars. */
69 return 0;
70 }
71 *current_mode = tmp;
72 return 1;
64 } 73 }
65 74
66 do { 75 mask = umask(0);
67 mode = 0; 76 umask(mask);
68 groups = 0; 77
69 NEXT_GROUP: 78 new_mode = *current_mode;
70 if ((c = *s++) == '\0') { 79
71 return -1; 80 /* Note: We allow empty clauses, and hence empty modes.
81 * We treat an empty mode as no change to perms. */
82
83 while (*s) { /* Process clauses. */
84
85 if (*s == ',') { /* We allow empty clauses. */
86 ++s;
87 continue;
72 } 88 }
73 for (p=group_chars ; *p ; p++) { 89
74 if (*p == c) { 90 /* Get a wholist. */
75 groups |= group_set[(int)(p-group_chars)]; 91 wholist = 0;
76 goto NEXT_GROUP; 92
93 WHO_LIST:
94 p = who_chars;
95 do {
96 if (*p == *s) {
97 wholist |= who_mask[(int)(p-who_chars)];
98 if (!*++s) {
99 return 0;
100 }
101 goto WHO_LIST;
77 } 102 }
78 } 103 } while (*++p);
79 switch (c) { 104
80 case '=': 105 do { /* Process action list. */
81 case '+': 106 if ((*s != '+') && (*s != '-')) {
82 case '-': 107 if (*s != '=') {
83 type = c; 108 return 0;
84 if (groups == 0) { /* The default is "all" */
85 groups |= S_ISUID | S_ISGID | S_ISVTX
86 | S_IRWXU | S_IRWXG | S_IRWXO;
87 } 109 }
88 break; 110 /* Since op is '=', clear all bits corresponding to the
89 default: 111 * wholist, of all file bits if wholist is empty. */
90 if ((c < '0') || (c > '7') || (mode | groups)) { 112 permlist = ~FILEMODEBITS;
91 return (FALSE); 113 if (wholist) {
92 } else { 114 permlist = ~wholist;
93 *theMode = strtol(--s, NULL, 8);
94 return (TRUE);
95 } 115 }
96 } 116 new_mode &= permlist;
117 }
118 op = *s++;
119
120 /* Check for permcopy. */
121 p = who_chars + 1; /* Skip 'a' entry. */
122 do {
123 if (*p == *s) {
124 int i = 0;
125 permlist = who_mask[(int)(p-who_chars)]
126 & (S_IRWXU | S_IRWXG | S_IRWXO)
127 & new_mode;
128 do {
129 if (permlist & perm_mask[i]) {
130 permlist |= perm_mask[i];
131 }
132 } while (++i < 3);
133 ++s;
134 goto GOT_ACTION;
135 }
136 } while (*++p);
97 137
98 NEXT_MODE: 138 /* It was not a permcopy, so get a permlist. */
99 if (((c = *s++) != '\0') && (c != ',')) { 139 permlist = 0;
100 for (p=mode_chars ; *p ; p++) { 140
101 if (*p == c) { 141 PERM_LIST:
102 mode |= mode_set[(int)(p-mode_chars)]; 142 p = perm_chars;
103 goto NEXT_MODE; 143 do {
144 if (*p == *s) {
145 if ((*p != 'X')
146 || (new_mode & (S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH))
147 ) {
148 permlist |= perm_mask[(int)(p-perm_chars)];
149 }
150 if (!*++s) {
151 break;
152 }
153 goto PERM_LIST;
154 }
155 } while (*++p);
156
157 GOT_ACTION:
158 if (permlist) { /* The permlist was nonempty. */
159 mode_t tmp = ~mask;
160 if (wholist) {
161 tmp = wholist;
162 }
163 permlist &= tmp;
164
165 if (op == '-') {
166 new_mode &= ~permlist;
167 } else {
168 new_mode |= permlist;
104 } 169 }
105 } 170 }
106 break; /* We're done so break out of loop.*/ 171 } while (*s && (*s != ','));
107 } 172 }
108 switch (type) {
109 case '=':
110 andMode &= ~(groups); /* Now fall through. */
111 case '+':
112 orMode |= mode & groups;
113 break;
114 case '-':
115 andMode &= ~(mode & groups);
116 orMode &= ~(mode & groups);
117 break;
118 }
119 } while (c == ',');
120 173
121 *theMode &= andMode; 174 *current_mode = new_mode;
122 *theMode |= orMode;
123 175
124 return TRUE; 176 return 1;
125} 177}
126
127/* END CODE */
128/*
129Local Variables:
130c-file-style: "linux"
131c-basic-offset: 4
132tab-width: 4
133End:
134*/
diff --git a/libbb/parse_number.c b/libbb/parse_number.c
index 755a357ad..92ad6a216 100644
--- a/libbb/parse_number.c
+++ b/libbb/parse_number.c
@@ -1,70 +1,64 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * Utility routines. 3 * bb_xparse_number implementation for busybox
4 * 4 *
5 * Copyright (C) many different people. If you wrote this, please 5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 * acknowledge your work.
7 * 6 *
8 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 9 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version. 10 * (at your option) any later version.
12 * 11 *
13 * This program is distributed in the hope that it will be useful, but 12 * This program is distributed in the hope that it will be useful,
14 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details. 15 * General Public License for more details.
17 * 16 *
18 * You should have received a copy of the GNU General Public License 17 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software 18 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * USA 20 *
22 */ 21 */
23 22
24#include <stdio.h>
25#include <string.h>
26#include <stdlib.h> 23#include <stdlib.h>
24#include <string.h>
25#include <limits.h>
26#include <errno.h>
27#include <assert.h>
27#include "libbb.h" 28#include "libbb.h"
28 29
29 30extern
30unsigned long parse_number(const char *numstr, 31unsigned long bb_xparse_number(const char *numstr,
31 const struct suffix_mult *suffixes) 32 const struct suffix_mult *suffixes)
32{ 33{
33 const struct suffix_mult *sm; 34 unsigned long int r;
34 unsigned long int ret; 35 char *e;
35 int len; 36 int old_errno;
36 char *end;
37 37
38 ret = strtoul(numstr, &end, 10); 38 /* Since this is a lib function, we're not allowed to reset errno to 0.
39 if (numstr == end) 39 * Doing so could break an app that is deferring checking of errno.
40 error_msg_and_die("invalid number `%s'", numstr); 40 * So, save the old value so that we can restore it if successful. */
41 while (end[0] != '\0') { 41 old_errno = errno;
42 sm = suffixes; 42 errno = 0;
43 while ( sm != 0 ) { 43 r = strtoul(numstr, &e, 10);
44 if(sm->suffix) { 44
45 len = strlen(sm->suffix); 45 if ((numstr != e) && !errno) {
46 if (strncmp(sm->suffix, end, len) == 0) { 46 errno = old_errno; /* Ok. So restore errno. */
47 ret *= sm->mult; 47 if (!*e) {
48 end += len; 48 return r;
49 break; 49 }
50 if (suffixes) {
51 assert(suffixes->suffix); /* No nul suffixes. */
52 do {
53 if (strcmp(suffixes->suffix, e) == 0) {
54 if (ULONG_MAX / suffixes->mult < r) { /* Overflow! */
55 break;
56 }
57 return r * suffixes->mult;
50 } 58 }
51 sm++; 59 ++suffixes;
52 60 } while (suffixes->suffix);
53 } else
54 sm = 0;
55 } 61 }
56 if (sm == 0)
57 error_msg_and_die("invalid number `%s'", numstr);
58 } 62 }
59 return ret; 63 bb_error_msg_and_die("invalid number `%s'", numstr);
60} 64}
61
62
63/* END CODE */
64/*
65Local Variables:
66c-file-style: "linux"
67c-basic-offset: 4
68tab-width: 4
69End:
70*/
diff --git a/libbb/perror_msg.c b/libbb/perror_msg.c
index 8c57b0d16..f32c1c8fe 100644
--- a/libbb/perror_msg.c
+++ b/libbb/perror_msg.c
@@ -25,12 +25,12 @@
25#include <stdlib.h> 25#include <stdlib.h>
26#include "libbb.h" 26#include "libbb.h"
27 27
28extern void perror_msg(const char *s, ...) 28extern void bb_perror_msg(const char *s, ...)
29{ 29{
30 va_list p; 30 va_list p;
31 31
32 va_start(p, s); 32 va_start(p, s);
33 vperror_msg(s, p); 33 bb_vperror_msg(s, p);
34 va_end(p); 34 va_end(p);
35} 35}
36 36
diff --git a/libbb/perror_msg_and_die.c b/libbb/perror_msg_and_die.c
index 9004925cc..57a21136d 100644
--- a/libbb/perror_msg_and_die.c
+++ b/libbb/perror_msg_and_die.c
@@ -25,14 +25,14 @@
25#include <stdlib.h> 25#include <stdlib.h>
26#include "libbb.h" 26#include "libbb.h"
27 27
28extern void perror_msg_and_die(const char *s, ...) 28extern void bb_perror_msg_and_die(const char *s, ...)
29{ 29{
30 va_list p; 30 va_list p;
31 31
32 va_start(p, s); 32 va_start(p, s);
33 vperror_msg(s, p); 33 bb_vperror_msg(s, p);
34 va_end(p); 34 va_end(p);
35 exit(EXIT_FAILURE); 35 exit(bb_default_error_retval);
36} 36}
37 37
38 38
diff --git a/libbb/perror_nomsg.c b/libbb/perror_nomsg.c
new file mode 100644
index 000000000..464cb86c4
--- /dev/null
+++ b/libbb/perror_nomsg.c
@@ -0,0 +1,30 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * bb_perror_nomsg implementation for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <stddef.h>
24#include <libbb.h>
25
26extern void bb_perror_nomsg(void)
27{
28 /* Ignore the gcc warning about a null format string. */
29 bb_perror_msg(NULL);
30}
diff --git a/libbb/perror_nomsg_and_die.c b/libbb/perror_nomsg_and_die.c
new file mode 100644
index 000000000..bab228455
--- /dev/null
+++ b/libbb/perror_nomsg_and_die.c
@@ -0,0 +1,30 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * bb_perror_nomsg_and_die implementation for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <stddef.h>
24#include "libbb.h"
25
26extern void bb_perror_nomsg_and_die(void)
27{
28 /* Ignore the gcc warning about a null format string. */
29 bb_perror_msg_and_die(NULL);
30}
diff --git a/libbb/print_file.c b/libbb/print_file.c
index cdd60e7a0..8417c10d3 100644
--- a/libbb/print_file.c
+++ b/libbb/print_file.c
@@ -21,39 +21,50 @@
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include <stdlib.h> 23#include <stdlib.h>
24#include <sys/stat.h>
25#include "libbb.h" 24#include "libbb.h"
26 25
27 26extern void bb_xprint_and_close_file(FILE *file)
28extern void print_file(FILE *file)
29{ 27{
30 fflush(stdout); 28 bb_xfflush_stdout();
31 if (copyfd(fileno(file), fileno(stdout), 0) == -1) { 29 /* Note: Do not use STDOUT_FILENO here, as this is a lib routine
32 exit(EXIT_FAILURE); 30 * and the calling code may have reassigned stdout. */
31 if (bb_copyfd(fileno(file), fileno(stdout), 0) == -1) {
32 /* bb_copyfd outputs any needed messages, so just die. */
33 exit(bb_default_error_retval);
33 } 34 }
35 /* Note: Since we're reading, don't bother checking the return value
36 * of fclose(). The only possible failure is EINTR which
37 * should already have been taken care of. */
34 fclose(file); 38 fclose(file);
35} 39}
36 40
37extern int print_file_by_name(char *filename) 41/* Returns:
42 * 0 if successful
43 * -1 if 'filename' does not exist or is a directory
44 * exits with default error code if an error occurs
45 */
46
47extern int bb_xprint_file_by_name(const char *filename)
38{ 48{
49 FILE *f;
50
51#if 0
52 /* This check shouldn't be necessary for linux, but is left
53 * here disabled just in case. */
39 struct stat statBuf; 54 struct stat statBuf;
40 int status = TRUE;
41 55
42 if(is_directory(filename, TRUE, &statBuf)==TRUE) { 56 if(is_directory(filename, TRUE, &statBuf)) {
43 error_msg("%s: Is directory", filename); 57 bb_error_msg("%s: Is directory", filename);
44 status = FALSE; 58 } else
45 } else { 59#endif
46 FILE *f = wfopen(filename, "r"); 60 if ((f = bb_wfopen(filename, "r")) != NULL) {
47 if(f!=NULL) 61 bb_xprint_and_close_file(f);
48 print_file(f); 62 return 0;
49 else
50 status = FALSE;
51 } 63 }
52 64
53 return status; 65 return -1;
54} 66}
55 67
56
57/* END CODE */ 68/* END CODE */
58/* 69/*
59Local Variables: 70Local Variables:
diff --git a/libbb/printf.c b/libbb/printf.c
new file mode 100644
index 000000000..686257699
--- /dev/null
+++ b/libbb/printf.c
@@ -0,0 +1,177 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * *printf implementations for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23/* Mar 12, 2003 Manuel Novoa III
24 *
25 * While fwrite(), fputc(), fputs(), etc. all set the stream error flag
26 * on failure, the *printf functions are unique in that they can fail
27 * for reasons not related to the actual output itself. Among the possible
28 * reasons for failure which don't set the streams error indicator,
29 * SUSv3 lists EILSEQ, EINVAL, and ENOMEM.
30 *
31 * In some cases, it would be desireable to have a group of *printf()
32 * functions available that _always_ set the stream error indicator on
33 * failure. That would allow us to defer error checking until applet
34 * exit. Unfortunately, there is no standard way of setting a streams
35 * error indicator... even though we can clear it with clearerr().
36 *
37 * Therefore, we have to resort to implementation dependent code. Feel
38 * free to send patches for stdio implementations where the following
39 * fails.
40 *
41 * NOTE: None of this is threadsafe. As busybox is a nonthreaded app,
42 * that isn't currently an issue.
43 */
44
45#include <stdio.h>
46#include <stdarg.h>
47#include "libbb.h"
48
49#if defined(__UCLIBC__)
50
51# if defined(__FLAG_ERROR)
52/* Using my newer stdio implementation. Unlocked macros are:
53 * #define __CLEARERR(stream) \
54 ((stream)->modeflags &= ~(__FLAG_EOF|__FLAG_ERROR), (void)0)
55 * #define __FEOF(stream) ((stream)->modeflags & __FLAG_EOF)
56 * #define __FERROR(stream) ((stream)->modeflags & __FLAG_ERROR)
57 */
58#define SET_FERROR_UNLOCKED(S) ((S)->modeflags |= __FLAG_ERROR)
59
60#elif defined(__MODE_ERR)
61/* Using either the original stdio implementation (from dev86) or
62 * my original stdio rewrite. Macros were:
63 * #define ferror(fp) (((fp)->mode&__MODE_ERR) != 0)
64 * #define feof(fp) (((fp)->mode&__MODE_EOF) != 0)
65 * #define clearerr(fp) ((fp)->mode &= ~(__MODE_EOF|__MODE_ERR),0)
66 */
67#define SET_FERROR_UNLOCKED(S) ((S)->mode |= __MODE_ERR)
68
69#else
70#error unknown uClibc stdio implemenation!
71#endif
72
73#elif defined(__GLIBC__)
74
75# if defined(_STDIO_USES_IOSTREAM)
76/* Apparently using the newer libio implementation, with associated defines:
77 * #define _IO_feof_unlocked(__fp) (((__fp)->_flags & _IO_EOF_SEEN) != 0)
78 * #define _IO_ferror_unlocked(__fp) (((__fp)->_flags & _IO_ERR_SEEN) != 0)
79 */
80#define SET_FERROR_UNLOCKED(S) ((S)->_flags |= _IO_ERR_SEEN)
81
82# else
83/* Assume the older version of glibc which used a bitfield entry
84 * as a stream error flag. The associated defines were:
85 * #define __clearerr(stream) ((stream)->__error = (stream)->__eof = 0)
86 * #define feof_unlocked(stream) ((stream)->__eof != 0)
87 * #define ferror_unlocked(stream) ((stream)->__error != 0)
88 */
89#define SET_FERROR_UNLOCKED(S) ((S)->__error = 1)
90
91# endif
92
93#elif defined(__NEWLIB_H__)
94/* I honestly don't know if there are different versions of stdio in
95 * newlibs history. Anyway, here's what's current.
96 * #define __sfeof(p) (((p)->_flags & __SEOF) != 0)
97 * #define __sferror(p) (((p)->_flags & __SERR) != 0)
98 * #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF)))
99 */
100#define SET_FERROR_UNLOCKED(S) ((S)->_flags |= __SERR)
101
102#elif defined(__dietlibc__)
103/*
104 * WARNING!!! dietlibc is quite buggy. WARNING!!!
105 *
106 * Some example bugs as of March 12, 2003...
107 * 1) fputc() doesn't set the error indicator on failure.
108 * 2) freopen() doesn't maintain the same stream object, contary to
109 * standards. This makes it useless in its primary role of
110 * reassociating stdin/stdout/stderr.
111 * 3) printf() often fails to correctly format output when conversions
112 * involve padding. It is also practically useless for floating
113 * point output.
114 *
115 * But, if you're determined to use it anyway, (as of the current version)
116 * you can extract the information you need from dietstdio.h. See the
117 * other library implementations for examples.
118 */
119#error dietlibc is currently not supported. Please see the commented source.
120
121#else /* some other lib */
122/* Please see the comments for the above supported libaries for examples
123 * of what is required to support your stdio implementation.
124 */
125#error Your stdio library is currently not supported. Please see the commented source.
126#endif
127
128#ifdef L_vfprintf
129extern int bb_vfprintf(FILE * __restrict stream,
130 const char * __restrict format,
131 va_list arg)
132{
133 int rv;
134
135 if ((rv = vfprintf(stream, format, arg)) < 0) {
136 SET_FERROR_UNLOCKED(stream);
137 }
138
139 return rv;
140}
141#endif
142
143#ifdef L_vprintf
144extern int bb_vprintf(const char * __restrict format, va_list arg)
145{
146 return bb_vfprintf(stdout, format, arg);
147}
148#endif
149
150#ifdef L_fprintf
151extern int bb_fprintf(FILE * __restrict stream,
152 const char * __restrict format, ...)
153{
154 va_list arg;
155 int rv;
156
157 va_start(arg, format);
158 rv = bb_vfprintf(stream, format, arg);
159 va_end(arg);
160
161 return rv;
162}
163#endif
164
165#ifdef L_printf
166extern int bb_printf(const char * __restrict format, ...)
167{
168 va_list arg;
169 int rv;
170
171 va_start(arg, format);
172 rv = bb_vfprintf(stdout, format, arg);
173 va_end(arg);
174
175 return rv;
176}
177#endif
diff --git a/libbb/process_escape_sequence.c b/libbb/process_escape_sequence.c
index 9a16f80ab..ef2717bdd 100644
--- a/libbb/process_escape_sequence.c
+++ b/libbb/process_escape_sequence.c
@@ -2,7 +2,7 @@
2/* 2/*
3 * Utility routines. 3 * Utility routines.
4 * 4 *
5 * Copyright (C) Manuel Nova III <mnovoa3@bellsouth.net> 5 * Copyright (C) Manuel Novoa III <mjn3@codepoet.org>
6 * and Vladimir Oleynik <dzo@simtreas.ru> 6 * and Vladimir Oleynik <dzo@simtreas.ru>
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
@@ -26,9 +26,7 @@
26#include <limits.h> 26#include <limits.h>
27#include "libbb.h" 27#include "libbb.h"
28 28
29 29char bb_process_escape_sequence(const char **ptr)
30
31char process_escape_sequence(const char **ptr)
32{ 30{
33 static const char charmap[] = { 31 static const char charmap[] = {
34 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0, 32 'a', 'b', 'f', 'n', 'r', 't', 'v', '\\', 0,
@@ -36,40 +34,43 @@ char process_escape_sequence(const char **ptr)
36 34
37 const char *p; 35 const char *p;
38 const char *q; 36 const char *q;
39 int num_digits; 37 unsigned int num_digits;
38 unsigned int r;
40 unsigned int n; 39 unsigned int n;
41 40
42 n = 0; 41 n = 0;
43 q = *ptr; 42 q = *ptr;
44 43
45 for ( num_digits = 0 ; num_digits < 3 ; ++num_digits) { 44 num_digits = 0;
46 if ((*q < '0') || (*q > '7')) { /* not a digit? */ 45 do {
47 break; 46 if (((unsigned int)(*q - '0')) <= 7) {
47 r = n * 8 + (*q - '0');
48 if (r <= UCHAR_MAX) {
49 n = r;
50 ++q;
51 if (++num_digits < 3) {
52 continue;
53 }
54 }
48 } 55 }
49 n = n * 8 + (*q++ - '0'); 56 break;
50 } 57 } while (1);
51 58
52 if (num_digits == 0) { /* mnemonic escape sequence? */ 59 if (num_digits == 0) { /* mnemonic escape sequence? */
53 for (p=charmap ; *p ; p++) { 60 p = charmap;
61 do {
54 if (*p == *q) { 62 if (*p == *q) {
55 q++; 63 q++;
56 break; 64 break;
57 } 65 }
58 } 66 } while (*++p);
59 n = *(p+(sizeof(charmap)/2)); 67 n = *(p+(sizeof(charmap)/2));
60 } 68 }
61 69
62 /* doesn't hurt to fall through to here from mnemonic case */
63 if (n > UCHAR_MAX) { /* is octal code too big for a char? */
64 n /= 8; /* adjust value and */
65 --q; /* back up one char */
66 }
67
68 *ptr = q; 70 *ptr = q;
69 return (char) n; 71 return (char) n;
70} 72}
71 73
72
73/* END CODE */ 74/* END CODE */
74/* 75/*
75Local Variables: 76Local Variables:
diff --git a/libbb/procps.c b/libbb/procps.c
index fcc9c2d3a..a513f3f7f 100644
--- a/libbb/procps.c
+++ b/libbb/procps.c
@@ -34,7 +34,7 @@ extern procps_status_t * procps_scan(int save_user_arg0)
34 if (!dir) { 34 if (!dir) {
35 dir = opendir("/proc"); 35 dir = opendir("/proc");
36 if(!dir) 36 if(!dir)
37 error_msg_and_die("Can't open /proc"); 37 bb_error_msg_and_die("Can't open /proc");
38 } 38 }
39 for(;;) { 39 for(;;) {
40 if((entry = readdir(dir)) == NULL) { 40 if((entry = readdir(dir)) == NULL) {
diff --git a/libbb/read_package_field.c b/libbb/read_package_field.c
index 867b198ba..42628f35c 100644
--- a/libbb/read_package_field.c
+++ b/libbb/read_package_field.c
@@ -102,10 +102,10 @@ int read_package_field(const char *package_buffer, char **field_name, char **fie
102 if (name_length == 0) { 102 if (name_length == 0) {
103 *field_name = NULL; 103 *field_name = NULL;
104 } else { 104 } else {
105 *field_name = xstrndup(&package_buffer[offset_name_start], name_length); 105 *field_name = bb_xstrndup(&package_buffer[offset_name_start], name_length);
106 } 106 }
107 if (value_length > 0) { 107 if (value_length > 0) {
108 *field_value = xstrndup(&package_buffer[offset_value_start], value_length); 108 *field_value = bb_xstrndup(&package_buffer[offset_value_start], value_length);
109 } else { 109 } else {
110 *field_value = NULL; 110 *field_value = NULL;
111 } 111 }
diff --git a/libbb/recursive_action.c b/libbb/recursive_action.c
index 8f9cc2f91..a4a4a7be3 100644
--- a/libbb/recursive_action.c
+++ b/libbb/recursive_action.c
@@ -60,10 +60,10 @@ int recursive_action(const char *fileName,
60 60
61 if (status < 0) { 61 if (status < 0) {
62#ifdef DEBUG_RECURS_ACTION 62#ifdef DEBUG_RECURS_ACTION
63 error_msg("status=%d followLinks=%d TRUE=%d", 63 bb_error_msg("status=%d followLinks=%d TRUE=%d",
64 status, followLinks, TRUE); 64 status, followLinks, TRUE);
65#endif 65#endif
66 perror_msg("%s", fileName); 66 bb_perror_msg("%s", fileName);
67 return FALSE; 67 return FALSE;
68 } 68 }
69 69
@@ -89,14 +89,14 @@ int recursive_action(const char *fileName,
89 if (dirAction != NULL && ! depthFirst) { 89 if (dirAction != NULL && ! depthFirst) {
90 status = dirAction(fileName, &statbuf, userData); 90 status = dirAction(fileName, &statbuf, userData);
91 if (! status) { 91 if (! status) {
92 perror_msg("%s", fileName); 92 bb_perror_msg("%s", fileName);
93 return FALSE; 93 return FALSE;
94 } else if (status == SKIP) 94 } else if (status == SKIP)
95 return TRUE; 95 return TRUE;
96 } 96 }
97 dir = opendir(fileName); 97 dir = opendir(fileName);
98 if (!dir) { 98 if (!dir) {
99 perror_msg("%s", fileName); 99 bb_perror_msg("%s", fileName);
100 return FALSE; 100 return FALSE;
101 } 101 }
102 status = TRUE; 102 status = TRUE;
@@ -117,7 +117,7 @@ int recursive_action(const char *fileName,
117 closedir(dir); 117 closedir(dir);
118 if (dirAction != NULL && depthFirst) { 118 if (dirAction != NULL && depthFirst) {
119 if (! dirAction(fileName, &statbuf, userData)) { 119 if (! dirAction(fileName, &statbuf, userData)) {
120 perror_msg("%s", fileName); 120 bb_perror_msg("%s", fileName);
121 return FALSE; 121 return FALSE;
122 } 122 }
123 } 123 }
diff --git a/libbb/remove_file.c b/libbb/remove_file.c
index 988b09124..65708a252 100644
--- a/libbb/remove_file.c
+++ b/libbb/remove_file.c
@@ -37,7 +37,7 @@ extern int remove_file(const char *path, int flags)
37 37
38 if (lstat(path, &path_stat) < 0) { 38 if (lstat(path, &path_stat) < 0) {
39 if (errno != ENOENT) { 39 if (errno != ENOENT) {
40 perror_msg("unable to stat `%s'", path); 40 bb_perror_msg("unable to stat `%s'", path);
41 return -1; 41 return -1;
42 } 42 }
43 43
@@ -46,7 +46,7 @@ extern int remove_file(const char *path, int flags)
46 46
47 if (!path_exists) { 47 if (!path_exists) {
48 if (!(flags & FILEUTILS_FORCE)) { 48 if (!(flags & FILEUTILS_FORCE)) {
49 perror_msg("cannot remove `%s'", path); 49 bb_perror_msg("cannot remove `%s'", path);
50 return -1; 50 return -1;
51 } 51 }
52 return 0; 52 return 0;
@@ -58,21 +58,21 @@ extern int remove_file(const char *path, int flags)
58 int status = 0; 58 int status = 0;
59 59
60 if (!(flags & FILEUTILS_RECUR)) { 60 if (!(flags & FILEUTILS_RECUR)) {
61 error_msg("%s: is a directory", path); 61 bb_error_msg("%s: is a directory", path);
62 return -1; 62 return -1;
63 } 63 }
64 64
65 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && 65 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
66 isatty(0)) || 66 isatty(0)) ||
67 (flags & FILEUTILS_INTERACTIVE)) { 67 (flags & FILEUTILS_INTERACTIVE)) {
68 fprintf(stderr, "%s: descend into directory `%s'? ", applet_name, 68 fprintf(stderr, "%s: descend into directory `%s'? ", bb_applet_name,
69 path); 69 path);
70 if (!ask_confirmation()) 70 if (!bb_ask_confirmation())
71 return 0; 71 return 0;
72 } 72 }
73 73
74 if ((dp = opendir(path)) == NULL) { 74 if ((dp = opendir(path)) == NULL) {
75 perror_msg("unable to open `%s'", path); 75 bb_perror_msg("unable to open `%s'", path);
76 return -1; 76 return -1;
77 } 77 }
78 78
@@ -90,18 +90,18 @@ extern int remove_file(const char *path, int flags)
90 } 90 }
91 91
92 if (closedir(dp) < 0) { 92 if (closedir(dp) < 0) {
93 perror_msg("unable to close `%s'", path); 93 bb_perror_msg("unable to close `%s'", path);
94 return -1; 94 return -1;
95 } 95 }
96 96
97 if (flags & FILEUTILS_INTERACTIVE) { 97 if (flags & FILEUTILS_INTERACTIVE) {
98 fprintf(stderr, "%s: remove directory `%s'? ", applet_name, path); 98 fprintf(stderr, "%s: remove directory `%s'? ", bb_applet_name, path);
99 if (!ask_confirmation()) 99 if (!bb_ask_confirmation())
100 return status; 100 return status;
101 } 101 }
102 102
103 if (rmdir(path) < 0) { 103 if (rmdir(path) < 0) {
104 perror_msg("unable to remove `%s'", path); 104 bb_perror_msg("unable to remove `%s'", path);
105 return -1; 105 return -1;
106 } 106 }
107 107
@@ -111,13 +111,13 @@ extern int remove_file(const char *path, int flags)
111 !S_ISLNK(path_stat.st_mode) && 111 !S_ISLNK(path_stat.st_mode) &&
112 isatty(0)) || 112 isatty(0)) ||
113 (flags & FILEUTILS_INTERACTIVE)) { 113 (flags & FILEUTILS_INTERACTIVE)) {
114 fprintf(stderr, "%s: remove `%s'? ", applet_name, path); 114 fprintf(stderr, "%s: remove `%s'? ", bb_applet_name, path);
115 if (!ask_confirmation()) 115 if (!bb_ask_confirmation())
116 return 0; 116 return 0;
117 } 117 }
118 118
119 if (unlink(path) < 0) { 119 if (unlink(path) < 0) {
120 perror_msg("unable to remove `%s'", path); 120 bb_perror_msg("unable to remove `%s'", path);
121 return -1; 121 return -1;
122 } 122 }
123 123
diff --git a/libbb/run_parts.c b/libbb/run_parts.c
index 7829a84ba..58645660b 100644
--- a/libbb/run_parts.c
+++ b/libbb/run_parts.c
@@ -69,7 +69,7 @@ extern int run_parts(char **args, const unsigned char test_mode)
69 if (test_mode & 2) { 69 if (test_mode & 2) {
70 return(2); 70 return(2);
71 } 71 }
72 perror_msg_and_die("failed to open directory %s", arg0); 72 bb_perror_msg_and_die("failed to open directory %s", arg0);
73 } 73 }
74 74
75 for (i = 0; i < entries; i++) { 75 for (i = 0; i < entries; i++) {
@@ -77,7 +77,7 @@ extern int run_parts(char **args, const unsigned char test_mode)
77 filename = concat_path_file(arg0, namelist[i]->d_name); 77 filename = concat_path_file(arg0, namelist[i]->d_name);
78 78
79 if (stat(filename, &st) < 0) { 79 if (stat(filename, &st) < 0) {
80 perror_msg_and_die("failed to stat component %s", filename); 80 bb_perror_msg_and_die("failed to stat component %s", filename);
81 } 81 }
82 if (S_ISREG(st.st_mode) && !access(filename, X_OK)) { 82 if (S_ISREG(st.st_mode) && !access(filename, X_OK)) {
83 if (test_mode & 1) { 83 if (test_mode & 1) {
@@ -89,7 +89,7 @@ extern int run_parts(char **args, const unsigned char test_mode)
89 int pid; 89 int pid;
90 90
91 if ((pid = vfork()) < 0) { 91 if ((pid = vfork()) < 0) {
92 perror_msg_and_die("failed to fork"); 92 bb_perror_msg_and_die("failed to fork");
93 } else if (!pid) { 93 } else if (!pid) {
94 args[0] = filename; 94 args[0] = filename;
95 execv(filename, args); 95 execv(filename, args);
@@ -100,19 +100,19 @@ extern int run_parts(char **args, const unsigned char test_mode)
100 waitpid(pid, &result, 0); 100 waitpid(pid, &result, 0);
101 if(exec_errno) { 101 if(exec_errno) {
102 errno = exec_errno; 102 errno = exec_errno;
103 perror_msg_and_die("failed to exec %s", filename); 103 bb_perror_msg_and_die("failed to exec %s", filename);
104 } 104 }
105 if (WIFEXITED(result) && WEXITSTATUS(result)) { 105 if (WIFEXITED(result) && WEXITSTATUS(result)) {
106 perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result)); 106 bb_perror_msg("%s exited with return code %d", filename, WEXITSTATUS(result));
107 exitstatus = 1; 107 exitstatus = 1;
108 } else if (WIFSIGNALED(result)) { 108 } else if (WIFSIGNALED(result)) {
109 perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result)); 109 bb_perror_msg("%s exited because of uncaught signal %d", filename, WTERMSIG(result));
110 exitstatus = 1; 110 exitstatus = 1;
111 } 111 }
112 } 112 }
113 } 113 }
114 else if (!S_ISDIR(st.st_mode)) { 114 else if (!S_ISDIR(st.st_mode)) {
115 error_msg("component %s is not an executable plain file", filename); 115 bb_error_msg("component %s is not an executable plain file", filename);
116 exitstatus = 1; 116 exitstatus = 1;
117 } 117 }
118 118
diff --git a/libbb/run_shell.c b/libbb/run_shell.c
index b26eba115..d154b9852 100644
--- a/libbb/run_shell.c
+++ b/libbb/run_shell.c
@@ -57,11 +57,11 @@ void run_shell ( const char *shell, int loginshell, const char *command, const c
57 else 57 else
58 args = (const char **) xmalloc (sizeof (char *) * 4 ); 58 args = (const char **) xmalloc (sizeof (char *) * 4 );
59 59
60 args [0] = get_last_path_component ( xstrdup ( shell )); 60 args [0] = bb_get_last_path_component ( bb_xstrdup ( shell ));
61 61
62 if ( loginshell ) { 62 if ( loginshell ) {
63 char *args0; 63 char *args0;
64 bb_asprintf ( &args0, "-%s", args [0] ); 64 bb_xasprintf ( &args0, "-%s", args [0] );
65 args [0] = args0; 65 args [0] = args0;
66 } 66 }
67 67
@@ -75,6 +75,6 @@ void run_shell ( const char *shell, int loginshell, const char *command, const c
75 } 75 }
76 args [argno] = 0; 76 args [argno] = 0;
77 execv ( shell, (char **) args ); 77 execv ( shell, (char **) args );
78 perror_msg_and_die ( "cannot run %s", shell ); 78 bb_perror_msg_and_die ( "cannot run %s", shell );
79} 79}
80 80
diff --git a/libbb/setup_environment.c b/libbb/setup_environment.c
index dc171fa1f..30d317cea 100644
--- a/libbb/setup_environment.c
+++ b/libbb/setup_environment.c
@@ -45,7 +45,7 @@
45static void xsetenv ( const char *key, const char *value ) 45static void xsetenv ( const char *key, const char *value )
46{ 46{
47 if ( setenv ( key, value, 1 )) 47 if ( setenv ( key, value, 1 ))
48 error_msg_and_die ( "out of memory" ); 48 bb_error_msg_and_die ( "out of memory" );
49} 49}
50 50
51void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw ) 51void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw )
@@ -62,7 +62,7 @@ void setup_environment ( const char *shell, int loginshell, int changeenv, const
62 if ( chdir ( pw-> pw_dir )) { 62 if ( chdir ( pw-> pw_dir )) {
63 if ( chdir ( "/" )) { 63 if ( chdir ( "/" )) {
64 syslog ( LOG_WARNING, "unable to cd to %s' for user %s'\n", pw-> pw_dir, pw-> pw_name ); 64 syslog ( LOG_WARNING, "unable to cd to %s' for user %s'\n", pw-> pw_dir, pw-> pw_name );
65 error_msg_and_die ( "cannot cd to home directory or /" ); 65 bb_error_msg_and_die ( "cannot cd to home directory or /" );
66 } 66 }
67 fputs ( "warning: cannot change to home directory\n", stderr ); 67 fputs ( "warning: cannot change to home directory\n", stderr );
68 } 68 }
diff --git a/libbb/simplify_path.c b/libbb/simplify_path.c
index 7b2a1ca51..743133cd1 100644
--- a/libbb/simplify_path.c
+++ b/libbb/simplify_path.c
@@ -1,8 +1,8 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * simplify_path implementation for busybox 3 * bb_simplify_path implementation for busybox
4 * 4 *
5 * Copyright (C) 2001 Manuel Novoa III <mjn3@opensource.lineo.com> 5 * Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org>
6 * 6 *
7 * This program is free software; you can redistribute it and/or modify 7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 8 * it under the terms of the GNU General Public License as published by
@@ -21,15 +21,14 @@
21 */ 21 */
22 22
23#include <stdlib.h> 23#include <stdlib.h>
24
25#include "libbb.h" 24#include "libbb.h"
26 25
27char *simplify_path(const char *path) 26char *bb_simplify_path(const char *path)
28{ 27{
29 char *s, *start, *p; 28 char *s, *start, *p;
30 29
31 if (path[0] == '/') 30 if (path[0] == '/')
32 start = xstrdup(path); 31 start = bb_xstrdup(path);
33 else { 32 else {
34 s = xgetcwd(NULL); 33 s = xgetcwd(NULL);
35 start = concat_path_file(s, path); 34 start = concat_path_file(s, path);
diff --git a/libbb/skip_whitespace.c b/libbb/skip_whitespace.c
new file mode 100644
index 000000000..bf049a2d2
--- /dev/null
+++ b/libbb/skip_whitespace.c
@@ -0,0 +1,33 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * skip_whitespace implementation for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <ctype.h>
24#include "libbb.h"
25
26extern const char *bb_skip_whitespace(const char *s)
27{
28 while (isspace(*s)) {
29 ++s;
30 }
31
32 return s;
33}
diff --git a/libbb/speed_table.c b/libbb/speed_table.c
new file mode 100644
index 000000000..b04429e91
--- /dev/null
+++ b/libbb/speed_table.c
@@ -0,0 +1,130 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * compact speed_t <-> speed functions for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <termios.h>
24#include "libbb.h"
25
26struct speed_map {
27 unsigned short speed;
28 unsigned short value;
29};
30
31static const struct speed_map speeds[] = {
32 {B0, 0},
33 {B50, 50},
34 {B75, 75},
35 {B110, 110},
36 {B134, 134},
37 {B150, 150},
38 {B200, 200},
39 {B300, 300},
40 {B600, 600},
41 {B1200, 1200},
42 {B1800, 1800},
43 {B2400, 2400},
44 {B4800, 4800},
45 {B9600, 9600},
46#ifdef B19200
47 {B19200, 19200},
48#elif defined(EXTA)
49 {EXTA, 19200},
50#endif
51#ifdef B38400
52 {B38400, 38400/256 + 0x8000U},
53#elif defined(EXTB)
54 {EXTB, 38400/256 + 0x8000U},
55#endif
56#ifdef B57600
57 {B57600, 57600/256 + 0x8000U},
58#endif
59#ifdef B115200
60 {B115200, 115200/256 + 0x8000U},
61#endif
62#ifdef B230400
63 {B230400, 230400/256 + 0x8000U},
64#endif
65#ifdef B460800
66 {B460800, 460800/256 + 0x8000U},
67#endif
68};
69
70static const int NUM_SPEEDS = (sizeof(speeds) / sizeof(struct speed_map));
71
72unsigned long bb_baud_to_value(speed_t speed)
73{
74 int i = 0;
75
76 do {
77 if (speed == speeds[i].speed) {
78 if (speeds[i].value & 0x8000U) {
79 return ((unsigned long) (speeds[i].value) & 0x7fffU) * 256;
80 }
81 return speeds[i].value;
82 }
83 } while (++i < NUM_SPEEDS);
84
85 return 0;
86}
87
88speed_t bb_value_to_baud(unsigned long value)
89{
90 int i = 0;
91
92 do {
93 if (value == bb_baud_to_value(speeds[i].speed)) {
94 return speeds[i].speed;
95 }
96 } while (++i < NUM_SPEEDS);
97
98 return (speed_t) - 1;
99}
100
101#if 0
102/* testing code */
103#include <stdio.h>
104
105int main(void)
106{
107 unsigned long v;
108 speed_t s;
109
110 for (v = 0 ; v < 500000 ; v++) {
111 s = bb_value_to_baud(v);
112 if (s == (speed_t) -1) {
113 continue;
114 }
115 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
116 }
117
118 printf("-------------------------------\n");
119
120 for (s = 0 ; s < 010017+1 ; s++) {
121 v = bb_baud_to_value(s);
122 if (!v) {
123 continue;
124 }
125 printf("v = %lu -- s = %0lo\n", v, (unsigned long) s);
126 }
127
128 return 0;
129}
130#endif
diff --git a/libbb/syscalls.c b/libbb/syscalls.c
index 91e97b178..8d8c689f1 100644
--- a/libbb/syscalls.c
+++ b/libbb/syscalls.c
@@ -53,7 +53,7 @@ int pivot_root(const char * new_root,const char * put_old)
53 * you will need to recompile with a kernel supporting the 53 * you will need to recompile with a kernel supporting the
54 * pivot_root system call. 54 * pivot_root system call.
55 */ 55 */
56 error_msg("\n\nTo make this application work, you will need to recompile\n" 56 bb_error_msg("\n\nTo make this application work, you will need to recompile\n"
57 "with a kernel supporting the pivot_root system call. -Erik\n"); 57 "with a kernel supporting the pivot_root system call. -Erik\n");
58 errno=ENOSYS; 58 errno=ENOSYS;
59 return -1; 59 return -1;
@@ -91,7 +91,7 @@ int umount2(const char * special_file, int flags)
91 * you will need to recompile with a kernel supporting the 91 * you will need to recompile with a kernel supporting the
92 * umount2 system call. 92 * umount2 system call.
93 */ 93 */
94 error_msg("\n\nTo make this application work, you will need to recompile\n" 94 bb_error_msg("\n\nTo make this application work, you will need to recompile\n"
95 "with a kernel supporting the umount2 system call. -Erik\n"); 95 "with a kernel supporting the umount2 system call. -Erik\n");
96 errno=ENOSYS; 96 errno=ENOSYS;
97 return -1; 97 return -1;
diff --git a/libbb/syslog_msg_with_name.c b/libbb/syslog_msg_with_name.c
index 6474da459..bd3f44824 100644
--- a/libbb/syslog_msg_with_name.c
+++ b/libbb/syslog_msg_with_name.c
@@ -32,7 +32,7 @@ void syslog_msg_with_name(const char *name, int facility, int pri, const char *m
32 32
33void syslog_msg(int facility, int pri, const char *msg) 33void syslog_msg(int facility, int pri, const char *msg)
34{ 34{
35 syslog_msg_with_name(applet_name, facility, pri, msg); 35 syslog_msg_with_name(bb_applet_name, facility, pri, msg);
36} 36}
37 37
38/* END CODE */ 38/* END CODE */
diff --git a/libbb/verror_msg.c b/libbb/verror_msg.c
index 21cde2047..4612bf378 100644
--- a/libbb/verror_msg.c
+++ b/libbb/verror_msg.c
@@ -25,10 +25,10 @@
25#include <stdlib.h> 25#include <stdlib.h>
26#include "libbb.h" 26#include "libbb.h"
27 27
28extern void verror_msg(const char *s, va_list p) 28extern void bb_verror_msg(const char *s, va_list p)
29{ 29{
30 fflush(stdout); 30 fflush(stdout);
31 fprintf(stderr, "%s: ", applet_name); 31 fprintf(stderr, "%s: ", bb_applet_name);
32 vfprintf(stderr, s, p); 32 vfprintf(stderr, s, p);
33} 33}
34 34
diff --git a/libbb/vherror_msg.c b/libbb/vherror_msg.c
index 67db17fe4..eb341bf84 100644
--- a/libbb/vherror_msg.c
+++ b/libbb/vherror_msg.c
@@ -26,11 +26,11 @@
26#include "libbb.h" 26#include "libbb.h"
27 27
28 28
29extern void vherror_msg(const char *s, va_list p) 29extern void bb_vherror_msg(const char *s, va_list p)
30{ 30{
31 if(s == 0) 31 if(s == 0)
32 s = ""; 32 s = "";
33 verror_msg(s, p); 33 bb_verror_msg(s, p);
34 if (*s) 34 if (*s)
35 fputs(": ", stderr); 35 fputs(": ", stderr);
36 herror(""); 36 herror("");
diff --git a/libbb/vperror_msg.c b/libbb/vperror_msg.c
index 7da5bae0a..febe4e22e 100644
--- a/libbb/vperror_msg.c
+++ b/libbb/vperror_msg.c
@@ -25,11 +25,11 @@
25#include <stdlib.h> 25#include <stdlib.h>
26#include "libbb.h" 26#include "libbb.h"
27 27
28extern void vperror_msg(const char *s, va_list p) 28extern void bb_vperror_msg(const char *s, va_list p)
29{ 29{
30 int err=errno; 30 int err=errno;
31 if(s == 0) s = ""; 31 if(s == 0) s = "";
32 verror_msg(s, p); 32 bb_verror_msg(s, p);
33 if (*s) s = ": "; 33 if (*s) s = ": ";
34 fprintf(stderr, "%s%s\n", s, strerror(err)); 34 fprintf(stderr, "%s%s\n", s, strerror(err));
35} 35}
diff --git a/libbb/warn_ignoring_args.c b/libbb/warn_ignoring_args.c
new file mode 100644
index 000000000..223831fd1
--- /dev/null
+++ b/libbb/warn_ignoring_args.c
@@ -0,0 +1,30 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * warn_ingoring_args implementations for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <libbb.h>
24
25extern void bb_warn_ignoring_args(int n)
26{
27 if (n) {
28 bb_perror_msg("ignoring all arguments");
29 }
30}
diff --git a/libbb/wfopen.c b/libbb/wfopen.c
index f58ec90c0..22f22b373 100644
--- a/libbb/wfopen.c
+++ b/libbb/wfopen.c
@@ -23,11 +23,11 @@
23#include <errno.h> 23#include <errno.h>
24#include "libbb.h" 24#include "libbb.h"
25 25
26FILE *wfopen(const char *path, const char *mode) 26FILE *bb_wfopen(const char *path, const char *mode)
27{ 27{
28 FILE *fp; 28 FILE *fp;
29 if ((fp = fopen(path, mode)) == NULL) { 29 if ((fp = fopen(path, mode)) == NULL) {
30 perror_msg("%s", path); 30 bb_perror_msg("%s", path);
31 errno = 0; 31 errno = 0;
32 } 32 }
33 return fp; 33 return fp;
diff --git a/libbb/wfopen_input.c b/libbb/wfopen_input.c
new file mode 100644
index 000000000..bff6606b5
--- /dev/null
+++ b/libbb/wfopen_input.c
@@ -0,0 +1,54 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * wfopen_input implementation for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23/* A number of applets need to open a file for reading, where the filename
24 * is a command line arg. Since often that arg is '-' (meaning stdin),
25 * we avoid testing everywhere by consolidating things in this routine.
26 *
27 * Note: We also consider "" to main stdin (for 'cmp' at least).
28 */
29
30#include <stdio.h>
31#include <sys/stat.h>
32#include <libbb.h>
33
34FILE *bb_wfopen_input(const char *filename)
35{
36 FILE *fp = stdin;
37
38 if ((filename != bb_msg_standard_input)
39 && filename[0] && ((filename[0] != '-') || filename[1])
40 ) {
41#if 0
42 /* This check shouldn't be necessary for linux, but is left
43 * here disabled just in case. */
44 struct stat stat_buf;
45 if (is_directory(filename, 1, &stat_buf)) {
46 bb_error_msg("%s: Is a directory", filename);
47 return NULL;
48 }
49#endif
50 fp = bb_wfopen(filename, "r");
51 }
52
53 return fp;
54}
diff --git a/libbb/xconnect.c b/libbb/xconnect.c
index bc6505a40..2945d760f 100644
--- a/libbb/xconnect.c
+++ b/libbb/xconnect.c
@@ -30,7 +30,7 @@ int xconnect(const char *host, const char *port)
30 hints.ai_socktype = SOCK_STREAM; 30 hints.ai_socktype = SOCK_STREAM;
31 error = getaddrinfo(host, port, &hints, &res); 31 error = getaddrinfo(host, port, &hints, &res);
32 if (error||!res) 32 if (error||!res)
33 perror_msg_and_die(gai_strerror(error)); 33 bb_perror_msg_and_die(gai_strerror(error));
34 addr_info=res; 34 addr_info=res;
35 while (res) { 35 while (res) {
36 s=socket(res->ai_family, res->ai_socktype, res->ai_protocol); 36 s=socket(res->ai_family, res->ai_socktype, res->ai_protocol);
@@ -50,7 +50,7 @@ int xconnect(const char *host, const char *port)
50 freeaddrinfo(addr_info); 50 freeaddrinfo(addr_info);
51 if (error < 0) 51 if (error < 0)
52 { 52 {
53 perror_msg_and_die("Unable to connect to remote host (%s)", host); 53 bb_perror_msg_and_die("Unable to connect to remote host (%s)", host);
54 } 54 }
55 return s; 55 return s;
56#else 56#else
@@ -72,7 +72,7 @@ int xconnect(const char *host, const char *port)
72 72
73 if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0) 73 if (connect(s, (struct sockaddr *)&s_addr, sizeof s_addr) < 0)
74 { 74 {
75 perror_msg_and_die("Unable to connect to remote host (%s)", host); 75 bb_perror_msg_and_die("Unable to connect to remote host (%s)", host);
76 } 76 }
77 return s; 77 return s;
78#endif 78#endif
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index 820a0d7cc..43e8aef0c 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -30,31 +30,38 @@
30 30
31 31
32#ifndef DMALLOC 32#ifndef DMALLOC
33#ifdef L_xmalloc
33extern void *xmalloc(size_t size) 34extern void *xmalloc(size_t size)
34{ 35{
35 void *ptr = malloc(size); 36 void *ptr = malloc(size);
36 if (ptr == NULL && size != 0) 37 if (ptr == NULL && size != 0)
37 error_msg_and_die(memory_exhausted); 38 bb_error_msg_and_die(bb_msg_memory_exhausted);
38 return ptr; 39 return ptr;
39} 40}
41#endif
40 42
43#ifdef L_xrealloc
41extern void *xrealloc(void *ptr, size_t size) 44extern void *xrealloc(void *ptr, size_t size)
42{ 45{
43 ptr = realloc(ptr, size); 46 ptr = realloc(ptr, size);
44 if (ptr == NULL && size != 0) 47 if (ptr == NULL && size != 0)
45 error_msg_and_die(memory_exhausted); 48 bb_error_msg_and_die(bb_msg_memory_exhausted);
46 return ptr; 49 return ptr;
47} 50}
51#endif
48 52
53#ifdef L_xcalloc
49extern void *xcalloc(size_t nmemb, size_t size) 54extern void *xcalloc(size_t nmemb, size_t size)
50{ 55{
51 void *ptr = calloc(nmemb, size); 56 void *ptr = calloc(nmemb, size);
52 if (ptr == NULL && nmemb != 0 && size != 0) 57 if (ptr == NULL && nmemb != 0 && size != 0)
53 error_msg_and_die(memory_exhausted); 58 bb_error_msg_and_die(bb_msg_memory_exhausted);
54 return ptr; 59 return ptr;
55} 60}
61#endif
56 62
57extern char * xstrdup (const char *s) { 63#ifdef L_xstrdup
64extern char * bb_xstrdup (const char *s) {
58 char *t; 65 char *t;
59 66
60 if (s == NULL) 67 if (s == NULL)
@@ -63,79 +70,121 @@ extern char * xstrdup (const char *s) {
63 t = strdup (s); 70 t = strdup (s);
64 71
65 if (t == NULL) 72 if (t == NULL)
66 error_msg_and_die(memory_exhausted); 73 bb_error_msg_and_die(bb_msg_memory_exhausted);
67 74
68 return t; 75 return t;
69} 76}
70#endif 77#endif
78#endif /* DMALLOC */
71 79
72extern char * xstrndup (const char *s, int n) { 80#ifdef L_xstrndup
81extern char * bb_xstrndup (const char *s, int n) {
73 char *t; 82 char *t;
74 83
75 if (s == NULL) 84 if (s == NULL)
76 error_msg_and_die("xstrndup bug"); 85 bb_error_msg_and_die("bb_xstrndup bug");
77 86
78 t = xmalloc(++n); 87 t = xmalloc(++n);
79 88
80 return safe_strncpy(t,s,n); 89 return safe_strncpy(t,s,n);
81} 90}
91#endif
82 92
83FILE *xfopen(const char *path, const char *mode) 93#ifdef L_xfopen
94FILE *bb_xfopen(const char *path, const char *mode)
84{ 95{
85 FILE *fp; 96 FILE *fp;
86 if ((fp = fopen(path, mode)) == NULL) 97 if ((fp = fopen(path, mode)) == NULL)
87 perror_msg_and_die("%s", path); 98 bb_perror_msg_and_die("%s", path);
88 return fp; 99 return fp;
89} 100}
101#endif
90 102
91extern int xopen(const char *pathname, int flags) 103#ifdef L_xopen
104extern int bb_xopen(const char *pathname, int flags)
92{ 105{
93 int ret; 106 int ret;
94 107
95 ret = open(pathname, flags, 0777); 108 ret = open(pathname, flags, 0777);
96 if (ret == -1) { 109 if (ret == -1) {
97 perror_msg_and_die("%s", pathname); 110 bb_perror_msg_and_die("%s", pathname);
98 } 111 }
99 return ret; 112 return ret;
100} 113}
114#endif
101 115
102extern ssize_t xread(int fd, void *buf, size_t count) 116#ifdef L_xread
117extern ssize_t bb_xread(int fd, void *buf, size_t count)
103{ 118{
104 ssize_t size; 119 ssize_t size;
105 120
106 size = read(fd, buf, count); 121 size = read(fd, buf, count);
107 if (size == -1) { 122 if (size == -1) {
108 perror_msg_and_die("Read error"); 123 bb_perror_msg_and_die("Read error");
109 } 124 }
110 return(size); 125 return(size);
111} 126}
127#endif
112 128
113extern void xread_all(int fd, void *buf, size_t count) 129#ifdef L_xread_all
130extern void bb_xread_all(int fd, void *buf, size_t count)
114{ 131{
115 ssize_t size; 132 ssize_t size;
116 133
117 size = xread(fd, buf, count); 134 while (count) {
118 if (size != count) { 135 if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */
119 error_msg_and_die("Short read"); 136 bb_error_msg_and_die("Short read");
137 }
138 count -= size;
120 } 139 }
121 return; 140 return;
122} 141}
142#endif
123 143
124extern unsigned char xread_char(int fd) 144#ifdef L_xread_char
145extern unsigned char bb_xread_char(int fd)
125{ 146{
126 char tmp; 147 char tmp;
127 148
128 xread_all(fd, &tmp, 1); 149 bb_xread_all(fd, &tmp, 1);
129 150
130 return(tmp); 151 return(tmp);
131} 152}
153#endif
154
155#ifdef L_xferror
156extern void bb_xferror(FILE *fp, const char *fn)
157{
158 if (ferror(fp)) {
159 bb_error_msg_and_die("%s", fn);
160 }
161}
162#endif
163
164#ifdef L_xferror_stdout
165extern void bb_xferror_stdout(void)
166{
167 bb_xferror(stdout, bb_msg_standard_output);
168}
169#endif
170
171#ifdef L_xfflush_stdout
172extern void bb_xfflush_stdout(void)
173{
174 if (fflush(stdout)) {
175 bb_perror_msg_and_die(bb_msg_standard_output);
176 }
177}
178#endif
132 179
180#ifdef L_strlen
133/* Stupid gcc always includes its own builtin strlen()... */ 181/* Stupid gcc always includes its own builtin strlen()... */
134#undef strlen 182#undef strlen
135size_t xstrlen(const char *string) 183size_t bb_strlen(const char *string)
136{ 184{
137 return(strlen(string)); 185 return(strlen(string));
138} 186}
187#endif
139 188
140/* END CODE */ 189/* END CODE */
141/* 190/*
diff --git a/libbb/xgetcwd.c b/libbb/xgetcwd.c
index 54e9785ed..85a5c4125 100644
--- a/libbb/xgetcwd.c
+++ b/libbb/xgetcwd.c
@@ -40,7 +40,7 @@ xgetcwd (char *cwd)
40 40
41 if (ret == NULL) { 41 if (ret == NULL) {
42 free (cwd); 42 free (cwd);
43 perror_msg("getcwd()"); 43 bb_perror_msg("getcwd()");
44 return NULL; 44 return NULL;
45 } 45 }
46 46
diff --git a/libbb/xgethostbyname.c b/libbb/xgethostbyname.c
index b71979701..6b2dff711 100644
--- a/libbb/xgethostbyname.c
+++ b/libbb/xgethostbyname.c
@@ -29,7 +29,7 @@ struct hostent *xgethostbyname(const char *name)
29 struct hostent *retval; 29 struct hostent *retval;
30 30
31 if ((retval = gethostbyname(name)) == NULL) 31 if ((retval = gethostbyname(name)) == NULL)
32 herror_msg_and_die("%s", name); 32 bb_herror_msg_and_die("%s", name);
33 33
34 return retval; 34 return retval;
35} 35}
diff --git a/libbb/xgethostbyname2.c b/libbb/xgethostbyname2.c
index f4cbb6a56..3a16ae4dc 100644
--- a/libbb/xgethostbyname2.c
+++ b/libbb/xgethostbyname2.c
@@ -30,7 +30,7 @@ struct hostent *xgethostbyname2(const char *name, int af)
30 struct hostent *retval; 30 struct hostent *retval;
31 31
32 if ((retval = gethostbyname2(name, af)) == NULL) 32 if ((retval = gethostbyname2(name, af)) == NULL)
33 herror_msg_and_die("%s", name); 33 bb_herror_msg_and_die("%s", name);
34 34
35 return retval; 35 return retval;
36} 36}
diff --git a/libbb/xgetlarg.c b/libbb/xgetlarg.c
index 598b0b3d6..06e776dc9 100644
--- a/libbb/xgetlarg.c
+++ b/libbb/xgetlarg.c
@@ -9,6 +9,7 @@
9#include <getopt.h> 9#include <getopt.h>
10#include <errno.h> 10#include <errno.h>
11#include <assert.h> 11#include <assert.h>
12#include <ctype.h>
12 13
13#include "busybox.h" 14#include "busybox.h"
14 15
@@ -19,10 +20,16 @@ extern long bb_xgetlarg(char *arg, int base, long lower, long upper)
19 int errno_save = errno; 20 int errno_save = errno;
20 21
21 assert(arg!=NULL); 22 assert(arg!=NULL);
23
24 /* Don't allow leading whitespace. */
25 if ((isspace)(*arg)) { /* Use an actual funciton call for minimal size. */
26 bb_show_usage();
27 }
28
22 errno = 0; 29 errno = 0;
23 result = strtol(arg, &endptr, base); 30 result = strtol(arg, &endptr, base);
24 if (errno != 0 || *endptr!='\0' || result < lower || result > upper) 31 if (errno != 0 || *endptr!='\0' || endptr==arg || result < lower || result > upper)
25 show_usage(); 32 bb_show_usage();
26 errno = errno_save; 33 errno = errno_save;
27 return result; 34 return result;
28} 35}
diff --git a/libbb/xgetularg.c b/libbb/xgetularg.c
new file mode 100644
index 000000000..d743520c3
--- /dev/null
+++ b/libbb/xgetularg.c
@@ -0,0 +1,160 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * xgetularg* implementations for busybox
4 *
5 * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include <stdlib.h>
24#include <string.h>
25#include <limits.h>
26#include <ctype.h>
27#include <errno.h>
28#include <assert.h>
29#include "libbb.h"
30
31#ifdef L_xgetularg_bnd_sfx
32extern
33unsigned long bb_xgetularg_bnd_sfx(const char *arg, int base,
34 unsigned long lower,
35 unsigned long upper,
36 const struct suffix_mult *suffixes)
37{
38 unsigned long r;
39 int old_errno;
40 char *e;
41
42 assert(arg);
43
44 /* Disallow '-' and any leading whitespace. Speed isn't critical here
45 * since we're parsing commandline args. So make sure we get the
46 * actual isspace function rather than a larger macro implementaion. */
47 if ((*arg == '-') || (isspace)(*arg)) {
48 bb_show_usage();
49 }
50
51 /* Since this is a lib function, we're not allowed to reset errno to 0.
52 * Doing so could break an app that is deferring checking of errno.
53 * So, save the old value so that we can restore it if successful. */
54 old_errno = errno;
55 errno = 0;
56 r = strtoul(arg, &e, base);
57 /* Do the initial validity check. Note: The standards do not
58 * guarantee that errno is set if no digits were found. So we
59 * must test for this explicitly. */
60 if (errno || (arg == e)) { /* error or no digits */
61 bb_show_usage();
62 }
63 errno = old_errno; /* Ok. So restore errno. */
64
65 /* Do optional suffix parsing. Allow 'empty' suffix tables.
66 * Note that we also all nul suffixes with associated multipliers,
67 * to allow for scaling of the arg by some default multiplier. */
68
69 if (suffixes) {
70 while (suffixes->suffix) {
71 if (strcmp(suffixes->suffix, e) == 0) {
72 if (ULONG_MAX / suffixes->mult < r) { /* Overflow! */
73 bb_show_usage();
74 }
75 ++e;
76 r *= suffixes->mult;
77 break;
78 }
79 ++suffixes;
80 }
81 }
82
83 /* Finally, check for illegal trailing chars and range limits. */
84 /* Note: although we allow leading space (via stroul), trailing space
85 * is an error. It would be easy enough to allow though if desired. */
86 if (*e || (r < lower) || (r > upper)) {
87 bb_show_usage();
88 }
89
90 return r;
91}
92#endif
93
94#ifdef L_xgetlarg_bnd_sfx
95extern
96long bb_xgetlarg_bnd_sfx(const char *arg, int base,
97 long lower,
98 long upper,
99 const struct suffix_mult *suffixes)
100{
101 unsigned long u = LONG_MAX;
102 long r;
103 const char *p = arg;
104
105 if ((*p == '-') && (p[1] != '+')) {
106 ++p;
107#if LONG_MAX == (-(LONG_MIN + 1))
108 ++u; /* two's complement */
109#endif
110 }
111
112 r = bb_xgetularg_bnd_sfx(p, base, 0, u, suffixes);
113
114 if (*arg == '-') {
115 r = -r;
116 }
117
118 if ((r < lower) || (r > upper)) {
119 bb_show_usage();
120 }
121
122 return r;
123}
124#endif
125
126#ifdef L_getlarg10_sfx
127extern
128long bb_xgetlarg10_sfx(const char *arg, const struct suffix_mult *suffixes)
129{
130 return bb_xgetlarg_bnd_sfx(arg, 10, LONG_MIN, LONG_MAX, suffixes);
131}
132#endif
133
134#ifdef L_xgetularg_bnd
135extern
136unsigned long bb_xgetularg_bnd(const char *arg, int base,
137 unsigned long lower,
138 unsigned long upper)
139{
140 return bb_xgetularg_bnd_sfx(arg, base, lower, upper, NULL);
141}
142#endif
143
144#ifdef L_xgetularg10_bnd
145extern
146unsigned long bb_xgetularg10_bnd(const char *arg,
147 unsigned long lower,
148 unsigned long upper)
149{
150 return bb_xgetularg_bnd(arg, 10, lower, upper);
151}
152#endif
153
154#ifdef L_xgetularg10
155extern
156unsigned long bb_xgetularg10(const char *arg)
157{
158 return bb_xgetularg10_bnd(arg, 0, ULONG_MAX);
159}
160#endif
diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c
index 9944b5129..b8cfe617a 100644
--- a/libbb/xreadlink.c
+++ b/libbb/xreadlink.c
@@ -24,7 +24,7 @@ extern char *xreadlink(const char *path)
24 buf = xrealloc(buf, bufsize += GROWBY); 24 buf = xrealloc(buf, bufsize += GROWBY);
25 readsize = readlink(path, buf, bufsize); /* 1st try */ 25 readsize = readlink(path, buf, bufsize); /* 1st try */
26 if (readsize == -1) { 26 if (readsize == -1) {
27 perror_msg("%s", path); 27 bb_perror_msg("%s", path);
28 return NULL; 28 return NULL;
29 } 29 }
30 } 30 }
diff --git a/libbb/xregcomp.c b/libbb/xregcomp.c
index 07cf779d1..56746ac53 100644
--- a/libbb/xregcomp.c
+++ b/libbb/xregcomp.c
@@ -34,7 +34,7 @@ void xregcomp(regex_t *preg, const char *regex, int cflags)
34 int errmsgsz = regerror(ret, preg, NULL, 0); 34 int errmsgsz = regerror(ret, preg, NULL, 0);
35 char *errmsg = xmalloc(errmsgsz); 35 char *errmsg = xmalloc(errmsgsz);
36 regerror(ret, preg, errmsg, errmsgsz); 36 regerror(ret, preg, errmsg, errmsgsz);
37 error_msg_and_die("xregcomp: %s", errmsg); 37 bb_error_msg_and_die("xregcomp: %s", errmsg);
38 } 38 }
39} 39}
40 40
diff --git a/libpwdgrp/shadow.c b/libpwdgrp/shadow.c
index 2d2b108f2..f3d624213 100644
--- a/libpwdgrp/shadow.c
+++ b/libpwdgrp/shadow.c
@@ -55,7 +55,7 @@ void setspent(void)
55 rewind(shadow); 55 rewind(shadow);
56 } else { 56 } else {
57 if ((shadow = fopen("/etc/shadow", "r")) == NULL) 57 if ((shadow = fopen("/etc/shadow", "r")) == NULL)
58 perror_msg_and_die("/etc/shadow"); 58 bb_perror_msg_and_die("/etc/shadow");
59 } 59 }
60} 60}
61 61
diff --git a/loginutils/addgroup.c b/loginutils/addgroup.c
index 1d754af8e..2e8188dec 100644
--- a/loginutils/addgroup.c
+++ b/loginutils/addgroup.c
@@ -54,16 +54,16 @@ static int group_study(const char *filename, struct group *g)
54 struct group *grp; 54 struct group *grp;
55 const int max = 65000; 55 const int max = 65000;
56 56
57 etc_group = xfopen(filename, "r"); 57 etc_group = bb_xfopen(filename, "r");
58 58
59 /* make sure gr_name isn't taken, make sure gid is kosher */ 59 /* make sure gr_name isn't taken, make sure gid is kosher */
60 desired = g->gr_gid; 60 desired = g->gr_gid;
61 while ((grp = fgetgrent(etc_group))) { 61 while ((grp = fgetgrent(etc_group))) {
62 if ((strcmp(grp->gr_name, g->gr_name)) == 0) { 62 if ((strcmp(grp->gr_name, g->gr_name)) == 0) {
63 error_msg_and_die("%s: group already in use\n", g->gr_name); 63 bb_error_msg_and_die("%s: group already in use\n", g->gr_name);
64 } 64 }
65 if ((desired) && grp->gr_gid == desired) { 65 if ((desired) && grp->gr_gid == desired) {
66 error_msg_and_die("%d: gid has already been allocated\n", 66 bb_error_msg_and_die("%d: gid has already been allocated\n",
67 desired); 67 desired);
68 } 68 }
69 if ((grp->gr_gid > g->gr_gid) && (grp->gr_gid < max)) { 69 if ((grp->gr_gid > g->gr_gid) && (grp->gr_gid < max)) {
@@ -89,7 +89,6 @@ static int addgroup(const char *filename, char *group, gid_t gid, const char *us
89 89
90#ifdef CONFIG_FEATURE_SHADOWPASSWDS 90#ifdef CONFIG_FEATURE_SHADOWPASSWDS
91 FILE *etc_gshadow; 91 FILE *etc_gshadow;
92 const char *gshadow = gshadow_file;
93#endif 92#endif
94 93
95 struct group gr; 94 struct group gr;
@@ -104,7 +103,7 @@ static int addgroup(const char *filename, char *group, gid_t gid, const char *us
104 return 1; 103 return 1;
105 104
106 /* add entry to group */ 105 /* add entry to group */
107 etc_group = xfopen(filename, "a"); 106 etc_group = bb_xfopen(filename, "a");
108 107
109 fprintf(etc_group, entryfmt, group, default_passwd, gr.gr_gid, user); 108 fprintf(etc_group, entryfmt, group, default_passwd, gr.gr_gid, user);
110 fclose(etc_group); 109 fclose(etc_group);
@@ -112,8 +111,8 @@ static int addgroup(const char *filename, char *group, gid_t gid, const char *us
112 111
113#ifdef CONFIG_FEATURE_SHADOWPASSWDS 112#ifdef CONFIG_FEATURE_SHADOWPASSWDS
114 /* add entry to gshadow if necessary */ 113 /* add entry to gshadow if necessary */
115 if (access(gshadow, F_OK|W_OK) == 0) { 114 if (access(bb_path_gshadow_file, F_OK|W_OK) == 0) {
116 etc_gshadow = xfopen(gshadow, "a"); 115 etc_gshadow = bb_xfopen(bb_path_gshadow_file, "a");
117 fprintf(etc_gshadow, "%s:!::\n", group); 116 fprintf(etc_gshadow, "%s:!::\n", group);
118 fclose(etc_gshadow); 117 fclose(etc_gshadow);
119 } 118 }
@@ -144,7 +143,7 @@ int addgroup_main(int argc, char **argv)
144 gid = strtol(optarg, NULL, 10); 143 gid = strtol(optarg, NULL, 10);
145 break; 144 break;
146 default: 145 default:
147 show_usage(); 146 bb_show_usage();
148 break; 147 break;
149 } 148 }
150 } 149 }
@@ -153,7 +152,7 @@ int addgroup_main(int argc, char **argv)
153 group = argv[optind]; 152 group = argv[optind];
154 optind++; 153 optind++;
155 } else { 154 } else {
156 show_usage(); 155 bb_show_usage();
157 } 156 }
158 157
159 if (optind < argc) { 158 if (optind < argc) {
@@ -164,12 +163,12 @@ int addgroup_main(int argc, char **argv)
164 } 163 }
165 164
166 if (geteuid() != 0) { 165 if (geteuid() != 0) {
167 error_msg_and_die 166 bb_error_msg_and_die
168 ("Only root may add a group to the system."); 167 ("Only root may add a group to the system.");
169 } 168 }
170 169
171 /* werk */ 170 /* werk */
172 return addgroup(group_file, group, gid, user); 171 return addgroup(bb_path_group_file, group, gid, user);
173} 172}
174 173
175/* $Id: addgroup.c,v 1.9 2003/01/09 18:53:53 andersen Exp $ */ 174/* $Id: addgroup.c,v 1.10 2003/03/19 09:12:20 mjn3 Exp $ */
diff --git a/loginutils/adduser.c b/loginutils/adduser.c
index 3485611cc..cfaf860e0 100644
--- a/loginutils/adduser.c
+++ b/loginutils/adduser.c
@@ -66,7 +66,7 @@ static int passwd_study(const char *filename, struct passwd *p)
66 const int min = 500; 66 const int min = 500;
67 const int max = 65000; 67 const int max = 65000;
68 68
69 passwd = wfopen(filename, "r"); 69 passwd = bb_wfopen(filename, "r");
70 if (!passwd) 70 if (!passwd)
71 return 4; 71 return 4;
72 72
@@ -112,7 +112,7 @@ static void addgroup_wrapper(const char *login, gid_t gid)
112{ 112{
113 char *cmd; 113 char *cmd;
114 114
115 bb_asprintf(&cmd, "addgroup -g %d %s", gid, login); 115 bb_xasprintf(&cmd, "addgroup -g %d %s", gid, login);
116 system(cmd); 116 system(cmd);
117 free(cmd); 117 free(cmd);
118} 118}
@@ -123,7 +123,7 @@ static void passwd_wrapper(const char *login)
123{ 123{
124 static const char prog[] = "passwd"; 124 static const char prog[] = "passwd";
125 execlp(prog, prog, login, NULL); 125 execlp(prog, prog, login, NULL);
126 error_msg_and_die("Failed to execute '%s', you must set the password for '%s' manually", prog, login); 126 bb_error_msg_and_die("Failed to execute '%s', you must set the password for '%s' manually", prog, login);
127} 127}
128 128
129/* putpwent(3) remix */ 129/* putpwent(3) remix */
@@ -137,7 +137,7 @@ static int adduser(const char *filename, struct passwd *p)
137#endif 137#endif
138 138
139 /* make sure everything is kosher and setup uid && gid */ 139 /* make sure everything is kosher and setup uid && gid */
140 passwd = wfopen(filename, "a"); 140 passwd = bb_wfopen(filename, "a");
141 if (passwd == NULL) { 141 if (passwd == NULL) {
142 return 1; 142 return 1;
143 } 143 }
@@ -147,13 +147,13 @@ static int adduser(const char *filename, struct passwd *p)
147 r = passwd_study(filename, p); 147 r = passwd_study(filename, p);
148 if (r) { 148 if (r) {
149 if (r == 1) 149 if (r == 1)
150 error_msg("%s: login already in use", p->pw_name); 150 bb_error_msg("%s: login already in use", p->pw_name);
151 else if (r == 2) 151 else if (r == 2)
152 error_msg("illegal uid or no uids left"); 152 bb_error_msg("illegal uid or no uids left");
153 else if (r == 3) 153 else if (r == 3)
154 error_msg("group name %s already in use", p->pw_name); 154 bb_error_msg("group name %s already in use", p->pw_name);
155 else 155 else
156 error_msg("generic error."); 156 bb_error_msg("generic error.");
157 return 1; 157 return 1;
158 } 158 }
159 159
@@ -166,7 +166,7 @@ static int adduser(const char *filename, struct passwd *p)
166#ifdef CONFIG_FEATURE_SHADOWPASSWDS 166#ifdef CONFIG_FEATURE_SHADOWPASSWDS
167 /* add to shadow if necessary */ 167 /* add to shadow if necessary */
168 if (shadow_enabled) { 168 if (shadow_enabled) {
169 shadow = wfopen(shadow_file, "a"); 169 shadow = bb_wfopen(bb_path_shadow_file, "a");
170 if (shadow == NULL) { 170 if (shadow == NULL) {
171 return 1; 171 return 1;
172 } 172 }
@@ -191,16 +191,16 @@ static int adduser(const char *filename, struct passwd *p)
191 191
192 /* mkdir */ 192 /* mkdir */
193 if (mkdir(p->pw_dir, 0755)) { 193 if (mkdir(p->pw_dir, 0755)) {
194 perror_msg("%s", p->pw_dir); 194 bb_perror_msg("%s", p->pw_dir);
195 } 195 }
196 /* Set the owner and group so it is owned by the new user. */ 196 /* Set the owner and group so it is owned by the new user. */
197 if (chown(p->pw_dir, p->pw_uid, p->pw_gid)) { 197 if (chown(p->pw_dir, p->pw_uid, p->pw_gid)) {
198 perror_msg("%s", p->pw_dir); 198 bb_perror_msg("%s", p->pw_dir);
199 } 199 }
200 /* Now fix up the permissions to 2755. Can't do it before now 200 /* Now fix up the permissions to 2755. Can't do it before now
201 * since chown will clear the setgid bit */ 201 * since chown will clear the setgid bit */
202 if (chmod(p->pw_dir, 02755)) { 202 if (chmod(p->pw_dir, 02755)) {
203 perror_msg("%s", p->pw_dir); 203 bb_perror_msg("%s", p->pw_dir);
204 } 204 }
205 /* interactively set passwd */ 205 /* interactively set passwd */
206 passwd_wrapper(p->pw_name); 206 passwd_wrapper(p->pw_name);
@@ -234,7 +234,7 @@ int adduser_main(int argc, char **argv)
234 234
235 /* init */ 235 /* init */
236 if (argc < 2) { 236 if (argc < 2) {
237 show_usage(); 237 bb_show_usage();
238 } 238 }
239 gecos = default_gecos; 239 gecos = default_gecos;
240 shell = default_shell; 240 shell = default_shell;
@@ -252,18 +252,18 @@ int adduser_main(int argc, char **argv)
252 shell = optarg; 252 shell = optarg;
253 break; 253 break;
254 default: 254 default:
255 show_usage (); 255 bb_show_usage();
256 break; 256 break;
257 } 257 }
258 258
259 /* got root? */ 259 /* got root? */
260 if (i_am_not_root()) { 260 if (i_am_not_root()) {
261 error_msg_and_die( "Only root may add a user or group to the system."); 261 bb_error_msg_and_die( "Only root may add a user or group to the system.");
262 } 262 }
263 263
264 /* get login */ 264 /* get login */
265 if (optind >= argc) { 265 if (optind >= argc) {
266 error_msg_and_die( "no user specified"); 266 bb_error_msg_and_die( "no user specified");
267 } 267 }
268 login = argv[optind]; 268 login = argv[optind];
269 269
@@ -273,7 +273,7 @@ int adduser_main(int argc, char **argv)
273 } 273 }
274#ifdef CONFIG_FEATURE_SHADOWPASSWDS 274#ifdef CONFIG_FEATURE_SHADOWPASSWDS
275 /* is /etc/shadow in use? */ 275 /* is /etc/shadow in use? */
276 shadow_enabled = (0 == access(shadow_file, F_OK)); 276 shadow_enabled = (0 == access(bb_path_shadow_file, F_OK));
277#endif 277#endif
278 278
279 /* create a passwd struct */ 279 /* create a passwd struct */
@@ -286,7 +286,7 @@ int adduser_main(int argc, char **argv)
286 pw.pw_shell = (char *)shell; 286 pw.pw_shell = (char *)shell;
287 287
288 /* grand finale */ 288 /* grand finale */
289 return adduser(passwd_file, &pw); 289 return adduser(bb_path_passwd_file, &pw);
290} 290}
291 291
292/* $Id: adduser.c,v 1.4 2002/09/16 06:22:24 andersen Exp $ */ 292/* $Id: adduser.c,v 1.5 2003/03/19 09:12:20 mjn3 Exp $ */
diff --git a/loginutils/deluser.c b/loginutils/deluser.c
index c7d6ece64..ad62d41e0 100644
--- a/loginutils/deluser.c
+++ b/loginutils/deluser.c
@@ -116,19 +116,19 @@ int delgroup_main(int argc, char **argv)
116 int failure; 116 int failure;
117 117
118 if (argc != 2) { 118 if (argc != 2) {
119 show_usage(); 119 bb_show_usage();
120 } else { 120 } else {
121 121
122 failure = del_line_matching(argv[1], group_file); 122 failure = del_line_matching(argv[1], bb_path_group_file);
123#ifdef CONFIG_FEATURE_SHADOWPASSWDS 123#ifdef CONFIG_FEATURE_SHADOWPASSWDS
124 if (access(gshadow_file, W_OK) == 0) { 124 if (access(bb_path_gshadow_file, W_OK) == 0) {
125 /* EDR the |= works if the error is not 0, so he had it wrong */ 125 /* EDR the |= works if the error is not 0, so he had it wrong */
126 failure |= del_line_matching(argv[1], gshadow_file); 126 failure |= del_line_matching(argv[1], bb_path_gshadow_file);
127 } 127 }
128#endif /* CONFIG_FEATURE_SHADOWPASSWDS */ 128#endif /* CONFIG_FEATURE_SHADOWPASSWDS */
129 /* if (!successful) { */ 129 /* if (!successful) { */
130 if (failure) { 130 if (failure) {
131 error_msg_and_die("%s: Group could not be removed\n", argv[1]); 131 bb_error_msg_and_die("%s: Group could not be removed\n", argv[1]);
132 } 132 }
133 133
134 } 134 }
@@ -142,38 +142,38 @@ int deluser_main(int argc, char **argv)
142 int failure; 142 int failure;
143 143
144 if (argc != 2) { 144 if (argc != 2) {
145 show_usage(); 145 bb_show_usage();
146 } else { 146 } else {
147 147
148 failure = del_line_matching(argv[1], passwd_file); 148 failure = del_line_matching(argv[1], bb_path_passwd_file);
149 /* if (!successful) { */ 149 /* if (!successful) { */
150 if (failure) { 150 if (failure) {
151 error_msg_and_die("%s: User could not be removed from %s\n", 151 bb_error_msg_and_die("%s: User could not be removed from %s\n",
152 argv[1], passwd_file); 152 argv[1], bb_path_passwd_file);
153 } 153 }
154#ifdef CONFIG_FEATURE_SHADOWPASSWDS 154#ifdef CONFIG_FEATURE_SHADOWPASSWDS
155 failure = del_line_matching(argv[1], shadow_file); 155 failure = del_line_matching(argv[1], bb_path_shadow_file);
156 /* if (!successful) { */ 156 /* if (!successful) { */
157 if (failure) { 157 if (failure) {
158 error_msg_and_die("%s: User could not be removed from %s\n", 158 bb_error_msg_and_die("%s: User could not be removed from %s\n",
159 argv[1], shadow_file); 159 argv[1], bb_path_shadow_file);
160 } 160 }
161 failure = del_line_matching(argv[1], gshadow_file); 161 failure = del_line_matching(argv[1], bb_path_gshadow_file);
162 /* if (!successful) { */ 162 /* if (!successful) { */
163 if (failure) { 163 if (failure) {
164 error_msg_and_die("%s: User could not be removed from %s\n", 164 bb_error_msg_and_die("%s: User could not be removed from %s\n",
165 argv[1], gshadow_file); 165 argv[1], bb_path_gshadow_file);
166 } 166 }
167#endif /* CONFIG_FEATURE_SHADOWPASSWDS */ 167#endif /* CONFIG_FEATURE_SHADOWPASSWDS */
168 failure = del_line_matching(argv[1], group_file); 168 failure = del_line_matching(argv[1], bb_path_group_file);
169 /* if (!successful) { */ 169 /* if (!successful) { */
170 if (failure) { 170 if (failure) {
171 error_msg_and_die("%s: User could not be removed from %s\n", 171 bb_error_msg_and_die("%s: User could not be removed from %s\n",
172 argv[1], group_file); 172 argv[1], bb_path_group_file);
173 } 173 }
174 174
175 } 175 }
176 return (EXIT_SUCCESS); 176 return (EXIT_SUCCESS);
177} 177}
178 178
179/* $Id: deluser.c,v 1.2 2002/06/23 04:24:24 andersen Exp $ */ 179/* $Id: deluser.c,v 1.3 2003/03/19 09:12:20 mjn3 Exp $ */
diff --git a/loginutils/getty.c b/loginutils/getty.c
index b8be8b603..4219ff821 100644
--- a/loginutils/getty.c
+++ b/loginutils/getty.c
@@ -169,6 +169,7 @@ struct chardata init_chardata = {
169 0, /* no capslock */ 169 0, /* no capslock */
170}; 170};
171 171
172#if 0
172struct Speedtab { 173struct Speedtab {
173 long speed; 174 long speed;
174 int code; 175 int code;
@@ -211,6 +212,7 @@ static struct Speedtab speedtab[] = {
211#endif 212#endif
212 {0, 0}, 213 {0, 0},
213}; 214};
215#endif
214 216
215static void parse_args(int argc, char **argv, struct options *op); 217static void parse_args(int argc, char **argv, struct options *op);
216static void parse_speeds(struct options *op, char *arg); 218static void parse_speeds(struct options *op, char *arg);
@@ -263,7 +265,7 @@ int getty_main(int argc, char **argv)
263 }; 265 };
264 266
265#ifdef DEBUGGING 267#ifdef DEBUGGING
266 dbf = xfopen(DEBUGTERM, "w"); 268 dbf = bb_xfopen(DEBUGTERM, "w");
267 269
268 { 270 {
269 int i; 271 int i;
@@ -383,7 +385,7 @@ static void parse_args(int argc, char **argv, struct options *op)
383 switch (c) { 385 switch (c) {
384 case 'I': 386 case 'I':
385 if (!(op->initstring = strdup(optarg))) 387 if (!(op->initstring = strdup(optarg)))
386 error(memory_exhausted); 388 error(bb_msg_memory_exhausted);
387 389
388 { 390 {
389 const char *p; 391 const char *p;
@@ -396,7 +398,7 @@ static void parse_args(int argc, char **argv, struct options *op)
396 while (*p) { 398 while (*p) {
397 if (*p == '\\') { 399 if (*p == '\\') {
398 p++; 400 p++;
399 *q++ = process_escape_sequence(&p); 401 *q++ = bb_process_escape_sequence(&p);
400 } else { 402 } else {
401 *q++ = *p++; 403 *q++ = *p++;
402 } 404 }
@@ -439,12 +441,12 @@ static void parse_args(int argc, char **argv, struct options *op)
439 op->flags |= F_WAITCRLF; 441 op->flags |= F_WAITCRLF;
440 break; 442 break;
441 default: 443 default:
442 show_usage(); 444 bb_show_usage();
443 } 445 }
444 } 446 }
445 debug("after getopt loop\n"); 447 debug("after getopt loop\n");
446 if (argc < optind + 2) /* check parameter count */ 448 if (argc < optind + 2) /* check parameter count */
447 show_usage(); 449 bb_show_usage();
448 450
449 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */ 451 /* we loosen up a bit and accept both "baudrate tty" and "tty baudrate" */
450 if ('0' <= argv[optind][0] && argv[optind][0] <= '9') { 452 if ('0' <= argv[optind][0] && argv[optind][0] <= '9') {
@@ -953,6 +955,7 @@ static int caps_lock(const char *s)
953/* bcode - convert speed string to speed code; return 0 on failure */ 955/* bcode - convert speed string to speed code; return 0 on failure */
954static int bcode(const char *s) 956static int bcode(const char *s)
955{ 957{
958#if 0
956 struct Speedtab *sp; 959 struct Speedtab *sp;
957 long speed = atol(s); 960 long speed = atol(s);
958 961
@@ -960,6 +963,14 @@ static int bcode(const char *s)
960 if (sp->speed == speed) 963 if (sp->speed == speed)
961 return (sp->code); 964 return (sp->code);
962 return (0); 965 return (0);
966#else
967 int r;
968
969 if ((r = bb_value_to_baud(atol(s))) > 0) {
970 return r;
971 }
972 return 0;
973#endif
963} 974}
964 975
965/* error - report errors to console or syslog; only understands %s and %m */ 976/* error - report errors to console or syslog; only understands %s and %m */
@@ -982,7 +993,7 @@ static void error(const char *fmt, ...)
982 buf[0] = '\0'; 993 buf[0] = '\0';
983 bp = buf; 994 bp = buf;
984#else 995#else
985 strncpy(buf, applet_name, 256); 996 strncpy(buf, bb_applet_name, 256);
986 strncat(buf, ": ", 256); 997 strncat(buf, ": ", 256);
987 buf[255] = 0; 998 buf[255] = 0;
988 bp = buf + strlen(buf); 999 bp = buf + strlen(buf);
diff --git a/loginutils/login.c b/loginutils/login.c
index c1ea165c8..e239f421c 100644
--- a/loginutils/login.c
+++ b/loginutils/login.c
@@ -94,10 +94,10 @@ extern int login_main(int argc, char **argv)
94 * (-f root, *NOT* -froot). --marekm 94 * (-f root, *NOT* -froot). --marekm
95 */ 95 */
96 if ( optarg != argv[optind-1] ) 96 if ( optarg != argv[optind-1] )
97 show_usage ( ); 97 bb_show_usage( );
98 98
99 if ( !amroot ) /* Auth bypass only if real UID is zero */ 99 if ( !amroot ) /* Auth bypass only if real UID is zero */
100 error_msg_and_die ( "-f permission denied" ); 100 bb_error_msg_and_die ( "-f permission denied" );
101 101
102 safe_strncpy(username, optarg, USERNAME_SIZE); 102 safe_strncpy(username, optarg, USERNAME_SIZE);
103 opt_fflag = 1; 103 opt_fflag = 1;
@@ -106,7 +106,7 @@ extern int login_main(int argc, char **argv)
106 opt_host = optarg; 106 opt_host = optarg;
107 break; 107 break;
108 default: 108 default:
109 show_usage ( ); 109 bb_show_usage( );
110 } 110 }
111 } 111 }
112 112
@@ -275,11 +275,11 @@ static int login_prompt ( char *buf_name )
275 275
276static int check_nologin ( int amroot ) 276static int check_nologin ( int amroot )
277{ 277{
278 if ( access ( nologin_file, F_OK ) == 0 ) { 278 if ( access ( bb_path_nologin_file, F_OK ) == 0 ) {
279 FILE *fp; 279 FILE *fp;
280 int c; 280 int c;
281 281
282 if (( fp = fopen ( nologin_file, "r" ))) { 282 if (( fp = fopen ( bb_path_nologin_file, "r" ))) {
283 while (( c = getc ( fp )) != EOF ) 283 while (( c = getc ( fp )) != EOF )
284 putchar (( c == '\n' ) ? '\r' : c ); 284 putchar (( c == '\n' ) ? '\r' : c );
285 285
@@ -304,9 +304,9 @@ static int check_tty ( const char *tty )
304 int i; 304 int i;
305 char buf[BUFSIZ]; 305 char buf[BUFSIZ];
306 306
307 if (( fp = fopen ( securetty_file, "r" ))) { 307 if (( fp = fopen ( bb_path_securetty_file, "r" ))) {
308 while ( fgets ( buf, sizeof( buf ) - 1, fp )) { 308 while ( fgets ( buf, sizeof( buf ) - 1, fp )) {
309 for ( i = xstrlen( buf ) - 1; i >= 0; --i ) { 309 for ( i = bb_strlen( buf ) - 1; i >= 0; --i ) {
310 if ( !isspace ( buf[i] )) 310 if ( !isspace ( buf[i] ))
311 break; 311 break;
312 } 312 }
@@ -348,7 +348,7 @@ static void motd ( )
348 FILE *fp; 348 FILE *fp;
349 register int c; 349 register int c;
350 350
351 if (( fp = fopen ( motd_file, "r" ))) { 351 if (( fp = fopen ( bb_path_motd_file, "r" ))) {
352 while (( c = getc ( fp )) != EOF ) 352 while (( c = getc ( fp )) != EOF )
353 putchar ( c ); 353 putchar ( c );
354 fclose ( fp ); 354 fclose ( fp );
diff --git a/loginutils/passwd.c b/loginutils/passwd.c
index e02cc4b81..e8577066a 100644
--- a/loginutils/passwd.c
+++ b/loginutils/passwd.c
@@ -46,13 +46,13 @@ extern int update_passwd(const struct passwd *pw, char *crypt_pw)
46 struct stat sb; 46 struct stat sb;
47 struct flock lock; 47 struct flock lock;
48 48
49 if (access(shadow_file, F_OK) == 0) { 49 if (access(bb_path_shadow_file, F_OK) == 0) {
50 has_shadow = 1; 50 has_shadow = 1;
51 } 51 }
52 if (has_shadow) { 52 if (has_shadow) {
53 snprintf(filename, sizeof filename, "%s", shadow_file); 53 snprintf(filename, sizeof filename, "%s", bb_path_shadow_file);
54 } else { 54 } else {
55 snprintf(filename, sizeof filename, "%s", passwd_file); 55 snprintf(filename, sizeof filename, "%s", bb_path_passwd_file);
56 } 56 }
57 57
58 if (((fp = fopen(filename, "r+")) == 0) || (fstat(fileno(fp), &sb))) { 58 if (((fp = fopen(filename, "r+")) == 0) || (fstat(fileno(fp), &sb))) {
@@ -167,30 +167,30 @@ extern int passwd_main(int argc, char **argv)
167 uflg++; 167 uflg++;
168 break; 168 break;
169 default: 169 default:
170 show_usage(); 170 bb_show_usage();
171 } 171 }
172 } 172 }
173 ruid = getuid(); 173 ruid = getuid();
174 pw = (struct passwd *) getpwuid(ruid); 174 pw = (struct passwd *) getpwuid(ruid);
175 if (!pw) { 175 if (!pw) {
176 error_msg_and_die("Cannot determine your user name."); 176 bb_error_msg_and_die("Cannot determine your user name.");
177 } 177 }
178 myname = (char *) xstrdup(pw->pw_name); 178 myname = (char *) bb_xstrdup(pw->pw_name);
179 if (optind < argc) { 179 if (optind < argc) {
180 name = argv[optind]; 180 name = argv[optind];
181 } else { 181 } else {
182 name = myname; 182 name = myname;
183 } 183 }
184 if ((lflg || uflg || dflg) && (optind >= argc || !amroot)) { 184 if ((lflg || uflg || dflg) && (optind >= argc || !amroot)) {
185 show_usage(); 185 bb_show_usage();
186 } 186 }
187 pw = getpwnam(name); 187 pw = getpwnam(name);
188 if (!pw) { 188 if (!pw) {
189 error_msg_and_die("Unknown user %s\n", name); 189 bb_error_msg_and_die("Unknown user %s\n", name);
190 } 190 }
191 if (!amroot && pw->pw_uid != getuid()) { 191 if (!amroot && pw->pw_uid != getuid()) {
192 syslog(LOG_WARNING, "can't change pwd for `%s'", name); 192 syslog(LOG_WARNING, "can't change pwd for `%s'", name);
193 error_msg_and_die("Permission denied.\n"); 193 bb_error_msg_and_die("Permission denied.\n");
194 } 194 }
195#ifdef CONFIG_FEATURE_SHADOWPASSWDS 195#ifdef CONFIG_FEATURE_SHADOWPASSWDS
196 sp = getspnam(name); 196 sp = getspnam(name);
@@ -209,12 +209,12 @@ extern int passwd_main(int argc, char **argv)
209 if (!amroot) { 209 if (!amroot) {
210 if (cp[0] == '!') { 210 if (cp[0] == '!') {
211 syslog(LOG_WARNING, "password locked for `%s'", np); 211 syslog(LOG_WARNING, "password locked for `%s'", np);
212 error_msg_and_die( "The password for `%s' cannot be changed.\n", np); 212 bb_error_msg_and_die( "The password for `%s' cannot be changed.\n", np);
213 } 213 }
214 } 214 }
215 printf("Changing password for %s\n", name); 215 printf("Changing password for %s\n", name);
216 if (new_password(pw, amroot, algo)) { 216 if (new_password(pw, amroot, algo)) {
217 error_msg_and_die( "The password for %s is unchanged.\n", name); 217 bb_error_msg_and_die( "The password for %s is unchanged.\n", name);
218 } 218 }
219 } else if (lflg) { 219 } else if (lflg) {
220 if (crypt_passwd[0] != '!') { 220 if (crypt_passwd[0] != '!') {
@@ -238,7 +238,7 @@ extern int passwd_main(int argc, char **argv)
238 umask(077); 238 umask(077);
239 if (setuid(0)) { 239 if (setuid(0)) {
240 syslog(LOG_ERR, "can't setuid(0)"); 240 syslog(LOG_ERR, "can't setuid(0)");
241 error_msg_and_die( "Cannot change ID to root.\n"); 241 bb_error_msg_and_die( "Cannot change ID to root.\n");
242 } 242 }
243 if (!update_passwd(pw, crypt_passwd)) { 243 if (!update_passwd(pw, crypt_passwd)) {
244 syslog(LOG_INFO, "password for `%s' changed by user `%s'", name, 244 syslog(LOG_INFO, "password for `%s' changed by user `%s'", name,
@@ -246,7 +246,7 @@ extern int passwd_main(int argc, char **argv)
246 printf("Password changed.\n"); 246 printf("Password changed.\n");
247 } else { 247 } else {
248 syslog(LOG_WARNING, "an error occurred updating the password file"); 248 syslog(LOG_WARNING, "an error occurred updating the password file");
249 error_msg_and_die("An error occurred updating the password file.\n"); 249 bb_error_msg_and_die("An error occurred updating the password file.\n");
250 } 250 }
251 return (0); 251 return (0);
252} 252}
diff --git a/loginutils/su.c b/loginutils/su.c
index 6d427262e..5e40cf2e4 100644
--- a/loginutils/su.c
+++ b/loginutils/su.c
@@ -94,7 +94,7 @@ int su_main ( int argc, char **argv )
94 opt_loginshell = 1; 94 opt_loginshell = 1;
95 break; 95 break;
96 default: 96 default:
97 show_usage ( ); 97 bb_show_usage( );
98 break; 98 break;
99 } 99 }
100 } 100 }
@@ -114,7 +114,7 @@ int su_main ( int argc, char **argv )
114 114
115 pw = getpwnam ( opt_username ); 115 pw = getpwnam ( opt_username );
116 if ( !pw ) 116 if ( !pw )
117 error_msg_and_die ( "user %s does not exist", opt_username ); 117 bb_error_msg_and_die ( "user %s does not exist", opt_username );
118 118
119 /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER 119 /* Make sure pw->pw_shell is non-NULL. It may be NULL when NEW_USER
120 is a username that is retrieved via NIS (YP), but that doesn't have 120 is a username that is retrieved via NIS (YP), but that doesn't have
@@ -127,15 +127,15 @@ int su_main ( int argc, char **argv )
127 the static data through the getlogin call from log_su. */ 127 the static data through the getlogin call from log_su. */
128 pw_copy = *pw; 128 pw_copy = *pw;
129 pw = &pw_copy; 129 pw = &pw_copy;
130 pw-> pw_name = xstrdup ( pw-> pw_name ); 130 pw-> pw_name = bb_xstrdup ( pw-> pw_name );
131 pw-> pw_dir = xstrdup ( pw-> pw_dir ); 131 pw-> pw_dir = bb_xstrdup ( pw-> pw_dir );
132 pw-> pw_shell = xstrdup ( pw-> pw_shell ); 132 pw-> pw_shell = bb_xstrdup ( pw-> pw_shell );
133 133
134 if (( getuid ( ) == 0 ) || correct_password ( pw )) 134 if (( getuid ( ) == 0 ) || correct_password ( pw ))
135 log_su ( pw, 1 ); 135 log_su ( pw, 1 );
136 else { 136 else {
137 log_su ( pw, 0 ); 137 log_su ( pw, 0 );
138 error_msg_and_die ( "incorrect password" ); 138 bb_error_msg_and_die ( "incorrect password" );
139 } 139 }
140 140
141 if ( !opt_shell && opt_preserve ) 141 if ( !opt_shell && opt_preserve )
@@ -152,7 +152,7 @@ int su_main ( int argc, char **argv )
152 } 152 }
153 153
154 if ( !opt_shell ) 154 if ( !opt_shell )
155 opt_shell = xstrdup ( pw-> pw_shell ); 155 opt_shell = bb_xstrdup ( pw-> pw_shell );
156 156
157 change_identity ( pw ); 157 change_identity ( pw );
158 setup_environment ( opt_shell, opt_loginshell, !opt_preserve, pw ); 158 setup_environment ( opt_shell, opt_loginshell, !opt_preserve, pw );
diff --git a/loginutils/sulogin.c b/loginutils/sulogin.c
index a654ffb89..bb4716e0d 100644
--- a/loginutils/sulogin.c
+++ b/loginutils/sulogin.c
@@ -117,9 +117,9 @@ extern int sulogin_main(int argc, char **argv)
117 } 117 }
118 } 118 }
119 } 119 }
120 if (access(passwd_file, 0) == -1) { 120 if (access(bb_path_passwd_file, 0) == -1) {
121 syslog(LOG_WARNING, "No password file\n"); 121 syslog(LOG_WARNING, "No password file\n");
122 error_msg_and_die("No password file\n"); 122 bb_error_msg_and_die("No password file\n");
123 } 123 }
124 if (!isatty(0) || !isatty(1) || !isatty(2)) { 124 if (!isatty(0) || !isatty(1) || !isatty(2)) {
125 exit(EXIT_FAILURE); 125 exit(EXIT_FAILURE);
@@ -135,7 +135,7 @@ extern int sulogin_main(int argc, char **argv)
135 alarm(timeout); 135 alarm(timeout);
136 if (!(pwd = getpwnam(name))) { 136 if (!(pwd = getpwnam(name))) {
137 syslog(LOG_WARNING, "No password entry for `root'\n"); 137 syslog(LOG_WARNING, "No password entry for `root'\n");
138 error_msg_and_die("No password entry for `root'\n"); 138 bb_error_msg_and_die("No password entry for `root'\n");
139 } 139 }
140 pwent = *pwd; 140 pwent = *pwd;
141#ifdef CONFIG_FEATURE_SHADOWPASSWDS 141#ifdef CONFIG_FEATURE_SHADOWPASSWDS
diff --git a/loginutils/vlock.c b/loginutils/vlock.c
index 18f4b9407..7abf120d9 100644
--- a/loginutils/vlock.c
+++ b/loginutils/vlock.c
@@ -29,6 +29,7 @@
29 * It now works with md5, sha1, etc passwords. */ 29 * It now works with md5, sha1, etc passwords. */
30 30
31#include <stdio.h> 31#include <stdio.h>
32#include <stdlib.h>
32#include <sys/vt.h> 33#include <sys/vt.h>
33#include <signal.h> 34#include <signal.h>
34#include <string.h> 35#include <string.h>
@@ -96,26 +97,26 @@ extern int vlock_main(int argc, char **argv)
96 struct termios term; 97 struct termios term;
97 98
98 if (argc > 2) { 99 if (argc > 2) {
99 show_usage(); 100 bb_show_usage();
100 } 101 }
101 102
102 if (argc == 2) { 103 if (argc == 2) {
103 if (strncmp(argv[1], "-a", 2)) { 104 if (strncmp(argv[1], "-a", 2)) {
104 show_usage(); 105 bb_show_usage();
105 } else { 106 } else {
106 o_lock_all = 1; 107 o_lock_all = 1;
107 } 108 }
108 } 109 }
109 110
110 if ((pw = getpwuid(getuid())) == NULL) { 111 if ((pw = getpwuid(getuid())) == NULL) {
111 error_msg_and_die("no password for uid %d\n", getuid()); 112 bb_error_msg_and_die("no password for uid %d\n", getuid());
112 } 113 }
113#ifdef CONFIG_FEATURE_SHADOWPASSWDS 114#ifdef CONFIG_FEATURE_SHADOWPASSWDS
114 if ((strcmp(pw->pw_passwd, "x") == 0) 115 if ((strcmp(pw->pw_passwd, "x") == 0)
115 || (strcmp(pw->pw_passwd, "*") == 0)) { 116 || (strcmp(pw->pw_passwd, "*") == 0)) {
116 117
117 if ((spw = getspuid(getuid())) == NULL) { 118 if ((spw = getspuid(getuid())) == NULL) {
118 error_msg_and_die("could not read shadow password for uid %d: %s\n", 119 bb_error_msg_and_die("could not read shadow password for uid %d: %s\n",
119 getuid(), strerror(errno)); 120 getuid(), strerror(errno));
120 } 121 }
121 if (spw->sp_pwdp) { 122 if (spw->sp_pwdp) {
@@ -124,7 +125,7 @@ extern int vlock_main(int argc, char **argv)
124 } 125 }
125#endif /* CONFIG_FEATURE_SHADOWPASSWDS */ 126#endif /* CONFIG_FEATURE_SHADOWPASSWDS */
126 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*') { 127 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*') {
127 error_msg_and_die("Account disabled for uid %d\n", getuid()); 128 bb_error_msg_and_die("Account disabled for uid %d\n", getuid());
128 } 129 }
129 130
130 /* we no longer need root privs */ 131 /* we no longer need root privs */
@@ -132,11 +133,11 @@ extern int vlock_main(int argc, char **argv)
132 setgid(getgid()); 133 setgid(getgid());
133 134
134 if ((vfd = open("/dev/tty", O_RDWR)) < 0) { 135 if ((vfd = open("/dev/tty", O_RDWR)) < 0) {
135 error_msg_and_die("/dev/tty"); 136 bb_error_msg_and_die("/dev/tty");
136 }; 137 };
137 138
138 if (ioctl(vfd, VT_GETMODE, &vtm) < 0) { 139 if (ioctl(vfd, VT_GETMODE, &vtm) < 0) {
139 error_msg_and_die("/dev/tty"); 140 bb_error_msg_and_die("/dev/tty");
140 }; 141 };
141 142
142 /* mask a bunch of signals */ 143 /* mask a bunch of signals */
diff --git a/miscutils/adjtimex.c b/miscutils/adjtimex.c
index 848f7340e..d541848f0 100644
--- a/miscutils/adjtimex.c
+++ b/miscutils/adjtimex.c
@@ -87,7 +87,7 @@ void usage(char *prog)
87 "Usage: %s [ -q ] [ -o offset ] [ -f frequency ] [ -p timeconstant ] [ -t tick ]\n", 87 "Usage: %s [ -q ] [ -o offset ] [ -f frequency ] [ -p timeconstant ] [ -t tick ]\n",
88 prog); 88 prog);
89} 89}
90#define show_usage() usage(argv[0]) 90#define bb_show_usage() usage(argv[0])
91#endif 91#endif
92 92
93int main(int argc, char ** argv) 93int main(int argc, char ** argv)
@@ -121,12 +121,12 @@ int main(int argc, char ** argv)
121 txc.modes |= ADJ_TICK; 121 txc.modes |= ADJ_TICK;
122 break; 122 break;
123 default: 123 default:
124 show_usage(); 124 bb_show_usage();
125 exit(1); 125 exit(1);
126 } 126 }
127 } 127 }
128 if (argc != optind) { /* no valid non-option parameters */ 128 if (argc != optind) { /* no valid non-option parameters */
129 show_usage(); 129 bb_show_usage();
130 exit(1); 130 exit(1);
131 } 131 }
132 132
diff --git a/miscutils/crond.c b/miscutils/crond.c
index 8ae54a536..cbb4ffc58 100644
--- a/miscutils/crond.c
+++ b/miscutils/crond.c
@@ -154,7 +154,7 @@ crond_main(int ac, char **av)
154 if (*optarg != 0) CDir = optarg; 154 if (*optarg != 0) CDir = optarg;
155 break; 155 break;
156 default: /* parse error */ 156 default: /* parse error */
157 show_usage(); 157 bb_show_usage();
158 } 158 }
159 } 159 }
160 160
@@ -163,7 +163,7 @@ crond_main(int ac, char **av)
163 */ 163 */
164 164
165 if (chdir(CDir) != 0) 165 if (chdir(CDir) != 0)
166 perror_msg_and_die("chdir"); 166 bb_perror_msg_and_die("chdir");
167 167
168 /* 168 /*
169 * close stdin and stdout, stderr. 169 * close stdin and stdout, stderr.
@@ -173,7 +173,7 @@ crond_main(int ac, char **av)
173 173
174 if (ForegroundOpt == 0) { 174 if (ForegroundOpt == 0) {
175 if(daemon(1, 0) < 0) 175 if(daemon(1, 0) < 0)
176 perror_msg_and_die("daemon"); 176 bb_perror_msg_and_die("daemon");
177 } 177 }
178 178
179 (void)startlogger(); /* need if syslog mode selected */ 179 (void)startlogger(); /* need if syslog mode selected */
@@ -268,7 +268,7 @@ vlog(int level, int MLOG_LEVEL, const char *ctl, va_list va)
268 close(logfd); 268 close(logfd);
269 } else 269 } else
270#ifdef FEATURE_DEBUG_OPT 270#ifdef FEATURE_DEBUG_OPT
271 perror_msg("Can't open log file") 271 bb_perror_msg("Can't open log file")
272#endif 272#endif
273 ; 273 ;
274 } 274 }
@@ -385,7 +385,7 @@ startlogger(void)
385 int logfd; 385 int logfd;
386 386
387 if (LoggerOpt == 0) 387 if (LoggerOpt == 0)
388 openlog(applet_name, LOG_CONS|LOG_PID,LOG_CRON); 388 openlog(bb_applet_name, LOG_CONS|LOG_PID,LOG_CRON);
389 389
390 else { /* test logfile */ 390 else { /* test logfile */
391 if ((logfd = open(LogFile,O_WRONLY|O_CREAT|O_APPEND,600)) >= 0) 391 if ((logfd = open(LogFile,O_WRONLY|O_CREAT|O_APPEND,600)) >= 0)
diff --git a/miscutils/crontab.c b/miscutils/crontab.c
index 5bd7db6e5..c6c33ace9 100644
--- a/miscutils/crontab.c
+++ b/miscutils/crontab.c
@@ -61,7 +61,7 @@ crontab_main(int ac, char **av)
61 61
62 UserId = getuid(); 62 UserId = getuid();
63 if ((pas = getpwuid(UserId)) == NULL) 63 if ((pas = getpwuid(UserId)) == NULL)
64 perror_msg_and_die("getpwuid"); 64 bb_perror_msg_and_die("getpwuid");
65 65
66 strncpy(caller, pas->pw_name, sizeof(caller)); 66 strncpy(caller, pas->pw_name, sizeof(caller));
67 67
@@ -105,10 +105,10 @@ crontab_main(int ac, char **av)
105 if (pas) { 105 if (pas) {
106 UserId = pas->pw_uid; 106 UserId = pas->pw_uid;
107 } else { 107 } else {
108 error_msg_and_die("user %s unknown", av[i]); 108 bb_error_msg_and_die("user %s unknown", av[i]);
109 } 109 }
110 } else { 110 } else {
111 error_msg_and_die("only the superuser may specify a user"); 111 bb_error_msg_and_die("only the superuser may specify a user");
112 } 112 }
113 } 113 }
114 break; 114 break;
@@ -116,7 +116,7 @@ crontab_main(int ac, char **av)
116 if (getuid() == geteuid()) { 116 if (getuid() == geteuid()) {
117 CDir = (*ptr) ? ptr : av[++i]; 117 CDir = (*ptr) ? ptr : av[++i];
118 } else { 118 } else {
119 error_msg_and_die("-c option: superuser only"); 119 bb_error_msg_and_die("-c option: superuser only");
120 } 120 }
121 break; 121 break;
122 default: 122 default:
@@ -125,14 +125,14 @@ crontab_main(int ac, char **av)
125 } 125 }
126 } 126 }
127 if (i != ac || option == NONE) 127 if (i != ac || option == NONE)
128 show_usage(); 128 bb_show_usage();
129 129
130 /* 130 /*
131 * Get password entry 131 * Get password entry
132 */ 132 */
133 133
134 if ((pas = getpwuid(UserId)) == NULL) 134 if ((pas = getpwuid(UserId)) == NULL)
135 perror_msg_and_die("getpwuid"); 135 bb_perror_msg_and_die("getpwuid");
136 136
137 /* 137 /*
138 * If there is a replacement file, obtain a secure descriptor to it. 138 * If there is a replacement file, obtain a secure descriptor to it.
@@ -141,7 +141,7 @@ crontab_main(int ac, char **av)
141 if (repFile) { 141 if (repFile) {
142 repFd = GetReplaceStream(caller, repFile); 142 repFd = GetReplaceStream(caller, repFile);
143 if (repFd < 0) 143 if (repFd < 0)
144 error_msg_and_die("unable to read replacement file"); 144 bb_error_msg_and_die("unable to read replacement file");
145 } 145 }
146 146
147 /* 147 /*
@@ -149,7 +149,7 @@ crontab_main(int ac, char **av)
149 */ 149 */
150 150
151 if (chdir(CDir) < 0) 151 if (chdir(CDir) < 0)
152 perror_msg_and_die("cannot change dir to %s", CDir); 152 bb_perror_msg_and_die("cannot change dir to %s", CDir);
153 153
154 /* 154 /*
155 * Handle options as appropriate 155 * Handle options as appropriate
@@ -166,7 +166,7 @@ crontab_main(int ac, char **av)
166 fputs(buf, stdout); 166 fputs(buf, stdout);
167 fclose(fi); 167 fclose(fi);
168 } else { 168 } else {
169 error_msg("no crontab for %s", pas->pw_name); 169 bb_error_msg("no crontab for %s", pas->pw_name);
170 } 170 }
171 } 171 }
172 break; 172 break;
@@ -190,7 +190,7 @@ crontab_main(int ac, char **av)
190 lseek(fd, 0L, 0); 190 lseek(fd, 0L, 0);
191 repFd = fd; 191 repFd = fd;
192 } else { 192 } else {
193 error_msg_and_die("unable to create %s", tmp); 193 bb_error_msg_and_die("unable to create %s", tmp);
194 } 194 }
195 195
196 } 196 }
@@ -211,7 +211,7 @@ crontab_main(int ac, char **av)
211 close(fd); 211 close(fd);
212 rename(path, pas->pw_name); 212 rename(path, pas->pw_name);
213 } else { 213 } else {
214 error_msg("unable to create %s/%s", CDir, path); 214 bb_error_msg("unable to create %s/%s", CDir, path);
215 } 215 }
216 close(repFd); 216 close(repFd);
217 } 217 }
@@ -244,7 +244,7 @@ crontab_main(int ac, char **av)
244 /* loop */ 244 /* loop */
245 } 245 }
246 if (fo == NULL) { 246 if (fo == NULL) {
247 error_msg("unable to append to %s/%s", CDir, CRONUPDATE); 247 bb_error_msg("unable to append to %s/%s", CDir, CRONUPDATE);
248 } 248 }
249 } 249 }
250 return 0; 250 return 0;
@@ -291,7 +291,7 @@ GetReplaceStream(const char *user, const char *file)
291 291
292 fd = open(file, O_RDONLY); 292 fd = open(file, O_RDONLY);
293 if (fd < 0) { 293 if (fd < 0) {
294 error_msg("unable to open %s", file); 294 bb_error_msg("unable to open %s", file);
295 exit(0); 295 exit(0);
296 } 296 }
297 buf[0] = 0; 297 buf[0] = 0;
@@ -328,7 +328,7 @@ EditFile(const char *user, const char *file)
328 /* 328 /*
329 * PARENT - failure 329 * PARENT - failure
330 */ 330 */
331 perror_msg_and_die("fork"); 331 bb_perror_msg_and_die("fork");
332 } 332 }
333 wait4(pid, NULL, 0, NULL); 333 wait4(pid, NULL, 0, NULL);
334} 334}
diff --git a/miscutils/dc.c b/miscutils/dc.c
index c7b43ea0a..5e367fe68 100644
--- a/miscutils/dc.c
+++ b/miscutils/dc.c
@@ -16,14 +16,14 @@ static unsigned char base;
16static void push(double a) 16static void push(double a)
17{ 17{
18 if (pointer >= (sizeof(stack) / sizeof(*stack))) 18 if (pointer >= (sizeof(stack) / sizeof(*stack)))
19 error_msg_and_die("stack overflow"); 19 bb_error_msg_and_die("stack overflow");
20 stack[pointer++] = a; 20 stack[pointer++] = a;
21} 21}
22 22
23static double pop(void) 23static double pop(void)
24{ 24{
25 if (pointer == 0) 25 if (pointer == 0)
26 error_msg_and_die("stack underflow"); 26 bb_error_msg_and_die("stack underflow");
27 return stack[--pointer]; 27 return stack[--pointer];
28} 28}
29 29
@@ -154,7 +154,7 @@ static void stack_machine(const char *argument)
154 } 154 }
155 o++; 155 o++;
156 } 156 }
157 error_msg_and_die("%s: syntax error.", argument); 157 bb_error_msg_and_die("%s: syntax error.", argument);
158} 158}
159 159
160/* return pointer to next token in buffer and set *buffer to one char 160/* return pointer to next token in buffer and set *buffer to one char
@@ -191,7 +191,7 @@ int dc_main(int argc, char **argv)
191 char *line = NULL; 191 char *line = NULL;
192 char *cursor = NULL; 192 char *cursor = NULL;
193 char *token = NULL; 193 char *token = NULL;
194 while ((line = get_line_from_file(stdin))) { 194 while ((line = bb_get_chomped_line_from_file(stdin))) {
195 cursor = line; 195 cursor = line;
196 len = number_of_tokens(line); 196 len = number_of_tokens(line);
197 for (i = 0; i < len; i++) { 197 for (i = 0; i < len; i++) {
@@ -203,7 +203,7 @@ int dc_main(int argc, char **argv)
203 } 203 }
204 } else { 204 } else {
205 if (*argv[1]=='-') 205 if (*argv[1]=='-')
206 show_usage(); 206 bb_show_usage();
207 while (argc >= 2) { 207 while (argc >= 2) {
208 stack_machine(argv[1]); 208 stack_machine(argv[1]);
209 argv++; 209 argv++;
diff --git a/miscutils/dutmp.c b/miscutils/dutmp.c
index 19e09fbb0..113f850fe 100644
--- a/miscutils/dutmp.c
+++ b/miscutils/dutmp.c
@@ -8,57 +8,76 @@
8 * versions of 'who', 'last', etc. IP Addr is output in hex, 8 * versions of 'who', 'last', etc. IP Addr is output in hex,
9 * little endian on x86. 9 * little endian on x86.
10 * 10 *
11 * Modified to support all sorts of libcs by
12 * Erik Andersen <andersen@lineo.com>
13 */ 11 */
14 12
15#include <sys/types.h> 13/* Mar 13, 2003 Manuel Novoa III
16#include <fcntl.h> 14 *
17#include <errno.h> 15 * 1) Added proper error checking.
18#include <utmp.h> 16 * 2) Allow '-' arg for stdin.
17 * 3) For modern libcs, take into account that utmp char[] members
18 * need not be nul-terminated.
19 */
20
19#include <stdlib.h> 21#include <stdlib.h>
20#include <unistd.h> 22#include <unistd.h>
21#include <time.h> 23#include <fcntl.h>
24#include <utmp.h>
22#include "busybox.h" 25#include "busybox.h"
23 26
27/* Grr... utmp char[] members do not have to be nul-terminated.
28 * Do what we can while still keeping this reasonably small.
29 * Note: We are assuming the ut_id[] size is fixed at 4. */
30
31#if __GNU_LIBRARY__ < 5
32#warning the format string needs to be changed
33#else
34#if (UT_LINESIZE != 32) || (UT_NAMESIZE != 32) || (UT_HOSTSIZE != 256)
35#error struct utmp member char[] size(s) have changed!
36#endif
37#endif
38
24extern int dutmp_main(int argc, char **argv) 39extern int dutmp_main(int argc, char **argv)
25{ 40{
26 41 int file = STDIN_FILENO;
27 int file; 42 ssize_t n;
28 struct utmp ut; 43 struct utmp ut;
29 44
30 if (argc<2) { 45 if (argc > 2) {
31 file = fileno(stdin); 46 bb_show_usage();
32 } else if (*argv[1] == '-' ) { 47 }
33 show_usage(); 48 ++argv;
34 } else { 49 if ((argc == 2) && ((argv[0][0] != '-') || argv[0][1])) {
35 file = open(argv[1], O_RDONLY); 50 file = bb_xopen(*argv, O_RDONLY);
36 if (file < 0) {
37 error_msg_and_die(io_error, argv[1]);
38 }
39 } 51 }
40 52
41/* Kludge around the fact that the binary format for utmp has changed. */ 53
54 while ((n = safe_read(file, (void*)&ut, sizeof(struct utmp))) != 0) {
55
56 if (n != sizeof(struct utmp)) {
57 bb_perror_msg_and_die("short read");
58 }
59
60 /* Kludge around the fact that the binary format for utmp has changed. */
42#if __GNU_LIBRARY__ < 5 61#if __GNU_LIBRARY__ < 5
43 /* Linux libc5 */ 62 /* Linux libc5 */
44 while (read(file, (void*)&ut, sizeof(struct utmp))) { 63
45 printf("%d|%d|%s|%s|%s|%s|%s|%lx\n", 64 bb_printf("%d|%d|%s|%s|%s|%s|%s|%lx\n",
46 ut.ut_type, ut.ut_pid, ut.ut_line, 65 ut.ut_type, ut.ut_pid, ut.ut_line,
47 ut.ut_id, ut.ut_user, ut.ut_host, 66 ut.ut_id, ut.ut_user, ut.ut_host,
48 ctime(&(ut.ut_time)), 67 ctime(&(ut.ut_time)),
49 (long)ut.ut_addr); 68 (long)ut.ut_addr);
50 }
51#else 69#else
52 /* Glibc, uClibc, etc. */ 70 /* Glibc, uClibc, etc. */
53 while (read(file, (void*)&ut, sizeof(struct utmp))) { 71
54 printf("%d|%d|%s|%s|%s|%s|%d|%d|%ld|%ld|%ld|%x\n", 72 bb_printf("%d|%d|%.32s|%.4s|%.32s|%.256s|%d|%d|%ld|%ld|%ld|%x\n",
55 ut.ut_type, ut.ut_pid, ut.ut_line, 73 ut.ut_type, ut.ut_pid, ut.ut_line,
56 ut.ut_id, ut.ut_user, ut.ut_host, 74 ut.ut_id, ut.ut_user, ut.ut_host,
57 ut.ut_exit.e_termination, ut.ut_exit.e_exit, 75 ut.ut_exit.e_termination, ut.ut_exit.e_exit,
58 ut.ut_session, 76 ut.ut_session,
59 ut.ut_tv.tv_sec, ut.ut_tv.tv_usec, 77 ut.ut_tv.tv_sec, ut.ut_tv.tv_usec,
60 ut.ut_addr); 78 ut.ut_addr);
61 }
62#endif 79#endif
63 return EXIT_SUCCESS; 80 }
81
82 bb_fflush_stdout_and_exit(EXIT_SUCCESS);
64} 83}
diff --git a/miscutils/makedevs.c b/miscutils/makedevs.c
index 67b28b534..308d651ae 100644
--- a/miscutils/makedevs.c
+++ b/miscutils/makedevs.c
@@ -22,7 +22,7 @@ int makedevs_main(int argc, char **argv)
22 int major, Sminor, S, E; 22 int major, Sminor, S, E;
23 23
24 if (argc < 7 || *argv[1]=='-') 24 if (argc < 7 || *argv[1]=='-')
25 show_usage(); 25 bb_show_usage();
26 26
27 basedev = argv[1]; 27 basedev = argv[1];
28 type = argv[2]; 28 type = argv[2];
@@ -45,7 +45,7 @@ int makedevs_main(int argc, char **argv)
45 mode |= S_IFIFO; 45 mode |= S_IFIFO;
46 break; 46 break;
47 default: 47 default:
48 show_usage(); 48 bb_show_usage();
49 } 49 }
50 50
51 while (S <= E) { 51 while (S <= E) {
@@ -53,12 +53,12 @@ int makedevs_main(int argc, char **argv)
53 53
54 sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S); 54 sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S);
55 if(sz<0 || sz>=sizeof(buf)) /* libc different */ 55 if(sz<0 || sz>=sizeof(buf)) /* libc different */
56 error_msg_and_die("%s too large", basedev); 56 bb_error_msg_and_die("%s too large", basedev);
57 57
58 /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */ 58 /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */
59 59
60 if (mknod(nodname, mode, major | Sminor)) 60 if (mknod(nodname, mode, major | Sminor))
61 error_msg("Failed to create: %s", nodname); 61 bb_error_msg("Failed to create: %s", nodname);
62 62
63 if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */ 63 if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */
64 nodname = buf; 64 nodname = buf;
diff --git a/miscutils/mt.c b/miscutils/mt.c
index 49dc70ac6..e79954552 100644
--- a/miscutils/mt.c
+++ b/miscutils/mt.c
@@ -59,12 +59,12 @@ extern int mt_main(int argc, char **argv)
59 int fd, mode; 59 int fd, mode;
60 60
61 if (argc < 2) { 61 if (argc < 2) {
62 show_usage(); 62 bb_show_usage();
63 } 63 }
64 64
65 if (strcmp(argv[1], "-f") == 0) { 65 if (strcmp(argv[1], "-f") == 0) {
66 if (argc < 4) { 66 if (argc < 4) {
67 show_usage(); 67 bb_show_usage();
68 } 68 }
69 file = argv[2]; 69 file = argv[2];
70 argv += 2; 70 argv += 2;
@@ -78,7 +78,7 @@ extern int mt_main(int argc, char **argv)
78 } 78 }
79 79
80 if (code->name == 0) { 80 if (code->name == 0) {
81 error_msg("unrecognized opcode %s.", argv[1]); 81 bb_error_msg("unrecognized opcode %s.", argv[1]);
82 return EXIT_FAILURE; 82 return EXIT_FAILURE;
83 } 83 }
84 84
@@ -102,18 +102,18 @@ extern int mt_main(int argc, char **argv)
102 } 102 }
103 103
104 if ((fd = open(file, mode, 0)) < 0) 104 if ((fd = open(file, mode, 0)) < 0)
105 perror_msg_and_die("%s", file); 105 bb_perror_msg_and_die("%s", file);
106 106
107 switch (code->value) { 107 switch (code->value) {
108 case MTTELL: 108 case MTTELL:
109 if (ioctl(fd, MTIOCPOS, &position) < 0) 109 if (ioctl(fd, MTIOCPOS, &position) < 0)
110 perror_msg_and_die("%s", file); 110 bb_perror_msg_and_die("%s", file);
111 printf ("At block %d.\n", (int) position.mt_blkno); 111 printf ("At block %d.\n", (int) position.mt_blkno);
112 break; 112 break;
113 113
114 default: 114 default:
115 if (ioctl(fd, MTIOCTOP, &op) != 0) 115 if (ioctl(fd, MTIOCTOP, &op) != 0)
116 perror_msg_and_die("%s", file); 116 bb_perror_msg_and_die("%s", file);
117 break; 117 break;
118 } 118 }
119 119
diff --git a/miscutils/strings.c b/miscutils/strings.c
index 8d08b3f23..2413c3f2d 100644
--- a/miscutils/strings.c
+++ b/miscutils/strings.c
@@ -57,7 +57,7 @@ int strings_main(int argc, char **argv)
57 n = bb_xgetlarg(optarg, 10, 1, LONG_MAX); 57 n = bb_xgetlarg(optarg, 10, 1, LONG_MAX);
58 break; 58 break;
59 default: 59 default:
60 show_usage(); 60 bb_show_usage();
61 } 61 }
62 62
63 argc -= optind; 63 argc -= optind;
@@ -118,7 +118,7 @@ pipe:
118 } 118 }
119 else 119 else
120 { 120 {
121 perror_msg("%s",argv[a]); 121 bb_perror_msg("%s",argv[a]);
122 status=EXIT_FAILURE; 122 status=EXIT_FAILURE;
123 } 123 }
124 } 124 }
diff --git a/miscutils/time.c b/miscutils/time.c
index fa352a899..c30ef4311 100644
--- a/miscutils/time.c
+++ b/miscutils/time.c
@@ -141,7 +141,7 @@ static void fprintargv (FILE *fp, char *const *argv, const char *filler)
141 fputs (*av, fp); 141 fputs (*av, fp);
142 } 142 }
143 if (ferror (fp)) 143 if (ferror (fp))
144 error_msg_and_die("write error"); 144 bb_error_msg_and_die("write error");
145} 145}
146 146
147/* Return the number of kilobytes corresponding to a number of pages PAGES. 147/* Return the number of kilobytes corresponding to a number of pages PAGES.
@@ -416,12 +416,12 @@ static void summarize (FILE *fp, const char *fmt, char **command, resource_t *re
416 } 416 }
417 417
418 if (ferror (fp)) 418 if (ferror (fp))
419 error_msg_and_die("write error"); 419 bb_error_msg_and_die("write error");
420 } 420 }
421 putc ('\n', fp); 421 putc ('\n', fp);
422 422
423 if (ferror (fp)) 423 if (ferror (fp))
424 error_msg_and_die("write error"); 424 bb_error_msg_and_die("write error");
425} 425}
426 426
427/* Run command CMD and return statistics on it. 427/* Run command CMD and return statistics on it.
@@ -434,13 +434,13 @@ static void run_command (char *const *cmd, resource_t *resp)
434 gettimeofday (&resp->start, (struct timezone *) 0); 434 gettimeofday (&resp->start, (struct timezone *) 0);
435 pid = fork (); /* Run CMD as child process. */ 435 pid = fork (); /* Run CMD as child process. */
436 if (pid < 0) 436 if (pid < 0)
437 error_msg_and_die("cannot fork"); 437 bb_error_msg_and_die("cannot fork");
438 else if (pid == 0) 438 else if (pid == 0)
439 { /* If child. */ 439 { /* If child. */
440 /* Don't cast execvp arguments; that causes errors on some systems, 440 /* Don't cast execvp arguments; that causes errors on some systems,
441 versus merely warnings if the cast is left off. */ 441 versus merely warnings if the cast is left off. */
442 execvp (cmd[0], cmd); 442 execvp (cmd[0], cmd);
443 error_msg("cannot run %s", cmd[0]); 443 bb_error_msg("cannot run %s", cmd[0]);
444 _exit (errno == ENOENT ? 127 : 126); 444 _exit (errno == ENOENT ? 127 : 126);
445 } 445 }
446 446
@@ -449,7 +449,7 @@ static void run_command (char *const *cmd, resource_t *resp)
449 quit_signal = signal (SIGQUIT, SIG_IGN); 449 quit_signal = signal (SIGQUIT, SIG_IGN);
450 450
451 if (resuse_end (pid, resp) == 0) 451 if (resuse_end (pid, resp) == 0)
452 error_msg("error waiting for child process"); 452 bb_error_msg("error waiting for child process");
453 453
454 /* Re-enable signals. */ 454 /* Re-enable signals. */
455 signal (SIGINT, interrupt_signal); 455 signal (SIGINT, interrupt_signal);
@@ -477,7 +477,7 @@ extern int time_main (int argc, char **argv)
477 output_format = posix_format; 477 output_format = posix_format;
478 break; 478 break;
479 default: 479 default:
480 show_usage(); 480 bb_show_usage();
481 } 481 }
482 argc--; 482 argc--;
483 argv++; 483 argv++;
@@ -486,7 +486,7 @@ extern int time_main (int argc, char **argv)
486 } 486 }
487 487
488 if (argv == NULL || *argv == NULL) 488 if (argv == NULL || *argv == NULL)
489 show_usage(); 489 bb_show_usage();
490 490
491 run_command (argv, &res); 491 run_command (argv, &res);
492 summarize (stdout, output_format, argv, &res); 492 summarize (stdout, output_format, argv, &res);
diff --git a/miscutils/update.c b/miscutils/update.c
index 27a04ddee..48ea02deb 100644
--- a/miscutils/update.c
+++ b/miscutils/update.c
@@ -62,12 +62,12 @@ extern int update_main(int argc, char **argv)
62 flush_duration = atoi(optarg); 62 flush_duration = atoi(optarg);
63 break; 63 break;
64 default: 64 default:
65 show_usage(); 65 bb_show_usage();
66 } 66 }
67 } 67 }
68 68
69 if (daemon(0, 1) < 0) 69 if (daemon(0, 1) < 0)
70 perror_msg_and_die("daemon"); 70 bb_perror_msg_and_die("daemon");
71 71
72#ifdef OPEN_MAX 72#ifdef OPEN_MAX
73 for (pid = 0; pid < OPEN_MAX; pid++) close(pid); 73 for (pid = 0; pid < OPEN_MAX; pid++) close(pid);
diff --git a/miscutils/watchdog.c b/miscutils/watchdog.c
index f0b0ebd0e..cfe19abc3 100644
--- a/miscutils/watchdog.c
+++ b/miscutils/watchdog.c
@@ -33,11 +33,11 @@ extern int watchdog_main(int argc, char **argv)
33 int fd; 33 int fd;
34 34
35 if (argc != 2) { 35 if (argc != 2) {
36 show_usage(); 36 bb_show_usage();
37 } 37 }
38 38
39 if ((fd=open(argv[1], O_WRONLY)) == -1) { 39 if ((fd=open(argv[1], O_WRONLY)) == -1) {
40 perror_msg_and_die(argv[1]); 40 bb_perror_msg_and_die(argv[1]);
41 } 41 }
42 42
43 while (1) { 43 while (1) {
diff --git a/modutils/insmod.c b/modutils/insmod.c
index 56e78e833..b45357a69 100644
--- a/modutils/insmod.c
+++ b/modutils/insmod.c
@@ -247,7 +247,7 @@
247#ifndef MODUTILS_MODULE_H 247#ifndef MODUTILS_MODULE_H
248static const int MODUTILS_MODULE_H = 1; 248static const int MODUTILS_MODULE_H = 1;
249 249
250#ident "$Id: insmod.c,v 1.96 2003/03/13 18:57:08 andersen Exp $" 250#ident "$Id: insmod.c,v 1.97 2003/03/19 09:12:33 mjn3 Exp $"
251 251
252/* This file contains the structures used by the 2.0 and 2.1 kernels. 252/* This file contains the structures used by the 2.0 and 2.1 kernels.
253 We do not use the kernel headers directly because we do not wish 253 We do not use the kernel headers directly because we do not wish
@@ -468,7 +468,7 @@ int delete_module(const char *);
468#ifndef MODUTILS_OBJ_H 468#ifndef MODUTILS_OBJ_H
469static const int MODUTILS_OBJ_H = 1; 469static const int MODUTILS_OBJ_H = 1;
470 470
471#ident "$Id: insmod.c,v 1.96 2003/03/13 18:57:08 andersen Exp $" 471#ident "$Id: insmod.c,v 1.97 2003/03/19 09:12:33 mjn3 Exp $"
472 472
473/* The relocatable object is manipulated using elfin types. */ 473/* The relocatable object is manipulated using elfin types. */
474 474
@@ -769,12 +769,12 @@ static int check_module_name_match(const char *filename, struct stat *statbuf,
769 if (fullname[0] == '\0') 769 if (fullname[0] == '\0')
770 return (FALSE); 770 return (FALSE);
771 else { 771 else {
772 char *tmp, *tmp1 = xstrdup(filename); 772 char *tmp, *tmp1 = bb_xstrdup(filename);
773 tmp = get_last_path_component(tmp1); 773 tmp = bb_get_last_path_component(tmp1);
774 if (strcmp(tmp, fullname) == 0) { 774 if (strcmp(tmp, fullname) == 0) {
775 free(tmp1); 775 free(tmp1);
776 /* Stop searching if we find a match */ 776 /* Stop searching if we find a match */
777 m_filename = xstrdup(filename); 777 m_filename = bb_xstrdup(filename);
778 return (TRUE); 778 return (TRUE);
779 } 779 }
780 free(tmp1); 780 free(tmp1);
@@ -1700,7 +1700,7 @@ obj_add_symbol(struct obj_file *f, const char *name,
1700 /* Don't report an error if the symbol is coming from 1700 /* Don't report an error if the symbol is coming from
1701 the kernel or some external module. */ 1701 the kernel or some external module. */
1702 if (secidx <= SHN_HIRESERVE) 1702 if (secidx <= SHN_HIRESERVE)
1703 error_msg("%s multiply defined", name); 1703 bb_error_msg("%s multiply defined", name);
1704 return sym; 1704 return sym;
1705 } 1705 }
1706 } 1706 }
@@ -1713,7 +1713,7 @@ obj_add_symbol(struct obj_file *f, const char *name,
1713 1713
1714 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) { 1714 if (ELFW(ST_BIND)(info) == STB_LOCAL && symidx != -1) {
1715 if (symidx >= f->local_symtab_size) 1715 if (symidx >= f->local_symtab_size)
1716 error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld", 1716 bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
1717 name, (long) symidx, (long) f->local_symtab_size); 1717 name, (long) symidx, (long) f->local_symtab_size);
1718 else 1718 else
1719 f->local_symtab[symidx] = sym; 1719 f->local_symtab[symidx] = sym;
@@ -1996,7 +1996,7 @@ old_process_module_arguments(struct obj_file *f, int argc, char **argv)
1996 1996
1997 /* Also check that the parameter was not resolved from the kernel. */ 1997 /* Also check that the parameter was not resolved from the kernel. */
1998 if (sym == NULL || sym->secidx > SHN_HIRESERVE) { 1998 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
1999 error_msg("symbol for parameter %s not found", p); 1999 bb_error_msg("symbol for parameter %s not found", p);
2000 return 0; 2000 return 0;
2001 } 2001 }
2002 2002
@@ -2009,7 +2009,7 @@ old_process_module_arguments(struct obj_file *f, int argc, char **argv)
2009 str = alloca(strlen(q)); 2009 str = alloca(strlen(q));
2010 for (r = str, q++; *q != '"'; ++q, ++r) { 2010 for (r = str, q++; *q != '"'; ++q, ++r) {
2011 if (*q == '\0') { 2011 if (*q == '\0') {
2012 error_msg("improperly terminated string argument for %s", p); 2012 bb_error_msg("improperly terminated string argument for %s", p);
2013 return 0; 2013 return 0;
2014 } else if (*q == '\\') 2014 } else if (*q == '\\')
2015 switch (*++q) { 2015 switch (*++q) {
@@ -2139,9 +2139,9 @@ static int old_get_kernel_symbols(const char *m_name)
2139 nks = get_kernel_syms(NULL); 2139 nks = get_kernel_syms(NULL);
2140 if (nks <= 0) { 2140 if (nks <= 0) {
2141 if (nks) 2141 if (nks)
2142 perror_msg("get_kernel_syms: %s", m_name); 2142 bb_perror_msg("get_kernel_syms: %s", m_name);
2143 else 2143 else
2144 error_msg("No kernel symbols"); 2144 bb_error_msg("No kernel symbols");
2145 return 0; 2145 return 0;
2146 } 2146 }
2147 2147
@@ -2322,7 +2322,7 @@ old_init_module(const char *m_name, struct obj_file *f,
2322 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN 2322 m_size | (flag_autoclean ? OLD_MOD_AUTOCLEAN
2323 : 0), &routines, symtab); 2323 : 0), &routines, symtab);
2324 if (ret) 2324 if (ret)
2325 perror_msg("init_module: %s", m_name); 2325 bb_perror_msg("init_module: %s", m_name);
2326 2326
2327 free(image); 2327 free(image);
2328 free(symtab); 2328 free(symtab);
@@ -2365,7 +2365,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2365 p = get_modinfo_value(f, key); 2365 p = get_modinfo_value(f, key);
2366 key += 5; 2366 key += 5;
2367 if (p == NULL) { 2367 if (p == NULL) {
2368 error_msg("invalid parameter %s", key); 2368 bb_error_msg("invalid parameter %s", key);
2369 return 0; 2369 return 0;
2370 } 2370 }
2371 2371
@@ -2380,7 +2380,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2380 2380
2381 /* Also check that the parameter was not resolved from the kernel. */ 2381 /* Also check that the parameter was not resolved from the kernel. */
2382 if (sym == NULL || sym->secidx > SHN_HIRESERVE) { 2382 if (sym == NULL || sym->secidx > SHN_HIRESERVE) {
2383 error_msg("symbol for parameter %s not found", key); 2383 bb_error_msg("symbol for parameter %s not found", key);
2384 return 0; 2384 return 0;
2385 } 2385 }
2386 2386
@@ -2408,7 +2408,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2408 str = alloca(strlen(q)); 2408 str = alloca(strlen(q));
2409 for (r = str, q++; *q != '"'; ++q, ++r) { 2409 for (r = str, q++; *q != '"'; ++q, ++r) {
2410 if (*q == '\0') { 2410 if (*q == '\0') {
2411 error_msg("improperly terminated string argument for %s", 2411 bb_error_msg("improperly terminated string argument for %s",
2412 key); 2412 key);
2413 return 0; 2413 return 0;
2414 } else if (*q == '\\') 2414 } else if (*q == '\\')
@@ -2502,7 +2502,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2502 /* Get the size of each member */ 2502 /* Get the size of each member */
2503 /* Probably we should do that outside the loop ? */ 2503 /* Probably we should do that outside the loop ? */
2504 if (!isdigit(*(p + 1))) { 2504 if (!isdigit(*(p + 1))) {
2505 error_msg("parameter type 'c' for %s must be followed by" 2505 bb_error_msg("parameter type 'c' for %s must be followed by"
2506 " the maximum size", key); 2506 " the maximum size", key);
2507 return 0; 2507 return 0;
2508 } 2508 }
@@ -2510,7 +2510,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2510 2510
2511 /* Check length */ 2511 /* Check length */
2512 if (strlen(str) >= charssize) { 2512 if (strlen(str) >= charssize) {
2513 error_msg("string too long for %s (max %ld)", key, 2513 bb_error_msg("string too long for %s (max %ld)", key,
2514 charssize - 1); 2514 charssize - 1);
2515 return 0; 2515 return 0;
2516 } 2516 }
@@ -2539,7 +2539,7 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2539 break; 2539 break;
2540 2540
2541 default: 2541 default:
2542 error_msg("unknown parameter type '%c' for %s", *p, key); 2542 bb_error_msg("unknown parameter type '%c' for %s", *p, key);
2543 return 0; 2543 return 0;
2544 } 2544 }
2545 } 2545 }
@@ -2558,21 +2558,21 @@ new_process_module_arguments(struct obj_file *f, int argc, char **argv)
2558 2558
2559 case ',': 2559 case ',':
2560 if (++n > max) { 2560 if (++n > max) {
2561 error_msg("too many values for %s (max %d)", key, max); 2561 bb_error_msg("too many values for %s (max %d)", key, max);
2562 return 0; 2562 return 0;
2563 } 2563 }
2564 ++q; 2564 ++q;
2565 break; 2565 break;
2566 2566
2567 default: 2567 default:
2568 error_msg("invalid argument syntax for %s", key); 2568 bb_error_msg("invalid argument syntax for %s", key);
2569 return 0; 2569 return 0;
2570 } 2570 }
2571 } 2571 }
2572 2572
2573 end_of_arg: 2573 end_of_arg:
2574 if (n < min) { 2574 if (n < min) {
2575 error_msg("too few values for %s (min %d)", key, min); 2575 bb_error_msg("too few values for %s (min %d)", key, min);
2576 return 0; 2576 return 0;
2577 } 2577 }
2578 2578
@@ -2641,7 +2641,7 @@ static int new_get_kernel_symbols(void)
2641 module_names = xrealloc(module_names, bufsize = ret); 2641 module_names = xrealloc(module_names, bufsize = ret);
2642 goto retry_modules_load; 2642 goto retry_modules_load;
2643 } 2643 }
2644 perror_msg("QM_MODULES"); 2644 bb_perror_msg("QM_MODULES");
2645 return 0; 2645 return 0;
2646 } 2646 }
2647 2647
@@ -2661,7 +2661,7 @@ static int new_get_kernel_symbols(void)
2661 /* The module was removed out from underneath us. */ 2661 /* The module was removed out from underneath us. */
2662 continue; 2662 continue;
2663 } 2663 }
2664 perror_msg("query_module: QM_INFO: %s", mn); 2664 bb_perror_msg("query_module: QM_INFO: %s", mn);
2665 return 0; 2665 return 0;
2666 } 2666 }
2667 2667
@@ -2676,7 +2676,7 @@ static int new_get_kernel_symbols(void)
2676 /* The module was removed out from underneath us. */ 2676 /* The module was removed out from underneath us. */
2677 continue; 2677 continue;
2678 default: 2678 default:
2679 perror_msg("query_module: QM_SYMBOLS: %s", mn); 2679 bb_perror_msg("query_module: QM_SYMBOLS: %s", mn);
2680 return 0; 2680 return 0;
2681 } 2681 }
2682 } 2682 }
@@ -2702,7 +2702,7 @@ static int new_get_kernel_symbols(void)
2702 syms = xrealloc(syms, bufsize = ret); 2702 syms = xrealloc(syms, bufsize = ret);
2703 goto retry_kern_sym_load; 2703 goto retry_kern_sym_load;
2704 } 2704 }
2705 perror_msg("kernel: QM_SYMBOLS"); 2705 bb_perror_msg("kernel: QM_SYMBOLS");
2706 return 0; 2706 return 0;
2707 } 2707 }
2708 nksyms = nsyms = ret; 2708 nksyms = nsyms = ret;
@@ -2863,7 +2863,7 @@ new_init_module(const char *m_name, struct obj_file *f,
2863 2863
2864 sec = obj_find_section(f, ".this"); 2864 sec = obj_find_section(f, ".this");
2865 if (!sec || !sec->contents) { 2865 if (!sec || !sec->contents) {
2866 perror_msg_and_die("corrupt module %s?",m_name); 2866 bb_perror_msg_and_die("corrupt module %s?",m_name);
2867 } 2867 }
2868 module = (struct new_module *) sec->contents; 2868 module = (struct new_module *) sec->contents;
2869 m_addr = sec->header.sh_addr; 2869 m_addr = sec->header.sh_addr;
@@ -2927,7 +2927,7 @@ new_init_module(const char *m_name, struct obj_file *f,
2927 2927
2928 ret = new_sys_init_module(m_name, (struct new_module *) image); 2928 ret = new_sys_init_module(m_name, (struct new_module *) image);
2929 if (ret) 2929 if (ret)
2930 perror_msg("init_module: %s", m_name); 2930 bb_perror_msg("init_module: %s", m_name);
2931 2931
2932 free(image); 2932 free(image);
2933 2933
@@ -3008,7 +3008,7 @@ static int obj_check_undefineds(struct obj_file *f)
3008 sym->value = 0; 3008 sym->value = 0;
3009 } else { 3009 } else {
3010 if (!flag_quiet) { 3010 if (!flag_quiet) {
3011 error_msg("unresolved symbol %s", sym->name); 3011 bb_error_msg("unresolved symbol %s", sym->name);
3012 } 3012 }
3013 ret = 0; 3013 ret = 0;
3014 } 3014 }
@@ -3236,11 +3236,11 @@ static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
3236 errmsg = "Unhandled relocation"; 3236 errmsg = "Unhandled relocation";
3237 bad_reloc: 3237 bad_reloc:
3238 if (extsym) { 3238 if (extsym) {
3239 error_msg("%s of type %ld for %s", errmsg, 3239 bb_error_msg("%s of type %ld for %s", errmsg,
3240 (long) ELFW(R_TYPE) (rel->r_info), 3240 (long) ELFW(R_TYPE) (rel->r_info),
3241 strtab + extsym->st_name); 3241 strtab + extsym->st_name);
3242 } else { 3242 } else {
3243 error_msg("%s of type %ld", errmsg, 3243 bb_error_msg("%s of type %ld", errmsg,
3244 (long) ELFW(R_TYPE) (rel->r_info)); 3244 (long) ELFW(R_TYPE) (rel->r_info));
3245 } 3245 }
3246 ret = 0; 3246 ret = 0;
@@ -3317,7 +3317,7 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3317 3317
3318 fseek(fp, 0, SEEK_SET); 3318 fseek(fp, 0, SEEK_SET);
3319 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) { 3319 if (fread(&f->header, sizeof(f->header), 1, fp) != 1) {
3320 perror_msg("error reading ELF header"); 3320 bb_perror_msg("error reading ELF header");
3321 return NULL; 3321 return NULL;
3322 } 3322 }
3323 3323
@@ -3325,25 +3325,25 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3325 || f->header.e_ident[EI_MAG1] != ELFMAG1 3325 || f->header.e_ident[EI_MAG1] != ELFMAG1
3326 || f->header.e_ident[EI_MAG2] != ELFMAG2 3326 || f->header.e_ident[EI_MAG2] != ELFMAG2
3327 || f->header.e_ident[EI_MAG3] != ELFMAG3) { 3327 || f->header.e_ident[EI_MAG3] != ELFMAG3) {
3328 error_msg("not an ELF file"); 3328 bb_error_msg("not an ELF file");
3329 return NULL; 3329 return NULL;
3330 } 3330 }
3331 if (f->header.e_ident[EI_CLASS] != ELFCLASSM 3331 if (f->header.e_ident[EI_CLASS] != ELFCLASSM
3332 || f->header.e_ident[EI_DATA] != ELFDATAM 3332 || f->header.e_ident[EI_DATA] != ELFDATAM
3333 || f->header.e_ident[EI_VERSION] != EV_CURRENT 3333 || f->header.e_ident[EI_VERSION] != EV_CURRENT
3334 || !MATCH_MACHINE(f->header.e_machine)) { 3334 || !MATCH_MACHINE(f->header.e_machine)) {
3335 error_msg("ELF file not for this architecture"); 3335 bb_error_msg("ELF file not for this architecture");
3336 return NULL; 3336 return NULL;
3337 } 3337 }
3338 if (f->header.e_type != ET_REL) { 3338 if (f->header.e_type != ET_REL) {
3339 error_msg("ELF file not a relocatable object"); 3339 bb_error_msg("ELF file not a relocatable object");
3340 return NULL; 3340 return NULL;
3341 } 3341 }
3342 3342
3343 /* Read the section headers. */ 3343 /* Read the section headers. */
3344 3344
3345 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) { 3345 if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
3346 error_msg("section header size mismatch: %lu != %lu", 3346 bb_error_msg("section header size mismatch: %lu != %lu",
3347 (unsigned long) f->header.e_shentsize, 3347 (unsigned long) f->header.e_shentsize,
3348 (unsigned long) sizeof(ElfW(Shdr))); 3348 (unsigned long) sizeof(ElfW(Shdr)));
3349 return NULL; 3349 return NULL;
@@ -3356,7 +3356,7 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3356 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum); 3356 section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
3357 fseek(fp, f->header.e_shoff, SEEK_SET); 3357 fseek(fp, f->header.e_shoff, SEEK_SET);
3358 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) { 3358 if (fread(section_headers, sizeof(ElfW(Shdr)), shnum, fp) != shnum) {
3359 perror_msg("error reading ELF section headers"); 3359 bb_perror_msg("error reading ELF section headers");
3360 return NULL; 3360 return NULL;
3361 } 3361 }
3362 3362
@@ -3392,7 +3392,7 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3392 sec->contents = xmalloc(sec->header.sh_size); 3392 sec->contents = xmalloc(sec->header.sh_size);
3393 fseek(fp, sec->header.sh_offset, SEEK_SET); 3393 fseek(fp, sec->header.sh_offset, SEEK_SET);
3394 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) { 3394 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3395 perror_msg("error reading ELF section data"); 3395 bb_perror_msg("error reading ELF section data");
3396 return NULL; 3396 return NULL;
3397 } 3397 }
3398 } else { 3398 } else {
@@ -3402,11 +3402,11 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3402 3402
3403#if SHT_RELM == SHT_REL 3403#if SHT_RELM == SHT_REL
3404 case SHT_RELA: 3404 case SHT_RELA:
3405 error_msg("RELA relocations not supported on this architecture"); 3405 bb_error_msg("RELA relocations not supported on this architecture");
3406 return NULL; 3406 return NULL;
3407#else 3407#else
3408 case SHT_REL: 3408 case SHT_REL:
3409 error_msg("REL relocations not supported on this architecture"); 3409 bb_error_msg("REL relocations not supported on this architecture");
3410 return NULL; 3410 return NULL;
3411#endif 3411#endif
3412 3412
@@ -3419,7 +3419,7 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3419 break; 3419 break;
3420 } 3420 }
3421 3421
3422 error_msg("can't handle sections of type %ld", 3422 bb_error_msg("can't handle sections of type %ld",
3423 (long) sec->header.sh_type); 3423 (long) sec->header.sh_type);
3424 return NULL; 3424 return NULL;
3425 } 3425 }
@@ -3454,7 +3454,7 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3454 ElfW(Sym) * sym; 3454 ElfW(Sym) * sym;
3455 3455
3456 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) { 3456 if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
3457 error_msg("symbol size mismatch: %lu != %lu", 3457 bb_error_msg("symbol size mismatch: %lu != %lu",
3458 (unsigned long) sec->header.sh_entsize, 3458 (unsigned long) sec->header.sh_entsize,
3459 (unsigned long) sizeof(ElfW(Sym))); 3459 (unsigned long) sizeof(ElfW(Sym)));
3460 return NULL; 3460 return NULL;
@@ -3484,7 +3484,7 @@ static struct obj_file *obj_load(FILE * fp, int loadprogbits)
3484 3484
3485 case SHT_RELM: 3485 case SHT_RELM:
3486 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) { 3486 if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
3487 error_msg("relocation entry size mismatch: %lu != %lu", 3487 bb_error_msg("relocation entry size mismatch: %lu != %lu",
3488 (unsigned long) sec->header.sh_entsize, 3488 (unsigned long) sec->header.sh_entsize,
3489 (unsigned long) sizeof(ElfW(RelM))); 3489 (unsigned long) sizeof(ElfW(RelM)));
3490 return NULL; 3490 return NULL;
@@ -3523,7 +3523,7 @@ static int obj_load_progbits(FILE * fp, struct obj_file* f, char* imagebase)
3523 sec->contents = imagebase + (sec->header.sh_addr - base); 3523 sec->contents = imagebase + (sec->header.sh_addr - base);
3524 fseek(fp, sec->header.sh_offset, SEEK_SET); 3524 fseek(fp, sec->header.sh_offset, SEEK_SET);
3525 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) { 3525 if (fread(sec->contents, sec->header.sh_size, 1, fp) != 1) {
3526 error_msg("error reading ELF section data: %s\n", strerror(errno)); 3526 bb_error_msg("error reading ELF section data: %s\n", strerror(errno));
3527 return 0; 3527 return 0;
3528 } 3528 }
3529 3529
@@ -3710,14 +3710,14 @@ add_ksymoops_symbols(struct obj_file *f, const char *filename,
3710 }; 3710 };
3711 3711
3712 if (realpath(filename, real)) { 3712 if (realpath(filename, real)) {
3713 absolute_filename = xstrdup(real); 3713 absolute_filename = bb_xstrdup(real);
3714 } 3714 }
3715 else { 3715 else {
3716 int save_errno = errno; 3716 int save_errno = errno;
3717 error_msg("cannot get realpath for %s", filename); 3717 bb_error_msg("cannot get realpath for %s", filename);
3718 errno = save_errno; 3718 errno = save_errno;
3719 perror(""); 3719 perror("");
3720 absolute_filename = xstrdup(filename); 3720 absolute_filename = bb_xstrdup(filename);
3721 } 3721 }
3722 3722
3723 lm_name = strlen(m_name); 3723 lm_name = strlen(m_name);
@@ -3953,7 +3953,7 @@ extern int insmod_main( int argc, char **argv)
3953 break; 3953 break;
3954 case 'o': /* name the output module */ 3954 case 'o': /* name the output module */
3955 free(m_name); 3955 free(m_name);
3956 m_name = xstrdup(optarg); 3956 m_name = bb_xstrdup(optarg);
3957 break; 3957 break;
3958 case 'L': /* Stub warning */ 3958 case 'L': /* Stub warning */
3959 /* This is needed for compatibility with modprobe. 3959 /* This is needed for compatibility with modprobe.
@@ -3967,16 +3967,16 @@ extern int insmod_main( int argc, char **argv)
3967 break; 3967 break;
3968#endif 3968#endif
3969 default: 3969 default:
3970 show_usage(); 3970 bb_show_usage();
3971 } 3971 }
3972 } 3972 }
3973 3973
3974 if (argv[optind] == NULL) { 3974 if (argv[optind] == NULL) {
3975 show_usage(); 3975 bb_show_usage();
3976 } 3976 }
3977 3977
3978 /* Grab the module name */ 3978 /* Grab the module name */
3979 tmp1 = xstrdup(argv[optind]); 3979 tmp1 = bb_xstrdup(argv[optind]);
3980 tmp = basename(tmp1); 3980 tmp = basename(tmp1);
3981 len = strlen(tmp); 3981 len = strlen(tmp);
3982 3982
@@ -3985,7 +3985,7 @@ extern int insmod_main( int argc, char **argv)
3985 tmp[len] = '\0'; 3985 tmp[len] = '\0';
3986 } 3986 }
3987 3987
3988 bb_asprintf(&m_fullName, "%s.o", tmp); 3988 bb_xasprintf(&m_fullName, "%s.o", tmp);
3989 3989
3990 if (!m_name) { 3990 if (!m_name) {
3991 m_name = tmp; 3991 m_name = tmp;
@@ -4038,19 +4038,19 @@ extern int insmod_main( int argc, char **argv)
4038 if (m_filename == 0 4038 if (m_filename == 0
4039 || ((fp = fopen(m_filename, "r")) == NULL)) 4039 || ((fp = fopen(m_filename, "r")) == NULL))
4040 { 4040 {
4041 error_msg("%s: no module by that name found", m_fullName); 4041 bb_error_msg("%s: no module by that name found", m_fullName);
4042 goto out; 4042 goto out;
4043 } 4043 }
4044 } else 4044 } else
4045 error_msg_and_die("%s: no module by that name found", m_fullName); 4045 bb_error_msg_and_die("%s: no module by that name found", m_fullName);
4046 } 4046 }
4047 } else 4047 } else
4048 m_filename = xstrdup(argv[optind]); 4048 m_filename = bb_xstrdup(argv[optind]);
4049 4049
4050 printf("Using %s\n", m_filename); 4050 printf("Using %s\n", m_filename);
4051 4051
4052 if ((f = obj_load(fp, LOADBITS)) == NULL) 4052 if ((f = obj_load(fp, LOADBITS)) == NULL)
4053 perror_msg_and_die("Could not load the module"); 4053 bb_perror_msg_and_die("Could not load the module");
4054 4054
4055 if (get_modinfo_value(f, "kernel_version") == NULL) 4055 if (get_modinfo_value(f, "kernel_version") == NULL)
4056 m_has_modinfo = 0; 4056 m_has_modinfo = 0;
@@ -4067,7 +4067,7 @@ extern int insmod_main( int argc, char **argv)
4067 } else { 4067 } else {
4068 m_version = old_get_module_version(f, m_strversion); 4068 m_version = old_get_module_version(f, m_strversion);
4069 if (m_version == -1) { 4069 if (m_version == -1) {
4070 error_msg("couldn't find the kernel version the module was " 4070 bb_error_msg("couldn't find the kernel version the module was "
4071 "compiled for"); 4071 "compiled for");
4072 goto out; 4072 goto out;
4073 } 4073 }
@@ -4075,12 +4075,12 @@ extern int insmod_main( int argc, char **argv)
4075 4075
4076 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) { 4076 if (strncmp(uts_info.release, m_strversion, STRVERSIONLEN) != 0) {
4077 if (flag_force_load) { 4077 if (flag_force_load) {
4078 error_msg("Warning: kernel-module version mismatch\n" 4078 bb_error_msg("Warning: kernel-module version mismatch\n"
4079 "\t%s was compiled for kernel version %s\n" 4079 "\t%s was compiled for kernel version %s\n"
4080 "\twhile this kernel is version %s", 4080 "\twhile this kernel is version %s",
4081 m_filename, m_strversion, uts_info.release); 4081 m_filename, m_strversion, uts_info.release);
4082 } else { 4082 } else {
4083 error_msg("kernel-module version mismatch\n" 4083 bb_error_msg("kernel-module version mismatch\n"
4084 "\t%s was compiled for kernel version %s\n" 4084 "\t%s was compiled for kernel version %s\n"
4085 "\twhile this kernel is version %s.", 4085 "\twhile this kernel is version %s.",
4086 m_filename, m_strversion, uts_info.release); 4086 m_filename, m_strversion, uts_info.release);
@@ -4099,7 +4099,7 @@ extern int insmod_main( int argc, char **argv)
4099 goto out; 4099 goto out;
4100 k_crcs = new_is_kernel_checksummed(); 4100 k_crcs = new_is_kernel_checksummed();
4101#else 4101#else
4102 error_msg("Not configured to support new kernels"); 4102 bb_error_msg("Not configured to support new kernels");
4103 goto out; 4103 goto out;
4104#endif 4104#endif
4105 } else { 4105 } else {
@@ -4108,7 +4108,7 @@ extern int insmod_main( int argc, char **argv)
4108 goto out; 4108 goto out;
4109 k_crcs = old_is_kernel_checksummed(); 4109 k_crcs = old_is_kernel_checksummed();
4110#else 4110#else
4111 error_msg("Not configured to support old kernels"); 4111 bb_error_msg("Not configured to support old kernels");
4112 goto out; 4112 goto out;
4113#endif 4113#endif
4114 } 4114 }
@@ -4170,14 +4170,14 @@ extern int insmod_main( int argc, char **argv)
4170 m_addr = create_module(m_name, m_size); 4170 m_addr = create_module(m_name, m_size);
4171 if (m_addr == -1) switch (errno) { 4171 if (m_addr == -1) switch (errno) {
4172 case EEXIST: 4172 case EEXIST:
4173 error_msg("A module named %s already exists", m_name); 4173 bb_error_msg("A module named %s already exists", m_name);
4174 goto out; 4174 goto out;
4175 case ENOMEM: 4175 case ENOMEM:
4176 error_msg("Can't allocate kernel memory for module; needed %lu bytes", 4176 bb_error_msg("Can't allocate kernel memory for module; needed %lu bytes",
4177 m_size); 4177 m_size);
4178 goto out; 4178 goto out;
4179 default: 4179 default:
4180 perror_msg("create_module: %s", m_name); 4180 bb_perror_msg("create_module: %s", m_name);
4181 goto out; 4181 goto out;
4182 } 4182 }
4183 4183
diff --git a/modutils/lsmod.c b/modutils/lsmod.c
index b87a26a57..bb9947a23 100644
--- a/modutils/lsmod.c
+++ b/modutils/lsmod.c
@@ -108,7 +108,7 @@ extern int lsmod_main(int argc, char **argv)
108 module_names = xmalloc(bufsize = 256); 108 module_names = xmalloc(bufsize = 256);
109 if (my_query_module(NULL, QM_MODULES, (void **)&module_names, &bufsize, 109 if (my_query_module(NULL, QM_MODULES, (void **)&module_names, &bufsize,
110 &nmod)) { 110 &nmod)) {
111 perror_msg_and_die("QM_MODULES"); 111 bb_perror_msg_and_die("QM_MODULES");
112 } 112 }
113 113
114 deps = xmalloc(depsize = 256); 114 deps = xmalloc(depsize = 256);
@@ -122,14 +122,14 @@ extern int lsmod_main(int argc, char **argv)
122 continue; 122 continue;
123 } 123 }
124 /* else choke */ 124 /* else choke */
125 perror_msg_and_die("module %s: QM_INFO", mn); 125 bb_perror_msg_and_die("module %s: QM_INFO", mn);
126 } 126 }
127 if (my_query_module(mn, QM_REFS, (void **)&deps, &depsize, &count)) { 127 if (my_query_module(mn, QM_REFS, (void **)&deps, &depsize, &count)) {
128 if (errno == ENOENT) { 128 if (errno == ENOENT) {
129 /* The module was removed out from underneath us. */ 129 /* The module was removed out from underneath us. */
130 continue; 130 continue;
131 } 131 }
132 perror_msg_and_die("module %s: QM_REFS", mn); 132 bb_perror_msg_and_die("module %s: QM_REFS", mn);
133 } 133 }
134 printf("%-20s%8lu%4ld", mn, info.size, info.usecount); 134 printf("%-20s%8lu%4ld", mn, info.size, info.usecount);
135 if (info.flags & NEW_MOD_DELETED) 135 if (info.flags & NEW_MOD_DELETED)
@@ -167,9 +167,10 @@ extern int lsmod_main(int argc, char **argv)
167 printf("Module Size Used by"); 167 printf("Module Size Used by");
168 check_tainted(); 168 check_tainted();
169 169
170 if(print_file_by_name("/proc/modules") == FALSE) 170 if (bb_xprint_file_by_name("/proc/modules") < 0) {
171 return 0;
172 }
171 return 1; 173 return 1;
172 return 0;
173} 174}
174 175
175#endif /* CONFIG_FEATURE_QUERY_MODULE_INTERFACE */ 176#endif /* CONFIG_FEATURE_QUERY_MODULE_INTERFACE */
diff --git a/modutils/modprobe.c b/modutils/modprobe.c
index 99650ff03..154e66224 100644
--- a/modutils/modprobe.c
+++ b/modutils/modprobe.c
@@ -73,7 +73,7 @@ int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
73 *ptag = tag; 73 *ptag = tag;
74 *pvalue = value; 74 *pvalue = value;
75 75
76 return xstrlen( tag ) && xstrlen( value ); 76 return bb_strlen( tag ) && bb_strlen( value );
77} 77}
78 78
79/* Jump through hoops to simulate how fgets() grabs just one line at a 79/* Jump through hoops to simulate how fgets() grabs just one line at a
@@ -119,7 +119,7 @@ static struct dep_t *build_dep ( void )
119 return 0; 119 return 0;
120 120
121 // check for buffer overflow in following code 121 // check for buffer overflow in following code
122 if ( xstrlen ( un.release ) > ( sizeof( buffer ) - 64 )) { 122 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
123 return 0; 123 return 0;
124 } 124 }
125 125
@@ -131,7 +131,7 @@ static struct dep_t *build_dep ( void )
131 return 0; 131 return 0;
132 132
133 while ( reads ( fd, buffer, sizeof( buffer ))) { 133 while ( reads ( fd, buffer, sizeof( buffer ))) {
134 int l = xstrlen ( buffer ); 134 int l = bb_strlen ( buffer );
135 char *p = 0; 135 char *p = 0;
136 136
137 while ( isspace ( buffer [l-1] )) { 137 while ( isspace ( buffer [l-1] )) {
@@ -163,7 +163,7 @@ static struct dep_t *build_dep ( void )
163 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' )) 163 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
164 ext = 2; 164 ext = 2;
165 165
166 mod = xstrndup ( mods, col - mods - ext ); 166 mod = bb_xstrndup ( mods, col - mods - ext );
167 167
168 if ( !current ) { 168 if ( !current ) {
169 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t )); 169 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
@@ -216,7 +216,7 @@ static struct dep_t *build_dep ( void )
216 if ((end-deps-ext+1) <= 0) 216 if ((end-deps-ext+1) <= 0)
217 continue; 217 continue;
218 218
219 dep = xstrndup ( deps, end - deps - ext + 1 ); 219 dep = bb_xstrndup ( deps, end - deps - ext + 1 );
220 220
221 current-> m_depcnt++; 221 current-> m_depcnt++;
222 current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt ); 222 current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt );
@@ -247,7 +247,7 @@ static struct dep_t *build_dep ( void )
247 if ( p ) 247 if ( p )
248 *p = 0; 248 *p = 0;
249 249
250 l = xstrlen ( buffer ); 250 l = bb_strlen ( buffer );
251 251
252 while ( l && isspace ( buffer [l-1] )) { 252 while ( l && isspace ( buffer [l-1] )) {
253 buffer [l-1] = 0; 253 buffer [l-1] = 0;
@@ -273,7 +273,7 @@ static struct dep_t *build_dep ( void )
273 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t )); 273 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
274 current = current-> m_next; 274 current = current-> m_next;
275 } 275 }
276 current-> m_module = xstrdup ( alias ); 276 current-> m_module = bb_xstrdup ( alias );
277 current-> m_isalias = 1; 277 current-> m_isalias = 1;
278 278
279 if (( strcmp ( alias, "off" ) == 0 ) || ( strcmp ( alias, "null" ) == 0 )) { 279 if (( strcmp ( alias, "off" ) == 0 ) || ( strcmp ( alias, "null" ) == 0 )) {
@@ -283,7 +283,7 @@ static struct dep_t *build_dep ( void )
283 else { 283 else {
284 current-> m_depcnt = 1; 284 current-> m_depcnt = 1;
285 current-> m_deparr = xmalloc ( 1 * sizeof( char * )); 285 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
286 current-> m_deparr[0] = xstrdup ( mod ); 286 current-> m_deparr[0] = bb_xstrdup ( mod );
287 } 287 }
288 current-> m_next = 0; 288 current-> m_next = 0;
289 } 289 }
@@ -299,7 +299,7 @@ static struct dep_t *build_dep ( void )
299 break; 299 break;
300 } 300 }
301 if ( dt ) { 301 if ( dt ) {
302 dt-> m_options = xrealloc ( dt-> m_options, xstrlen( opt ) + 1 ); 302 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
303 strcpy ( dt-> m_options, opt ); 303 strcpy ( dt-> m_options, opt );
304 304
305 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_module, dt-> m_options ); 305 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_module, dt-> m_options );
@@ -346,7 +346,7 @@ static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t *
346 int lm; 346 int lm;
347 347
348 // remove .o extension 348 // remove .o extension
349 lm = xstrlen ( mod ); 349 lm = bb_strlen ( mod );
350 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' )) 350 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
351 mod [lm-2] = 0; 351 mod [lm-2] = 0;
352 352
@@ -441,7 +441,7 @@ static int mod_insert ( char *mod, int argc, char **argv )
441 441
442 // append module args 442 // append module args
443 for ( i = 0; i < argc; i++ ) 443 for ( i = 0; i < argc; i++ )
444 l += ( xstrlen ( argv [i] ) + 1 ); 444 l += ( bb_strlen ( argv [i] ) + 1 );
445 445
446 head-> m_options = xrealloc ( head-> m_options, l + 1 ); 446 head-> m_options = xrealloc ( head-> m_options, l + 1 );
447 head-> m_options [0] = 0; 447 head-> m_options [0] = 0;
@@ -494,7 +494,7 @@ extern int modprobe_main(int argc, char** argv)
494 break; 494 break;
495 case 'C': // no config used 495 case 'C': // no config used
496 case 't': // no pattern matching 496 case 't': // no pattern matching
497 error_msg_and_die("-t and -C not supported"); 497 bb_error_msg_and_die("-t and -C not supported");
498 498
499 case 'a': // ignore 499 case 'a': // ignore
500 case 'd': // ignore 500 case 'd': // ignore
@@ -519,7 +519,7 @@ extern int modprobe_main(int argc, char** argv)
519 break; 519 break;
520 case 'V': 520 case 'V':
521 default: 521 default:
522 show_usage(); 522 bb_show_usage();
523 break; 523 break;
524 } 524 }
525 } 525 }
@@ -527,21 +527,21 @@ extern int modprobe_main(int argc, char** argv)
527 depend = build_dep ( ); 527 depend = build_dep ( );
528 528
529 if ( !depend ) 529 if ( !depend )
530 error_msg_and_die ( "could not parse modules.dep\n" ); 530 bb_error_msg_and_die ( "could not parse modules.dep\n" );
531 531
532 if (remove_opt) { 532 if (remove_opt) {
533 do { 533 do {
534 mod_remove ( optind < argc ? xstrdup ( argv [optind] ) : NULL ); 534 mod_remove ( optind < argc ? bb_xstrdup ( argv [optind] ) : NULL );
535 } while ( ++optind < argc ); 535 } while ( ++optind < argc );
536 536
537 return EXIT_SUCCESS; 537 return EXIT_SUCCESS;
538 } 538 }
539 539
540 if (optind >= argc) 540 if (optind >= argc)
541 error_msg_and_die ( "No module or pattern provided\n" ); 541 bb_error_msg_and_die ( "No module or pattern provided\n" );
542 542
543 if ( mod_insert ( xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 )) 543 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
544 error_msg_and_die ( "failed to load module %s", argv [optind] ); 544 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
545 545
546 return EXIT_SUCCESS; 546 return EXIT_SUCCESS;
547} 547}
diff --git a/modutils/rmmod.c b/modutils/rmmod.c
index 0103d9145..3154b30cc 100644
--- a/modutils/rmmod.c
+++ b/modutils/rmmod.c
@@ -48,11 +48,11 @@ extern int rmmod_main(int argc, char **argv)
48 buf = xmalloc(bufsize = 256); 48 buf = xmalloc(bufsize = 256);
49 while (nmod != pnmod) { 49 while (nmod != pnmod) {
50 if (delete_module(NULL)) 50 if (delete_module(NULL))
51 perror_msg_and_die("rmmod"); 51 bb_perror_msg_and_die("rmmod");
52 pnmod = nmod; 52 pnmod = nmod;
53 /* 1 == QM_MODULES */ 53 /* 1 == QM_MODULES */
54 if (my_query_module(NULL, 1, &buf, &bufsize, &nmod)) { 54 if (my_query_module(NULL, 1, &buf, &bufsize, &nmod)) {
55 perror_msg_and_die("QM_MODULES"); 55 bb_perror_msg_and_die("QM_MODULES");
56 } 56 }
57 } 57 }
58#ifdef CONFIG_FEATURE_CLEAN_UP 58#ifdef CONFIG_FEATURE_CLEAN_UP
@@ -60,16 +60,16 @@ extern int rmmod_main(int argc, char **argv)
60#endif 60#endif
61 return EXIT_SUCCESS; 61 return EXIT_SUCCESS;
62 default: 62 default:
63 show_usage(); 63 bb_show_usage();
64 } 64 }
65 } 65 }
66 66
67 if (optind == argc) 67 if (optind == argc)
68 show_usage(); 68 bb_show_usage();
69 69
70 for (n = optind; n < argc; n++) { 70 for (n = optind; n < argc; n++) {
71 if (delete_module(argv[n]) < 0) { 71 if (delete_module(argv[n]) < 0) {
72 perror_msg("%s", argv[n]); 72 bb_perror_msg("%s", argv[n]);
73 ret = EXIT_FAILURE; 73 ret = EXIT_FAILURE;
74 } 74 }
75 } 75 }
diff --git a/networking/arping.c b/networking/arping.c
index bf0a77cf4..2e1adf0a2 100644
--- a/networking/arping.c
+++ b/networking/arping.c
@@ -302,9 +302,9 @@ int arping_main(int argc, char **argv)
302 break; 302 break;
303 case 'I': 303 case 'I':
304 if (optarg == NULL) 304 if (optarg == NULL)
305 show_usage(); 305 bb_show_usage();
306 if (xstrlen(optarg) > IF_NAMESIZE) { 306 if (bb_strlen(optarg) > IF_NAMESIZE) {
307 error_msg("Interface name `%s' must be less than %d", optarg, 307 bb_error_msg("Interface name `%s' must be less than %d", optarg,
308 IF_NAMESIZE); 308 IF_NAMESIZE);
309 exit(2); 309 exit(2);
310 } 310 }
@@ -319,20 +319,20 @@ int arping_main(int argc, char **argv)
319 case 'h': 319 case 'h':
320 case '?': 320 case '?':
321 default: 321 default:
322 show_usage(); 322 bb_show_usage();
323 } 323 }
324 } 324 }
325 argc -= optind; 325 argc -= optind;
326 argv += optind; 326 argv += optind;
327 327
328 if (argc != 1) 328 if (argc != 1)
329 show_usage(); 329 bb_show_usage();
330 330
331 target = *argv; 331 target = *argv;
332 332
333 333
334 if (s < 0) { 334 if (s < 0) {
335 error_msg("socket"); 335 bb_error_msg("socket");
336 exit(socket_errno); 336 exit(socket_errno);
337 } 337 }
338 338
@@ -342,21 +342,21 @@ int arping_main(int argc, char **argv)
342 memset(&ifr, 0, sizeof(ifr)); 342 memset(&ifr, 0, sizeof(ifr));
343 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1); 343 strncpy(ifr.ifr_name, device, IFNAMSIZ - 1);
344 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) { 344 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
345 error_msg("Interface %s not found", device); 345 bb_error_msg("Interface %s not found", device);
346 exit(2); 346 exit(2);
347 } 347 }
348 ifindex = ifr.ifr_ifindex; 348 ifindex = ifr.ifr_ifindex;
349 349
350 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) { 350 if (ioctl(s, SIOCGIFFLAGS, (char *) &ifr)) {
351 error_msg("SIOCGIFFLAGS"); 351 bb_error_msg("SIOCGIFFLAGS");
352 exit(2); 352 exit(2);
353 } 353 }
354 if (!(ifr.ifr_flags & IFF_UP)) { 354 if (!(ifr.ifr_flags & IFF_UP)) {
355 error_msg("Interface %s is down", device); 355 bb_error_msg("Interface %s is down", device);
356 exit(2); 356 exit(2);
357 } 357 }
358 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) { 358 if (ifr.ifr_flags & (IFF_NOARP | IFF_LOOPBACK)) {
359 error_msg("Interface %s is not ARPable", device); 359 bb_error_msg("Interface %s is not ARPable", device);
360 exit(dad ? 0 : 2); 360 exit(dad ? 0 : 2);
361 } 361 }
362 } 362 }
@@ -366,14 +366,14 @@ int arping_main(int argc, char **argv)
366 366
367 hp = gethostbyname2(target, AF_INET); 367 hp = gethostbyname2(target, AF_INET);
368 if (!hp) { 368 if (!hp) {
369 error_msg("invalid or unknown target %s", target); 369 bb_error_msg("invalid or unknown target %s", target);
370 exit(2); 370 exit(2);
371 } 371 }
372 memcpy(&dst, hp->h_addr, 4); 372 memcpy(&dst, hp->h_addr, 4);
373 } 373 }
374 374
375 if (source && !inet_aton(source, &src)) { 375 if (source && !inet_aton(source, &src)) {
376 error_msg("invalid source address %s", source); 376 bb_error_msg("invalid source address %s", source);
377 exit(2); 377 exit(2);
378 } 378 }
379 379
@@ -385,21 +385,21 @@ int arping_main(int argc, char **argv)
385 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0); 385 int probe_fd = socket(AF_INET, SOCK_DGRAM, 0);
386 386
387 if (probe_fd < 0) { 387 if (probe_fd < 0) {
388 error_msg("socket"); 388 bb_error_msg("socket");
389 exit(2); 389 exit(2);
390 } 390 }
391 if (device) { 391 if (device) {
392 if (setsockopt 392 if (setsockopt
393 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device, 393 (probe_fd, SOL_SOCKET, SO_BINDTODEVICE, device,
394 strlen(device) + 1) == -1) 394 strlen(device) + 1) == -1)
395 error_msg("WARNING: interface %s is ignored", device); 395 bb_error_msg("WARNING: interface %s is ignored", device);
396 } 396 }
397 memset(&saddr, 0, sizeof(saddr)); 397 memset(&saddr, 0, sizeof(saddr));
398 saddr.sin_family = AF_INET; 398 saddr.sin_family = AF_INET;
399 if (src.s_addr) { 399 if (src.s_addr) {
400 saddr.sin_addr = src; 400 saddr.sin_addr = src;
401 if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) { 401 if (bind(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) == -1) {
402 error_msg("bind"); 402 bb_error_msg("bind");
403 exit(2); 403 exit(2);
404 } 404 }
405 } else if (!dad) { 405 } else if (!dad) {
@@ -415,12 +415,12 @@ int arping_main(int argc, char **argv)
415 perror("WARNING: setsockopt(SO_DONTROUTE)"); 415 perror("WARNING: setsockopt(SO_DONTROUTE)");
416 if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr)) 416 if (connect(probe_fd, (struct sockaddr *) &saddr, sizeof(saddr))
417 == -1) { 417 == -1) {
418 error_msg("connect"); 418 bb_error_msg("connect");
419 exit(2); 419 exit(2);
420 } 420 }
421 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) == 421 if (getsockname(probe_fd, (struct sockaddr *) &saddr, &alen) ==
422 -1) { 422 -1) {
423 error_msg("getsockname"); 423 bb_error_msg("getsockname");
424 exit(2); 424 exit(2);
425 } 425 }
426 src = saddr.sin_addr; 426 src = saddr.sin_addr;
@@ -432,7 +432,7 @@ int arping_main(int argc, char **argv)
432 me.sll_ifindex = ifindex; 432 me.sll_ifindex = ifindex;
433 me.sll_protocol = htons(ETH_P_ARP); 433 me.sll_protocol = htons(ETH_P_ARP);
434 if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) { 434 if (bind(s, (struct sockaddr *) &me, sizeof(me)) == -1) {
435 error_msg("bind"); 435 bb_error_msg("bind");
436 exit(2); 436 exit(2);
437 } 437 }
438 438
@@ -440,12 +440,12 @@ int arping_main(int argc, char **argv)
440 int alen = sizeof(me); 440 int alen = sizeof(me);
441 441
442 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) { 442 if (getsockname(s, (struct sockaddr *) &me, &alen) == -1) {
443 error_msg("getsockname"); 443 bb_error_msg("getsockname");
444 exit(2); 444 exit(2);
445 } 445 }
446 } 446 }
447 if (me.sll_halen == 0) { 447 if (me.sll_halen == 0) {
448 error_msg("Interface \"%s\" is not ARPable (no ll address)", device); 448 bb_error_msg("Interface \"%s\" is not ARPable (no ll address)", device);
449 exit(dad ? 0 : 2); 449 exit(dad ? 0 : 2);
450 } 450 }
451 he = me; 451 he = me;
@@ -458,7 +458,7 @@ int arping_main(int argc, char **argv)
458 } 458 }
459 459
460 if (!src.s_addr && !dad) { 460 if (!src.s_addr && !dad) {
461 error_msg("no src address in the non-DAD mode"); 461 bb_error_msg("no src address in the non-DAD mode");
462 exit(2); 462 exit(2);
463 } 463 }
464 464
diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c
index 44e91c37a..394389043 100644
--- a/networking/ftpgetput.c
+++ b/networking/ftpgetput.c
@@ -77,7 +77,7 @@ static ftp_host_info_t *ftp_init(void)
77static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf) 77static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
78{ 78{
79 if (verbose_flag) { 79 if (verbose_flag) {
80 error_msg("cmd %s%s", s1, s2); 80 bb_error_msg("cmd %s%s", s1, s2);
81 } 81 }
82 82
83 if (s1) { 83 if (s1) {
@@ -90,7 +90,7 @@ static int ftpcmd(const char *s1, const char *s2, FILE *stream, char *buf)
90 90
91 do { 91 do {
92 if (fgets(buf, 510, stream) == NULL) { 92 if (fgets(buf, 510, stream) == NULL) {
93 perror_msg_and_die("fgets()"); 93 bb_perror_msg_and_die("fgets()");
94 } 94 }
95 } while (! isdigit(buf[0]) || buf[3] != ' '); 95 } while (! isdigit(buf[0]) || buf[3] != ' ');
96 96
@@ -125,11 +125,11 @@ static FILE *ftp_login(ftp_host_info_t *server)
125 control_fd = xconnect(server->host, server->port); 125 control_fd = xconnect(server->host, server->port);
126 control_stream = fdopen(control_fd, "r+"); 126 control_stream = fdopen(control_fd, "r+");
127 if (control_stream == NULL) { 127 if (control_stream == NULL) {
128 perror_msg_and_die("Couldnt open control stream"); 128 bb_perror_msg_and_die("Couldnt open control stream");
129 } 129 }
130 130
131 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) { 131 if (ftpcmd(NULL, NULL, control_stream, buf) != 220) {
132 error_msg_and_die("%s", buf + 4); 132 bb_error_msg_and_die("%s", buf + 4);
133 } 133 }
134 134
135 /* Login to the server */ 135 /* Login to the server */
@@ -138,11 +138,11 @@ static FILE *ftp_login(ftp_host_info_t *server)
138 break; 138 break;
139 case 331: 139 case 331:
140 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) { 140 if (ftpcmd("PASS ", server->password, control_stream, buf) != 230) {
141 error_msg_and_die("PASS error: %s", buf + 4); 141 bb_error_msg_and_die("PASS error: %s", buf + 4);
142 } 142 }
143 break; 143 break;
144 default: 144 default:
145 error_msg_and_die("USER error: %s", buf + 4); 145 bb_error_msg_and_die("USER error: %s", buf + 4);
146 } 146 }
147 147
148 ftpcmd("TYPE I", NULL, control_stream, buf); 148 ftpcmd("TYPE I", NULL, control_stream, buf);
@@ -161,12 +161,12 @@ static int ftp_recieve(FILE *control_stream, const char *host, const char *local
161 int fd_local; 161 int fd_local;
162 off_t beg_range = 0; 162 off_t beg_range = 0;
163 163
164 filename = get_last_path_component(server_path); 164 filename = bb_get_last_path_component(server_path);
165 local_file = concat_path_file(local_path, filename); 165 local_file = concat_path_file(local_path, filename);
166 166
167 /* Connect to the data socket */ 167 /* Connect to the data socket */
168 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { 168 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
169 error_msg_and_die("PASV error: %s", buf + 4); 169 bb_error_msg_and_die("PASV error: %s", buf + 4);
170 } 170 }
171 fd_data = xconnect_ftpdata(host, buf); 171 fd_data = xconnect_ftpdata(host, buf);
172 172
@@ -177,7 +177,7 @@ static int ftp_recieve(FILE *control_stream, const char *host, const char *local
177 if (do_continue) { 177 if (do_continue) {
178 struct stat sbuf; 178 struct stat sbuf;
179 if (lstat(local_file, &sbuf) < 0) { 179 if (lstat(local_file, &sbuf) < 0) {
180 perror_msg_and_die("fstat()"); 180 bb_perror_msg_and_die("fstat()");
181 } 181 }
182 if (sbuf.st_size > 0) { 182 if (sbuf.st_size > 0) {
183 beg_range = sbuf.st_size; 183 beg_range = sbuf.st_size;
@@ -196,25 +196,25 @@ static int ftp_recieve(FILE *control_stream, const char *host, const char *local
196 } 196 }
197 197
198 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) { 198 if (ftpcmd("RETR ", server_path, control_stream, buf) > 150) {
199 error_msg_and_die("RETR error: %s", buf + 4); 199 bb_error_msg_and_die("RETR error: %s", buf + 4);
200 } 200 }
201 201
202 /* only make a local file if we know that one exists on the remote server */ 202 /* only make a local file if we know that one exists on the remote server */
203 if (do_continue) { 203 if (do_continue) {
204 fd_local = xopen(local_file, O_APPEND | O_WRONLY); 204 fd_local = bb_xopen(local_file, O_APPEND | O_WRONLY);
205 } else { 205 } else {
206 fd_local = xopen(local_file, O_CREAT | O_TRUNC | O_WRONLY); 206 fd_local = bb_xopen(local_file, O_CREAT | O_TRUNC | O_WRONLY);
207 } 207 }
208 208
209 /* Copy the file */ 209 /* Copy the file */
210 if (copyfd(fd_data, fd_local, filesize) == -1) { 210 if (bb_copyfd(fd_data, fd_local, filesize) == -1) {
211 exit(EXIT_FAILURE); 211 exit(EXIT_FAILURE);
212 } 212 }
213 213
214 /* close it all down */ 214 /* close it all down */
215 close(fd_data); 215 close(fd_data);
216 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { 216 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
217 error_msg_and_die("ftp error: %s", buf + 4); 217 bb_error_msg_and_die("ftp error: %s", buf + 4);
218 } 218 }
219 ftpcmd("QUIT", NULL, control_stream, buf); 219 ftpcmd("QUIT", NULL, control_stream, buf);
220 220
@@ -233,16 +233,16 @@ static int ftp_send(FILE *control_stream, const char *host, const char *server_p
233 233
234 /* Connect to the data socket */ 234 /* Connect to the data socket */
235 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) { 235 if (ftpcmd("PASV", NULL, control_stream, buf) != 227) {
236 error_msg_and_die("PASV error: %s", buf + 4); 236 bb_error_msg_and_die("PASV error: %s", buf + 4);
237 } 237 }
238 fd_data = xconnect_ftpdata(host, buf); 238 fd_data = xconnect_ftpdata(host, buf);
239 239
240 if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) { 240 if (ftpcmd("CWD ", server_path, control_stream, buf) != 250) {
241 error_msg_and_die("CWD error: %s", buf + 4); 241 bb_error_msg_and_die("CWD error: %s", buf + 4);
242 } 242 }
243 243
244 /* get the local file */ 244 /* get the local file */
245 fd_local = xopen(local_path, O_RDONLY); 245 fd_local = bb_xopen(local_path, O_RDONLY);
246 fstat(fd_local, &sbuf); 246 fstat(fd_local, &sbuf);
247 247
248 sprintf(buf, "ALLO %lu", sbuf.st_size); 248 sprintf(buf, "ALLO %lu", sbuf.st_size);
@@ -253,7 +253,7 @@ static int ftp_send(FILE *control_stream, const char *host, const char *server_p
253 break; 253 break;
254 default: 254 default:
255 close(fd_local); 255 close(fd_local);
256 error_msg_and_die("ALLO error: %s", buf + 4); 256 bb_error_msg_and_die("ALLO error: %s", buf + 4);
257 break; 257 break;
258 } 258 }
259 259
@@ -264,18 +264,18 @@ static int ftp_send(FILE *control_stream, const char *host, const char *server_p
264 break; 264 break;
265 default: 265 default:
266 close(fd_local); 266 close(fd_local);
267 error_msg_and_die("STOR error: %s", buf + 4); 267 bb_error_msg_and_die("STOR error: %s", buf + 4);
268 } 268 }
269 269
270 /* transfer the file */ 270 /* transfer the file */
271 if (copyfd(fd_local, fd_data, 0) == -1) { 271 if (bb_copyfd(fd_local, fd_data, 0) == -1) {
272 exit(EXIT_FAILURE); 272 exit(EXIT_FAILURE);
273 } 273 }
274 274
275 /* close it all down */ 275 /* close it all down */
276 close(fd_data); 276 close(fd_data);
277 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) { 277 if (ftpcmd(NULL, NULL, control_stream, buf) != 226) {
278 error_msg_and_die("error: %s", buf + 4); 278 bb_error_msg_and_die("error: %s", buf + 4);
279 } 279 }
280 ftpcmd("QUIT", NULL, control_stream, buf); 280 ftpcmd("QUIT", NULL, control_stream, buf);
281 281
@@ -307,12 +307,12 @@ int ftpgetput_main(int argc, char **argv)
307 }; 307 };
308 308
309#ifdef CONFIG_FTPPUT 309#ifdef CONFIG_FTPPUT
310 if (applet_name[3] == 'p') { 310 if (bb_applet_name[3] == 'p') {
311 ftp_action = ftp_send; 311 ftp_action = ftp_send;
312 } 312 }
313#endif 313#endif
314#ifdef CONFIG_FTPGET 314#ifdef CONFIG_FTPGET
315 if (applet_name[3] == 'g') { 315 if (bb_applet_name[3] == 'g') {
316 ftp_action = ftp_recieve; 316 ftp_action = ftp_recieve;
317 } 317 }
318#endif 318#endif
@@ -342,7 +342,7 @@ int ftpgetput_main(int argc, char **argv)
342 verbose_flag = 1; 342 verbose_flag = 1;
343 break; 343 break;
344 default: 344 default:
345 show_usage(); 345 bb_show_usage();
346 } 346 }
347 } 347 }
348 348
@@ -350,7 +350,7 @@ int ftpgetput_main(int argc, char **argv)
350 * Process the non-option command line arguments 350 * Process the non-option command line arguments
351 */ 351 */
352 if (argc - optind != 3) { 352 if (argc - optind != 3) {
353 show_usage(); 353 bb_show_usage();
354 } 354 }
355 355
356 /* Connect/Setup/Configure the FTP session */ 356 /* Connect/Setup/Configure the FTP session */
diff --git a/networking/hostname.c b/networking/hostname.c
index f7c9fc426..6522bb21b 100644
--- a/networking/hostname.c
+++ b/networking/hostname.c
@@ -1,6 +1,6 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * $Id: hostname.c,v 1.34 2002/11/10 22:07:48 bug1 Exp $ 3 * $Id: hostname.c,v 1.35 2003/03/19 09:12:37 mjn3 Exp $
4 * Mini hostname implementation for busybox 4 * Mini hostname implementation for busybox
5 * 5 *
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> 6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
@@ -46,12 +46,12 @@ static void do_sethostname(char *s, int isfile)
46 if (!isfile) { 46 if (!isfile) {
47 if (sethostname(s, strlen(s)) < 0) { 47 if (sethostname(s, strlen(s)) < 0) {
48 if (errno == EPERM) 48 if (errno == EPERM)
49 error_msg_and_die("you must be root to change the hostname"); 49 bb_error_msg_and_die("you must be root to change the hostname");
50 else 50 else
51 perror_msg_and_die("sethostname"); 51 bb_perror_msg_and_die("sethostname");
52 } 52 }
53 } else { 53 } else {
54 f = xfopen(s, "r"); 54 f = bb_xfopen(s, "r");
55 while (fgets(buf, 255, f) != NULL) { 55 while (fgets(buf, 255, f) != NULL) {
56 if (buf[0] =='#') { 56 if (buf[0] =='#') {
57 continue; 57 continue;
@@ -75,7 +75,7 @@ int hostname_main(int argc, char **argv)
75 char *p = NULL; 75 char *p = NULL;
76 76
77 if (argc < 1) 77 if (argc < 1)
78 show_usage(); 78 bb_show_usage();
79 79
80 while ((opt = getopt(argc, argv, "dfisF:")) > 0) { 80 while ((opt = getopt(argc, argv, "dfisF:")) > 0) {
81 switch (opt) { 81 switch (opt) {
@@ -89,7 +89,7 @@ int hostname_main(int argc, char **argv)
89 filename = optarg; 89 filename = optarg;
90 break; 90 break;
91 default: 91 default:
92 show_usage(); 92 bb_show_usage();
93 } 93 }
94 } 94 }
95 95
diff --git a/networking/httpd.c b/networking/httpd.c
index fb37224cf..e62168d38 100644
--- a/networking/httpd.c
+++ b/networking/httpd.c
@@ -178,12 +178,12 @@ static const char home[] = "/www";
178#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP 178#define CONFIG_FEATURE_HTTPD_RELOAD_CONFIG_SIGHUP
179 179
180/* require from libbb.a for linking */ 180/* require from libbb.a for linking */
181const char *applet_name = "httpd"; 181const char *bb_applet_name = "httpd";
182 182
183void show_usage(void) 183void bb_show_usage(void)
184{ 184{
185 fprintf(stderr, "Usage: %s [-p <port>] [-c configFile] [-d/-e <string>] " 185 fprintf(stderr, "Usage: %s [-p <port>] [-c configFile] [-d/-e <string>] "
186 "[-r realm] [-u user]\n", applet_name); 186 "[-r realm] [-u user]\n", bb_applet_name);
187 exit(1); 187 exit(1);
188} 188}
189#endif 189#endif
@@ -395,7 +395,7 @@ static int conf_sort(const void *p1, const void *p2)
395 } 395 }
396#ifdef DEBUG 396#ifdef DEBUG
397 if(!test) 397 if(!test)
398 error_msg_and_die("sort: can`t found compares!"); 398 bb_error_msg_and_die("sort: can`t found compares!");
399#endif 399#endif
400 return test; 400 return test;
401} 401}
@@ -423,7 +423,7 @@ static void parse_conf(const char *path, int flag)
423 cf = p0 = alloca(strlen(path) + sizeof(httpd_conf) + 2); 423 cf = p0 = alloca(strlen(path) + sizeof(httpd_conf) + 2);
424 if(p0 == NULL) { 424 if(p0 == NULL) {
425 if(flag == FIRST_PARSE) 425 if(flag == FIRST_PARSE)
426 error_msg_and_die(memory_exhausted); 426 bb_error_msg_and_die(bb_msg_memory_exhausted);
427 return; 427 return;
428 } 428 }
429 sprintf(p0, "%s/%s", path, httpd_conf); 429 sprintf(p0, "%s/%s", path, httpd_conf);
@@ -433,7 +433,7 @@ static void parse_conf(const char *path, int flag)
433 if(flag != FIRST_PARSE) 433 if(flag != FIRST_PARSE)
434 return; /* subdir config not found */ 434 return; /* subdir config not found */
435 if(p0 == NULL) /* if -c option gived */ 435 if(p0 == NULL) /* if -c option gived */
436 perror_msg_and_die("%s", cf); 436 bb_perror_msg_and_die("%s", cf);
437 p0 = NULL; 437 p0 = NULL;
438 cf = httpd_conf; /* set -c ./httpd_conf */ 438 cf = httpd_conf; /* set -c ./httpd_conf */
439 } 439 }
@@ -543,7 +543,7 @@ static void parse_conf(const char *path, int flag)
543 pcur = alloca((n + 1) * sizeof(Htaccess *)); 543 pcur = alloca((n + 1) * sizeof(Htaccess *));
544 if(pcur == NULL) { 544 if(pcur == NULL) {
545 if(flag == FIRST_PARSE) 545 if(flag == FIRST_PARSE)
546 error_msg_and_die(memory_exhausted); 546 bb_error_msg_and_die(bb_msg_memory_exhausted);
547 return; 547 return;
548 } 548 }
549 n = 0; 549 n = 0;
@@ -557,7 +557,7 @@ static void parse_conf(const char *path, int flag)
557 config->Httpd_conf_parsed = *pcur; 557 config->Httpd_conf_parsed = *pcur;
558 for(cur = *pcur; cur; cur = cur->next) { 558 for(cur = *pcur; cur; cur = cur->next) {
559#ifdef DEBUG 559#ifdef DEBUG
560 error_msg("%s: %s:%s", cf, cur->before_colon, cur->after_colon); 560 bb_error_msg("%s: %s:%s", cf, cur->before_colon, cur->after_colon);
561#endif 561#endif
562 cur->next = *++pcur; 562 cur->next = *++pcur;
563 } 563 }
@@ -829,10 +829,10 @@ static int openServer(void)
829 listen(fd, 9); 829 listen(fd, 9);
830 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */ 830 signal(SIGCHLD, SIG_IGN); /* prevent zombie (defunct) processes */
831 } else { 831 } else {
832 perror_msg_and_die("bind"); 832 bb_perror_msg_and_die("bind");
833 } 833 }
834 } else { 834 } else {
835 perror_msg_and_die("create socket"); 835 bb_perror_msg_and_die("create socket");
836 } 836 }
837 return fd; 837 return fd;
838} 838}
@@ -905,7 +905,7 @@ static int sendHeaders(HttpResponseNum responseNum)
905#ifdef DEBUG 905#ifdef DEBUG
906 if (config->debugHttpd) fprintf(stderr, "Headers: '%s'", buf); 906 if (config->debugHttpd) fprintf(stderr, "Headers: '%s'", buf);
907#endif 907#endif
908 return full_write(a_c_w, buf, len); 908 return bb_full_write(a_c_w, buf, len);
909} 909}
910 910
911/**************************************************************************** 911/****************************************************************************
@@ -1105,7 +1105,7 @@ static int sendCgi(const char *url,
1105 outFd = toCgi[1]; 1105 outFd = toCgi[1];
1106 close(fromCgi[1]); 1106 close(fromCgi[1]);
1107 close(toCgi[0]); 1107 close(toCgi[0]);
1108 if (body) full_write(outFd, body, bodyLen); 1108 if (body) bb_full_write(outFd, body, bodyLen);
1109 close(outFd); 1109 close(outFd);
1110 1110
1111 while (1) { 1111 while (1) {
@@ -1129,9 +1129,9 @@ static int sendCgi(const char *url,
1129#ifdef DEBUG 1129#ifdef DEBUG
1130 if (config->debugHttpd) { 1130 if (config->debugHttpd) {
1131 if (WIFEXITED(status)) 1131 if (WIFEXITED(status))
1132 error_msg("piped has exited with status=%d", WEXITSTATUS(status)); 1132 bb_error_msg("piped has exited with status=%d", WEXITSTATUS(status));
1133 if (WIFSIGNALED(status)) 1133 if (WIFSIGNALED(status))
1134 error_msg("piped has exited with signal=%d", WTERMSIG(status)); 1134 bb_error_msg("piped has exited with signal=%d", WTERMSIG(status));
1135 } 1135 }
1136#endif 1136#endif
1137 pid = -1; 1137 pid = -1;
@@ -1141,7 +1141,7 @@ static int sendCgi(const char *url,
1141 int s = a_c_w; 1141 int s = a_c_w;
1142 1142
1143 // There is something to read 1143 // There is something to read
1144 count = full_read(inFd, buf, sizeof(buf)-1); 1144 count = bb_full_read(inFd, buf, sizeof(buf)-1);
1145 // If a read returns 0 at this point then some type of error has 1145 // If a read returns 0 at this point then some type of error has
1146 // occurred. Bail now. 1146 // occurred. Bail now.
1147 if (count == 0) break; 1147 if (count == 0) break;
@@ -1149,14 +1149,14 @@ static int sendCgi(const char *url,
1149 if (firstLine) { 1149 if (firstLine) {
1150 /* check to see if the user script added headers */ 1150 /* check to see if the user script added headers */
1151 if (strncmp(buf, "HTTP/1.0 200 OK\n", 4) != 0) { 1151 if (strncmp(buf, "HTTP/1.0 200 OK\n", 4) != 0) {
1152 full_write(s, "HTTP/1.0 200 OK\n", 16); 1152 bb_full_write(s, "HTTP/1.0 200 OK\n", 16);
1153 } 1153 }
1154 if (strstr(buf, "ontent-") == 0) { 1154 if (strstr(buf, "ontent-") == 0) {
1155 full_write(s, "Content-type: text/plain\n\n", 26); 1155 bb_full_write(s, "Content-type: text/plain\n\n", 26);
1156 } 1156 }
1157 firstLine=0; 1157 firstLine=0;
1158 } 1158 }
1159 full_write(s, buf, count); 1159 bb_full_write(s, buf, count);
1160#ifdef DEBUG 1160#ifdef DEBUG
1161 if (config->debugHttpd) 1161 if (config->debugHttpd)
1162 fprintf(stderr, "cgi read %d bytes\n", count); 1162 fprintf(stderr, "cgi read %d bytes\n", count);
@@ -1223,14 +1223,14 @@ static int sendFile(const char *url, char *buf)
1223 int count; 1223 int count;
1224 1224
1225 sendHeaders(HTTP_OK); 1225 sendHeaders(HTTP_OK);
1226 while ((count = full_read(f, buf, MAX_MEMORY_BUFF)) > 0) { 1226 while ((count = bb_full_read(f, buf, MAX_MEMORY_BUFF)) > 0) {
1227 full_write(a_c_w, buf, count); 1227 bb_full_write(a_c_w, buf, count);
1228 } 1228 }
1229 close(f); 1229 close(f);
1230 } else { 1230 } else {
1231#ifdef DEBUG 1231#ifdef DEBUG
1232 if (config->debugHttpd) 1232 if (config->debugHttpd)
1233 perror_msg("Unable to open '%s'", url); 1233 bb_perror_msg("Unable to open '%s'", url);
1234#endif 1234#endif
1235 sendHeaders(HTTP_NOT_FOUND); 1235 sendHeaders(HTTP_NOT_FOUND);
1236 } 1236 }
@@ -1399,7 +1399,7 @@ BAD_REQUEST:
1399 but CGI script can`t be a directory */ 1399 but CGI script can`t be a directory */
1400 } 1400 }
1401 1401
1402 /* algorithm stolen from libbb simplify_path(), 1402 /* algorithm stolen from libbb bb_simplify_path(),
1403 but don`t strdup and reducing trailing slash */ 1403 but don`t strdup and reducing trailing slash */
1404 purl = test = url; 1404 purl = test = url;
1405 1405
@@ -1517,7 +1517,7 @@ FORBIDDEN: /* protect listing /cgi-bin */
1517 if (length > 0) { 1517 if (length > 0) {
1518 body = malloc(length + 1); 1518 body = malloc(length + 1);
1519 if (body) { 1519 if (body) {
1520 length = full_read(a_c_r, body, length); 1520 length = bb_full_read(a_c_r, body, length);
1521 if(length < 0) // closed 1521 if(length < 0) // closed
1522 length = 0; 1522 length = 0;
1523 body[length] = 0; // always null terminate for safety 1523 body[length] = 0; // always null terminate for safety
@@ -1629,7 +1629,7 @@ static int miniHttpd(int server)
1629 config->port = ntohs(fromAddr.sin_port); 1629 config->port = ntohs(fromAddr.sin_port);
1630#ifdef DEBUG 1630#ifdef DEBUG
1631 if (config->debugHttpd) { 1631 if (config->debugHttpd) {
1632 error_msg("connection from IP=%s, port %u\n", 1632 bb_error_msg("connection from IP=%s, port %u\n",
1633 config->rmt_ip, config->port); 1633 config->rmt_ip, config->port);
1634 } 1634 }
1635#endif 1635#endif
@@ -1748,7 +1748,7 @@ int httpd_main(int argc, char *argv[])
1748 case 'p': 1748 case 'p':
1749 config->port = atoi(optarg); 1749 config->port = atoi(optarg);
1750 if(config->port <= 0 || config->port > 0xffff) 1750 if(config->port <= 0 || config->port > 0xffff)
1751 error_msg_and_die("invalid %s for -p", optarg); 1751 bb_error_msg_and_die("invalid %s for -p", optarg);
1752 break; 1752 break;
1753#endif 1753#endif
1754#ifdef CONFIG_FEATURE_HTTPD_DECODE_URL_STR 1754#ifdef CONFIG_FEATURE_HTTPD_DECODE_URL_STR
@@ -1780,13 +1780,13 @@ int httpd_main(int argc, char *argv[])
1780 break; 1780 break;
1781#endif 1781#endif
1782 default: 1782 default:
1783 error_msg("%s", httpdVersion); 1783 bb_error_msg("%s", httpdVersion);
1784 show_usage(); 1784 bb_show_usage();
1785 } 1785 }
1786 } 1786 }
1787 1787
1788 if(chdir(home_httpd)) { 1788 if(chdir(home_httpd)) {
1789 perror_msg_and_die("can`t chdir to %s", home_httpd); 1789 bb_perror_msg_and_die("can`t chdir to %s", home_httpd);
1790 } 1790 }
1791#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY 1791#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1792 server = openServer(); 1792 server = openServer();
@@ -1809,7 +1809,7 @@ int httpd_main(int argc, char *argv[])
1809#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY 1809#ifndef CONFIG_FEATURE_HTTPD_USAGE_FROM_INETD_ONLY
1810 if (!config->debugHttpd) { 1810 if (!config->debugHttpd) {
1811 if (daemon(1, 0) < 0) /* don`t change curent directory */ 1811 if (daemon(1, 0) < 0) /* don`t change curent directory */
1812 perror_msg_and_die("daemon"); 1812 bb_perror_msg_and_die("daemon");
1813 } 1813 }
1814 return miniHttpd(server); 1814 return miniHttpd(server);
1815#else 1815#else
diff --git a/networking/ifconfig.c b/networking/ifconfig.c
index 95cc07ab0..c09c48c58 100644
--- a/networking/ifconfig.c
+++ b/networking/ifconfig.c
@@ -15,7 +15,7 @@
15 * Foundation; either version 2 of the License, or (at 15 * Foundation; either version 2 of the License, or (at
16 * your option) any later version. 16 * your option) any later version.
17 * 17 *
18 * $Id: ifconfig.c,v 1.22 2003/01/14 08:54:07 andersen Exp $ 18 * $Id: ifconfig.c,v 1.23 2003/03/19 09:12:38 mjn3 Exp $
19 * 19 *
20 */ 20 */
21 21
@@ -323,14 +323,14 @@ int ifconfig_main(int argc, char **argv)
323#ifdef CONFIG_FEATURE_IFCONFIG_STATUS 323#ifdef CONFIG_FEATURE_IFCONFIG_STATUS
324 return display_interfaces(argc ? *argv : NULL); 324 return display_interfaces(argc ? *argv : NULL);
325#else 325#else
326 error_msg_and_die 326 bb_error_msg_and_die
327 ("ifconfig was not compiled with interface status display support."); 327 ("ifconfig was not compiled with interface status display support.");
328#endif 328#endif
329 } 329 }
330 330
331 /* Create a channel to the NET kernel. */ 331 /* Create a channel to the NET kernel. */
332 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { 332 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
333 perror_msg_and_die("socket"); 333 bb_perror_msg_and_die("socket");
334 } 334 }
335 335
336 /* get interface name */ 336 /* get interface name */
@@ -366,11 +366,11 @@ int ifconfig_main(int argc, char **argv)
366 mask = op->arg_flags; 366 mask = op->arg_flags;
367 a1op = Arg1Opt + (op - OptArray); 367 a1op = Arg1Opt + (op - OptArray);
368 if (mask & A_NETMASK & did_flags) { 368 if (mask & A_NETMASK & did_flags) {
369 show_usage(); 369 bb_show_usage();
370 } 370 }
371 if (*++argv == NULL) { 371 if (*++argv == NULL) {
372 if (mask & A_ARG_REQ) { 372 if (mask & A_ARG_REQ) {
373 show_usage(); 373 bb_show_usage();
374 } else { 374 } else {
375 --argv; 375 --argv;
376 mask &= A_SET_AFTER; /* just for broadcast */ 376 mask &= A_SET_AFTER; /* just for broadcast */
@@ -421,7 +421,7 @@ int ifconfig_main(int argc, char **argv)
421 421
422 /* Create a channel to the NET kernel. */ 422 /* Create a channel to the NET kernel. */
423 if ((sockfd6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { 423 if ((sockfd6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
424 perror_msg_and_die("socket6"); 424 bb_perror_msg_and_die("socket6");
425 } 425 }
426 if (ioctl(sockfd6, SIOGIFINDEX, &ifr) < 0) { 426 if (ioctl(sockfd6, SIOGIFINDEX, &ifr) < 0) {
427 perror("SIOGIFINDEX"); 427 perror("SIOGIFINDEX");
@@ -454,11 +454,11 @@ int ifconfig_main(int argc, char **argv)
454 } else { /* A_CAST_HOST_COPY_IN_ETHER */ 454 } else { /* A_CAST_HOST_COPY_IN_ETHER */
455 /* This is the "hw" arg case. */ 455 /* This is the "hw" arg case. */
456 if (strcmp("ether", *argv) || (*++argv == NULL)) { 456 if (strcmp("ether", *argv) || (*++argv == NULL)) {
457 show_usage(); 457 bb_show_usage();
458 } 458 }
459 safe_strncpy(host, *argv, (sizeof host)); 459 safe_strncpy(host, *argv, (sizeof host));
460 if (in_ether(host, &sa)) { 460 if (in_ether(host, &sa)) {
461 error_msg("invalid hw-addr %s", host); 461 bb_error_msg("invalid hw-addr %s", host);
462 ++goterr; 462 ++goterr;
463 continue; 463 continue;
464 } 464 }
diff --git a/networking/ifupdown.c b/networking/ifupdown.c
index 0511a5b95..9bbb90496 100644
--- a/networking/ifupdown.c
+++ b/networking/ifupdown.c
@@ -244,7 +244,7 @@ static char *parse(char *command, interface_defn_t *ifd)
244 varvalue = get_var(command, nextpercent - command, ifd); 244 varvalue = get_var(command, nextpercent - command, ifd);
245 245
246 if (varvalue) { 246 if (varvalue) {
247 addstr(&result, &len, &pos, varvalue, xstrlen(varvalue)); 247 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
248 } else { 248 } else {
249#ifdef CONFIG_FEATURE_IFUPDOWN_IP 249#ifdef CONFIG_FEATURE_IFUPDOWN_IP
250 /* Sigh... Add a special case for 'ip' to convert from 250 /* Sigh... Add a special case for 'ip' to convert from
@@ -255,7 +255,7 @@ static char *parse(char *command, interface_defn_t *ifd)
255 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) { 255 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
256 char argument[255]; 256 char argument[255];
257 sprintf(argument, "%d", res); 257 sprintf(argument, "%d", res);
258 addstr(&result, &len, &pos, argument, xstrlen(argument)); 258 addstr(&result, &len, &pos, argument, bb_strlen(argument));
259 command = nextpercent + 1; 259 command = nextpercent + 1;
260 break; 260 break;
261 } 261 }
@@ -734,9 +734,9 @@ static interfaces_file_t *read_interfaces(char *filename)
734 defn->mappings = NULL; 734 defn->mappings = NULL;
735 defn->ifaces = NULL; 735 defn->ifaces = NULL;
736 736
737 f = xfopen(filename, "r"); 737 f = bb_xfopen(filename, "r");
738 738
739 while ((buf = get_line_from_file(f)) != NULL) { 739 while ((buf = bb_get_line_from_file(f)) != NULL) {
740 char *buf_ptr = buf; 740 char *buf_ptr = buf;
741 741
742 /* Ignore comments */ 742 /* Ignore comments */
@@ -762,7 +762,7 @@ static interfaces_file_t *read_interfaces(char *filename)
762 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches); 762 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
763 } 763 }
764 764
765 currmap->match[currmap->n_matches++] = xstrdup(firstword); 765 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
766 } 766 }
767 currmap->max_mappings = 0; 767 currmap->max_mappings = 0;
768 currmap->n_mappings = 0; 768 currmap->n_mappings = 0;
@@ -802,26 +802,26 @@ static interfaces_file_t *read_interfaces(char *filename)
802 method_name = next_word(&buf_ptr); 802 method_name = next_word(&buf_ptr);
803 803
804 if (buf_ptr == NULL) { 804 if (buf_ptr == NULL) {
805 error_msg("too few parameters for line \"%s\"", buf); 805 bb_error_msg("too few parameters for line \"%s\"", buf);
806 return NULL; 806 return NULL;
807 } 807 }
808 808
809 if (buf_ptr[0] != '\0') { 809 if (buf_ptr[0] != '\0') {
810 error_msg("too many parameters \"%s\"", buf); 810 bb_error_msg("too many parameters \"%s\"", buf);
811 return NULL; 811 return NULL;
812 } 812 }
813 813
814 currif->iface = xstrdup(iface_name); 814 currif->iface = bb_xstrdup(iface_name);
815 815
816 currif->address_family = get_address_family(addr_fams, address_family_name); 816 currif->address_family = get_address_family(addr_fams, address_family_name);
817 if (!currif->address_family) { 817 if (!currif->address_family) {
818 error_msg("unknown address type \"%s\"", buf); 818 bb_error_msg("unknown address type \"%s\"", buf);
819 return NULL; 819 return NULL;
820 } 820 }
821 821
822 currif->method = get_method(currif->address_family, method_name); 822 currif->method = get_method(currif->address_family, method_name);
823 if (!currif->method) { 823 if (!currif->method) {
824 error_msg("unknown method \"%s\"", buf); 824 bb_error_msg("unknown method \"%s\"", buf);
825 return NULL; 825 return NULL;
826 } 826 }
827 827
@@ -836,7 +836,7 @@ static interfaces_file_t *read_interfaces(char *filename)
836 836
837 while (*where != NULL) { 837 while (*where != NULL) {
838 if (duplicate_if(*where, currif)) { 838 if (duplicate_if(*where, currif)) {
839 error_msg("duplicate interface \"%s\"", buf); 839 bb_error_msg("duplicate interface \"%s\"", buf);
840 return NULL; 840 return NULL;
841 } 841 }
842 where = &(*where)->next; 842 where = &(*where)->next;
@@ -852,7 +852,7 @@ static interfaces_file_t *read_interfaces(char *filename)
852 852
853 /* Check the interface isnt already listed */ 853 /* Check the interface isnt already listed */
854 if (find_list_string(defn->autointerfaces, firstword)) { 854 if (find_list_string(defn->autointerfaces, firstword)) {
855 perror_msg_and_die("interface declared auto twice \"%s\"", buf); 855 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
856 } 856 }
857 857
858 /* Add the interface to the list */ 858 /* Add the interface to the list */
@@ -865,8 +865,8 @@ static interfaces_file_t *read_interfaces(char *filename)
865 { 865 {
866 int i; 866 int i;
867 867
868 if (xstrlen(buf_ptr) == 0) { 868 if (bb_strlen(buf_ptr) == 0) {
869 error_msg("option with empty value \"%s\"", buf); 869 bb_error_msg("option with empty value \"%s\"", buf);
870 return NULL; 870 return NULL;
871 } 871 }
872 872
@@ -876,7 +876,7 @@ static interfaces_file_t *read_interfaces(char *filename)
876 && strcmp(firstword, "post-down") != 0) { 876 && strcmp(firstword, "post-down") != 0) {
877 for (i = 0; i < currif->n_options; i++) { 877 for (i = 0; i < currif->n_options; i++) {
878 if (strcmp(currif->option[i].name, firstword) == 0) { 878 if (strcmp(currif->option[i].name, firstword) == 0) {
879 error_msg("duplicate option \"%s\"", buf); 879 bb_error_msg("duplicate option \"%s\"", buf);
880 return NULL; 880 return NULL;
881 } 881 }
882 } 882 }
@@ -889,8 +889,8 @@ static interfaces_file_t *read_interfaces(char *filename)
889 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options); 889 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
890 currif->option = opt; 890 currif->option = opt;
891 } 891 }
892 currif->option[currif->n_options].name = xstrdup(firstword); 892 currif->option[currif->n_options].name = bb_xstrdup(firstword);
893 currif->option[currif->n_options].value = xstrdup(next_word(&buf_ptr)); 893 currif->option[currif->n_options].value = bb_xstrdup(next_word(&buf_ptr));
894 if (!currif->option[currif->n_options].name) { 894 if (!currif->option[currif->n_options].name) {
895 perror(filename); 895 perror(filename);
896 return NULL; 896 return NULL;
@@ -905,34 +905,34 @@ static interfaces_file_t *read_interfaces(char *filename)
905#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING 905#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
906 if (strcmp(firstword, "script") == 0) { 906 if (strcmp(firstword, "script") == 0) {
907 if (currmap->script != NULL) { 907 if (currmap->script != NULL) {
908 error_msg("duplicate script in mapping \"%s\"", buf); 908 bb_error_msg("duplicate script in mapping \"%s\"", buf);
909 return NULL; 909 return NULL;
910 } else { 910 } else {
911 currmap->script = xstrdup(next_word(&buf_ptr)); 911 currmap->script = bb_xstrdup(next_word(&buf_ptr));
912 } 912 }
913 } else if (strcmp(firstword, "map") == 0) { 913 } else if (strcmp(firstword, "map") == 0) {
914 if (currmap->max_mappings == currmap->n_mappings) { 914 if (currmap->max_mappings == currmap->n_mappings) {
915 currmap->max_mappings = currmap->max_mappings * 2 + 1; 915 currmap->max_mappings = currmap->max_mappings * 2 + 1;
916 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings); 916 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
917 } 917 }
918 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr)); 918 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
919 currmap->n_mappings++; 919 currmap->n_mappings++;
920 } else { 920 } else {
921 error_msg("misplaced option \"%s\"", buf); 921 bb_error_msg("misplaced option \"%s\"", buf);
922 return NULL; 922 return NULL;
923 } 923 }
924#endif 924#endif
925 break; 925 break;
926 case NONE: 926 case NONE:
927 default: 927 default:
928 error_msg("misplaced option \"%s\"", buf); 928 bb_error_msg("misplaced option \"%s\"", buf);
929 return NULL; 929 return NULL;
930 } 930 }
931 } 931 }
932 free(buf); 932 free(buf);
933 } 933 }
934 if (ferror(f) != 0) { 934 if (ferror(f) != 0) {
935 perror_msg_and_die("%s", filename); 935 bb_perror_msg_and_die("%s", filename);
936 } 936 }
937 fclose(f); 937 fclose(f);
938 938
@@ -945,7 +945,7 @@ static char *setlocalenv(char *format, char *name, char *value)
945 char *here; 945 char *here;
946 char *there; 946 char *there;
947 947
948 result = xmalloc(xstrlen(format) + xstrlen(name) + xstrlen(value) + 1); 948 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
949 949
950 sprintf(result, format, name, value); 950 sprintf(result, format, name, value);
951 951
@@ -960,7 +960,7 @@ static char *setlocalenv(char *format, char *name, char *value)
960 here++; 960 here++;
961 } 961 }
962 } 962 }
963 memmove(here, there, xstrlen(there) + 1); 963 memmove(here, there, bb_strlen(there) + 1);
964 964
965 return result; 965 return result;
966} 966}
@@ -1010,7 +1010,7 @@ static void set_environ(interface_defn_t *iface, char *mode)
1010static int doit(char *str) 1010static int doit(char *str)
1011{ 1011{
1012 if (verbose || no_act) { 1012 if (verbose || no_act) {
1013 error_msg("%s", str); 1013 bb_error_msg("%s", str);
1014 } 1014 }
1015 if (!no_act) { 1015 if (!no_act) {
1016 pid_t child; 1016 pid_t child;
@@ -1045,7 +1045,7 @@ static int execute_all(interface_defn_t *ifd, execfn *exec, const char *opt)
1045 } 1045 }
1046 } 1046 }
1047 1047
1048 buf = xmalloc(xstrlen(opt) + 19); 1048 buf = xmalloc(bb_strlen(opt) + 19);
1049 sprintf(buf, "/etc/network/if-%s.d", opt); 1049 sprintf(buf, "/etc/network/if-%s.d", opt);
1050 run_parts(&buf, 2); 1050 run_parts(&buf, 2);
1051 free(buf); 1051 free(buf);
@@ -1155,7 +1155,7 @@ static int run_mapping(char *physical, char *logical, int len, mapping_defn_t *
1155 waitpid(pid, &status, 0); 1155 waitpid(pid, &status, 0);
1156 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { 1156 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1157 if (fgets(logical, len, out)) { 1157 if (fgets(logical, len, out)) {
1158 char *pch = logical + xstrlen(logical) - 1; 1158 char *pch = logical + bb_strlen(logical) - 1;
1159 1159
1160 while (pch >= logical && isspace(*pch)) 1160 while (pch >= logical && isspace(*pch))
1161 *(pch--) = '\0'; 1161 *(pch--) = '\0';
@@ -1169,7 +1169,7 @@ static int run_mapping(char *physical, char *logical, int len, mapping_defn_t *
1169 1169
1170static llist_t *find_iface_state(llist_t *state_list, const char *iface) 1170static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1171{ 1171{
1172 unsigned short iface_len = xstrlen(iface); 1172 unsigned short iface_len = bb_strlen(iface);
1173 llist_t *search = state_list; 1173 llist_t *search = state_list;
1174 1174
1175 while (search) { 1175 while (search) {
@@ -1199,7 +1199,7 @@ extern int ifupdown_main(int argc, char **argv)
1199 int force = 0; 1199 int force = 0;
1200 int i; 1200 int i;
1201 1201
1202 if (applet_name[2] == 'u') { 1202 if (bb_applet_name[2] == 'u') {
1203 /* ifup command */ 1203 /* ifup command */
1204 cmds = iface_up; 1204 cmds = iface_up;
1205 } else { 1205 } else {
@@ -1214,7 +1214,7 @@ extern int ifupdown_main(int argc, char **argv)
1214#endif 1214#endif
1215 switch (i) { 1215 switch (i) {
1216 case 'i': /* interfaces */ 1216 case 'i': /* interfaces */
1217 interfaces = xstrdup(optarg); 1217 interfaces = bb_xstrdup(optarg);
1218 break; 1218 break;
1219 case 'v': /* verbose */ 1219 case 'v': /* verbose */
1220 verbose = 1; 1220 verbose = 1;
@@ -1234,18 +1234,18 @@ extern int ifupdown_main(int argc, char **argv)
1234 force = 1; 1234 force = 1;
1235 break; 1235 break;
1236 default: 1236 default:
1237 show_usage(); 1237 bb_show_usage();
1238 break; 1238 break;
1239 } 1239 }
1240 } 1240 }
1241 1241
1242 if (argc - optind > 0) { 1242 if (argc - optind > 0) {
1243 if (do_all) { 1243 if (do_all) {
1244 show_usage(); 1244 bb_show_usage();
1245 } 1245 }
1246 } else { 1246 } else {
1247 if (!do_all) { 1247 if (!do_all) {
1248 show_usage(); 1248 bb_show_usage();
1249 } 1249 }
1250 } 1250 }
1251 1251
@@ -1297,13 +1297,13 @@ extern int ifupdown_main(int argc, char **argv)
1297 if (cmds == iface_up) { 1297 if (cmds == iface_up) {
1298 /* ifup */ 1298 /* ifup */
1299 if (iface_state) { 1299 if (iface_state) {
1300 error_msg("interface %s already configured", iface); 1300 bb_error_msg("interface %s already configured", iface);
1301 continue; 1301 continue;
1302 } 1302 }
1303 } else { 1303 } else {
1304 /* ifdown */ 1304 /* ifdown */
1305 if (iface_state) { 1305 if (iface_state) {
1306 error_msg("interface %s not configured", iface); 1306 bb_error_msg("interface %s not configured", iface);
1307 continue; 1307 continue;
1308 } 1308 }
1309 } 1309 }
@@ -1319,7 +1319,7 @@ extern int ifupdown_main(int argc, char **argv)
1319 if (fnmatch(currmap->match[i], liface, 0) != 0) 1319 if (fnmatch(currmap->match[i], liface, 0) != 0)
1320 continue; 1320 continue;
1321 if (verbose) { 1321 if (verbose) {
1322 error_msg("Running mapping script %s on %s", currmap->script, liface); 1322 bb_error_msg("Running mapping script %s on %s", currmap->script, liface);
1323 } 1323 }
1324 run_mapping(iface, liface, sizeof(liface), currmap); 1324 run_mapping(iface, liface, sizeof(liface), currmap);
1325 break; 1325 break;
@@ -1336,7 +1336,7 @@ extern int ifupdown_main(int argc, char **argv)
1336 currif->iface = iface; 1336 currif->iface = iface;
1337 1337
1338 if (verbose) { 1338 if (verbose) {
1339 error_msg("Configuring interface %s=%s (%s)", iface, liface, currif->address_family->name); 1339 bb_error_msg("Configuring interface %s=%s (%s)", iface, liface, currif->address_family->name);
1340 } 1340 }
1341 1341
1342 /* Call the cmds function pointer, does either iface_up() or iface_down() */ 1342 /* Call the cmds function pointer, does either iface_up() or iface_down() */
@@ -1351,12 +1351,12 @@ extern int ifupdown_main(int argc, char **argv)
1351 } 1351 }
1352 1352
1353 if (!okay && !force) { 1353 if (!okay && !force) {
1354 error_msg("Ignoring unknown interface %s=%s.", iface, liface); 1354 bb_error_msg("Ignoring unknown interface %s=%s.", iface, liface);
1355 } else { 1355 } else {
1356 llist_t *iface_state = find_iface_state(state_list, iface); 1356 llist_t *iface_state = find_iface_state(state_list, iface);
1357 1357
1358 if (cmds == iface_up) { 1358 if (cmds == iface_up) {
1359 char *newiface = xmalloc(xstrlen(iface) + 1 + xstrlen(liface) + 1); 1359 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
1360 sprintf(newiface, "%s=%s", iface, liface); 1360 sprintf(newiface, "%s=%s", iface, liface);
1361 if (iface_state == NULL) { 1361 if (iface_state == NULL) {
1362 state_list = llist_add_to(state_list, newiface); 1362 state_list = llist_add_to(state_list, newiface);
@@ -1387,10 +1387,10 @@ extern int ifupdown_main(int argc, char **argv)
1387 1387
1388 if (state_fp) 1388 if (state_fp)
1389 fclose(state_fp); 1389 fclose(state_fp);
1390 state_fp = xfopen(statefile, "a+"); 1390 state_fp = bb_xfopen(statefile, "a+");
1391 1391
1392 if (ftruncate(fileno(state_fp), 0) < 0) { 1392 if (ftruncate(fileno(state_fp), 0) < 0) {
1393 error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno)); 1393 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
1394 } 1394 }
1395 1395
1396 rewind(state_fp); 1396 rewind(state_fp);
diff --git a/networking/inetd.c b/networking/inetd.c
index 2769c01b7..33b97ba94 100644
--- a/networking/inetd.c
+++ b/networking/inetd.c
@@ -519,7 +519,7 @@ enter(struct servtab *cp)
519 519
520 sep = (struct servtab *)malloc(sizeof (*sep)); 520 sep = (struct servtab *)malloc(sizeof (*sep));
521 if (sep == NULL) { 521 if (sep == NULL) {
522 syslog_err_and_discard_dg(SOCK_STREAM, memory_exhausted); 522 syslog_err_and_discard_dg(SOCK_STREAM, bb_msg_memory_exhausted);
523 } 523 }
524 *sep = *cp; 524 *sep = *cp;
525 sep->se_fd = -1; 525 sep->se_fd = -1;
@@ -835,7 +835,7 @@ inetd_main(int argc, char *argv[])
835 if (global_queuelen < 8) global_queuelen=8; 835 if (global_queuelen < 8) global_queuelen=8;
836 break; 836 break;
837 default: 837 default:
838 show_usage(); // "[-q len] [conf]" 838 bb_show_usage(); // "[-q len] [conf]"
839 } 839 }
840 argc -= optind; 840 argc -= optind;
841 argv += optind; 841 argv += optind;
@@ -844,7 +844,7 @@ inetd_main(int argc, char *argv[])
844 CONFIG = argv[0]; 844 CONFIG = argv[0];
845 845
846 daemon(0, 0); 846 daemon(0, 0);
847 openlog(applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON); 847 openlog(bb_applet_name, LOG_PID | LOG_NOWAIT, LOG_DAEMON);
848 { 848 {
849 FILE *fp; 849 FILE *fp;
850 850
diff --git a/networking/ip.c b/networking/ip.c
index e7cab74c9..a0781bdba 100644
--- a/networking/ip.c
+++ b/networking/ip.c
@@ -72,7 +72,7 @@ void ip_parse_common_args(int *argcp, char ***argvp)
72 } else if (matches(opt, "-oneline") == 0) { 72 } else if (matches(opt, "-oneline") == 0) {
73 ++oneline; 73 ++oneline;
74 } else { 74 } else {
75 show_usage(); 75 bb_show_usage();
76 } 76 }
77 argc--; argv++; 77 argc--; argv++;
78 } 78 }
@@ -109,7 +109,7 @@ int ip_main(int argc, char **argv)
109#endif 109#endif
110 } 110 }
111 if (ret) { 111 if (ret) {
112 show_usage(); 112 bb_show_usage();
113 } 113 }
114 return(EXIT_SUCCESS); 114 return(EXIT_SUCCESS);
115} 115}
diff --git a/networking/ipcalc.c b/networking/ipcalc.c
index f130f3b7c..2c23d17f6 100644
--- a/networking/ipcalc.c
+++ b/networking/ipcalc.c
@@ -82,24 +82,24 @@ int ipcalc_main(int argc, char **argv)
82 mode |= SILENT; 82 mode |= SILENT;
83#endif 83#endif
84 else { 84 else {
85 show_usage(); 85 bb_show_usage();
86 } 86 }
87 } 87 }
88 88
89 if (mode & (BROADCAST | NETWORK)) { 89 if (mode & (BROADCAST | NETWORK)) {
90 if (argc - optind > 2) { 90 if (argc - optind > 2) {
91 show_usage(); 91 bb_show_usage();
92 } 92 }
93 } else { 93 } else {
94 if (argc - optind != 1) { 94 if (argc - optind != 1) {
95 show_usage(); 95 bb_show_usage();
96 } 96 }
97 } 97 }
98 98
99 ipaddr = inet_addr(argv[optind]); 99 ipaddr = inet_addr(argv[optind]);
100 100
101 if (ipaddr == INADDR_NONE) { 101 if (ipaddr == INADDR_NONE) {
102 IPCALC_MSG(error_msg_and_die("bad IP address: %s\n", argv[optind]), 102 IPCALC_MSG(bb_error_msg_and_die("bad IP address: %s\n", argv[optind]),
103 exit(EXIT_FAILURE)); 103 exit(EXIT_FAILURE));
104 } 104 }
105 105
@@ -109,7 +109,7 @@ int ipcalc_main(int argc, char **argv)
109 } 109 }
110 110
111 if (ipaddr == INADDR_NONE) { 111 if (ipaddr == INADDR_NONE) {
112 IPCALC_MSG(error_msg_and_die("bad netmask: %s\n", argv[optind + 1]), 112 IPCALC_MSG(bb_error_msg_and_die("bad netmask: %s\n", argv[optind + 1]),
113 exit(EXIT_FAILURE)); 113 exit(EXIT_FAILURE));
114 } 114 }
115 115
@@ -138,7 +138,7 @@ int ipcalc_main(int argc, char **argv)
138 138
139 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET); 139 hostinfo = gethostbyaddr((char *) &ipaddr, sizeof(ipaddr), AF_INET);
140 if (!hostinfo) { 140 if (!hostinfo) {
141 IPCALC_MSG(error_msg("cannot find hostname for %s", argv[optind]); 141 IPCALC_MSG(bb_error_msg("cannot find hostname for %s", argv[optind]);
142 herror(NULL); 142 herror(NULL);
143 putc('\n', stderr);,); 143 putc('\n', stderr);,);
144 exit(EXIT_FAILURE); 144 exit(EXIT_FAILURE);
diff --git a/networking/libiproute/ip_parse_common_args.c b/networking/libiproute/ip_parse_common_args.c
index 4b4355ac1..21e9f74ba 100644
--- a/networking/libiproute/ip_parse_common_args.c
+++ b/networking/libiproute/ip_parse_common_args.c
@@ -48,7 +48,7 @@ void ip_parse_common_args(int *argcp, char ***argvp)
48 argc--; 48 argc--;
49 argv++; 49 argv++;
50 if (! argv[1]) 50 if (! argv[1])
51 show_usage(); 51 bb_show_usage();
52 if (strcmp(argv[1], "inet") == 0) 52 if (strcmp(argv[1], "inet") == 0)
53 preferred_family = AF_INET; 53 preferred_family = AF_INET;
54 else if (strcmp(argv[1], "inet6") == 0) 54 else if (strcmp(argv[1], "inet6") == 0)
@@ -66,7 +66,7 @@ void ip_parse_common_args(int *argcp, char ***argvp)
66 } else if (matches(opt, "-oneline") == 0) { 66 } else if (matches(opt, "-oneline") == 0) {
67 ++oneline; 67 ++oneline;
68 } else { 68 } else {
69 show_usage(); 69 bb_show_usage();
70 } 70 }
71 argc--; argv++; 71 argc--; argv++;
72 } 72 }
diff --git a/networking/libiproute/ipaddress.c b/networking/libiproute/ipaddress.c
index 8eba90c77..44e871ee5 100644
--- a/networking/libiproute/ipaddress.c
+++ b/networking/libiproute/ipaddress.c
@@ -125,7 +125,7 @@ static int print_linkinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg
125 memset(tb, 0, sizeof(tb)); 125 memset(tb, 0, sizeof(tb));
126 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); 126 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len);
127 if (tb[IFLA_IFNAME] == NULL) { 127 if (tb[IFLA_IFNAME] == NULL) {
128 error_msg("nil ifname"); 128 bb_error_msg("nil ifname");
129 return -1; 129 return -1;
130 } 130 }
131 if (filter.label && 131 if (filter.label &&
@@ -217,7 +217,7 @@ static int print_addrinfo(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg
217 return 0; 217 return 0;
218 len -= NLMSG_LENGTH(sizeof(*ifa)); 218 len -= NLMSG_LENGTH(sizeof(*ifa));
219 if (len < 0) { 219 if (len < 0) {
220 error_msg("wrong nlmsg len %d", len); 220 bb_error_msg("wrong nlmsg len %d", len);
221 return -1; 221 return -1;
222 } 222 }
223 223
@@ -489,17 +489,17 @@ extern int ipaddr_list_or_flush(int argc, char **argv, int flush)
489 exit(1); 489 exit(1);
490 490
491 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) { 491 if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETLINK) < 0) {
492 perror_msg_and_die("Cannot send dump request"); 492 bb_perror_msg_and_die("Cannot send dump request");
493 } 493 }
494 494
495 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) { 495 if (rtnl_dump_filter(&rth, store_nlmsg, &linfo, NULL, NULL) < 0) {
496 error_msg_and_die("Dump terminated"); 496 bb_error_msg_and_die("Dump terminated");
497 } 497 }
498 498
499 if (filter_dev) { 499 if (filter_dev) {
500 filter.ifindex = ll_name_to_index(filter_dev); 500 filter.ifindex = ll_name_to_index(filter_dev);
501 if (filter.ifindex <= 0) { 501 if (filter.ifindex <= 0) {
502 error_msg("Device \"%s\" does not exist.", filter_dev); 502 bb_error_msg("Device \"%s\" does not exist.", filter_dev);
503 return -1; 503 return -1;
504 } 504 }
505 } 505 }
@@ -539,11 +539,11 @@ extern int ipaddr_list_or_flush(int argc, char **argv, int flush)
539 539
540 if (filter.family != AF_PACKET) { 540 if (filter.family != AF_PACKET) {
541 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) { 541 if (rtnl_wilddump_request(&rth, filter.family, RTM_GETADDR) < 0) {
542 perror_msg_and_die("Cannot send dump request"); 542 bb_perror_msg_and_die("Cannot send dump request");
543 } 543 }
544 544
545 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) { 545 if (rtnl_dump_filter(&rth, store_nlmsg, &ainfo, NULL, NULL) < 0) {
546 error_msg_and_die("Dump terminated"); 546 bb_error_msg_and_die("Dump terminated");
547 } 547 }
548 } 548 }
549 549
@@ -749,11 +749,11 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
749 } 749 }
750 750
751 if (d == NULL) { 751 if (d == NULL) {
752 error_msg("Not enough information: \"dev\" argument is required."); 752 bb_error_msg("Not enough information: \"dev\" argument is required.");
753 return -1; 753 return -1;
754 } 754 }
755 if (l && matches(d, l) != 0) { 755 if (l && matches(d, l) != 0) {
756 error_msg_and_die("\"dev\" (%s) must match \"label\" (%s).", d, l); 756 bb_error_msg_and_die("\"dev\" (%s) must match \"label\" (%s).", d, l);
757 } 757 }
758 758
759 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) { 759 if (peer_len == 0 && local_len && cmd != RTM_DELADDR) {
@@ -767,7 +767,7 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
767 inet_prefix brd; 767 inet_prefix brd;
768 int i; 768 int i;
769 if (req.ifa.ifa_family != AF_INET) { 769 if (req.ifa.ifa_family != AF_INET) {
770 error_msg("Broadcast can be set only for IPv4 addresses"); 770 bb_error_msg("Broadcast can be set only for IPv4 addresses");
771 return -1; 771 return -1;
772 } 772 }
773 brd = peer; 773 brd = peer;
@@ -791,7 +791,7 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
791 ll_init_map(&rth); 791 ll_init_map(&rth);
792 792
793 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) { 793 if ((req.ifa.ifa_index = ll_name_to_index(d)) == 0) {
794 error_msg("Cannot find device \"%s\"", d); 794 bb_error_msg("Cannot find device \"%s\"", d);
795 return -1; 795 return -1;
796 } 796 }
797 797
@@ -821,5 +821,5 @@ extern int do_ipaddr(int argc, char **argv)
821 case 5: /* flush */ 821 case 5: /* flush */
822 return ipaddr_list_or_flush(argc-1, argv+1, 1); 822 return ipaddr_list_or_flush(argc-1, argv+1, 1);
823 } 823 }
824 error_msg_and_die("Unknown command %s", *argv); 824 bb_error_msg_and_die("Unknown command %s", *argv);
825} 825}
diff --git a/networking/libiproute/iplink.c b/networking/libiproute/iplink.c
index 1cfaf6d6a..f826ba081 100644
--- a/networking/libiproute/iplink.c
+++ b/networking/libiproute/iplink.c
@@ -42,7 +42,7 @@ static int do_link;
42 42
43static int on_off(char *msg) 43static int on_off(char *msg)
44{ 44{
45 error_msg("Error: argument of \"%s\" must be \"on\" or \"off\"", msg); 45 bb_error_msg("Error: argument of \"%s\" must be \"on\" or \"off\"", msg);
46 return -1; 46 return -1;
47} 47}
48 48
@@ -211,7 +211,7 @@ static int parse_address(char *dev, int hatype, int halen, char *lla, struct ifr
211 if (alen < 0) 211 if (alen < 0)
212 return -1; 212 return -1;
213 if (alen != halen) { 213 if (alen != halen) {
214 error_msg("Wrong address (%s) length: expected %d bytes", lla, halen); 214 bb_error_msg("Wrong address (%s) length: expected %d bytes", lla, halen);
215 return -1; 215 return -1;
216 } 216 }
217 return 0; 217 return 0;
@@ -293,7 +293,7 @@ static int do_set(int argc, char **argv)
293 } 293 }
294 294
295 if (!dev) { 295 if (!dev) {
296 error_msg("Not enough of information: \"dev\" argument is required."); 296 bb_error_msg("Not enough of information: \"dev\" argument is required.");
297 exit(-1); 297 exit(-1);
298 } 298 }
299 299
@@ -358,6 +358,6 @@ int do_iplink(int argc, char **argv)
358 } else 358 } else
359 return ipaddr_list_link(0, NULL); 359 return ipaddr_list_link(0, NULL);
360 360
361 error_msg("Command \"%s\" is unknown, try \"ip link help\".", *argv); 361 bb_error_msg("Command \"%s\" is unknown, try \"ip link help\".", *argv);
362 exit(-1); 362 exit(-1);
363} 363}
diff --git a/networking/libiproute/iproute.c b/networking/libiproute/iproute.c
index be4435f66..3dcafdb6f 100644
--- a/networking/libiproute/iproute.c
+++ b/networking/libiproute/iproute.c
@@ -85,7 +85,7 @@ static int print_route(struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
85 return 0; 85 return 0;
86 len -= NLMSG_LENGTH(sizeof(*r)); 86 len -= NLMSG_LENGTH(sizeof(*r));
87 if (len < 0) { 87 if (len < 0) {
88 error_msg("wrong nlmsg len %d", len); 88 bb_error_msg("wrong nlmsg len %d", len);
89 return -1; 89 return -1;
90 } 90 }
91 91
@@ -385,7 +385,7 @@ static int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
385 385
386 if (d) { 386 if (d) {
387 if ((idx = ll_name_to_index(d)) == 0) { 387 if ((idx = ll_name_to_index(d)) == 0) {
388 error_msg("Cannot find device \"%s\"", d); 388 bb_error_msg("Cannot find device \"%s\"", d);
389 return -1; 389 return -1;
390 } 390 }
391 addattr32(&req.n, sizeof(req), RTA_OIF, idx); 391 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
@@ -549,7 +549,7 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
549 549
550 if (id) { 550 if (id) {
551 if ((idx = ll_name_to_index(id)) == 0) { 551 if ((idx = ll_name_to_index(id)) == 0) {
552 error_msg("Cannot find device \"%s\"", id); 552 bb_error_msg("Cannot find device \"%s\"", id);
553 return -1; 553 return -1;
554 } 554 }
555 filter.iif = idx; 555 filter.iif = idx;
@@ -557,7 +557,7 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
557 } 557 }
558 if (od) { 558 if (od) {
559 if ((idx = ll_name_to_index(od)) == 0) { 559 if ((idx = ll_name_to_index(od)) == 0) {
560 error_msg("Cannot find device \"%s\"", od); 560 bb_error_msg("Cannot find device \"%s\"", od);
561 } 561 }
562 filter.oif = idx; 562 filter.oif = idx;
563 filter.oifmask = -1; 563 filter.oifmask = -1;
@@ -587,7 +587,7 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
587 } 587 }
588 filter.flushed = 0; 588 filter.flushed = 0;
589 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) { 589 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
590 error_msg("Flush terminated\n"); 590 bb_error_msg("Flush terminated\n");
591 return -1; 591 return -1;
592 } 592 }
593 if (filter.flushed == 0) { 593 if (filter.flushed == 0) {
@@ -606,16 +606,16 @@ static int iproute_list_or_flush(int argc, char **argv, int flush)
606 606
607 if (filter.tb != -1) { 607 if (filter.tb != -1) {
608 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) { 608 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
609 perror_msg_and_die("Cannot send dump request"); 609 bb_perror_msg_and_die("Cannot send dump request");
610 } 610 }
611 } else { 611 } else {
612 if (rtnl_rtcache_request(&rth, do_ipv6) < 0) { 612 if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
613 perror_msg_and_die("Cannot send dump request"); 613 bb_perror_msg_and_die("Cannot send dump request");
614 } 614 }
615 } 615 }
616 616
617 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) { 617 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
618 error_msg_and_die("Dump terminated"); 618 bb_error_msg_and_die("Dump terminated");
619 } 619 }
620 620
621 exit(0); 621 exit(0);
@@ -703,7 +703,7 @@ static int iproute_get(int argc, char **argv)
703 } 703 }
704 704
705 if (req.r.rtm_dst_len == 0) { 705 if (req.r.rtm_dst_len == 0) {
706 error_msg_and_die("need at least destination address"); 706 bb_error_msg_and_die("need at least destination address");
707 } 707 }
708 708
709 if (rtnl_open(&rth, 0) < 0) 709 if (rtnl_open(&rth, 0) < 0)
@@ -716,14 +716,14 @@ static int iproute_get(int argc, char **argv)
716 716
717 if (idev) { 717 if (idev) {
718 if ((idx = ll_name_to_index(idev)) == 0) { 718 if ((idx = ll_name_to_index(idev)) == 0) {
719 error_msg("Cannot find device \"%s\"", idev); 719 bb_error_msg("Cannot find device \"%s\"", idev);
720 return -1; 720 return -1;
721 } 721 }
722 addattr32(&req.n, sizeof(req), RTA_IIF, idx); 722 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
723 } 723 }
724 if (odev) { 724 if (odev) {
725 if ((idx = ll_name_to_index(odev)) == 0) { 725 if ((idx = ll_name_to_index(odev)) == 0) {
726 error_msg("Cannot find device \"%s\"", odev); 726 bb_error_msg("Cannot find device \"%s\"", odev);
727 return -1; 727 return -1;
728 } 728 }
729 addattr32(&req.n, sizeof(req), RTA_OIF, idx); 729 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
@@ -744,16 +744,16 @@ static int iproute_get(int argc, char **argv)
744 struct rtattr * tb[RTA_MAX+1]; 744 struct rtattr * tb[RTA_MAX+1];
745 745
746 if (print_route(NULL, &req.n, (void*)stdout) < 0) { 746 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
747 error_msg_and_die("An error :-)"); 747 bb_error_msg_and_die("An error :-)");
748 } 748 }
749 749
750 if (req.n.nlmsg_type != RTM_NEWROUTE) { 750 if (req.n.nlmsg_type != RTM_NEWROUTE) {
751 error_msg("Not a route?"); 751 bb_error_msg("Not a route?");
752 return -1; 752 return -1;
753 } 753 }
754 len -= NLMSG_LENGTH(sizeof(*r)); 754 len -= NLMSG_LENGTH(sizeof(*r));
755 if (len < 0) { 755 if (len < 0) {
756 error_msg("Wrong len %d", len); 756 bb_error_msg("Wrong len %d", len);
757 return -1; 757 return -1;
758 } 758 }
759 759
@@ -764,7 +764,7 @@ static int iproute_get(int argc, char **argv)
764 tb[RTA_PREFSRC]->rta_type = RTA_SRC; 764 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
765 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]); 765 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
766 } else if (!tb[RTA_SRC]) { 766 } else if (!tb[RTA_SRC]) {
767 error_msg("Failed to connect the route"); 767 bb_error_msg("Failed to connect the route");
768 return -1; 768 return -1;
769 } 769 }
770 if (!odev && tb[RTA_OIF]) { 770 if (!odev && tb[RTA_OIF]) {
@@ -785,7 +785,7 @@ static int iproute_get(int argc, char **argv)
785 } 785 }
786 786
787 if (print_route(NULL, &req.n, (void*)stdout) < 0) { 787 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
788 error_msg_and_die("An error :-)"); 788 bb_error_msg_and_die("An error :-)");
789 } 789 }
790 790
791 exit(0); 791 exit(0);
@@ -830,7 +830,7 @@ int do_iproute(int argc, char **argv)
830 case 11: /* flush */ 830 case 11: /* flush */
831 return iproute_list_or_flush(argc-1, argv+1, 1); 831 return iproute_list_or_flush(argc-1, argv+1, 1);
832 default: 832 default:
833 error_msg_and_die("Unknown command %s", *argv); 833 bb_error_msg_and_die("Unknown command %s", *argv);
834 } 834 }
835 835
836 return iproute_modify(cmd, flags, argc-1, argv+1); 836 return iproute_modify(cmd, flags, argc-1, argv+1);
diff --git a/networking/libiproute/iptunnel.c b/networking/libiproute/iptunnel.c
index 1eb17799c..eae5bb091 100644
--- a/networking/libiproute/iptunnel.c
+++ b/networking/libiproute/iptunnel.c
@@ -47,7 +47,7 @@ static int do_ioctl_get_ifindex(char *dev)
47 strcpy(ifr.ifr_name, dev); 47 strcpy(ifr.ifr_name, dev);
48 fd = socket(AF_INET, SOCK_DGRAM, 0); 48 fd = socket(AF_INET, SOCK_DGRAM, 0);
49 if (ioctl(fd, SIOCGIFINDEX, &ifr)) { 49 if (ioctl(fd, SIOCGIFINDEX, &ifr)) {
50 perror_msg("ioctl"); 50 bb_perror_msg("ioctl");
51 return 0; 51 return 0;
52 } 52 }
53 close(fd); 53 close(fd);
@@ -62,7 +62,7 @@ static int do_ioctl_get_iftype(char *dev)
62 strcpy(ifr.ifr_name, dev); 62 strcpy(ifr.ifr_name, dev);
63 fd = socket(AF_INET, SOCK_DGRAM, 0); 63 fd = socket(AF_INET, SOCK_DGRAM, 0);
64 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) { 64 if (ioctl(fd, SIOCGIFHWADDR, &ifr)) {
65 perror_msg("ioctl"); 65 bb_perror_msg("ioctl");
66 return -1; 66 return -1;
67 } 67 }
68 close(fd); 68 close(fd);
@@ -78,7 +78,7 @@ static char *do_ioctl_get_ifname(int idx)
78 ifr.ifr_ifindex = idx; 78 ifr.ifr_ifindex = idx;
79 fd = socket(AF_INET, SOCK_DGRAM, 0); 79 fd = socket(AF_INET, SOCK_DGRAM, 0);
80 if (ioctl(fd, SIOCGIFNAME, &ifr)) { 80 if (ioctl(fd, SIOCGIFNAME, &ifr)) {
81 perror_msg("ioctl"); 81 bb_perror_msg("ioctl");
82 return NULL; 82 return NULL;
83 } 83 }
84 close(fd); 84 close(fd);
@@ -98,7 +98,7 @@ static int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p)
98 fd = socket(AF_INET, SOCK_DGRAM, 0); 98 fd = socket(AF_INET, SOCK_DGRAM, 0);
99 err = ioctl(fd, SIOCGETTUNNEL, &ifr); 99 err = ioctl(fd, SIOCGETTUNNEL, &ifr);
100 if (err) { 100 if (err) {
101 perror_msg("ioctl"); 101 bb_perror_msg("ioctl");
102 } 102 }
103 close(fd); 103 close(fd);
104 return err; 104 return err;
@@ -119,7 +119,7 @@ static int do_add_ioctl(int cmd, char *basedev, struct ip_tunnel_parm *p)
119 fd = socket(AF_INET, SOCK_DGRAM, 0); 119 fd = socket(AF_INET, SOCK_DGRAM, 0);
120 err = ioctl(fd, cmd, &ifr); 120 err = ioctl(fd, cmd, &ifr);
121 if (err) { 121 if (err) {
122 perror_msg("ioctl"); 122 bb_perror_msg("ioctl");
123 } 123 }
124 close(fd); 124 close(fd);
125 return err; 125 return err;
@@ -140,7 +140,7 @@ static int do_del_ioctl(char *basedev, struct ip_tunnel_parm *p)
140 fd = socket(AF_INET, SOCK_DGRAM, 0); 140 fd = socket(AF_INET, SOCK_DGRAM, 0);
141 err = ioctl(fd, SIOCDELTUNNEL, &ifr); 141 err = ioctl(fd, SIOCDELTUNNEL, &ifr);
142 if (err) { 142 if (err) {
143 perror_msg("ioctl"); 143 bb_perror_msg("ioctl");
144 } 144 }
145 close(fd); 145 close(fd);
146 return err; 146 return err;
@@ -166,26 +166,26 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
166 if (strcmp(*argv, "ipip") == 0 || 166 if (strcmp(*argv, "ipip") == 0 ||
167 strcmp(*argv, "ip/ip") == 0) { 167 strcmp(*argv, "ip/ip") == 0) {
168 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) { 168 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPIP) {
169 error_msg("You managed to ask for more than one tunnel mode."); 169 bb_error_msg("You managed to ask for more than one tunnel mode.");
170 exit(-1); 170 exit(-1);
171 } 171 }
172 p->iph.protocol = IPPROTO_IPIP; 172 p->iph.protocol = IPPROTO_IPIP;
173 } else if (strcmp(*argv, "gre") == 0 || 173 } else if (strcmp(*argv, "gre") == 0 ||
174 strcmp(*argv, "gre/ip") == 0) { 174 strcmp(*argv, "gre/ip") == 0) {
175 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) { 175 if (p->iph.protocol && p->iph.protocol != IPPROTO_GRE) {
176 error_msg("You managed to ask for more than one tunnel mode."); 176 bb_error_msg("You managed to ask for more than one tunnel mode.");
177 exit(-1); 177 exit(-1);
178 } 178 }
179 p->iph.protocol = IPPROTO_GRE; 179 p->iph.protocol = IPPROTO_GRE;
180 } else if (strcmp(*argv, "sit") == 0 || 180 } else if (strcmp(*argv, "sit") == 0 ||
181 strcmp(*argv, "ipv6/ip") == 0) { 181 strcmp(*argv, "ipv6/ip") == 0) {
182 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) { 182 if (p->iph.protocol && p->iph.protocol != IPPROTO_IPV6) {
183 error_msg("You managed to ask for more than one tunnel mode."); 183 bb_error_msg("You managed to ask for more than one tunnel mode.");
184 exit(-1); 184 exit(-1);
185 } 185 }
186 p->iph.protocol = IPPROTO_IPV6; 186 p->iph.protocol = IPPROTO_IPV6;
187 } else { 187 } else {
188 error_msg("Cannot guess tunnel mode."); 188 bb_error_msg("Cannot guess tunnel mode.");
189 exit(-1); 189 exit(-1);
190 } 190 }
191 } else if (strcmp(*argv, "key") == 0) { 191 } else if (strcmp(*argv, "key") == 0) {
@@ -197,7 +197,7 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
197 p->i_key = p->o_key = get_addr32(*argv); 197 p->i_key = p->o_key = get_addr32(*argv);
198 else { 198 else {
199 if (get_unsigned(&uval, *argv, 0)<0) { 199 if (get_unsigned(&uval, *argv, 0)<0) {
200 error_msg("invalid value of \"key\""); 200 bb_error_msg("invalid value of \"key\"");
201 exit(-1); 201 exit(-1);
202 } 202 }
203 p->i_key = p->o_key = htonl(uval); 203 p->i_key = p->o_key = htonl(uval);
@@ -210,7 +210,7 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
210 p->o_key = get_addr32(*argv); 210 p->o_key = get_addr32(*argv);
211 else { 211 else {
212 if (get_unsigned(&uval, *argv, 0)<0) { 212 if (get_unsigned(&uval, *argv, 0)<0) {
213 error_msg("invalid value of \"ikey\""); 213 bb_error_msg("invalid value of \"ikey\"");
214 exit(-1); 214 exit(-1);
215 } 215 }
216 p->i_key = htonl(uval); 216 p->i_key = htonl(uval);
@@ -223,7 +223,7 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
223 p->o_key = get_addr32(*argv); 223 p->o_key = get_addr32(*argv);
224 else { 224 else {
225 if (get_unsigned(&uval, *argv, 0)<0) { 225 if (get_unsigned(&uval, *argv, 0)<0) {
226 error_msg("invalid value of \"okey\""); 226 bb_error_msg("invalid value of \"okey\"");
227 exit(-1); 227 exit(-1);
228 } 228 }
229 p->o_key = htonl(uval); 229 p->o_key = htonl(uval);
@@ -308,7 +308,7 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
308 308
309 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) { 309 if (p->iph.protocol == IPPROTO_IPIP || p->iph.protocol == IPPROTO_IPV6) {
310 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) { 310 if ((p->i_flags & GRE_KEY) || (p->o_flags & GRE_KEY)) {
311 error_msg("Keys are not allowed with ipip and sit."); 311 bb_error_msg("Keys are not allowed with ipip and sit.");
312 return -1; 312 return -1;
313 } 313 }
314 } 314 }
@@ -328,7 +328,7 @@ static int parse_args(int argc, char **argv, int cmd, struct ip_tunnel_parm *p)
328 p->o_flags |= GRE_KEY; 328 p->o_flags |= GRE_KEY;
329 } 329 }
330 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) { 330 if (IN_MULTICAST(ntohl(p->iph.daddr)) && !p->iph.saddr) {
331 error_msg("Broadcast tunnel requires a source address."); 331 bb_error_msg("Broadcast tunnel requires a source address.");
332 return -1; 332 return -1;
333 } 333 }
334 return 0; 334 return 0;
@@ -343,7 +343,7 @@ static int do_add(int cmd, int argc, char **argv)
343 return -1; 343 return -1;
344 344
345 if (p.iph.ttl && p.iph.frag_off == 0) { 345 if (p.iph.ttl && p.iph.frag_off == 0) {
346 error_msg("ttl != 0 and noptmudisc are incompatible"); 346 bb_error_msg("ttl != 0 and noptmudisc are incompatible");
347 return -1; 347 return -1;
348 } 348 }
349 349
@@ -355,7 +355,7 @@ static int do_add(int cmd, int argc, char **argv)
355 case IPPROTO_IPV6: 355 case IPPROTO_IPV6:
356 return do_add_ioctl(cmd, "sit0", &p); 356 return do_add_ioctl(cmd, "sit0", &p);
357 default: 357 default:
358 error_msg("cannot determine tunnel mode (ipip, gre or sit)"); 358 bb_error_msg("cannot determine tunnel mode (ipip, gre or sit)");
359 return -1; 359 return -1;
360 } 360 }
361 return -1; 361 return -1;
@@ -464,7 +464,7 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
464 buf[sizeof(buf) - 1] = 0; 464 buf[sizeof(buf) - 1] = 0;
465 if ((ptr = strchr(buf, ':')) == NULL || 465 if ((ptr = strchr(buf, ':')) == NULL ||
466 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) { 466 (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
467 error_msg("Wrong format of /proc/net/dev. Sorry."); 467 bb_error_msg("Wrong format of /proc/net/dev. Sorry.");
468 return -1; 468 return -1;
469 } 469 }
470 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld", 470 if (sscanf(ptr, "%ld%ld%ld%ld%ld%ld%ld%*d%ld%ld%ld%ld%ld%ld%ld",
@@ -477,7 +477,7 @@ static int do_tunnels_list(struct ip_tunnel_parm *p)
477 continue; 477 continue;
478 type = do_ioctl_get_iftype(name); 478 type = do_ioctl_get_iftype(name);
479 if (type == -1) { 479 if (type == -1) {
480 error_msg("Failed to get type of [%s]", name); 480 bb_error_msg("Failed to get type of [%s]", name);
481 continue; 481 continue;
482 } 482 }
483 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT) 483 if (type != ARPHRD_TUNNEL && type != ARPHRD_IPGRE && type != ARPHRD_SIT)
@@ -543,6 +543,6 @@ int do_iptunnel(int argc, char **argv)
543 } else 543 } else
544 return do_show(0, NULL); 544 return do_show(0, NULL);
545 545
546 error_msg("Command \"%s\" is unknown, try \"ip tunnel help\".", *argv); 546 bb_error_msg("Command \"%s\" is unknown, try \"ip tunnel help\".", *argv);
547 exit(-1); 547 exit(-1);
548} 548}
diff --git a/networking/libiproute/libnetlink.c b/networking/libiproute/libnetlink.c
index fbc555dfe..04411931d 100644
--- a/networking/libiproute/libnetlink.c
+++ b/networking/libiproute/libnetlink.c
@@ -34,7 +34,7 @@ int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
34 34
35 rth->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); 35 rth->fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
36 if (rth->fd < 0) { 36 if (rth->fd < 0) {
37 perror_msg("Cannot open netlink socket"); 37 bb_perror_msg("Cannot open netlink socket");
38 return -1; 38 return -1;
39 } 39 }
40 40
@@ -43,20 +43,20 @@ int rtnl_open(struct rtnl_handle *rth, unsigned subscriptions)
43 rth->local.nl_groups = subscriptions; 43 rth->local.nl_groups = subscriptions;
44 44
45 if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) { 45 if (bind(rth->fd, (struct sockaddr*)&rth->local, sizeof(rth->local)) < 0) {
46 perror_msg("Cannot bind netlink socket"); 46 bb_perror_msg("Cannot bind netlink socket");
47 return -1; 47 return -1;
48 } 48 }
49 addr_len = sizeof(rth->local); 49 addr_len = sizeof(rth->local);
50 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) { 50 if (getsockname(rth->fd, (struct sockaddr*)&rth->local, &addr_len) < 0) {
51 perror_msg("Cannot getsockname"); 51 bb_perror_msg("Cannot getsockname");
52 return -1; 52 return -1;
53 } 53 }
54 if (addr_len != sizeof(rth->local)) { 54 if (addr_len != sizeof(rth->local)) {
55 error_msg("Wrong address length %d", addr_len); 55 bb_error_msg("Wrong address length %d", addr_len);
56 return -1; 56 return -1;
57 } 57 }
58 if (rth->local.nl_family != AF_NETLINK) { 58 if (rth->local.nl_family != AF_NETLINK) {
59 error_msg("Wrong address family %d", rth->local.nl_family); 59 bb_error_msg("Wrong address family %d", rth->local.nl_family);
60 return -1; 60 return -1;
61 } 61 }
62 rth->seq = time(NULL); 62 rth->seq = time(NULL);
@@ -144,15 +144,15 @@ int rtnl_dump_filter(struct rtnl_handle *rth,
144 if (status < 0) { 144 if (status < 0) {
145 if (errno == EINTR) 145 if (errno == EINTR)
146 continue; 146 continue;
147 perror_msg("OVERRUN"); 147 bb_perror_msg("OVERRUN");
148 continue; 148 continue;
149 } 149 }
150 if (status == 0) { 150 if (status == 0) {
151 error_msg("EOF on netlink"); 151 bb_error_msg("EOF on netlink");
152 return -1; 152 return -1;
153 } 153 }
154 if (msg.msg_namelen != sizeof(nladdr)) { 154 if (msg.msg_namelen != sizeof(nladdr)) {
155 error_msg_and_die("sender address length == %d", msg.msg_namelen); 155 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
156 } 156 }
157 157
158 h = (struct nlmsghdr*)buf; 158 h = (struct nlmsghdr*)buf;
@@ -176,10 +176,10 @@ int rtnl_dump_filter(struct rtnl_handle *rth,
176 if (h->nlmsg_type == NLMSG_ERROR) { 176 if (h->nlmsg_type == NLMSG_ERROR) {
177 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h); 177 struct nlmsgerr *l_err = (struct nlmsgerr*)NLMSG_DATA(h);
178 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) { 178 if (h->nlmsg_len < NLMSG_LENGTH(sizeof(struct nlmsgerr))) {
179 error_msg("ERROR truncated"); 179 bb_error_msg("ERROR truncated");
180 } else { 180 } else {
181 errno = -l_err->error; 181 errno = -l_err->error;
182 perror_msg("RTNETLINK answers"); 182 bb_perror_msg("RTNETLINK answers");
183 } 183 }
184 return -1; 184 return -1;
185 } 185 }
@@ -192,11 +192,11 @@ skip_it:
192 h = NLMSG_NEXT(h, status); 192 h = NLMSG_NEXT(h, status);
193 } 193 }
194 if (msg.msg_flags & MSG_TRUNC) { 194 if (msg.msg_flags & MSG_TRUNC) {
195 error_msg("Message truncated"); 195 bb_error_msg("Message truncated");
196 continue; 196 continue;
197 } 197 }
198 if (status) { 198 if (status) {
199 error_msg_and_die("!!!Remnant of size %d", status); 199 bb_error_msg_and_die("!!!Remnant of size %d", status);
200 } 200 }
201 } 201 }
202} 202}
@@ -231,7 +231,7 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
231 status = sendmsg(rtnl->fd, &msg, 0); 231 status = sendmsg(rtnl->fd, &msg, 0);
232 232
233 if (status < 0) { 233 if (status < 0) {
234 perror_msg("Cannot talk to rtnetlink"); 234 bb_perror_msg("Cannot talk to rtnetlink");
235 return -1; 235 return -1;
236 } 236 }
237 237
@@ -245,15 +245,15 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
245 if (errno == EINTR) { 245 if (errno == EINTR) {
246 continue; 246 continue;
247 } 247 }
248 perror_msg("OVERRUN"); 248 bb_perror_msg("OVERRUN");
249 continue; 249 continue;
250 } 250 }
251 if (status == 0) { 251 if (status == 0) {
252 error_msg("EOF on netlink"); 252 bb_error_msg("EOF on netlink");
253 return -1; 253 return -1;
254 } 254 }
255 if (msg.msg_namelen != sizeof(nladdr)) { 255 if (msg.msg_namelen != sizeof(nladdr)) {
256 error_msg_and_die("sender address length == %d", msg.msg_namelen); 256 bb_error_msg_and_die("sender address length == %d", msg.msg_namelen);
257 } 257 }
258 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) { 258 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
259 int l_err; 259 int l_err;
@@ -262,10 +262,10 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
262 262
263 if (l<0 || len>status) { 263 if (l<0 || len>status) {
264 if (msg.msg_flags & MSG_TRUNC) { 264 if (msg.msg_flags & MSG_TRUNC) {
265 error_msg("Truncated message"); 265 bb_error_msg("Truncated message");
266 return -1; 266 return -1;
267 } 267 }
268 error_msg_and_die("!!!malformed message: len=%d", len); 268 bb_error_msg_and_die("!!!malformed message: len=%d", len);
269 } 269 }
270 270
271 if (h->nlmsg_pid != rtnl->local.nl_pid || 271 if (h->nlmsg_pid != rtnl->local.nl_pid ||
@@ -282,7 +282,7 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
282 if (h->nlmsg_type == NLMSG_ERROR) { 282 if (h->nlmsg_type == NLMSG_ERROR) {
283 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h); 283 struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(h);
284 if (l < sizeof(struct nlmsgerr)) { 284 if (l < sizeof(struct nlmsgerr)) {
285 error_msg("ERROR truncated"); 285 bb_error_msg("ERROR truncated");
286 } else { 286 } else {
287 errno = -err->error; 287 errno = -err->error;
288 if (errno == 0) { 288 if (errno == 0) {
@@ -291,7 +291,7 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
291 } 291 }
292 return 0; 292 return 0;
293 } 293 }
294 perror_msg("RTNETLINK answers"); 294 bb_perror_msg("RTNETLINK answers");
295 } 295 }
296 return -1; 296 return -1;
297 } 297 }
@@ -300,17 +300,17 @@ int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n, pid_t peer,
300 return 0; 300 return 0;
301 } 301 }
302 302
303 error_msg("Unexpected reply!!!"); 303 bb_error_msg("Unexpected reply!!!");
304 304
305 status -= NLMSG_ALIGN(len); 305 status -= NLMSG_ALIGN(len);
306 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); 306 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
307 } 307 }
308 if (msg.msg_flags & MSG_TRUNC) { 308 if (msg.msg_flags & MSG_TRUNC) {
309 error_msg("Message truncated"); 309 bb_error_msg("Message truncated");
310 continue; 310 continue;
311 } 311 }
312 if (status) { 312 if (status) {
313 error_msg_and_die("!!!Remnant of size %d", status); 313 bb_error_msg_and_die("!!!Remnant of size %d", status);
314 } 314 }
315 } 315 }
316} 316}
@@ -346,15 +346,15 @@ int rtnl_listen(struct rtnl_handle *rtnl,
346 if (status < 0) { 346 if (status < 0) {
347 if (errno == EINTR) 347 if (errno == EINTR)
348 continue; 348 continue;
349 perror_msg("OVERRUN"); 349 bb_perror_msg("OVERRUN");
350 continue; 350 continue;
351 } 351 }
352 if (status == 0) { 352 if (status == 0) {
353 error_msg("EOF on netlink"); 353 bb_error_msg("EOF on netlink");
354 return -1; 354 return -1;
355 } 355 }
356 if (msg.msg_namelen != sizeof(nladdr)) { 356 if (msg.msg_namelen != sizeof(nladdr)) {
357 error_msg_and_die("Sender address length == %d", msg.msg_namelen); 357 bb_error_msg_and_die("Sender address length == %d", msg.msg_namelen);
358 } 358 }
359 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) { 359 for (h = (struct nlmsghdr*)buf; status >= sizeof(*h); ) {
360 int err; 360 int err;
@@ -363,10 +363,10 @@ int rtnl_listen(struct rtnl_handle *rtnl,
363 363
364 if (l<0 || len>status) { 364 if (l<0 || len>status) {
365 if (msg.msg_flags & MSG_TRUNC) { 365 if (msg.msg_flags & MSG_TRUNC) {
366 error_msg("Truncated message"); 366 bb_error_msg("Truncated message");
367 return -1; 367 return -1;
368 } 368 }
369 error_msg_and_die("!!!malformed message: len=%d", len); 369 bb_error_msg_and_die("!!!malformed message: len=%d", len);
370 } 370 }
371 371
372 err = handler(&nladdr, h, jarg); 372 err = handler(&nladdr, h, jarg);
@@ -378,11 +378,11 @@ int rtnl_listen(struct rtnl_handle *rtnl,
378 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len)); 378 h = (struct nlmsghdr*)((char*)h + NLMSG_ALIGN(len));
379 } 379 }
380 if (msg.msg_flags & MSG_TRUNC) { 380 if (msg.msg_flags & MSG_TRUNC) {
381 error_msg("Message truncated"); 381 bb_error_msg("Message truncated");
382 continue; 382 continue;
383 } 383 }
384 if (status) { 384 if (status) {
385 error_msg_and_die("!!!Remnant of size %d", status); 385 bb_error_msg_and_die("!!!Remnant of size %d", status);
386 } 386 }
387 } 387 }
388} 388}
@@ -410,7 +410,7 @@ int rtnl_from_file(FILE *rtnl,
410 if (status < 0) { 410 if (status < 0) {
411 if (errno == EINTR) 411 if (errno == EINTR)
412 continue; 412 continue;
413 perror_msg("rtnl_from_file: fread"); 413 bb_perror_msg("rtnl_from_file: fread");
414 return -1; 414 return -1;
415 } 415 }
416 if (status == 0) 416 if (status == 0)
@@ -421,7 +421,7 @@ int rtnl_from_file(FILE *rtnl,
421 l = len - sizeof(*h); 421 l = len - sizeof(*h);
422 422
423 if (l<0 || len>sizeof(buf)) { 423 if (l<0 || len>sizeof(buf)) {
424 error_msg("!!!malformed message: len=%d @%lu", 424 bb_error_msg("!!!malformed message: len=%d @%lu",
425 len, ftell(rtnl)); 425 len, ftell(rtnl));
426 return -1; 426 return -1;
427 } 427 }
@@ -429,11 +429,11 @@ int rtnl_from_file(FILE *rtnl,
429 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl); 429 status = fread(NLMSG_DATA(h), 1, NLMSG_ALIGN(l), rtnl);
430 430
431 if (status < 0) { 431 if (status < 0) {
432 perror_msg("rtnl_from_file: fread"); 432 bb_perror_msg("rtnl_from_file: fread");
433 return -1; 433 return -1;
434 } 434 }
435 if (status < l) { 435 if (status < l) {
436 error_msg("rtnl-from_file: truncated message"); 436 bb_error_msg("rtnl-from_file: truncated message");
437 return -1; 437 return -1;
438 } 438 }
439 439
@@ -514,7 +514,7 @@ int parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len)
514 rta = RTA_NEXT(rta,len); 514 rta = RTA_NEXT(rta,len);
515 } 515 }
516 if (len) { 516 if (len) {
517 error_msg("!!!Deficit %d, rta_len=%d", len, rta->rta_len); 517 bb_error_msg("!!!Deficit %d, rta_len=%d", len, rta->rta_len);
518 } 518 }
519 return 0; 519 return 0;
520} 520}
diff --git a/networking/libiproute/ll_addr.c b/networking/libiproute/ll_addr.c
index 39e561fe5..ada685f4e 100644
--- a/networking/libiproute/ll_addr.c
+++ b/networking/libiproute/ll_addr.c
@@ -44,7 +44,7 @@ int ll_addr_a2n(unsigned char *lladdr, int len, char *arg)
44 if (strchr(arg, '.')) { 44 if (strchr(arg, '.')) {
45 inet_prefix pfx; 45 inet_prefix pfx;
46 if (get_addr_1(&pfx, arg, AF_INET)) { 46 if (get_addr_1(&pfx, arg, AF_INET)) {
47 error_msg("\"%s\" is invalid lladdr.", arg); 47 bb_error_msg("\"%s\" is invalid lladdr.", arg);
48 return -1; 48 return -1;
49 } 49 }
50 if (len < 4) { 50 if (len < 4) {
@@ -63,11 +63,11 @@ int ll_addr_a2n(unsigned char *lladdr, int len, char *arg)
63 cp++; 63 cp++;
64 } 64 }
65 if (sscanf(arg, "%x", &temp) != 1) { 65 if (sscanf(arg, "%x", &temp) != 1) {
66 error_msg("\"%s\" is invalid lladdr.", arg); 66 bb_error_msg("\"%s\" is invalid lladdr.", arg);
67 return -1; 67 return -1;
68 } 68 }
69 if (temp < 0 || temp > 255) { 69 if (temp < 0 || temp > 255) {
70 error_msg("\"%s\" is invalid lladdr.", arg); 70 bb_error_msg("\"%s\" is invalid lladdr.", arg);
71 return -1; 71 return -1;
72 } 72 }
73 lladdr[i] = temp; 73 lladdr[i] = temp;
diff --git a/networking/libiproute/utils.c b/networking/libiproute/utils.c
index 2c23cd294..fa1548609 100644
--- a/networking/libiproute/utils.c
+++ b/networking/libiproute/utils.c
@@ -209,10 +209,10 @@ int get_prefix_1(inet_prefix * dst, char *arg, int family)
209int get_addr(inet_prefix * dst, char *arg, int family) 209int get_addr(inet_prefix * dst, char *arg, int family)
210{ 210{
211 if (family == AF_PACKET) { 211 if (family == AF_PACKET) {
212 error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context.", arg); 212 bb_error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context.", arg);
213 } 213 }
214 if (get_addr_1(dst, arg, family)) { 214 if (get_addr_1(dst, arg, family)) {
215 error_msg_and_die("an inet address is expected rather than \"%s\".", arg); 215 bb_error_msg_and_die("an inet address is expected rather than \"%s\".", arg);
216 } 216 }
217 return 0; 217 return 0;
218} 218}
@@ -220,10 +220,10 @@ int get_addr(inet_prefix * dst, char *arg, int family)
220int get_prefix(inet_prefix * dst, char *arg, int family) 220int get_prefix(inet_prefix * dst, char *arg, int family)
221{ 221{
222 if (family == AF_PACKET) { 222 if (family == AF_PACKET) {
223 error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context.", arg); 223 bb_error_msg_and_die("\"%s\" may be inet address, but it is not allowed in this context.", arg);
224 } 224 }
225 if (get_prefix_1(dst, arg, family)) { 225 if (get_prefix_1(dst, arg, family)) {
226 error_msg_and_die("an inet address is expected rather than \"%s\".", arg); 226 bb_error_msg_and_die("an inet address is expected rather than \"%s\".", arg);
227 } 227 }
228 return 0; 228 return 0;
229} 229}
@@ -233,32 +233,32 @@ __u32 get_addr32(char *name)
233 inet_prefix addr; 233 inet_prefix addr;
234 234
235 if (get_addr_1(&addr, name, AF_INET)) { 235 if (get_addr_1(&addr, name, AF_INET)) {
236 error_msg_and_die("an IP address is expected rather than \"%s\"", name); 236 bb_error_msg_and_die("an IP address is expected rather than \"%s\"", name);
237 } 237 }
238 return addr.data[0]; 238 return addr.data[0];
239} 239}
240 240
241void incomplete_command() 241void incomplete_command()
242{ 242{
243 error_msg("Command line is not complete. Try option \"help\""); 243 bb_error_msg("Command line is not complete. Try option \"help\"");
244 exit(-1); 244 exit(-1);
245} 245}
246 246
247void invarg(char *msg, char *arg) 247void invarg(char *msg, char *arg)
248{ 248{
249 error_msg("argument \"%s\" is wrong: %s", arg, msg); 249 bb_error_msg("argument \"%s\" is wrong: %s", arg, msg);
250 exit(-1); 250 exit(-1);
251} 251}
252 252
253void duparg(char *key, char *arg) 253void duparg(char *key, char *arg)
254{ 254{
255 error_msg("duplicate \"%s\": \"%s\" is the second value.", key, arg); 255 bb_error_msg("duplicate \"%s\": \"%s\" is the second value.", key, arg);
256 exit(-1); 256 exit(-1);
257} 257}
258 258
259void duparg2(char *key, char *arg) 259void duparg2(char *key, char *arg)
260{ 260{
261 error_msg("either \"%s\" is duplicate, or \"%s\" is a garbage.", key, arg); 261 bb_error_msg("either \"%s\" is duplicate, or \"%s\" is a garbage.", key, arg);
262 exit(-1); 262 exit(-1);
263} 263}
264 264
diff --git a/networking/nameif.c b/networking/nameif.c
index f3b927158..e74a04213 100644
--- a/networking/nameif.c
+++ b/networking/nameif.c
@@ -63,11 +63,11 @@ static void serror(const char *s, ...)
63 va_start(ap, s); 63 va_start(ap, s);
64 64
65 if (use_syslog) { 65 if (use_syslog) {
66 openlog(applet_name, 0, LOG_LOCAL0); 66 openlog(bb_applet_name, 0, LOG_LOCAL0);
67 vsyslog(LOG_ERR, s, ap); 67 vsyslog(LOG_ERR, s, ap);
68 closelog(); 68 closelog();
69 } else { 69 } else {
70 verror_msg(s, ap); 70 bb_verror_msg(s, ap);
71 putc('\n', stderr); 71 putc('\n', stderr);
72 } 72 }
73 73
@@ -111,12 +111,12 @@ int nameif_main(int argc, char **argv)
111 use_syslog = 1; 111 use_syslog = 1;
112 break; 112 break;
113 default: 113 default:
114 show_usage(); 114 bb_show_usage();
115 } 115 }
116 } 116 }
117 117
118 if ((argc - optind) & 1) 118 if ((argc - optind) & 1)
119 show_usage(); 119 bb_show_usage();
120 120
121 if (optind < argc) { 121 if (optind < argc) {
122 char **a = argv + optind; 122 char **a = argv + optind;
@@ -126,7 +126,7 @@ int nameif_main(int argc, char **argv)
126 if (strlen(*a) > IF_NAMESIZE) 126 if (strlen(*a) > IF_NAMESIZE)
127 serror("interface name `%s' too long", *a); 127 serror("interface name `%s' too long", *a);
128 ch = xcalloc(1, sizeof(mactable_t)); 128 ch = xcalloc(1, sizeof(mactable_t));
129 ch->ifname = xstrdup(*a++); 129 ch->ifname = bb_xstrdup(*a++);
130 ch->mac = cc_macaddr(*a++); 130 ch->mac = cc_macaddr(*a++);
131 if (clist) 131 if (clist)
132 clist->prev = ch; 132 clist->prev = ch;
@@ -134,9 +134,9 @@ int nameif_main(int argc, char **argv)
134 clist = ch; 134 clist = ch;
135 } 135 }
136 } else { 136 } else {
137 ifh = xfopen(fname, "r"); 137 ifh = bb_xfopen(fname, "r");
138 138
139 while ((line = get_line_from_file(ifh)) != NULL) { 139 while ((line = bb_get_line_from_file(ifh)) != NULL) {
140 char *line_ptr; 140 char *line_ptr;
141 size_t name_length; 141 size_t name_length;
142 142
@@ -145,7 +145,7 @@ int nameif_main(int argc, char **argv)
145 continue; 145 continue;
146 name_length = strcspn(line_ptr, " \t"); 146 name_length = strcspn(line_ptr, " \t");
147 ch = xcalloc(1, sizeof(mactable_t)); 147 ch = xcalloc(1, sizeof(mactable_t));
148 ch->ifname = xstrndup(line_ptr, name_length); 148 ch->ifname = bb_xstrndup(line_ptr, name_length);
149 if (name_length > IF_NAMESIZE) 149 if (name_length > IF_NAMESIZE)
150 serror("interface name `%s' too long", ch->ifname); 150 serror("interface name `%s' too long", ch->ifname);
151 line_ptr += name_length; 151 line_ptr += name_length;
diff --git a/networking/nc.c b/networking/nc.c
index 63d8c5b19..4888ccceb 100644
--- a/networking/nc.c
+++ b/networking/nc.c
@@ -72,7 +72,7 @@ int nc_main(int argc, char **argv)
72 break; 72 break;
73#endif 73#endif
74 default: 74 default:
75 show_usage(); 75 bb_show_usage();
76 } 76 }
77 } 77 }
78 78
@@ -85,13 +85,13 @@ int nc_main(int argc, char **argv)
85 85
86 86
87 if ((do_listen && optind != argc) || (!do_listen && optind + 2 != argc)) 87 if ((do_listen && optind != argc) || (!do_listen && optind + 2 != argc))
88 show_usage(); 88 bb_show_usage();
89 89
90 if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 90 if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
91 perror_msg_and_die("socket"); 91 bb_perror_msg_and_die("socket");
92 x = 1; 92 x = 1;
93 if (setsockopt (sfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof (x)) == -1) 93 if (setsockopt (sfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof (x)) == -1)
94 perror_msg_and_die ("reuseaddr failed"); 94 bb_perror_msg_and_die ("reuseaddr failed");
95 address.sin_family = AF_INET; 95 address.sin_family = AF_INET;
96 96
97 if (lport != 0) { 97 if (lport != 0) {
@@ -99,17 +99,17 @@ int nc_main(int argc, char **argv)
99 address.sin_port = htons(lport); 99 address.sin_port = htons(lport);
100 100
101 if (bind(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) 101 if (bind(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
102 perror_msg_and_die("bind"); 102 bb_perror_msg_and_die("bind");
103 } 103 }
104 104
105 if (do_listen) { 105 if (do_listen) {
106 socklen_t addrlen = sizeof(address); 106 socklen_t addrlen = sizeof(address);
107 107
108 if (listen(sfd, 1) < 0) 108 if (listen(sfd, 1) < 0)
109 perror_msg_and_die("listen"); 109 bb_perror_msg_and_die("listen");
110 110
111 if ((tmpfd = accept(sfd, (struct sockaddr *) &address, &addrlen)) < 0) 111 if ((tmpfd = accept(sfd, (struct sockaddr *) &address, &addrlen)) < 0)
112 perror_msg_and_die("accept"); 112 bb_perror_msg_and_die("accept");
113 113
114 close(sfd); 114 close(sfd);
115 sfd = tmpfd; 115 sfd = tmpfd;
@@ -120,7 +120,7 @@ int nc_main(int argc, char **argv)
120 address.sin_port = htons(atoi(argv[optind+1])); 120 address.sin_port = htons(atoi(argv[optind+1]));
121 121
122 if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0) 122 if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
123 perror_msg_and_die("connect"); 123 bb_perror_msg_and_die("connect");
124 } 124 }
125 125
126#ifdef GAPING_SECURITY_HOLE 126#ifdef GAPING_SECURITY_HOLE
@@ -149,12 +149,12 @@ int nc_main(int argc, char **argv)
149 testfds = readfds; 149 testfds = readfds;
150 150
151 if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0) 151 if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
152 perror_msg_and_die("select"); 152 bb_perror_msg_and_die("select");
153 153
154 for (fd = 0; fd < FD_SETSIZE; fd++) { 154 for (fd = 0; fd < FD_SETSIZE; fd++) {
155 if (FD_ISSET(fd, &testfds)) { 155 if (FD_ISSET(fd, &testfds)) {
156 if ((nread = safe_read(fd, buf, sizeof(buf))) < 0) 156 if ((nread = safe_read(fd, buf, sizeof(buf))) < 0)
157 perror_msg_and_die("read"); 157 bb_perror_msg_and_die("read");
158 158
159 if (fd == sfd) { 159 if (fd == sfd) {
160 if (nread == 0) 160 if (nread == 0)
@@ -166,8 +166,8 @@ int nc_main(int argc, char **argv)
166 ofd = sfd; 166 ofd = sfd;
167 } 167 }
168 168
169 if (full_write(ofd, buf, nread) < 0) 169 if (bb_full_write(ofd, buf, nread) < 0)
170 perror_msg_and_die("write"); 170 bb_perror_msg_and_die("write");
171 if (delay > 0) { 171 if (delay > 0) {
172 sleep(delay); 172 sleep(delay);
173 } 173 }
diff --git a/networking/netstat.c b/networking/netstat.c
index d2b2d5ede..17a58876c 100644
--- a/networking/netstat.c
+++ b/networking/netstat.c
@@ -200,7 +200,7 @@ static void tcp_do_one(int lnr, const char *line)
200 } 200 }
201 201
202 if (num < 10) { 202 if (num < 10) {
203 error_msg("warning, got bogus tcp line."); 203 bb_error_msg("warning, got bogus tcp line.");
204 return; 204 return;
205 } 205 }
206 state_str = tcp_state[state]; 206 state_str = tcp_state[state];
@@ -271,7 +271,7 @@ static void udp_do_one(int lnr, const char *line)
271 } 271 }
272 272
273 if (num < 10) { 273 if (num < 10) {
274 error_msg("warning, got bogus udp line."); 274 bb_error_msg("warning, got bogus udp line.");
275 return; 275 return;
276 } 276 }
277 switch (state) { 277 switch (state) {
@@ -365,7 +365,7 @@ static void raw_do_one(int lnr, const char *line)
365 } 365 }
366 366
367 if (num < 10) { 367 if (num < 10) {
368 error_msg("warning, got bogus raw line."); 368 bb_error_msg("warning, got bogus raw line.");
369 return; 369 return;
370 } 370 }
371 state_str=itoa(state); 371 state_str=itoa(state);
@@ -418,7 +418,7 @@ static void unix_do_one(int nr, const char *line)
418 num = sscanf(line, "%p: %lX %lX %lX %X %X %d %s", 418 num = sscanf(line, "%p: %lX %lX %lX %X %X %d %s",
419 &d, &refcnt, &proto, &unix_flags, &type, &state, &inode, path); 419 &d, &refcnt, &proto, &unix_flags, &type, &state, &inode, path);
420 if (num < 6) { 420 if (num < 6) {
421 error_msg("warning, got bogus unix line."); 421 bb_error_msg("warning, got bogus unix line.");
422 return; 422 return;
423 } 423 }
424 if (!(has & HAS_INODE)) 424 if (!(has & HAS_INODE))
@@ -539,7 +539,7 @@ static void do_info(const char *file, const char *name, void (*proc)(int, const
539 if (errno != ENOENT) { 539 if (errno != ENOENT) {
540 perror(file); 540 perror(file);
541 } else { 541 } else {
542 error_msg("no support for `%s' on this system.", name); 542 bb_error_msg("no support for `%s' on this system.", name);
543 } 543 }
544 } else { 544 } else {
545 do { 545 do {
@@ -597,14 +597,14 @@ int netstat_main(int argc, char **argv)
597 new_flags |= NETSTAT_UNIX; 597 new_flags |= NETSTAT_UNIX;
598 break; 598 break;
599 default: 599 default:
600 show_usage(); 600 bb_show_usage();
601 } 601 }
602 if ( showroute ) { 602 if ( showroute ) {
603#ifdef CONFIG_ROUTE 603#ifdef CONFIG_ROUTE
604 displayroutes ( flags & NETSTAT_NUMERIC, !extended ); 604 displayroutes ( flags & NETSTAT_NUMERIC, !extended );
605 return 0; 605 return 0;
606#else 606#else
607 error_msg_and_die( "-r (display routing table) is not compiled in." ); 607 bb_error_msg_and_die( "-r (display routing table) is not compiled in." );
608#endif 608#endif
609 } 609 }
610 610
diff --git a/networking/nslookup.c b/networking/nslookup.c
index edbc65650..0f3102e30 100644
--- a/networking/nslookup.c
+++ b/networking/nslookup.c
@@ -185,7 +185,7 @@ int nslookup_main(int argc, char **argv)
185 */ 185 */
186 186
187 if (argc < 2 || *argv[1]=='-' || argc > 3) 187 if (argc < 2 || *argv[1]=='-' || argc > 3)
188 show_usage(); 188 bb_show_usage();
189 else if(argc == 3) 189 else if(argc == 3)
190 set_default_dns(argv[2]); 190 set_default_dns(argv[2]);
191 191
@@ -199,4 +199,4 @@ int nslookup_main(int argc, char **argv)
199 return EXIT_SUCCESS; 199 return EXIT_SUCCESS;
200} 200}
201 201
202/* $Id: nslookup.c,v 1.29 2002/07/24 00:56:56 sandman Exp $ */ 202/* $Id: nslookup.c,v 1.30 2003/03/19 09:12:38 mjn3 Exp $ */
diff --git a/networking/ping.c b/networking/ping.c
index 044b547ac..28b38db14 100644
--- a/networking/ping.c
+++ b/networking/ping.c
@@ -1,6 +1,6 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * $Id: ping.c,v 1.53 2003/01/12 06:08:33 andersen Exp $ 3 * $Id: ping.c,v 1.54 2003/03/19 09:12:38 mjn3 Exp $
4 * Mini ping implementation for busybox 4 * Mini ping implementation for busybox
5 * 5 *
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> 6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
@@ -208,7 +208,7 @@ static void ping(const char *host)
208 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); 208 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
209 209
210 if (c < 0 || c != sizeof(packet)) 210 if (c < 0 || c != sizeof(packet))
211 perror_msg_and_die("sendto"); 211 bb_perror_msg_and_die("sendto");
212 212
213 signal(SIGALRM, noresp); 213 signal(SIGALRM, noresp);
214 alarm(5); /* give the host 5000ms to respond */ 214 alarm(5); /* give the host 5000ms to respond */
@@ -221,7 +221,7 @@ static void ping(const char *host)
221 (struct sockaddr *) &from, &fromlen)) < 0) { 221 (struct sockaddr *) &from, &fromlen)) < 0) {
222 if (errno == EINTR) 222 if (errno == EINTR)
223 continue; 223 continue;
224 perror_msg("recvfrom"); 224 bb_perror_msg("recvfrom");
225 continue; 225 continue;
226 } 226 }
227 if (c >= 76) { /* ip + icmp */ 227 if (c >= 76) { /* ip + icmp */
@@ -241,7 +241,7 @@ extern int ping_main(int argc, char **argv)
241 argc--; 241 argc--;
242 argv++; 242 argv++;
243 if (argc < 1) 243 if (argc < 1)
244 show_usage(); 244 bb_show_usage();
245 ping(*argv); 245 ping(*argv);
246 return EXIT_SUCCESS; 246 return EXIT_SUCCESS;
247} 247}
@@ -313,9 +313,9 @@ static void sendping(int junk)
313 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in)); 313 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in));
314 314
315 if (i < 0) 315 if (i < 0)
316 perror_msg_and_die("sendto"); 316 bb_perror_msg_and_die("sendto");
317 else if ((size_t)i != sizeof(packet)) 317 else if ((size_t)i != sizeof(packet))
318 error_msg_and_die("ping wrote %d chars; %d expected", i, 318 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
319 (int)sizeof(packet)); 319 (int)sizeof(packet));
320 320
321 signal(SIGALRM, sendping); 321 signal(SIGALRM, sendping);
@@ -410,7 +410,7 @@ static void unpack(char *buf, int sz, struct sockaddr_in *from)
410 printf("\n"); 410 printf("\n");
411 } else 411 } else
412 if (icmppkt->icmp_type != ICMP_ECHO) 412 if (icmppkt->icmp_type != ICMP_ECHO)
413 error_msg("Warning: Got ICMP %d (%s)", 413 bb_error_msg("Warning: Got ICMP %d (%s)",
414 icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type)); 414 icmppkt->icmp_type, icmp_type_name (icmppkt->icmp_type));
415} 415}
416 416
@@ -426,7 +426,7 @@ static void ping(const char *host)
426 pingaddr.sin_family = AF_INET; 426 pingaddr.sin_family = AF_INET;
427 hostent = xgethostbyname(host); 427 hostent = xgethostbyname(host);
428 if (hostent->h_addrtype != AF_INET) 428 if (hostent->h_addrtype != AF_INET)
429 error_msg_and_die("unknown address type; only AF_INET is currently supported."); 429 bb_error_msg_and_die("unknown address type; only AF_INET is currently supported.");
430 430
431 memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr)); 431 memcpy(&pingaddr.sin_addr, hostent->h_addr, sizeof(pingaddr.sin_addr));
432 432
@@ -460,7 +460,7 @@ static void ping(const char *host)
460 (struct sockaddr *) &from, &fromlen)) < 0) { 460 (struct sockaddr *) &from, &fromlen)) < 0) {
461 if (errno == EINTR) 461 if (errno == EINTR)
462 continue; 462 continue;
463 perror_msg("recvfrom"); 463 bb_perror_msg("recvfrom");
464 continue; 464 continue;
465 } 465 }
466 unpack(packet, c, &from); 466 unpack(packet, c, &from);
@@ -489,24 +489,24 @@ extern int ping_main(int argc, char **argv)
489 break; 489 break;
490 case 'c': 490 case 'c':
491 if (--argc <= 0) 491 if (--argc <= 0)
492 show_usage(); 492 bb_show_usage();
493 argv++; 493 argv++;
494 pingcount = atoi(*argv); 494 pingcount = atoi(*argv);
495 break; 495 break;
496 case 's': 496 case 's':
497 if (--argc <= 0) 497 if (--argc <= 0)
498 show_usage(); 498 bb_show_usage();
499 argv++; 499 argv++;
500 datalen = atoi(*argv); 500 datalen = atoi(*argv);
501 break; 501 break;
502 default: 502 default:
503 show_usage(); 503 bb_show_usage();
504 } 504 }
505 argc--; 505 argc--;
506 argv++; 506 argv++;
507 } 507 }
508 if (argc < 1) 508 if (argc < 1)
509 show_usage(); 509 bb_show_usage();
510 510
511 myid = getpid() & 0xFFFF; 511 myid = getpid() & 0xFFFF;
512 ping(*argv); 512 ping(*argv);
diff --git a/networking/ping6.c b/networking/ping6.c
index f1ccff483..c40624219 100644
--- a/networking/ping6.c
+++ b/networking/ping6.c
@@ -1,6 +1,6 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/* 2/*
3 * $Id: ping6.c,v 1.3 2003/01/12 06:08:33 andersen Exp $ 3 * $Id: ping6.c,v 1.4 2003/03/19 09:12:38 mjn3 Exp $
4 * Mini ping implementation for busybox 4 * Mini ping implementation for busybox
5 * 5 *
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> 6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
@@ -112,7 +112,7 @@ static void ping(const char *host)
112 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6)); 112 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
113 113
114 if (c < 0 || c != sizeof(packet)) 114 if (c < 0 || c != sizeof(packet))
115 perror_msg_and_die("sendto"); 115 bb_perror_msg_and_die("sendto");
116 116
117 signal(SIGALRM, noresp); 117 signal(SIGALRM, noresp);
118 alarm(5); /* give the host 5000ms to respond */ 118 alarm(5); /* give the host 5000ms to respond */
@@ -125,7 +125,7 @@ static void ping(const char *host)
125 (struct sockaddr *) &from, &fromlen)) < 0) { 125 (struct sockaddr *) &from, &fromlen)) < 0) {
126 if (errno == EINTR) 126 if (errno == EINTR)
127 continue; 127 continue;
128 perror_msg("recvfrom"); 128 bb_perror_msg("recvfrom");
129 continue; 129 continue;
130 } 130 }
131 if (c >= 8) { /* icmp6_hdr */ 131 if (c >= 8) { /* icmp6_hdr */
@@ -143,7 +143,7 @@ extern int ping6_main(int argc, char **argv)
143 argc--; 143 argc--;
144 argv++; 144 argv++;
145 if (argc < 1) 145 if (argc < 1)
146 show_usage(); 146 bb_show_usage();
147 ping(*argv); 147 ping(*argv);
148 return EXIT_SUCCESS; 148 return EXIT_SUCCESS;
149} 149}
@@ -218,9 +218,9 @@ static void sendping(int junk)
218 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6)); 218 (struct sockaddr *) &pingaddr, sizeof(struct sockaddr_in6));
219 219
220 if (i < 0) 220 if (i < 0)
221 perror_msg_and_die("sendto"); 221 bb_perror_msg_and_die("sendto");
222 else if ((size_t)i != sizeof(packet)) 222 else if ((size_t)i != sizeof(packet))
223 error_msg_and_die("ping wrote %d chars; %d expected", i, 223 bb_error_msg_and_die("ping wrote %d chars; %d expected", i,
224 (int)sizeof(packet)); 224 (int)sizeof(packet));
225 225
226 signal(SIGALRM, sendping); 226 signal(SIGALRM, sendping);
@@ -308,7 +308,7 @@ static void unpack(char *packet, int sz, struct sockaddr_in6 *from, int hoplimit
308 printf("\n"); 308 printf("\n");
309 } else 309 } else
310 if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST) 310 if (icmppkt->icmp6_type != ICMP6_ECHO_REQUEST)
311 error_msg("Warning: Got ICMP %d (%s)", 311 bb_error_msg("Warning: Got ICMP %d (%s)",
312 icmppkt->icmp6_type, icmp6_type_name (icmppkt->icmp6_type)); 312 icmppkt->icmp6_type, icmp6_type_name (icmppkt->icmp6_type));
313} 313}
314 314
@@ -329,7 +329,7 @@ static void ping(const char *host)
329 pingaddr.sin6_family = AF_INET6; 329 pingaddr.sin6_family = AF_INET6;
330 hostent = xgethostbyname2(host, AF_INET6); 330 hostent = xgethostbyname2(host, AF_INET6);
331 if (hostent->h_addrtype != AF_INET6) 331 if (hostent->h_addrtype != AF_INET6)
332 error_msg_and_die("unknown address type; only AF_INET6 is currently supported."); 332 bb_error_msg_and_die("unknown address type; only AF_INET6 is currently supported.");
333 333
334 memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr)); 334 memcpy(&pingaddr.sin6_addr, hostent->h_addr, sizeof(pingaddr.sin6_addr));
335 335
@@ -350,7 +350,7 @@ static void ping(const char *host)
350 } 350 }
351 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt, 351 if (setsockopt(pingsock, IPPROTO_ICMPV6, ICMP6_FILTER, &filt,
352 sizeof(filt)) < 0) 352 sizeof(filt)) < 0)
353 error_msg_and_die("setsockopt(ICMP6_FILTER)"); 353 bb_error_msg_and_die("setsockopt(ICMP6_FILTER)");
354 } 354 }
355#endif /*ICMP6_FILTER*/ 355#endif /*ICMP6_FILTER*/
356 356
@@ -374,7 +374,7 @@ static void ping(const char *host)
374 374
375 if (ifname) { 375 if (ifname) {
376 if ((pingaddr.sin6_scope_id = if_nametoindex(ifname)) == 0) 376 if ((pingaddr.sin6_scope_id = if_nametoindex(ifname)) == 0)
377 error_msg_and_die("%s: invalid interface name", ifname); 377 bb_error_msg_and_die("%s: invalid interface name", ifname);
378 } 378 }
379 379
380 printf("PING %s (%s): %d data bytes\n", 380 printf("PING %s (%s): %d data bytes\n",
@@ -405,7 +405,7 @@ static void ping(const char *host)
405 if ((c = recvmsg(pingsock, &msg, 0)) < 0) { 405 if ((c = recvmsg(pingsock, &msg, 0)) < 0) {
406 if (errno == EINTR) 406 if (errno == EINTR)
407 continue; 407 continue;
408 perror_msg("recvfrom"); 408 bb_perror_msg("recvfrom");
409 continue; 409 continue;
410 } 410 }
411 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; 411 for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
@@ -446,30 +446,30 @@ extern int ping6_main(int argc, char **argv)
446 break; 446 break;
447 case 'c': 447 case 'c':
448 if (--argc <= 0) 448 if (--argc <= 0)
449 show_usage(); 449 bb_show_usage();
450 argv++; 450 argv++;
451 pingcount = atoi(*argv); 451 pingcount = atoi(*argv);
452 break; 452 break;
453 case 's': 453 case 's':
454 if (--argc <= 0) 454 if (--argc <= 0)
455 show_usage(); 455 bb_show_usage();
456 argv++; 456 argv++;
457 datalen = atoi(*argv); 457 datalen = atoi(*argv);
458 break; 458 break;
459 case 'I': 459 case 'I':
460 if (--argc <= 0) 460 if (--argc <= 0)
461 show_usage(); 461 bb_show_usage();
462 argv++; 462 argv++;
463 ifname = *argv; 463 ifname = *argv;
464 break; 464 break;
465 default: 465 default:
466 show_usage(); 466 bb_show_usage();
467 } 467 }
468 argc--; 468 argc--;
469 argv++; 469 argv++;
470 } 470 }
471 if (argc < 1) 471 if (argc < 1)
472 show_usage(); 472 bb_show_usage();
473 473
474 myid = getpid() & 0xFFFF; 474 myid = getpid() & 0xFFFF;
475 ping(*argv); 475 ping(*argv);
diff --git a/networking/route.c b/networking/route.c
index b564c6dcd..083149a3d 100644
--- a/networking/route.c
+++ b/networking/route.c
@@ -15,7 +15,7 @@
15 * Foundation; either version 2 of the License, or (at 15 * Foundation; either version 2 of the License, or (at
16 * your option) any later version. 16 * your option) any later version.
17 * 17 *
18 * $Id: route.c,v 1.21 2002/12/16 22:04:18 sandman Exp $ 18 * $Id: route.c,v 1.22 2003/03/19 09:12:39 mjn3 Exp $
19 * 19 *
20 * displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru> 20 * displayroute() code added by Vladimir N. Oleynik <dzo@simtreas.ru>
21 * adjustments by Larry Doolittle <LRDoolittle@lbl.gov> 21 * adjustments by Larry Doolittle <LRDoolittle@lbl.gov>
@@ -88,7 +88,7 @@ static int INET_setroute(int action, int options, char **args)
88 xflag = 0; 88 xflag = 0;
89 89
90 if (*args == NULL) 90 if (*args == NULL)
91 show_usage(); 91 bb_show_usage();
92 if (strcmp(*args, "-net") == 0) { 92 if (strcmp(*args, "-net") == 0) {
93 xflag = 1; 93 xflag = 1;
94 args++; 94 args++;
@@ -97,7 +97,7 @@ static int INET_setroute(int action, int options, char **args)
97 args++; 97 args++;
98 } 98 }
99 if (*args == NULL) 99 if (*args == NULL)
100 show_usage(); 100 bb_show_usage();
101 safe_strncpy(target, *args++, (sizeof target)); 101 safe_strncpy(target, *args++, (sizeof target));
102 102
103 /* Clean out the RTREQ structure. */ 103 /* Clean out the RTREQ structure. */
@@ -107,7 +107,7 @@ static int INET_setroute(int action, int options, char **args)
107 if ((isnet = 107 if ((isnet =
108 INET_resolve(target, (struct sockaddr_in *) &rt.rt_dst, 108 INET_resolve(target, (struct sockaddr_in *) &rt.rt_dst,
109 xflag != 1)) < 0) { 109 xflag != 1)) < 0) {
110 error_msg(_("can't resolve %s"), target); 110 bb_error_msg(_("can't resolve %s"), target);
111 return EXIT_FAILURE; /* XXX change to E_something */ 111 return EXIT_FAILURE; /* XXX change to E_something */
112 } 112 }
113 113
@@ -135,7 +135,7 @@ static int INET_setroute(int action, int options, char **args)
135 135
136 args++; 136 args++;
137 if (!*args || !isdigit(**args)) 137 if (!*args || !isdigit(**args))
138 show_usage(); 138 bb_show_usage();
139 metric = atoi(*args); 139 metric = atoi(*args);
140#if HAVE_NEW_ADDRT 140#if HAVE_NEW_ADDRT
141 rt.rt_metric = metric + 1; 141 rt.rt_metric = metric + 1;
@@ -151,12 +151,12 @@ static int INET_setroute(int action, int options, char **args)
151 151
152 args++; 152 args++;
153 if (!*args || mask_in_addr(rt)) 153 if (!*args || mask_in_addr(rt))
154 show_usage(); 154 bb_show_usage();
155 netmask = *args; 155 netmask = *args;
156 if ((isnet = 156 if ((isnet =
157 INET_resolve(netmask, (struct sockaddr_in *) &mask, 157 INET_resolve(netmask, (struct sockaddr_in *) &mask,
158 0)) < 0) { 158 0)) < 0) {
159 error_msg(_("can't resolve netmask %s"), netmask); 159 bb_error_msg(_("can't resolve netmask %s"), netmask);
160 return E_LOOKUP; 160 return E_LOOKUP;
161 } 161 }
162 rt.rt_genmask = full_mask(mask); 162 rt.rt_genmask = full_mask(mask);
@@ -167,18 +167,18 @@ static int INET_setroute(int action, int options, char **args)
167 if (strcmp(*args, "gw") == 0 || strcmp(*args, "gateway") == 0) { 167 if (strcmp(*args, "gw") == 0 || strcmp(*args, "gateway") == 0) {
168 args++; 168 args++;
169 if (!*args) 169 if (!*args)
170 show_usage(); 170 bb_show_usage();
171 if (rt.rt_flags & RTF_GATEWAY) 171 if (rt.rt_flags & RTF_GATEWAY)
172 show_usage(); 172 bb_show_usage();
173 safe_strncpy(gateway, *args, (sizeof gateway)); 173 safe_strncpy(gateway, *args, (sizeof gateway));
174 if ((isnet = 174 if ((isnet =
175 INET_resolve(gateway, (struct sockaddr_in *) &rt.rt_gateway, 175 INET_resolve(gateway, (struct sockaddr_in *) &rt.rt_gateway,
176 1)) < 0) { 176 1)) < 0) {
177 error_msg(_("can't resolve gw %s"), gateway); 177 bb_error_msg(_("can't resolve gw %s"), gateway);
178 return E_LOOKUP; 178 return E_LOOKUP;
179 } 179 }
180 if (isnet) { 180 if (isnet) {
181 error_msg(_("%s: cannot use a NETWORK as gateway!"), gateway); 181 bb_error_msg(_("%s: cannot use a NETWORK as gateway!"), gateway);
182 return E_OPTERR; 182 return E_OPTERR;
183 } 183 }
184 rt.rt_flags |= RTF_GATEWAY; 184 rt.rt_flags |= RTF_GATEWAY;
@@ -190,11 +190,11 @@ static int INET_setroute(int action, int options, char **args)
190 args++; 190 args++;
191 rt.rt_flags |= RTF_MSS; 191 rt.rt_flags |= RTF_MSS;
192 if (!*args) 192 if (!*args)
193 show_usage(); 193 bb_show_usage();
194 rt.rt_mss = atoi(*args); 194 rt.rt_mss = atoi(*args);
195 args++; 195 args++;
196 if (rt.rt_mss < 64 || rt.rt_mss > 32768) { 196 if (rt.rt_mss < 64 || rt.rt_mss > 32768) {
197 error_msg(_("Invalid MSS.")); 197 bb_error_msg(_("Invalid MSS."));
198 return E_OPTERR; 198 return E_OPTERR;
199 } 199 }
200 continue; 200 continue;
@@ -203,12 +203,12 @@ static int INET_setroute(int action, int options, char **args)
203 if (strcmp(*args, "window") == 0) { 203 if (strcmp(*args, "window") == 0) {
204 args++; 204 args++;
205 if (!*args) 205 if (!*args)
206 show_usage(); 206 bb_show_usage();
207 rt.rt_flags |= RTF_WINDOW; 207 rt.rt_flags |= RTF_WINDOW;
208 rt.rt_window = atoi(*args); 208 rt.rt_window = atoi(*args);
209 args++; 209 args++;
210 if (rt.rt_window < 128) { 210 if (rt.rt_window < 128) {
211 error_msg(_("Invalid window.")); 211 bb_error_msg(_("Invalid window."));
212 return E_OPTERR; 212 return E_OPTERR;
213 } 213 }
214 continue; 214 continue;
@@ -217,7 +217,7 @@ static int INET_setroute(int action, int options, char **args)
217 if (strcmp(*args, "irtt") == 0) { 217 if (strcmp(*args, "irtt") == 0) {
218 args++; 218 args++;
219 if (!*args) 219 if (!*args)
220 show_usage(); 220 bb_show_usage();
221 args++; 221 args++;
222#if HAVE_RTF_IRTT 222#if HAVE_RTF_IRTT
223 rt.rt_flags |= RTF_IRTT; 223 rt.rt_flags |= RTF_IRTT;
@@ -225,7 +225,7 @@ static int INET_setroute(int action, int options, char **args)
225 rt.rt_irtt *= (sysconf(_SC_CLK_TCK) / 100); /* FIXME */ 225 rt.rt_irtt *= (sysconf(_SC_CLK_TCK) / 100); /* FIXME */
226#if 0 /* FIXME: do we need to check anything of this? */ 226#if 0 /* FIXME: do we need to check anything of this? */
227 if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) { 227 if (rt.rt_irtt < 1 || rt.rt_irtt > (120 * HZ)) {
228 error_msg(_("Invalid initial rtt.")); 228 bb_error_msg(_("Invalid initial rtt."));
229 return E_OPTERR; 229 return E_OPTERR;
230 } 230 }
231#endif 231#endif
@@ -262,7 +262,7 @@ static int INET_setroute(int action, int options, char **args)
262 if (strcmp(*args, "device") == 0 || strcmp(*args, "dev") == 0) { 262 if (strcmp(*args, "device") == 0 || strcmp(*args, "dev") == 0) {
263 args++; 263 args++;
264 if (rt.rt_dev || *args == NULL) 264 if (rt.rt_dev || *args == NULL)
265 show_usage(); 265 bb_show_usage();
266 rt.rt_dev = *args++; 266 rt.rt_dev = *args++;
267 continue; 267 continue;
268 } 268 }
@@ -270,9 +270,9 @@ static int INET_setroute(int action, int options, char **args)
270 if (!rt.rt_dev) { 270 if (!rt.rt_dev) {
271 rt.rt_dev = *args++; 271 rt.rt_dev = *args++;
272 if (*args) 272 if (*args)
273 show_usage(); /* must be last to catch typos */ 273 bb_show_usage(); /* must be last to catch typos */
274 } else { 274 } else {
275 show_usage(); 275 bb_show_usage();
276 } 276 }
277 } 277 }
278 278
@@ -287,17 +287,17 @@ static int INET_setroute(int action, int options, char **args)
287 287
288 mask = ~ntohl(mask); 288 mask = ~ntohl(mask);
289 if ((rt.rt_flags & RTF_HOST) && mask != 0xffffffff) { 289 if ((rt.rt_flags & RTF_HOST) && mask != 0xffffffff) {
290 error_msg(_("netmask %.8x doesn't make sense with host route"), 290 bb_error_msg(_("netmask %.8x doesn't make sense with host route"),
291 (unsigned int) mask); 291 (unsigned int) mask);
292 return E_OPTERR; 292 return E_OPTERR;
293 } 293 }
294 if (mask & (mask + 1)) { 294 if (mask & (mask + 1)) {
295 error_msg(_("bogus netmask %s"), netmask); 295 bb_error_msg(_("bogus netmask %s"), netmask);
296 return E_OPTERR; 296 return E_OPTERR;
297 } 297 }
298 mask = ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr; 298 mask = ((struct sockaddr_in *) &rt.rt_dst)->sin_addr.s_addr;
299 if (mask & ~mask_in_addr(rt)) { 299 if (mask & ~mask_in_addr(rt)) {
300 error_msg(_("netmask doesn't match route address")); 300 bb_error_msg(_("netmask doesn't match route address"));
301 return E_OPTERR; 301 return E_OPTERR;
302 } 302 }
303 } 303 }
@@ -343,7 +343,7 @@ static int INET6_setroute(int action, int options, char **args)
343 int skfd; 343 int skfd;
344 344
345 if (*args == NULL) 345 if (*args == NULL)
346 show_usage(); 346 bb_show_usage();
347 347
348 strcpy(target, *args++); 348 strcpy(target, *args++);
349 if (!strcmp(target, "default")) { 349 if (!strcmp(target, "default")) {
@@ -353,13 +353,13 @@ static int INET6_setroute(int action, int options, char **args)
353 if ((cp = strchr(target, '/'))) { 353 if ((cp = strchr(target, '/'))) {
354 prefix_len = atol(cp + 1); 354 prefix_len = atol(cp + 1);
355 if ((prefix_len < 0) || (prefix_len > 128)) 355 if ((prefix_len < 0) || (prefix_len > 128))
356 show_usage(); 356 bb_show_usage();
357 *cp = 0; 357 *cp = 0;
358 } else { 358 } else {
359 prefix_len = 128; 359 prefix_len = 128;
360 } 360 }
361 if (INET6_resolve(target, (struct sockaddr_in6 *) &sa6) < 0) { 361 if (INET6_resolve(target, (struct sockaddr_in6 *) &sa6) < 0) {
362 error_msg(_("can't resolve %s"), target); 362 bb_error_msg(_("can't resolve %s"), target);
363 return EXIT_FAILURE; /* XXX change to E_something */ 363 return EXIT_FAILURE; /* XXX change to E_something */
364 } 364 }
365 } 365 }
@@ -381,7 +381,7 @@ static int INET6_setroute(int action, int options, char **args)
381 381
382 args++; 382 args++;
383 if (!*args || !isdigit(**args)) 383 if (!*args || !isdigit(**args))
384 show_usage(); 384 bb_show_usage();
385 metric = atoi(*args); 385 metric = atoi(*args);
386 rt.rtmsg_metric = metric; 386 rt.rtmsg_metric = metric;
387 args++; 387 args++;
@@ -390,12 +390,12 @@ static int INET6_setroute(int action, int options, char **args)
390 if (!strcmp(*args, "gw") || !strcmp(*args, "gateway")) { 390 if (!strcmp(*args, "gw") || !strcmp(*args, "gateway")) {
391 args++; 391 args++;
392 if (!*args) 392 if (!*args)
393 show_usage(); 393 bb_show_usage();
394 if (rt.rtmsg_flags & RTF_GATEWAY) 394 if (rt.rtmsg_flags & RTF_GATEWAY)
395 show_usage(); 395 bb_show_usage();
396 strcpy(gateway, *args); 396 strcpy(gateway, *args);
397 if (INET6_resolve(gateway, (struct sockaddr_in6 *) &sa6) < 0) { 397 if (INET6_resolve(gateway, (struct sockaddr_in6 *) &sa6) < 0) {
398 error_msg(_("can't resolve gw %s"), gateway); 398 bb_error_msg(_("can't resolve gw %s"), gateway);
399 return (E_LOOKUP); 399 return (E_LOOKUP);
400 } 400 }
401 memcpy(&rt.rtmsg_gateway, sa6.sin6_addr.s6_addr, 401 memcpy(&rt.rtmsg_gateway, sa6.sin6_addr.s6_addr,
@@ -417,9 +417,9 @@ static int INET6_setroute(int action, int options, char **args)
417 if (!strcmp(*args, "device") || !strcmp(*args, "dev")) { 417 if (!strcmp(*args, "device") || !strcmp(*args, "dev")) {
418 args++; 418 args++;
419 if (!*args) 419 if (!*args)
420 show_usage(); 420 bb_show_usage();
421 } else if (args[1]) 421 } else if (args[1])
422 show_usage(); 422 bb_show_usage();
423 423
424 devname = *args; 424 devname = *args;
425 args++; 425 args++;
@@ -493,7 +493,7 @@ void displayroutes(int noresolve, int netstatfmt)
493 493
494 char sdest[16], sgw[16]; 494 char sdest[16], sgw[16];
495 495
496 FILE *fp = xfopen("/proc/net/route", "r"); 496 FILE *fp = bb_xfopen("/proc/net/route", "r");
497 497
498 if (noresolve) 498 if (noresolve)
499 noresolve = 0x0fff; 499 noresolve = 0x0fff;
@@ -515,7 +515,7 @@ void displayroutes(int noresolve, int netstatfmt)
515 if (sscanf(buff + ifl + 1, "%lx%lx%X%d%d%d%lx%d%d%d", 515 if (sscanf(buff + ifl + 1, "%lx%lx%X%d%d%d%lx%d%d%d",
516 &d, &g, &flgs, &ref, &use, &metric, &m, &mtu, &win, 516 &d, &g, &flgs, &ref, &use, &metric, &m, &mtu, &win,
517 &ir) != 10) { 517 &ir) != 10) {
518 error_msg_and_die("Unsuported kernel route format\n"); 518 bb_error_msg_and_die("Unsuported kernel route format\n");
519 } 519 }
520 ifl = 0; /* parse flags */ 520 ifl = 0; /* parse flags */
521 if (flgs & RTF_UP) { 521 if (flgs & RTF_UP) {
@@ -570,7 +570,7 @@ static void INET6_displayroutes(int noresolve)
570 570
571 char addr6p[8][5], saddr6p[8][5], naddr6p[8][5]; 571 char addr6p[8][5], saddr6p[8][5], naddr6p[8][5];
572 572
573 FILE *fp = xfopen("/proc/net/ipv6_route", "r"); 573 FILE *fp = bb_xfopen("/proc/net/ipv6_route", "r");
574 574
575 flags[0] = 'U'; 575 flags[0] = 'U';
576 576
@@ -598,7 +598,7 @@ static void INET6_displayroutes(int noresolve)
598 naddr6p[0], naddr6p[1], naddr6p[2], naddr6p[3], 598 naddr6p[0], naddr6p[1], naddr6p[2], naddr6p[3],
599 naddr6p[4], naddr6p[5], naddr6p[6], naddr6p[7], 599 naddr6p[4], naddr6p[5], naddr6p[6], naddr6p[7],
600 &metric, &use, &refcnt, &iflags, iface) != 31) { 600 &metric, &use, &refcnt, &iflags, iface) != 31) {
601 error_msg_and_die("Unsuported kernel route format\n"); 601 bb_error_msg_and_die("Unsuported kernel route format\n");
602 } 602 }
603 603
604 ifl = 1; /* parse flags */ 604 ifl = 1; /* parse flags */
@@ -673,7 +673,7 @@ int route_main(int argc, char **argv)
673 break; 673 break;
674#endif 674#endif
675 default: 675 default:
676 show_usage(); 676 bb_show_usage();
677 } 677 }
678 } 678 }
679 679
@@ -694,7 +694,7 @@ int route_main(int argc, char **argv)
694 else if (strcmp(argv[1], "flush") == 0) 694 else if (strcmp(argv[1], "flush") == 0)
695 what = RTACTION_FLUSH; 695 what = RTACTION_FLUSH;
696 else 696 else
697 show_usage(); 697 bb_show_usage();
698 } 698 }
699 699
700#ifdef CONFIG_FEATURE_IPV6 700#ifdef CONFIG_FEATURE_IPV6
diff --git a/networking/telnet.c b/networking/telnet.c
index 0c794b62f..8f2573c27 100644
--- a/networking/telnet.c
+++ b/networking/telnet.c
@@ -604,7 +604,7 @@ extern int telnet_main(int argc, char** argv)
604 G.termios_raw = G.termios_def; 604 G.termios_raw = G.termios_def;
605 cfmakeraw(&G.termios_raw); 605 cfmakeraw(&G.termios_raw);
606 606
607 if (argc < 2) show_usage(); 607 if (argc < 2) bb_show_usage();
608 port = (argc > 2)? argv[2] : "23"; 608 port = (argc > 2)? argv[2] : "23";
609 609
610 host = argv[1]; 610 host = argv[1];
diff --git a/networking/telnetd.c b/networking/telnetd.c
index 8634a4233..077194cfd 100644
--- a/networking/telnetd.c
+++ b/networking/telnetd.c
@@ -1,4 +1,4 @@
1/* $Id: telnetd.c,v 1.4 2003/01/22 21:09:48 bug1 Exp $ 1/* $Id: telnetd.c,v 1.5 2003/03/19 09:12:39 mjn3 Exp $
2 * 2 *
3 * Simple telnet server 3 * Simple telnet server
4 * Bjorn Wesen, Axis Communications AB (bjornw@axis.com) 4 * Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
@@ -380,12 +380,12 @@ telnetd_main(int argc, char **argv)
380 break; 380 break;
381#endif /* CONFIG_FEATURE_TELNETD_INETD */ 381#endif /* CONFIG_FEATURE_TELNETD_INETD */
382 default: 382 default:
383 show_usage(); 383 bb_show_usage();
384 } 384 }
385 } 385 }
386 386
387 if (access(loginpath, X_OK) < 0) { 387 if (access(loginpath, X_OK) < 0) {
388 error_msg_and_die ("'%s' unavailable.", loginpath); 388 bb_error_msg_and_die ("'%s' unavailable.", loginpath);
389 } 389 }
390 390
391 argv_init[0] = loginpath; 391 argv_init[0] = loginpath;
@@ -400,7 +400,7 @@ telnetd_main(int argc, char **argv)
400 400
401 master_fd = socket(AF_INET, SOCK_STREAM, 0); 401 master_fd = socket(AF_INET, SOCK_STREAM, 0);
402 if (master_fd < 0) { 402 if (master_fd < 0) {
403 perror_msg_and_die("socket"); 403 bb_perror_msg_and_die("socket");
404 } 404 }
405 (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); 405 (void)setsockopt(master_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
406 406
@@ -411,15 +411,15 @@ telnetd_main(int argc, char **argv)
411 sa.sin_port = htons(portnbr); 411 sa.sin_port = htons(portnbr);
412 412
413 if (bind(master_fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) { 413 if (bind(master_fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
414 perror_msg_and_die("bind"); 414 bb_perror_msg_and_die("bind");
415 } 415 }
416 416
417 if (listen(master_fd, 1) < 0) { 417 if (listen(master_fd, 1) < 0) {
418 perror_msg_and_die("listen"); 418 bb_perror_msg_and_die("listen");
419 } 419 }
420 420
421 if (daemon(0, 0) < 0) 421 if (daemon(0, 0) < 0)
422 perror_msg_and_die("daemon"); 422 bb_perror_msg_and_die("daemon");
423 423
424 424
425 maxfd = master_fd; 425 maxfd = master_fd;
diff --git a/networking/tftp.c b/networking/tftp.c
index 378ba2fef..a33b5c21e 100644
--- a/networking/tftp.c
+++ b/networking/tftp.c
@@ -60,7 +60,7 @@
60#define TFTP_ERROR 5 60#define TFTP_ERROR 5
61#define TFTP_OACK 6 61#define TFTP_OACK 6
62 62
63static const char *tftp_error_msg[] = { 63static const char *tftp_bb_error_msg[] = {
64 "Undefined error", 64 "Undefined error",
65 "File not found", 65 "File not found",
66 "Access violation", 66 "Access violation",
@@ -86,7 +86,7 @@ static int tftp_blocksize_check(int blocksize, int bufsize)
86 86
87 if ((bufsize && (blocksize > bufsize)) || 87 if ((bufsize && (blocksize > bufsize)) ||
88 (blocksize < 8) || (blocksize > 65464)) { 88 (blocksize < 8) || (blocksize > 65464)) {
89 error_msg("bad blocksize"); 89 bb_error_msg("bad blocksize");
90 return 0; 90 return 0;
91 } 91 }
92 92
@@ -169,7 +169,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
169 tftp_bufsize += 4; 169 tftp_bufsize += 4;
170 170
171 if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { 171 if ((socketfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
172 perror_msg("socket"); 172 bb_perror_msg("socket");
173 return EXIT_FAILURE; 173 return EXIT_FAILURE;
174 } 174 }
175 175
@@ -223,7 +223,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
223 } 223 }
224 224
225 if (too_long || ((&buf[tftp_bufsize - 1] - cp) < 6)) { 225 if (too_long || ((&buf[tftp_bufsize - 1] - cp) < 6)) {
226 error_msg("too long remote-filename"); 226 bb_error_msg("too long remote-filename");
227 break; 227 break;
228 } 228 }
229 229
@@ -239,7 +239,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
239 if (len != TFTP_BLOCKSIZE_DEFAULT) { 239 if (len != TFTP_BLOCKSIZE_DEFAULT) {
240 240
241 if ((&buf[tftp_bufsize - 1] - cp) < 15) { 241 if ((&buf[tftp_bufsize - 1] - cp) < 15) {
242 error_msg("too long remote-filename"); 242 bb_error_msg("too long remote-filename");
243 break; 243 break;
244 } 244 }
245 245
@@ -270,7 +270,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
270 len = read(localfd, cp, tftp_bufsize - 4); 270 len = read(localfd, cp, tftp_bufsize - 4);
271 271
272 if (len < 0) { 272 if (len < 0) {
273 perror_msg("read"); 273 bb_perror_msg("read");
274 break; 274 break;
275 } 275 }
276 276
@@ -298,7 +298,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
298#endif 298#endif
299 if (sendto(socketfd, buf, len, 0, 299 if (sendto(socketfd, buf, len, 0,
300 (struct sockaddr *) &sa, sizeof(sa)) < 0) { 300 (struct sockaddr *) &sa, sizeof(sa)) < 0) {
301 perror_msg("send"); 301 bb_perror_msg("send");
302 len = -1; 302 len = -1;
303 break; 303 break;
304 } 304 }
@@ -325,7 +325,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
325 (struct sockaddr *) &from, &fromlen); 325 (struct sockaddr *) &from, &fromlen);
326 326
327 if (len < 0) { 327 if (len < 0) {
328 perror_msg("recvfrom"); 328 bb_perror_msg("recvfrom");
329 break; 329 break;
330 } 330 }
331 331
@@ -343,18 +343,18 @@ static inline int tftp(const int cmd, const struct hostent *host,
343 timeout = bb_tftp_num_retries; 343 timeout = bb_tftp_num_retries;
344 344
345 case 0: 345 case 0:
346 error_msg("timeout"); 346 bb_error_msg("timeout");
347 347
348 if (timeout == 0) { 348 if (timeout == 0) {
349 len = -1; 349 len = -1;
350 error_msg("last timeout"); 350 bb_error_msg("last timeout");
351 } else { 351 } else {
352 timeout--; 352 timeout--;
353 } 353 }
354 break; 354 break;
355 355
356 default: 356 default:
357 perror_msg("select"); 357 bb_perror_msg("select");
358 len = -1; 358 len = -1;
359 } 359 }
360 360
@@ -380,14 +380,14 @@ static inline int tftp(const int cmd, const struct hostent *host,
380 if (buf[4] != '\0') { 380 if (buf[4] != '\0') {
381 msg = &buf[4]; 381 msg = &buf[4];
382 buf[tftp_bufsize - 1] = '\0'; 382 buf[tftp_bufsize - 1] = '\0';
383 } else if (tmp < (sizeof(tftp_error_msg) 383 } else if (tmp < (sizeof(tftp_bb_error_msg)
384 / sizeof(char *))) { 384 / sizeof(char *))) {
385 385
386 msg = (char *) tftp_error_msg[tmp]; 386 msg = (char *) tftp_bb_error_msg[tmp];
387 } 387 }
388 388
389 if (msg) { 389 if (msg) {
390 error_msg("server says: %s", msg); 390 bb_error_msg("server says: %s", msg);
391 } 391 }
392 392
393 break; 393 break;
@@ -429,11 +429,11 @@ static inline int tftp(const int cmd, const struct hostent *host,
429 } 429 }
430 /* FIXME: 430 /* FIXME:
431 * we should send ERROR 8 */ 431 * we should send ERROR 8 */
432 error_msg("bad server option"); 432 bb_error_msg("bad server option");
433 break; 433 break;
434 } 434 }
435 435
436 error_msg("warning: blksize not supported by server" 436 bb_error_msg("warning: blksize not supported by server"
437 " - reverting to 512"); 437 " - reverting to 512");
438 438
439 tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4; 439 tftp_bufsize = TFTP_BLOCKSIZE_DEFAULT + 4;
@@ -447,7 +447,7 @@ static inline int tftp(const int cmd, const struct hostent *host,
447 len = write(localfd, &buf[4], len - 4); 447 len = write(localfd, &buf[4], len - 4);
448 448
449 if (len < 0) { 449 if (len < 0) {
450 perror_msg("write"); 450 bb_perror_msg("write");
451 break; 451 break;
452 } 452 }
453 453
@@ -538,16 +538,16 @@ int tftp_main(int argc, char **argv)
538 break; 538 break;
539#endif 539#endif
540 case 'l': 540 case 'l':
541 localfile = xstrdup(optarg); 541 localfile = bb_xstrdup(optarg);
542 break; 542 break;
543 case 'r': 543 case 'r':
544 remotefile = xstrdup(optarg); 544 remotefile = bb_xstrdup(optarg);
545 break; 545 break;
546 } 546 }
547 } 547 }
548 548
549 if ((cmd == 0) || (optind == argc)) { 549 if ((cmd == 0) || (optind == argc)) {
550 show_usage(); 550 bb_show_usage();
551 } 551 }
552 if(localfile && strcmp(localfile, "-") == 0) { 552 if(localfile && strcmp(localfile, "-") == 0) {
553 fd = fileno((cmd==tftp_cmd_get)? stdout : stdin); 553 fd = fileno((cmd==tftp_cmd_get)? stdout : stdin);
@@ -560,7 +560,7 @@ int tftp_main(int argc, char **argv)
560 fd = open(localfile, flags, 0644); 560 fd = open(localfile, flags, 0644);
561 } 561 }
562 if (fd < 0) { 562 if (fd < 0) {
563 perror_msg_and_die("local file"); 563 bb_perror_msg_and_die("local file");
564 } 564 }
565 565
566 host = xgethostbyname(argv[optind]); 566 host = xgethostbyname(argv[optind]);
diff --git a/networking/traceroute.c b/networking/traceroute.c
index a9cad4baf..be9ea1d95 100644
--- a/networking/traceroute.c
+++ b/networking/traceroute.c
@@ -448,7 +448,7 @@ traceroute_main(int argc, char *argv[])
448 case 'm': 448 case 'm':
449 max_ttl = atoi(optarg); 449 max_ttl = atoi(optarg);
450 if (max_ttl <= 1) 450 if (max_ttl <= 1)
451 error_msg_and_die("max ttl must be >1."); 451 bb_error_msg_and_die("max ttl must be >1.");
452 break; 452 break;
453 case 'n': 453 case 'n':
454 nflag++; 454 nflag++;
@@ -456,12 +456,12 @@ traceroute_main(int argc, char *argv[])
456 case 'p': 456 case 'p':
457 port = atoi(optarg); 457 port = atoi(optarg);
458 if (port < 1) 458 if (port < 1)
459 error_msg_and_die("port must be >0."); 459 bb_error_msg_and_die("port must be >0.");
460 break; 460 break;
461 case 'q': 461 case 'q':
462 nprobes = atoi(optarg); 462 nprobes = atoi(optarg);
463 if (nprobes < 1) 463 if (nprobes < 1)
464 error_msg_and_die("nprobes must be >0."); 464 bb_error_msg_and_die("nprobes must be >0.");
465 break; 465 break;
466 case 'r': 466 case 'r':
467 options |= SO_DONTROUTE; 467 options |= SO_DONTROUTE;
@@ -476,7 +476,7 @@ traceroute_main(int argc, char *argv[])
476 case 't': 476 case 't':
477 tos = atoi(optarg); 477 tos = atoi(optarg);
478 if (tos < 0 || tos > 255) 478 if (tos < 0 || tos > 255)
479 error_msg_and_die("tos must be 0 to 255."); 479 bb_error_msg_and_die("tos must be 0 to 255.");
480 break; 480 break;
481 case 'v': 481 case 'v':
482#ifdef CONFIG_FEATURE_TRACEROUTE_VERBOSE 482#ifdef CONFIG_FEATURE_TRACEROUTE_VERBOSE
@@ -486,16 +486,16 @@ traceroute_main(int argc, char *argv[])
486 case 'w': 486 case 'w':
487 waittime = atoi(optarg); 487 waittime = atoi(optarg);
488 if (waittime <= 1) 488 if (waittime <= 1)
489 error_msg_and_die("wait must be >1 sec."); 489 bb_error_msg_and_die("wait must be >1 sec.");
490 break; 490 break;
491 default: 491 default:
492 show_usage(); 492 bb_show_usage();
493 } 493 }
494 argc -= optind; 494 argc -= optind;
495 argv += optind; 495 argv += optind;
496 496
497 if (argc < 1) 497 if (argc < 1)
498 show_usage(); 498 bb_show_usage();
499 499
500 setlinebuf (stdout); 500 setlinebuf (stdout);
501 501
@@ -507,7 +507,7 @@ traceroute_main(int argc, char *argv[])
507 if (*++argv) 507 if (*++argv)
508 datalen = atoi(*argv); 508 datalen = atoi(*argv);
509 if (datalen < 0 || datalen >= MAXPACKET - sizeof(struct opacket)) 509 if (datalen < 0 || datalen >= MAXPACKET - sizeof(struct opacket))
510 error_msg_and_die("packet size must be 0 <= s < %d.", 510 bb_error_msg_and_die("packet size must be 0 <= s < %d.",
511 MAXPACKET - sizeof(struct opacket)); 511 MAXPACKET - sizeof(struct opacket));
512 datalen += sizeof(struct opacket); 512 datalen += sizeof(struct opacket);
513 outpacket = (struct opacket *)xmalloc((unsigned)datalen); 513 outpacket = (struct opacket *)xmalloc((unsigned)datalen);
@@ -520,7 +520,7 @@ traceroute_main(int argc, char *argv[])
520 ident = (getpid() & 0xffff) | 0x8000; 520 ident = (getpid() & 0xffff) | 0x8000;
521 521
522 if ((sndsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0) 522 if ((sndsock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW)) < 0)
523 perror_msg_and_die(can_not_create_raw_socket); 523 bb_perror_msg_and_die(bb_msg_can_not_create_raw_socket);
524 524
525 s = create_icmp_socket(); 525 s = create_icmp_socket();
526 526
@@ -535,12 +535,12 @@ traceroute_main(int argc, char *argv[])
535#ifdef SO_SNDBUF 535#ifdef SO_SNDBUF
536 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&datalen, 536 if (setsockopt(sndsock, SOL_SOCKET, SO_SNDBUF, (char *)&datalen,
537 sizeof(datalen)) < 0) 537 sizeof(datalen)) < 0)
538 perror_msg_and_die("SO_SNDBUF"); 538 bb_perror_msg_and_die("SO_SNDBUF");
539#endif 539#endif
540#ifdef IP_HDRINCL 540#ifdef IP_HDRINCL
541 if (setsockopt(sndsock, IPPROTO_IP, IP_HDRINCL, (char *)&on, 541 if (setsockopt(sndsock, IPPROTO_IP, IP_HDRINCL, (char *)&on,
542 sizeof(on)) < 0) 542 sizeof(on)) < 0)
543 perror_msg_and_die("IP_HDRINCL"); 543 bb_perror_msg_and_die("IP_HDRINCL");
544#endif 544#endif
545#ifdef CONFIG_FEATURE_TRACEROUTE_SO_DEBUG 545#ifdef CONFIG_FEATURE_TRACEROUTE_SO_DEBUG
546 if (options & SO_DEBUG) 546 if (options & SO_DEBUG)
@@ -556,11 +556,11 @@ traceroute_main(int argc, char *argv[])
556 from.sin_family = AF_INET; 556 from.sin_family = AF_INET;
557 from.sin_addr.s_addr = inet_addr(source); 557 from.sin_addr.s_addr = inet_addr(source);
558 if (from.sin_addr.s_addr == -1) 558 if (from.sin_addr.s_addr == -1)
559 error_msg_and_die("unknown host %s", source); 559 bb_error_msg_and_die("unknown host %s", source);
560 outpacket->ip.ip_src = from.sin_addr; 560 outpacket->ip.ip_src = from.sin_addr;
561#ifndef IP_HDRINCL 561#ifndef IP_HDRINCL
562 if (bind(sndsock, (struct sockaddr *)&from, sizeof(from)) < 0) 562 if (bind(sndsock, (struct sockaddr *)&from, sizeof(from)) < 0)
563 perror_msg_and_die("bind"); 563 bb_perror_msg_and_die("bind");
564#endif 564#endif
565 } 565 }
566 566
diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c
index fdcd17f79..c97e422e2 100644
--- a/networking/udhcp/dhcpc.c
+++ b/networking/udhcp/dhcpc.c
@@ -81,7 +81,7 @@ struct client_config_t client_config = {
81}; 81};
82 82
83#ifndef IN_BUSYBOX 83#ifndef IN_BUSYBOX
84static void __attribute__ ((noreturn)) show_usage(void) 84static void __attribute__ ((noreturn)) bb_show_usage(void)
85{ 85{
86 printf( 86 printf(
87"Usage: udhcpc [OPTIONS]\n\n" 87"Usage: udhcpc [OPTIONS]\n\n"
@@ -104,7 +104,7 @@ static void __attribute__ ((noreturn)) show_usage(void)
104 exit(0); 104 exit(0);
105} 105}
106#else 106#else
107extern void show_usage(void) __attribute__ ((noreturn)); 107extern void bb_show_usage(void) __attribute__ ((noreturn));
108#endif 108#endif
109 109
110 110
@@ -298,7 +298,7 @@ int main(int argc, char *argv[])
298 exit_client(0); 298 exit_client(0);
299 break; 299 break;
300 default: 300 default:
301 show_usage(); 301 bb_show_usage();
302 } 302 }
303 } 303 }
304 304
diff --git a/networking/vconfig.c b/networking/vconfig.c
index 3bd9c3040..d58c375f7 100644
--- a/networking/vconfig.c
+++ b/networking/vconfig.c
@@ -1,3 +1,27 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * vconfig implementation for busybox
4 *
5 * Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23/* BB_AUDIT SUSv3 N/A */
24
1#include <stdlib.h> 25#include <stdlib.h>
2#include <unistd.h> 26#include <unistd.h>
3#include <fcntl.h> 27#include <fcntl.h>
@@ -11,29 +35,14 @@
11#define VLAN_GROUP_ARRAY_LEN 4096 35#define VLAN_GROUP_ARRAY_LEN 4096
12#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */ 36#define SIOCSIFVLAN 0x8983 /* Set 802.1Q VLAN options */
13 37
14/* This is rather specialized in that we're passing a 'char **' in
15 * order to avoid the pointer dereference multiple times in the
16 * actual calls below. */
17static unsigned long xstrtoul10(char **str, unsigned long max_val)
18{
19 char *endptr;
20 unsigned long r;
21
22 r = strtoul(str[2], &endptr, 10);
23 if ((r > max_val) || (*endptr != 0)) {
24 show_usage();
25 }
26 return r;
27}
28
29/* On entry, table points to the length of the current string plus 38/* On entry, table points to the length of the current string plus
30 * nul terminator plus data lenght for the subsequent entry. The 39 * nul terminator plus data length for the subsequent entry. The
31 * return value is the last data entry for the matching string. */ 40 * return value is the last data entry for the matching string. */
32static const char *xfind_str(const char *table, const char *str) 41static const char *xfind_str(const char *table, const char *str)
33{ 42{
34 while (strcasecmp(str, table+1) != 0) { 43 while (strcasecmp(str, table+1) != 0) {
35 if (!*(table += table[0])) { 44 if (!*(table += table[0])) {
36 show_usage(); 45 bb_show_usage();
37 } 46 }
38 } 47 }
39 return table - 1; 48 return table - 1;
@@ -89,12 +98,12 @@ int vconfig_main(int argc, char **argv)
89 int fd; 98 int fd;
90 99
91 if (argc < 3) { 100 if (argc < 3) {
92 show_usage(); 101 bb_show_usage();
93 } 102 }
94 103
95 /* Don't bother closing the filedes. It will be closed on cleanup. */ 104 /* Don't bother closing the filedes. It will be closed on cleanup. */
96 if (open(conf_file_name, O_RDONLY) < 0) { /* Is 802.1q is present? */ 105 if (open(conf_file_name, O_RDONLY) < 0) { /* Is 802.1q is present? */
97 perror_msg_and_die("open %s", conf_file_name); 106 bb_perror_msg_and_die("open %s", conf_file_name);
98 } 107 }
99 108
100 memset(&ifr, 0, sizeof(struct vlan_ioctl_args)); 109 memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
@@ -103,16 +112,17 @@ int vconfig_main(int argc, char **argv)
103 p = xfind_str(cmds+2, *argv); 112 p = xfind_str(cmds+2, *argv);
104 ifr.cmd = *p; 113 ifr.cmd = *p;
105 if (argc != p[-1]) { 114 if (argc != p[-1]) {
106 show_usage(); 115 bb_show_usage();
107 } 116 }
108 117
109 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */ 118 if (ifr.cmd == SET_VLAN_NAME_TYPE_CMD) { /* set_name_type */
110 ifr.u.name_type = *xfind_str(name_types+1, argv[1]); 119 ifr.u.name_type = *xfind_str(name_types+1, argv[1]);
111 } else { 120 } else {
112 if (strlen(argv[1]) >= IF_NAMESIZE) { 121 if (strlen(argv[1]) >= IF_NAMESIZE) {
113 error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE); 122 bb_error_msg_and_die("if_name >= %d chars\n", IF_NAMESIZE);
114 } 123 }
115 strcpy(ifr.device1, argv[1]); 124 strcpy(ifr.device1, argv[1]);
125 p = argv[2];
116 126
117 /* I suppose one could try to combine some of the function calls below, 127 /* I suppose one could try to combine some of the function calls below,
118 * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized 128 * since ifr.u.flag, ifr.u.VID, and ifr.u.skb_priority are all same-sized
@@ -120,19 +130,19 @@ int vconfig_main(int argc, char **argv)
120 * doing so wouldn't save that much space and would also make maintainence 130 * doing so wouldn't save that much space and would also make maintainence
121 * more of a pain. */ 131 * more of a pain. */
122 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */ 132 if (ifr.cmd == SET_VLAN_FLAG_CMD) { /* set_flag */
123 ifr.u.flag = xstrtoul10(argv, 1); 133 ifr.u.flag = bb_xgetularg10_bnd(p, 0, 1);
124 } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */ 134 } else if (ifr.cmd == ADD_VLAN_CMD) { /* add */
125 ifr.u.VID = xstrtoul10(argv, VLAN_GROUP_ARRAY_LEN-1); 135 ifr.u.VID = bb_xgetularg10_bnd(p, 0, VLAN_GROUP_ARRAY_LEN-1);
126 } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */ 136 } else if (ifr.cmd != DEL_VLAN_CMD) { /* set_{egress|ingress}_map */
127 ifr.u.skb_priority = xstrtoul10(argv, ULONG_MAX); 137 ifr.u.skb_priority = bb_xgetularg10_bnd(p, 0, ULONG_MAX);
128 ifr.vlan_qos = xstrtoul10(argv+1, 7); 138 ifr.vlan_qos = bb_xgetularg10_bnd(argv[3], 0, 7);
129 } 139 }
130 } 140 }
131 141
132 if (((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 142 if (((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
133 || (ioctl(fd, SIOCSIFVLAN, &ifr) < 0) 143 || (ioctl(fd, SIOCSIFVLAN, &ifr) < 0)
134 ) { 144 ) {
135 perror_msg_and_die("socket or ioctl error for %s", *argv); 145 bb_perror_msg_and_die("socket or ioctl error for %s", *argv);
136 } 146 }
137 147
138 return 0; 148 return 0;
diff --git a/networking/wget.c b/networking/wget.c
index 7154faa11..d68b16524 100644
--- a/networking/wget.c
+++ b/networking/wget.c
@@ -123,7 +123,7 @@ static char *safe_fgets(char *s, int size, FILE *stream)
123 123
124#define close_delete_and_die(s...) { \ 124#define close_delete_and_die(s...) { \
125 close_and_delete_outfile(output, fname_out, do_continue); \ 125 close_and_delete_outfile(output, fname_out, do_continue); \
126 error_msg_and_die(s); } 126 bb_error_msg_and_die(s); }
127 127
128 128
129#ifdef CONFIG_FEATURE_WGET_AUTHENTICATION 129#ifdef CONFIG_FEATURE_WGET_AUTHENTICATION
@@ -140,7 +140,7 @@ char *base64enc(char *p, char *buf, int len) {
140 140
141 while(*p) { 141 while(*p) {
142 if (s >= buf+len-4) 142 if (s >= buf+len-4)
143 error_msg_and_die("buffer overflow"); 143 bb_error_msg_and_die("buffer overflow");
144 *(s++) = al[(*p >> 2) & 0x3F]; 144 *(s++) = al[(*p >> 2) & 0x3F];
145 *(s++) = al[((*p << 4) & 0x30) | ((*(p+1) >> 4) & 0x0F)]; 145 *(s++) = al[((*p << 4) & 0x30) | ((*(p+1) >> 4) & 0x0F)];
146 *s = *(s+1) = '='; 146 *s = *(s+1) = '=';
@@ -221,7 +221,7 @@ int wget_main(int argc, char **argv)
221 case LONG_HEADER: { 221 case LONG_HEADER: {
222 int arglen = strlen(optarg); 222 int arglen = strlen(optarg);
223 if(extra_headers_left - arglen - 2 <= 0) 223 if(extra_headers_left - arglen - 2 <= 0)
224 error_msg_and_die("extra_headers buffer too small(need %i)", extra_headers_left - arglen); 224 bb_error_msg_and_die("extra_headers buffer too small(need %i)", extra_headers_left - arglen);
225 strcpy(extra_headers_ptr, optarg); 225 strcpy(extra_headers_ptr, optarg);
226 extra_headers_ptr += arglen; 226 extra_headers_ptr += arglen;
227 extra_headers_left -= ( arglen + 2 ); 227 extra_headers_left -= ( arglen + 2 );
@@ -236,12 +236,12 @@ int wget_main(int argc, char **argv)
236 } 236 }
237 break; 237 break;
238 default: 238 default:
239 show_usage(); 239 bb_show_usage();
240 } 240 }
241 } 241 }
242 242
243 if (argc - optind != 1) 243 if (argc - optind != 1)
244 show_usage(); 244 bb_show_usage();
245 245
246 parse_url(argv[optind], &target); 246 parse_url(argv[optind], &target);
247 server.host = target.host; 247 server.host = target.host;
@@ -253,7 +253,7 @@ int wget_main(int argc, char **argv)
253 if (!noproxy) { 253 if (!noproxy) {
254 proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy"); 254 proxy = getenv(target.is_ftp ? "ftp_proxy" : "http_proxy");
255 if (proxy) 255 if (proxy)
256 parse_url(xstrdup(proxy), &server); 256 parse_url(bb_xstrdup(proxy), &server);
257 } 257 }
258 258
259 /* Guess an output filename */ 259 /* Guess an output filename */
@@ -262,7 +262,7 @@ int wget_main(int argc, char **argv)
262#ifdef CONFIG_FEATURE_WGET_STATUSBAR 262#ifdef CONFIG_FEATURE_WGET_STATUSBAR
263 curfile = 263 curfile =
264#endif 264#endif
265 get_last_path_component(target.path); 265 bb_get_last_path_component(target.path);
266 if (fname_out==NULL || strlen(fname_out)<1) { 266 if (fname_out==NULL || strlen(fname_out)<1) {
267 fname_out = 267 fname_out =
268#ifdef CONFIG_FEATURE_WGET_STATUSBAR 268#ifdef CONFIG_FEATURE_WGET_STATUSBAR
@@ -274,11 +274,11 @@ int wget_main(int argc, char **argv)
274 fname_out = concat_path_file(dir_prefix, fname_out); 274 fname_out = concat_path_file(dir_prefix, fname_out);
275#ifdef CONFIG_FEATURE_WGET_STATUSBAR 275#ifdef CONFIG_FEATURE_WGET_STATUSBAR
276 } else { 276 } else {
277 curfile = get_last_path_component(fname_out); 277 curfile = bb_get_last_path_component(fname_out);
278#endif 278#endif
279 } 279 }
280 if (do_continue && !fname_out) 280 if (do_continue && !fname_out)
281 error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); 281 bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)");
282 282
283 283
284 /* 284 /*
@@ -288,7 +288,7 @@ int wget_main(int argc, char **argv)
288 output = stdout; 288 output = stdout;
289 quiet_flag = TRUE; 289 quiet_flag = TRUE;
290 } else { 290 } else {
291 output = xfopen(fname_out, (do_continue ? "a" : "w")); 291 output = bb_xfopen(fname_out, (do_continue ? "a" : "w"));
292 } 292 }
293 293
294 /* 294 /*
@@ -296,7 +296,7 @@ int wget_main(int argc, char **argv)
296 */ 296 */
297 if (do_continue) { 297 if (do_continue) {
298 if (fstat(fileno(output), &sbuf) < 0) 298 if (fstat(fileno(output), &sbuf) < 0)
299 perror_msg_and_die("fstat()"); 299 bb_perror_msg_and_die("fstat()");
300 if (sbuf.st_size > 0) 300 if (sbuf.st_size > 0)
301 beg_range = sbuf.st_size; 301 beg_range = sbuf.st_size;
302 else 302 else
@@ -399,9 +399,9 @@ read_response: if (fgets(buf, sizeof(buf), sfp) == NULL)
399 } 399 }
400 if (strcasecmp(buf, "location") == 0) { 400 if (strcasecmp(buf, "location") == 0) {
401 if (s[0] == '/') 401 if (s[0] == '/')
402 target.path = xstrdup(s+1); 402 target.path = bb_xstrdup(s+1);
403 else { 403 else {
404 parse_url(xstrdup(s), &target); 404 parse_url(bb_xstrdup(s), &target);
405 if (!proxy) { 405 if (!proxy) {
406 server.host = target.host; 406 server.host = target.host;
407 server.port = target.port; 407 server.port = target.port;
@@ -419,7 +419,7 @@ read_response: if (fgets(buf, sizeof(buf), sfp) == NULL)
419 * FTP session 419 * FTP session
420 */ 420 */
421 if (! target.user) 421 if (! target.user)
422 target.user = xstrdup("anonymous:busybox@"); 422 target.user = bb_xstrdup("anonymous:busybox@");
423 423
424 sfp = open_socket(server.host, server.port); 424 sfp = open_socket(server.host, server.port);
425 if (ftpcmd(NULL, NULL, sfp, buf) != 220) 425 if (ftpcmd(NULL, NULL, sfp, buf) != 220)
@@ -496,7 +496,7 @@ read_response: if (fgets(buf, sizeof(buf), sfp) == NULL)
496 do { 496 do {
497 while ((filesize > 0 || !got_clen) && (n = safe_fread(buf, 1, chunked ? (filesize > sizeof(buf) ? sizeof(buf) : filesize) : sizeof(buf), dfp)) > 0) { 497 while ((filesize > 0 || !got_clen) && (n = safe_fread(buf, 1, chunked ? (filesize > sizeof(buf) ? sizeof(buf) : filesize) : sizeof(buf), dfp)) > 0) {
498 if (safe_fwrite(buf, 1, n, output) != n) 498 if (safe_fwrite(buf, 1, n, output) != n)
499 perror_msg_and_die("write error"); 499 bb_perror_msg_and_die("write error");
500#ifdef CONFIG_FEATURE_WGET_STATUSBAR 500#ifdef CONFIG_FEATURE_WGET_STATUSBAR
501 statbytes+=n; 501 statbytes+=n;
502#endif 502#endif
@@ -512,7 +512,7 @@ read_response: if (fgets(buf, sizeof(buf), sfp) == NULL)
512 } 512 }
513 513
514 if (n == 0 && ferror(dfp)) 514 if (n == 0 && ferror(dfp))
515 perror_msg_and_die("network read error"); 515 bb_perror_msg_and_die("network read error");
516 } while (chunked); 516 } while (chunked);
517#ifdef CONFIG_FEATURE_WGET_STATUSBAR 517#ifdef CONFIG_FEATURE_WGET_STATUSBAR
518 if (quiet_flag==FALSE) 518 if (quiet_flag==FALSE)
@@ -521,7 +521,7 @@ read_response: if (fgets(buf, sizeof(buf), sfp) == NULL)
521 if (!proxy && target.is_ftp) { 521 if (!proxy && target.is_ftp) {
522 fclose(dfp); 522 fclose(dfp);
523 if (ftpcmd(NULL, NULL, sfp, buf) != 226) 523 if (ftpcmd(NULL, NULL, sfp, buf) != 226)
524 error_msg_and_die("ftp error: %s", buf+4); 524 bb_error_msg_and_die("ftp error: %s", buf+4);
525 ftpcmd("QUIT", NULL, sfp, buf); 525 ftpcmd("QUIT", NULL, sfp, buf);
526 } 526 }
527 exit(EXIT_SUCCESS); 527 exit(EXIT_SUCCESS);
@@ -541,14 +541,14 @@ void parse_url(char *url, struct host_info *h)
541 h->host = url + 6; 541 h->host = url + 6;
542 h->is_ftp = 1; 542 h->is_ftp = 1;
543 } else 543 } else
544 error_msg_and_die("not an http or ftp url: %s", url); 544 bb_error_msg_and_die("not an http or ftp url: %s", url);
545 545
546 sp = strchr(h->host, '/'); 546 sp = strchr(h->host, '/');
547 if (sp != NULL) { 547 if (sp != NULL) {
548 *sp++ = '\0'; 548 *sp++ = '\0';
549 h->path = sp; 549 h->path = sp;
550 } else 550 } else
551 h->path = xstrdup(""); 551 h->path = bb_xstrdup("");
552 552
553 up = strrchr(h->host, '@'); 553 up = strrchr(h->host, '@');
554 if (up != NULL) { 554 if (up != NULL) {
@@ -580,7 +580,7 @@ FILE *open_socket(char *host, int port)
580 * Get the server onto a stdio stream. 580 * Get the server onto a stdio stream.
581 */ 581 */
582 if ((fp = fdopen(fd, "r+")) == NULL) 582 if ((fp = fdopen(fd, "r+")) == NULL)
583 perror_msg_and_die("fdopen()"); 583 bb_perror_msg_and_die("fdopen()");
584 584
585 return fp; 585 return fp;
586} 586}
@@ -609,7 +609,7 @@ char *gethdr(char *buf, size_t bufsiz, FILE *fp, int *istrunc)
609 609
610 /* verify we are at the end of the header name */ 610 /* verify we are at the end of the header name */
611 if (*s != ':') 611 if (*s != ':')
612 error_msg_and_die("bad header line: %s", buf); 612 bb_error_msg_and_die("bad header line: %s", buf);
613 613
614 /* locate the start of the header value */ 614 /* locate the start of the header value */
615 for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s) 615 for (*s++ = '\0' ; *s == ' ' || *s == '\t' ; ++s)
@@ -646,7 +646,7 @@ static int ftpcmd(char *s1, char *s2, FILE *fp, char *buf)
646 do { 646 do {
647 p = fgets(buf, 510, fp); 647 p = fgets(buf, 510, fp);
648 if (!p) 648 if (!p)
649 perror_msg_and_die("fgets()"); 649 bb_perror_msg_and_die("fgets()");
650 } while (! isdigit(buf[0]) || buf[3] != ' '); 650 } while (! isdigit(buf[0]) || buf[3] != ' ');
651 651
652 return atoi(buf); 652 return atoi(buf);
@@ -824,7 +824,7 @@ progressmeter(int flag)
824 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 824 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
825 * SUCH DAMAGE. 825 * SUCH DAMAGE.
826 * 826 *
827 * $Id: wget.c,v 1.52 2003/03/11 18:03:39 andersen Exp $ 827 * $Id: wget.c,v 1.53 2003/03/19 09:12:39 mjn3 Exp $
828 */ 828 */
829 829
830 830
diff --git a/procps/free.c b/procps/free.c
index 4a5469b10..2f520e418 100644
--- a/procps/free.c
+++ b/procps/free.c
@@ -63,7 +63,7 @@ extern int free_main(int argc, char **argv)
63 } 63 }
64 64
65 if (argc > 1 && **(argv + 1) == '-') 65 if (argc > 1 && **(argv + 1) == '-')
66 show_usage(); 66 bb_show_usage();
67 67
68 printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free", 68 printf("%6s%13s%13s%13s%13s%13s\n", "", "total", "used", "free",
69 "shared", "buffers"); 69 "shared", "buffers");
diff --git a/procps/kill.c b/procps/kill.c
index c7a3c7f72..3d6fe80cb 100644
--- a/procps/kill.c
+++ b/procps/kill.c
@@ -44,7 +44,7 @@ extern int kill_main(int argc, char **argv)
44 44
45#ifdef CONFIG_KILLALL 45#ifdef CONFIG_KILLALL
46 /* Figure out what we are trying to do here */ 46 /* Figure out what we are trying to do here */
47 whichApp = (strcmp(applet_name, "killall") == 0)? KILLALL : KILL; 47 whichApp = (strcmp(bb_applet_name, "killall") == 0)? KILLALL : KILL;
48#else 48#else
49 whichApp = KILL; 49 whichApp = KILL;
50#endif 50#endif
@@ -54,7 +54,7 @@ extern int kill_main(int argc, char **argv)
54 argv++; 54 argv++;
55 /* Parse any options */ 55 /* Parse any options */
56 if (argc < 1) 56 if (argc < 1)
57 show_usage(); 57 bb_show_usage();
58 58
59 while (argc > 0 && **argv == '-') { 59 while (argc > 0 && **argv == '-') {
60 while (*++(*argv)) { 60 while (*++(*argv)) {
@@ -87,11 +87,11 @@ extern int kill_main(int argc, char **argv)
87 } 87 }
88 return EXIT_SUCCESS; 88 return EXIT_SUCCESS;
89 case '-': 89 case '-':
90 show_usage(); 90 bb_show_usage();
91 default: 91 default:
92 name = u_signal_names(*argv, &sig, 0); 92 name = u_signal_names(*argv, &sig, 0);
93 if(name==NULL) 93 if(name==NULL)
94 error_msg_and_die( "bad signal name: %s", *argv); 94 bb_error_msg_and_die( "bad signal name: %s", *argv);
95 argc--; 95 argc--;
96 argv++; 96 argv++;
97 goto do_it_now; 97 goto do_it_now;
@@ -109,10 +109,10 @@ do_it_now:
109 int pid; 109 int pid;
110 110
111 if (!isdigit(**argv)) 111 if (!isdigit(**argv))
112 perror_msg_and_die( "Bad PID"); 112 bb_perror_msg_and_die( "Bad PID");
113 pid = strtol(*argv, NULL, 0); 113 pid = strtol(*argv, NULL, 0);
114 if (kill(pid, sig) != 0) { 114 if (kill(pid, sig) != 0) {
115 perror_msg( "Could not kill pid '%d'", pid); 115 bb_perror_msg( "Could not kill pid '%d'", pid);
116 errors++; 116 errors++;
117 } 117 }
118 argv++; 118 argv++;
@@ -130,7 +130,7 @@ do_it_now:
130 if (*pidList <= 0) { 130 if (*pidList <= 0) {
131 errors++; 131 errors++;
132 if (quiet==0) 132 if (quiet==0)
133 error_msg( "%s: no process killed", *argv); 133 bb_error_msg( "%s: no process killed", *argv);
134 } else { 134 } else {
135 long *pl; 135 long *pl;
136 136
@@ -140,7 +140,7 @@ do_it_now:
140 if (kill(*pl, sig) != 0) { 140 if (kill(*pl, sig) != 0) {
141 errors++; 141 errors++;
142 if (quiet==0) 142 if (quiet==0)
143 perror_msg( "Could not kill pid '%ld'", *pl); 143 bb_perror_msg( "Could not kill pid '%ld'", *pl);
144 } 144 }
145 } 145 }
146 } 146 }
diff --git a/procps/pidof.c b/procps/pidof.c
index 8cd1a91c8..d77f7f02b 100644
--- a/procps/pidof.c
+++ b/procps/pidof.c
@@ -46,7 +46,7 @@ extern int pidof_main(int argc, char **argv)
46 single_flag = 1; 46 single_flag = 1;
47 break; 47 break;
48 default: 48 default:
49 show_usage(); 49 bb_show_usage();
50 } 50 }
51 } 51 }
52 52
diff --git a/procps/renice.c b/procps/renice.c
index ec35bdcde..a81156eea 100644
--- a/procps/renice.c
+++ b/procps/renice.c
@@ -32,7 +32,7 @@ extern int renice_main(int argc, char **argv)
32{ 32{
33 int prio, status = EXIT_SUCCESS; 33 int prio, status = EXIT_SUCCESS;
34 34
35 if (argc < 3) show_usage(); 35 if (argc < 3) bb_show_usage();
36 36
37 prio = atoi(*++argv); 37 prio = atoi(*++argv);
38 if (prio > 20) prio = 20; 38 if (prio > 20) prio = 20;
@@ -45,7 +45,7 @@ extern int renice_main(int argc, char **argv)
45 if (setpriority(PRIO_PROCESS, ps, prio) == 0) { 45 if (setpriority(PRIO_PROCESS, ps, prio) == 0) {
46 printf("%d: old priority %d, new priority %d\n", ps, oldp, prio ); 46 printf("%d: old priority %d, new priority %d\n", ps, oldp, prio );
47 } else { 47 } else {
48 perror_msg("%d: setpriority", ps); 48 bb_perror_msg("%d: setpriority", ps);
49 status = EXIT_FAILURE; 49 status = EXIT_FAILURE;
50 } 50 }
51 } 51 }
diff --git a/procps/top.c b/procps/top.c
index 4204deaf2..b70a42a72 100644
--- a/procps/top.c
+++ b/procps/top.c
@@ -155,11 +155,11 @@ static unsigned long Hertz;
155 155
156#define FILE_TO_BUF(filename, fd) do{ \ 156#define FILE_TO_BUF(filename, fd) do{ \
157 if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \ 157 if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \
158 perror_msg_and_die("/proc not be mounted?"); \ 158 bb_perror_msg_and_die("/proc not be mounted?"); \
159 } \ 159 } \
160 lseek(fd, 0L, SEEK_SET); \ 160 lseek(fd, 0L, SEEK_SET); \
161 if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \ 161 if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \
162 perror_msg_and_die("%s", filename); \ 162 bb_perror_msg_and_die("%s", filename); \
163 } \ 163 } \
164 buf[local_n] = '\0'; \ 164 buf[local_n] = '\0'; \
165}while(0) 165}while(0)
@@ -167,7 +167,7 @@ static unsigned long Hertz;
167#define FILE_TO_BUF2(filename, fd) do{ \ 167#define FILE_TO_BUF2(filename, fd) do{ \
168 lseek(fd, 0L, SEEK_SET); \ 168 lseek(fd, 0L, SEEK_SET); \
169 if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \ 169 if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \
170 perror_msg_and_die("%s", filename); \ 170 bb_perror_msg_and_die("%s", filename); \
171 } \ 171 } \
172 buf[local_n] = '\0'; \ 172 buf[local_n] = '\0'; \
173}while(0) 173}while(0)
@@ -313,19 +313,19 @@ static unsigned long display_generic(void)
313 unsigned long total, used, mfree, shared, buffers, cached; 313 unsigned long total, used, mfree, shared, buffers, cached;
314 314
315 /* read memory info */ 315 /* read memory info */
316 fp = xfopen("meminfo", "r"); 316 fp = bb_xfopen("meminfo", "r");
317 fgets(buf, sizeof(buf), fp); /* skip first line */ 317 fgets(buf, sizeof(buf), fp); /* skip first line */
318 318
319 if (fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu", 319 if (fscanf(fp, "Mem: %lu %lu %lu %lu %lu %lu",
320 &total, &used, &mfree, &shared, &buffers, &cached) != 6) { 320 &total, &used, &mfree, &shared, &buffers, &cached) != 6) {
321 error_msg_and_die("failed to read '%s'", "meminfo"); 321 bb_error_msg_and_die("failed to read '%s'", "meminfo");
322 } 322 }
323 fclose(fp); 323 fclose(fp);
324 324
325 /* read load average */ 325 /* read load average */
326 fp = xfopen("loadavg", "r"); 326 fp = bb_xfopen("loadavg", "r");
327 if (fscanf(fp, "%f %f %f", &avg1, &avg2, &avg3) != 3) { 327 if (fscanf(fp, "%f %f %f", &avg1, &avg2, &avg3) != 3) {
328 error_msg_and_die("failed to read '%s'", "loadavg"); 328 bb_error_msg_and_die("failed to read '%s'", "loadavg");
329 } 329 }
330 fclose(fp); 330 fclose(fp);
331 331
@@ -447,7 +447,7 @@ int top_main(int argc, char **argv)
447 interval = atoi(optarg); 447 interval = atoi(optarg);
448 break; 448 break;
449 default: 449 default:
450 show_usage(); 450 bb_show_usage();
451 } 451 }
452 } 452 }
453 453
@@ -461,7 +461,7 @@ int top_main(int argc, char **argv)
461#endif 461#endif
462 /* change to /proc */ 462 /* change to /proc */
463 if (chdir("/proc") < 0) { 463 if (chdir("/proc") < 0) {
464 perror_msg_and_die("chdir('/proc')"); 464 bb_perror_msg_and_die("chdir('/proc')");
465 } 465 }
466#if defined CONFIG_FEATURE_USE_TERMIOS 466#if defined CONFIG_FEATURE_USE_TERMIOS
467 tcgetattr(0, (void *) &initial_settings); 467 tcgetattr(0, (void *) &initial_settings);
@@ -508,7 +508,7 @@ int top_main(int argc, char **argv)
508 memcpy(top + n, p, sizeof(procps_status_t)); 508 memcpy(top + n, p, sizeof(procps_status_t));
509 } 509 }
510 if (ntop == 0) { 510 if (ntop == 0) {
511 perror_msg_and_die("scandir('/proc')"); 511 bb_perror_msg_and_die("scandir('/proc')");
512 } 512 }
513#ifdef FEATURE_CPU_USAGE_PERCENTAGE 513#ifdef FEATURE_CPU_USAGE_PERCENTAGE
514 if(!Hertz) { 514 if(!Hertz) {
diff --git a/shell/ash.c b/shell/ash.c
index 828a88872..fb65c2da0 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -1329,13 +1329,13 @@ static void setalias(char *name, char *val)
1329 if (!(ap->flag & ALIASINUSE)) { 1329 if (!(ap->flag & ALIASINUSE)) {
1330 free(ap->val); 1330 free(ap->val);
1331 } 1331 }
1332 ap->val = xstrdup(val); 1332 ap->val = bb_xstrdup(val);
1333 ap->flag &= ~ALIASDEAD; 1333 ap->flag &= ~ALIASDEAD;
1334 } else { 1334 } else {
1335 /* not found */ 1335 /* not found */
1336 ap = xmalloc(sizeof(struct alias)); 1336 ap = xmalloc(sizeof(struct alias));
1337 ap->name = xstrdup(name); 1337 ap->name = bb_xstrdup(name);
1338 ap->val = xstrdup(val); 1338 ap->val = bb_xstrdup(val);
1339 ap->flag = 0; 1339 ap->flag = 0;
1340 ap->next = 0; 1340 ap->next = 0;
1341 *app = ap; 1341 *app = ap;
@@ -1829,7 +1829,7 @@ static void setpwd(const char *val, int setold)
1829 if (!val) 1829 if (!val)
1830 getpwd(); 1830 getpwd();
1831 else 1831 else
1832 curdir = simplify_path(val); 1832 curdir = bb_simplify_path(val);
1833 if (cated) 1833 if (cated)
1834 free(cated); 1834 free(cated);
1835 INTON; 1835 INTON;
@@ -3285,7 +3285,7 @@ static void tryexec(char *cmd, char **argv, char **envp)
3285 char *name = cmd; 3285 char *name = cmd;
3286 3286
3287#ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN 3287#ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN
3288 name = get_last_path_component(name); 3288 name = bb_get_last_path_component(name);
3289 if(find_applet_by_name(name) != NULL) 3289 if(find_applet_by_name(name) != NULL)
3290 flg_bb = 1; 3290 flg_bb = 1;
3291#else 3291#else
@@ -7552,7 +7552,7 @@ static int dotcmd(int argc, char **argv)
7552 exitstatus = 0; 7552 exitstatus = 0;
7553 7553
7554 for (sp = cmdenviron; sp; sp = sp->next) 7554 for (sp = cmdenviron; sp; sp = sp->next)
7555 setvareq(xstrdup(sp->text), VSTRFIXED | VTEXTFIXED); 7555 setvareq(bb_xstrdup(sp->text), VSTRFIXED | VTEXTFIXED);
7556 7556
7557 if (argc >= 2) { /* That's what SVR2 does */ 7557 if (argc >= 2) { /* That's what SVR2 does */
7558 char *fullname; 7558 char *fullname;
@@ -7950,7 +7950,7 @@ static int umaskcmd(int argc, char **argv)
7950 umask(mask); 7950 umask(mask);
7951 } else { 7951 } else {
7952 mask = ~mask & 0777; 7952 mask = ~mask & 0777;
7953 if (!parse_mode(ap, &mask)) { 7953 if (!bb_parse_mode(ap, &mask)) {
7954 error("Illegal mode: %s", ap); 7954 error("Illegal mode: %s", ap);
7955 } 7955 }
7956 umask(~mask & 0777); 7956 umask(~mask & 0777);
@@ -8795,7 +8795,7 @@ static void setparam(char **argv)
8795 for (nparam = 0; argv[nparam]; nparam++); 8795 for (nparam = 0; argv[nparam]; nparam++);
8796 ap = newparam = xmalloc((nparam + 1) * sizeof *ap); 8796 ap = newparam = xmalloc((nparam + 1) * sizeof *ap);
8797 while (*argv) { 8797 while (*argv) {
8798 *ap++ = xstrdup(*argv++); 8798 *ap++ = bb_xstrdup(*argv++);
8799 } 8799 }
8800 *ap = NULL; 8800 *ap = NULL;
8801 freeparam(&shellparam); 8801 freeparam(&shellparam);
@@ -11429,7 +11429,7 @@ static void opentrace()
11429#else 11429#else
11430 strcpy(s, "./trace"); 11430 strcpy(s, "./trace");
11431#endif /* not_this_way */ 11431#endif /* not_this_way */
11432 if ((tracefile = wfopen(s, "a")) == NULL) 11432 if ((tracefile = bb_wfopen(s, "a")) == NULL)
11433 return; 11433 return;
11434#ifdef O_APPEND 11434#ifdef O_APPEND
11435 if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0) 11435 if ((flags = fcntl(fileno(tracefile), F_GETFL, 0)) >= 0)
@@ -11482,7 +11482,7 @@ static int trapcmd(int argc, char **argv)
11482 if (action[0] == '-' && action[1] == '\0') 11482 if (action[0] == '-' && action[1] == '\0')
11483 action = NULL; 11483 action = NULL;
11484 else 11484 else
11485 action = xstrdup(action); 11485 action = bb_xstrdup(action);
11486 } 11486 }
11487 free(trap[signo]); 11487 free(trap[signo]);
11488 trap[signo] = action; 11488 trap[signo] = action;
@@ -11700,7 +11700,7 @@ static void initvar()
11700 vpp = hashvar(ip->text); 11700 vpp = hashvar(ip->text);
11701 vp->next = *vpp; 11701 vp->next = *vpp;
11702 *vpp = vp; 11702 *vpp = vp;
11703 vp->text = xstrdup(ip->text); 11703 vp->text = bb_xstrdup(ip->text);
11704 vp->flags = ip->flags; 11704 vp->flags = ip->flags;
11705 vp->func = ip->func; 11705 vp->func = ip->func;
11706 } 11706 }
@@ -11713,7 +11713,7 @@ static void initvar()
11713 vpp = hashvar("PS1=$ "); 11713 vpp = hashvar("PS1=$ ");
11714 vps1.next = *vpp; 11714 vps1.next = *vpp;
11715 *vpp = &vps1; 11715 *vpp = &vps1;
11716 vps1.text = xstrdup(geteuid()? "PS1=$ " : "PS1=# "); 11716 vps1.text = bb_xstrdup(geteuid()? "PS1=$ " : "PS1=# ");
11717 vps1.flags = VSTRFIXED | VTEXTFIXED; 11717 vps1.flags = VSTRFIXED | VTEXTFIXED;
11718 } 11718 }
11719#endif 11719#endif
@@ -11833,7 +11833,7 @@ static void listsetvar(struct strlist *mylist)
11833 11833
11834 INTOFF; 11834 INTOFF;
11835 for (lp = mylist; lp; lp = lp->next) { 11835 for (lp = mylist; lp; lp = lp->next) {
11836 setvareq(xstrdup(lp->text), 0); 11836 setvareq(bb_xstrdup(lp->text), 0);
11837 } 11837 }
11838 INTON; 11838 INTON;
11839} 11839}
@@ -11996,7 +11996,7 @@ static void mklocal(char *name)
11996 vp = *findvar(vpp, name); 11996 vp = *findvar(vpp, name);
11997 if (vp == NULL) { 11997 if (vp == NULL) {
11998 if (strchr(name, '=')) 11998 if (strchr(name, '='))
11999 setvareq(xstrdup(name), VSTRFIXED); 11999 setvareq(bb_xstrdup(name), VSTRFIXED);
12000 else 12000 else
12001 setvar(name, NULL, VSTRFIXED); 12001 setvar(name, NULL, VSTRFIXED);
12002 vp = *vpp; /* the new variable */ 12002 vp = *vpp; /* the new variable */
@@ -12007,7 +12007,7 @@ static void mklocal(char *name)
12007 lvp->flags = vp->flags; 12007 lvp->flags = vp->flags;
12008 vp->flags |= VSTRFIXED | VTEXTFIXED; 12008 vp->flags |= VSTRFIXED | VTEXTFIXED;
12009 if (strchr(name, '=')) 12009 if (strchr(name, '='))
12010 setvareq(xstrdup(name), 0); 12010 setvareq(bb_xstrdup(name), 0);
12011 } 12011 }
12012 } 12012 }
12013 lvp->vp = vp; 12013 lvp->vp = vp;
@@ -12243,7 +12243,7 @@ int letcmd(int argc, char **argv)
12243 return 0; 12243 return 0;
12244 } 12244 }
12245 snprintf(p, 12, "%ld", result); 12245 snprintf(p, 12, "%ld", result);
12246 setvar(argv[1], xstrdup(p), 0); 12246 setvar(argv[1], bb_xstrdup(p), 0);
12247 } else if (argc >= 3) 12247 } else if (argc >= 3)
12248 synerror("invalid operand"); 12248 synerror("invalid operand");
12249 return !result; 12249 return !result;
diff --git a/shell/cmdedit.c b/shell/cmdedit.c
index 2ea61614d..3cbdce0ef 100644
--- a/shell/cmdedit.c
+++ b/shell/cmdedit.c
@@ -330,7 +330,7 @@ static void parse_prompt(const char *prmt_ptr)
330 char *pbuf; 330 char *pbuf;
331 331
332 if (!pwd_buf) { 332 if (!pwd_buf) {
333 pwd_buf=(char *)unknown; 333 pwd_buf=(char *)bb_msg_unknown;
334 } 334 }
335 335
336 while (*prmt_ptr) { 336 while (*prmt_ptr) {
@@ -341,7 +341,7 @@ static void parse_prompt(const char *prmt_ptr)
341 const char *cp = prmt_ptr; 341 const char *cp = prmt_ptr;
342 int l; 342 int l;
343 343
344 c = process_escape_sequence(&prmt_ptr); 344 c = bb_process_escape_sequence(&prmt_ptr);
345 if(prmt_ptr==cp) { 345 if(prmt_ptr==cp) {
346 if (*cp == 0) 346 if (*cp == 0)
347 break; 347 break;
@@ -430,7 +430,7 @@ static void parse_prompt(const char *prmt_ptr)
430 if (flg_not_length == ']') 430 if (flg_not_length == ']')
431 sub_len++; 431 sub_len++;
432 } 432 }
433 if(pwd_buf!=(char *)unknown) 433 if(pwd_buf!=(char *)bb_msg_unknown)
434 free(pwd_buf); 434 free(pwd_buf);
435 cmdedit_prompt = prmt_mem_ptr; 435 cmdedit_prompt = prmt_mem_ptr;
436 cmdedit_prmt_len = prmt_len - sub_len; 436 cmdedit_prmt_len = prmt_len - sub_len;
@@ -520,8 +520,8 @@ static void cmdedit_init(void)
520 my_euid = geteuid(); 520 my_euid = geteuid();
521 entry = getpwuid(my_euid); 521 entry = getpwuid(my_euid);
522 if (entry) { 522 if (entry) {
523 user_buf = xstrdup(entry->pw_name); 523 user_buf = bb_xstrdup(entry->pw_name);
524 home_pwd_buf = xstrdup(entry->pw_dir); 524 home_pwd_buf = bb_xstrdup(entry->pw_dir);
525 } 525 }
526#endif 526#endif
527 527
@@ -598,7 +598,7 @@ static char **username_tab_completion(char *ud, int *num_matches)
598 /* Null usernames should result in all users as possible completions. */ 598 /* Null usernames should result in all users as possible completions. */
599 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) { 599 if ( /*!userlen || */ !strncmp(ud, entry->pw_name, userlen)) {
600 600
601 bb_asprintf(&temp, "~%s/", entry->pw_name); 601 bb_xasprintf(&temp, "~%s/", entry->pw_name);
602 matches = xrealloc(matches, (nm + 1) * sizeof(char *)); 602 matches = xrealloc(matches, (nm + 1) * sizeof(char *));
603 603
604 matches[nm++] = temp; 604 matches[nm++] = temp;
@@ -647,7 +647,7 @@ static int path_parse(char ***p, int flags)
647 *p = xmalloc(npth * sizeof(char *)); 647 *p = xmalloc(npth * sizeof(char *));
648 648
649 tmp = pth; 649 tmp = pth;
650 (*p)[0] = xstrdup(tmp); 650 (*p)[0] = bb_xstrdup(tmp);
651 npth = 1; /* count words is + 1 count ':' */ 651 npth = 1; /* count words is + 1 count ':' */
652 652
653 for (;;) { 653 for (;;) {
@@ -1071,7 +1071,7 @@ static void input_tab(int *lastWasTab)
1071 qsort(matches, num_matches, sizeof(char *), match_compare); 1071 qsort(matches, num_matches, sizeof(char *), match_compare);
1072 1072
1073 /* find minimal match */ 1073 /* find minimal match */
1074 tmp = xstrdup(matches[0]); 1074 tmp = bb_xstrdup(matches[0]);
1075 for (tmp1 = tmp; *tmp1; tmp1++) 1075 for (tmp1 = tmp; *tmp1; tmp1++)
1076 for (len_found = 1; len_found < num_matches; len_found++) 1076 for (len_found = 1; len_found < num_matches; len_found++)
1077 if (matches[len_found][(tmp1 - tmp)] != *tmp1) { 1077 if (matches[len_found][(tmp1 - tmp)] != *tmp1) {
@@ -1132,7 +1132,7 @@ static void get_previous_history(void)
1132{ 1132{
1133 if(command_ps[0] != 0 || history[cur_history] == 0) { 1133 if(command_ps[0] != 0 || history[cur_history] == 0) {
1134 free(history[cur_history]); 1134 free(history[cur_history]);
1135 history[cur_history] = xstrdup(command_ps); 1135 history[cur_history] = bb_xstrdup(command_ps);
1136 } 1136 }
1137 cur_history--; 1137 cur_history--;
1138} 1138}
@@ -1166,12 +1166,11 @@ extern void load_history ( const char *fromfile )
1166 if (( fp = fopen ( fromfile, "r" ))) { 1166 if (( fp = fopen ( fromfile, "r" ))) {
1167 1167
1168 for ( hi = 0; hi < MAX_HISTORY; ) { 1168 for ( hi = 0; hi < MAX_HISTORY; ) {
1169 char * hl = get_line_from_file(fp); 1169 char * hl = bb_get_chomped_line_from_file(fp);
1170 int l; 1170 int l;
1171 1171
1172 if(!hl) 1172 if(!hl)
1173 break; 1173 break;
1174 chomp(hl);
1175 l = strlen(hl); 1174 l = strlen(hl);
1176 if(l >= BUFSIZ) 1175 if(l >= BUFSIZ)
1177 hl[BUFSIZ-1] = 0; 1176 hl[BUFSIZ-1] = 0;
@@ -1500,7 +1499,7 @@ rewrite_line:
1500 for(i = 0; i < (MAX_HISTORY-1); i++) 1499 for(i = 0; i < (MAX_HISTORY-1); i++)
1501 history[i] = history[i+1]; 1500 history[i] = history[i+1];
1502 } 1501 }
1503 history[i++] = xstrdup(command); 1502 history[i++] = bb_xstrdup(command);
1504 cur_history = i; 1503 cur_history = i;
1505 n_history = i; 1504 n_history = i;
1506#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT) 1505#if defined(CONFIG_FEATURE_SH_FANCY_PROMPT)
@@ -1535,7 +1534,7 @@ rewrite_line:
1535 1534
1536#ifdef TEST 1535#ifdef TEST
1537 1536
1538const char *applet_name = "debug stuff usage"; 1537const char *bb_applet_name = "debug stuff usage";
1539const char *memory_exhausted = "Memory exhausted"; 1538const char *memory_exhausted = "Memory exhausted";
1540 1539
1541#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT 1540#ifdef CONFIG_FEATURE_NONPRINTABLE_INVERSE_PUT
diff --git a/shell/hush.c b/shell/hush.c
index e20bf7a06..94b29c03d 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -110,7 +110,7 @@
110#include "busybox.h" 110#include "busybox.h"
111#include "cmdedit.h" 111#include "cmdedit.h"
112#else 112#else
113#define applet_name "hush" 113#define bb_applet_name "hush"
114#include "standalone.h" 114#include "standalone.h"
115#define hush_main main 115#define hush_main main
116#undef CONFIG_FEATURE_SH_FANCY_PROMPT 116#undef CONFIG_FEATURE_SH_FANCY_PROMPT
@@ -320,7 +320,7 @@ static inline void debug_printf(const char *format, ...) { }
320#define final_printf debug_printf 320#define final_printf debug_printf
321 321
322static void __syntax(char *file, int line) { 322static void __syntax(char *file, int line) {
323 error_msg("syntax error %s:%d", file, line); 323 bb_error_msg("syntax error %s:%d", file, line);
324} 324}
325#define syntax() __syntax(__FILE__, __LINE__) 325#define syntax() __syntax(__FILE__, __LINE__)
326 326
@@ -441,11 +441,11 @@ static struct built_in_command bltins[] = {
441 441
442static const char *set_cwd(void) 442static const char *set_cwd(void)
443{ 443{
444 if(cwd==unknown) 444 if(cwd==bb_msg_unknown)
445 cwd = NULL; /* xgetcwd(arg) called free(arg) */ 445 cwd = NULL; /* xgetcwd(arg) called free(arg) */
446 cwd = xgetcwd((char *)cwd); 446 cwd = xgetcwd((char *)cwd);
447 if (!cwd) 447 if (!cwd)
448 cwd = unknown; 448 cwd = bb_msg_unknown;
449 return cwd; 449 return cwd;
450} 450}
451 451
@@ -548,7 +548,7 @@ static int builtin_export(struct child_prog *child)
548 } 548 }
549 } 549 }
550 if (res<0) 550 if (res<0)
551 perror_msg("export"); 551 bb_perror_msg("export");
552 else if(res==0) 552 else if(res==0)
553 res = set_local_var(name, 1); 553 res = set_local_var(name, 1);
554 else 554 else
@@ -573,12 +573,12 @@ static int builtin_fg_bg(struct child_prog *child)
573 } 573 }
574 } 574 }
575 if (!pi) { 575 if (!pi) {
576 error_msg("%s: no current job", child->argv[0]); 576 bb_error_msg("%s: no current job", child->argv[0]);
577 return EXIT_FAILURE; 577 return EXIT_FAILURE;
578 } 578 }
579 } else { 579 } else {
580 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) { 580 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
581 error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]); 581 bb_error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]);
582 return EXIT_FAILURE; 582 return EXIT_FAILURE;
583 } 583 }
584 for (pi = job_list; pi; pi = pi->next) { 584 for (pi = job_list; pi; pi = pi->next) {
@@ -587,7 +587,7 @@ static int builtin_fg_bg(struct child_prog *child)
587 } 587 }
588 } 588 }
589 if (!pi) { 589 if (!pi) {
590 error_msg("%s: %d: no such job", child->argv[0], jobnum); 590 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
591 return EXIT_FAILURE; 591 return EXIT_FAILURE;
592 } 592 }
593 } 593 }
@@ -605,7 +605,7 @@ static int builtin_fg_bg(struct child_prog *child)
605 if (i == ESRCH) { 605 if (i == ESRCH) {
606 remove_bg_job(pi); 606 remove_bg_job(pi);
607 } else { 607 } else {
608 perror_msg("kill (SIGCONT)"); 608 bb_perror_msg("kill (SIGCONT)");
609 } 609 }
610 } 610 }
611 611
@@ -728,7 +728,7 @@ static int builtin_source(struct child_prog *child)
728 /* XXX search through $PATH is missing */ 728 /* XXX search through $PATH is missing */
729 input = fopen(child->argv[1], "r"); 729 input = fopen(child->argv[1], "r");
730 if (!input) { 730 if (!input) {
731 error_msg("Couldn't open file '%s'", child->argv[1]); 731 bb_error_msg("Couldn't open file '%s'", child->argv[1]);
732 return EXIT_FAILURE; 732 return EXIT_FAILURE;
733 } 733 }
734 734
@@ -996,7 +996,7 @@ static void mark_closed(int fd)
996{ 996{
997 struct close_me *tmp; 997 struct close_me *tmp;
998 if (close_me_head == NULL || close_me_head->fd != fd) 998 if (close_me_head == NULL || close_me_head->fd != fd)
999 error_msg_and_die("corrupt close_me"); 999 bb_error_msg_and_die("corrupt close_me");
1000 tmp = close_me_head; 1000 tmp = close_me_head;
1001 close_me_head = close_me_head->next; 1001 close_me_head = close_me_head->next;
1002 free(tmp); 1002 free(tmp);
@@ -1029,7 +1029,7 @@ static int setup_redirects(struct child_prog *prog, int squirrel[])
1029 if (openfd < 0) { 1029 if (openfd < 0) {
1030 /* this could get lost if stderr has been redirected, but 1030 /* this could get lost if stderr has been redirected, but
1031 bash and ash both lose it as well (though zsh doesn't!) */ 1031 bash and ash both lose it as well (though zsh doesn't!) */
1032 perror_msg("error opening %s", redir->word.gl_pathv[0]); 1032 bb_perror_msg("error opening %s", redir->word.gl_pathv[0]);
1033 return 1; 1033 return 1;
1034 } 1034 }
1035 } else { 1035 } else {
@@ -1125,14 +1125,14 @@ static void pseudo_exec(struct child_prog *child)
1125#ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN 1125#ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN
1126 /* Following discussions from November 2000 on the busybox mailing 1126 /* Following discussions from November 2000 on the busybox mailing
1127 * list, the default configuration, (without 1127 * list, the default configuration, (without
1128 * get_last_path_component()) lets the user force use of an 1128 * bb_get_last_path_component()) lets the user force use of an
1129 * external command by specifying the full (with slashes) filename. 1129 * external command by specifying the full (with slashes) filename.
1130 * If you enable CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN, then applets 1130 * If you enable CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN, then applets
1131 * _aways_ override external commands, so if you want to run 1131 * _aways_ override external commands, so if you want to run
1132 * /bin/cat, it will use BusyBox cat even if /bin/cat exists on the 1132 * /bin/cat, it will use BusyBox cat even if /bin/cat exists on the
1133 * filesystem and is _not_ busybox. Some systems may want this, 1133 * filesystem and is _not_ busybox. Some systems may want this,
1134 * most do not. */ 1134 * most do not. */
1135 name = get_last_path_component(name); 1135 name = bb_get_last_path_component(name);
1136#endif 1136#endif
1137 /* Count argc for use in a second... */ 1137 /* Count argc for use in a second... */
1138 for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++); 1138 for(argc_l=0;*argv_l!=NULL; argv_l++, argc_l++);
@@ -1143,7 +1143,7 @@ static void pseudo_exec(struct child_prog *child)
1143#endif 1143#endif
1144 debug_printf("exec of %s\n",child->argv[0]); 1144 debug_printf("exec of %s\n",child->argv[0]);
1145 execvp(child->argv[0],child->argv); 1145 execvp(child->argv[0],child->argv);
1146 perror_msg("couldn't exec: %s",child->argv[0]); 1146 bb_perror_msg("couldn't exec: %s",child->argv[0]);
1147 _exit(1); 1147 _exit(1);
1148 } else if (child->group) { 1148 } else if (child->group) {
1149 debug_printf("runtime nesting to group\n"); 1149 debug_printf("runtime nesting to group\n");
@@ -1292,11 +1292,11 @@ static int checkjobs(struct pipe* fg_pipe)
1292 } 1292 }
1293 1293
1294 if (childpid == -1 && errno != ECHILD) 1294 if (childpid == -1 && errno != ECHILD)
1295 perror_msg("waitpid"); 1295 bb_perror_msg("waitpid");
1296 1296
1297 /* move the shell to the foreground */ 1297 /* move the shell to the foreground */
1298 //if (interactive && tcsetpgrp(shell_terminal, getpgid(0))) 1298 //if (interactive && tcsetpgrp(shell_terminal, getpgid(0)))
1299 // perror_msg("tcsetpgrp-2"); 1299 // bb_perror_msg("tcsetpgrp-2");
1300 return -1; 1300 return -1;
1301} 1301}
1302 1302
@@ -1381,7 +1381,7 @@ static int run_pipe_real(struct pipe *pi)
1381 * variable. */ 1381 * variable. */
1382 int export_me=0; 1382 int export_me=0;
1383 char *name, *value; 1383 char *name, *value;
1384 name = xstrdup(child->argv[i]); 1384 name = bb_xstrdup(child->argv[i]);
1385 debug_printf("Local environment set: %s\n", name); 1385 debug_printf("Local environment set: %s\n", name);
1386 value = strchr(name, '='); 1386 value = strchr(name, '=');
1387 if (value) 1387 if (value)
@@ -1441,7 +1441,7 @@ static int run_pipe_real(struct pipe *pi)
1441 1441
1442 /* pipes are inserted between pairs of commands */ 1442 /* pipes are inserted between pairs of commands */
1443 if ((i + 1) < pi->num_progs) { 1443 if ((i + 1) < pi->num_progs) {
1444 if (pipe(pipefds)<0) perror_msg_and_die("pipe"); 1444 if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
1445 nextout = pipefds[1]; 1445 nextout = pipefds[1];
1446 } else { 1446 } else {
1447 nextout=1; 1447 nextout=1;
@@ -1626,11 +1626,11 @@ static int run_list_real(struct pipe *pi)
1626 if (interactive) { 1626 if (interactive) {
1627 /* move the new process group into the foreground */ 1627 /* move the new process group into the foreground */
1628 if (tcsetpgrp(shell_terminal, pi->pgrp) && errno != ENOTTY) 1628 if (tcsetpgrp(shell_terminal, pi->pgrp) && errno != ENOTTY)
1629 perror_msg("tcsetpgrp-3"); 1629 bb_perror_msg("tcsetpgrp-3");
1630 rcode = checkjobs(pi); 1630 rcode = checkjobs(pi);
1631 /* move the shell to the foreground */ 1631 /* move the shell to the foreground */
1632 if (tcsetpgrp(shell_terminal, getpgid(0)) && errno != ENOTTY) 1632 if (tcsetpgrp(shell_terminal, getpgid(0)) && errno != ENOTTY)
1633 perror_msg("tcsetpgrp-4"); 1633 bb_perror_msg("tcsetpgrp-4");
1634 } else { 1634 } else {
1635 rcode = checkjobs(pi); 1635 rcode = checkjobs(pi);
1636 } 1636 }
@@ -1825,9 +1825,9 @@ static int xglob(o_string *dest, int flags, glob_t *pglob)
1825 debug_printf("globhack returned %d\n",gr); 1825 debug_printf("globhack returned %d\n",gr);
1826 } 1826 }
1827 if (gr == GLOB_NOSPACE) 1827 if (gr == GLOB_NOSPACE)
1828 error_msg_and_die("out of memory during glob"); 1828 bb_error_msg_and_die("out of memory during glob");
1829 if (gr != 0) { /* GLOB_ABORTED ? */ 1829 if (gr != 0) { /* GLOB_ABORTED ? */
1830 error_msg("glob(3) error %d",gr); 1830 bb_error_msg("glob(3) error %d",gr);
1831 } 1831 }
1832 /* globprint(glob_target); */ 1832 /* globprint(glob_target); */
1833 return gr; 1833 return gr;
@@ -1881,7 +1881,7 @@ static int set_local_var(const char *s, int flg_export)
1881 result++; 1881 result++;
1882 } else { 1882 } else {
1883 if(cur->flg_read_only) { 1883 if(cur->flg_read_only) {
1884 error_msg("%s: readonly variable", name); 1884 bb_error_msg("%s: readonly variable", name);
1885 result = -1; 1885 result = -1;
1886 } else { 1886 } else {
1887 if(flg_export>0 || cur->flg_export>1) 1887 if(flg_export>0 || cur->flg_export>1)
@@ -1935,7 +1935,7 @@ static void unset_local_var(const char *name)
1935 if(cur!=0) { 1935 if(cur!=0) {
1936 struct variables *next = top_vars; 1936 struct variables *next = top_vars;
1937 if(cur->flg_read_only) { 1937 if(cur->flg_read_only) {
1938 error_msg("%s: readonly variable", name); 1938 bb_error_msg("%s: readonly variable", name);
1939 return; 1939 return;
1940 } else { 1940 } else {
1941 if(cur->flg_export) 1941 if(cur->flg_export)
@@ -2139,7 +2139,7 @@ static int done_word(o_string *dest, struct p_context *ctx)
2139 if (ctx->pending_redirect) { 2139 if (ctx->pending_redirect) {
2140 ctx->pending_redirect=NULL; 2140 ctx->pending_redirect=NULL;
2141 if (glob_target->gl_pathc != 1) { 2141 if (glob_target->gl_pathc != 1) {
2142 error_msg("ambiguous redirect"); 2142 bb_error_msg("ambiguous redirect");
2143 return 1; 2143 return 1;
2144 } 2144 }
2145 } else { 2145 } else {
@@ -2231,7 +2231,7 @@ static int redirect_dup_num(struct in_str *input)
2231 } 2231 }
2232 if (ok) return d; 2232 if (ok) return d;
2233 2233
2234 error_msg("ambiguous redirect"); 2234 bb_error_msg("ambiguous redirect");
2235 return -2; 2235 return -2;
2236} 2236}
2237 2237
@@ -2267,14 +2267,14 @@ FILE *generate_stream_from_list(struct pipe *head)
2267 FILE *pf; 2267 FILE *pf;
2268#if 1 2268#if 1
2269 int pid, channel[2]; 2269 int pid, channel[2];
2270 if (pipe(channel)<0) perror_msg_and_die("pipe"); 2270 if (pipe(channel)<0) bb_perror_msg_and_die("pipe");
2271#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__) 2271#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
2272 pid=fork(); 2272 pid=fork();
2273#else 2273#else
2274 pid=vfork(); 2274 pid=vfork();
2275#endif 2275#endif
2276 if (pid<0) { 2276 if (pid<0) {
2277 perror_msg_and_die("fork"); 2277 bb_perror_msg_and_die("fork");
2278 } else if (pid==0) { 2278 } else if (pid==0) {
2279 close(channel[0]); 2279 close(channel[0]);
2280 if (channel[1] != 1) { 2280 if (channel[1] != 1) {
@@ -2450,7 +2450,7 @@ static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *i
2450 case '-': 2450 case '-':
2451 case '_': 2451 case '_':
2452 /* still unhandled, but should be eventually */ 2452 /* still unhandled, but should be eventually */
2453 error_msg("unhandled syntax: $%c",ch); 2453 bb_error_msg("unhandled syntax: $%c",ch);
2454 return 1; 2454 return 1;
2455 break; 2455 break;
2456 default: 2456 default:
@@ -2811,7 +2811,7 @@ int hush_main(int argc, char **argv)
2811 " or: sh -c command [args]...\n\n"); 2811 " or: sh -c command [args]...\n\n");
2812 exit(EXIT_FAILURE); 2812 exit(EXIT_FAILURE);
2813#else 2813#else
2814 show_usage(); 2814 bb_show_usage();
2815#endif 2815#endif
2816 } 2816 }
2817 } 2817 }
@@ -2845,12 +2845,12 @@ int hush_main(int argc, char **argv)
2845 debug_printf("\nrunning script '%s'\n", argv[optind]); 2845 debug_printf("\nrunning script '%s'\n", argv[optind]);
2846 global_argv = argv+optind; 2846 global_argv = argv+optind;
2847 global_argc = argc-optind; 2847 global_argc = argc-optind;
2848 input = xfopen(argv[optind], "r"); 2848 input = bb_xfopen(argv[optind], "r");
2849 opt = parse_file_outer(input); 2849 opt = parse_file_outer(input);
2850 2850
2851#ifdef CONFIG_FEATURE_CLEAN_UP 2851#ifdef CONFIG_FEATURE_CLEAN_UP
2852 fclose(input); 2852 fclose(input);
2853 if (cwd && cwd != unknown) 2853 if (cwd && cwd != bb_msg_unknown)
2854 free((char*)cwd); 2854 free((char*)cwd);
2855 { 2855 {
2856 struct variables *cur, *tmp; 2856 struct variables *cur, *tmp;
diff --git a/shell/lash.c b/shell/lash.c
index 35929898e..8f864b355 100644
--- a/shell/lash.c
+++ b/shell/lash.c
@@ -246,7 +246,7 @@ static int builtin_cd(struct child_prog *child)
246 } 246 }
247 cwd = xgetcwd((char *)cwd); 247 cwd = xgetcwd((char *)cwd);
248 if (!cwd) 248 if (!cwd)
249 cwd = unknown; 249 cwd = bb_msg_unknown;
250 return EXIT_SUCCESS; 250 return EXIT_SUCCESS;
251} 251}
252 252
@@ -284,12 +284,12 @@ static int builtin_fg_bg(struct child_prog *child)
284 } 284 }
285 } 285 }
286 if (!job) { 286 if (!job) {
287 error_msg("%s: no current job", child->argv[0]); 287 bb_error_msg("%s: no current job", child->argv[0]);
288 return EXIT_FAILURE; 288 return EXIT_FAILURE;
289 } 289 }
290 } else { 290 } else {
291 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) { 291 if (sscanf(child->argv[1], "%%%d", &jobnum) != 1) {
292 error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]); 292 bb_error_msg("%s: bad argument '%s'", child->argv[0], child->argv[1]);
293 return EXIT_FAILURE; 293 return EXIT_FAILURE;
294 } 294 }
295 for (job = child->family->job_list->head; job; job = job->next) { 295 for (job = child->family->job_list->head; job; job = job->next) {
@@ -298,7 +298,7 @@ static int builtin_fg_bg(struct child_prog *child)
298 } 298 }
299 } 299 }
300 if (!job) { 300 if (!job) {
301 error_msg("%s: %d: no such job", child->argv[0], jobnum); 301 bb_error_msg("%s: %d: no such job", child->argv[0], jobnum);
302 return EXIT_FAILURE; 302 return EXIT_FAILURE;
303 } 303 }
304 } 304 }
@@ -320,7 +320,7 @@ static int builtin_fg_bg(struct child_prog *child)
320 if (i == ESRCH) { 320 if (i == ESRCH) {
321 remove_job(&job_list, job); 321 remove_job(&job_list, job);
322 } else { 322 } else {
323 perror_msg("kill (SIGCONT)"); 323 bb_perror_msg("kill (SIGCONT)");
324 } 324 }
325 } 325 }
326 326
@@ -371,7 +371,7 @@ static int builtin_pwd(struct child_prog *dummy)
371{ 371{
372 cwd = xgetcwd((char *)cwd); 372 cwd = xgetcwd((char *)cwd);
373 if (!cwd) 373 if (!cwd)
374 cwd = unknown; 374 cwd = bb_msg_unknown;
375 puts(cwd); 375 puts(cwd);
376 return EXIT_SUCCESS; 376 return EXIT_SUCCESS;
377} 377}
@@ -489,7 +489,7 @@ static void mark_closed(int fd)
489{ 489{
490 struct close_me *tmp; 490 struct close_me *tmp;
491 if (close_me_head == NULL || close_me_head->fd != fd) 491 if (close_me_head == NULL || close_me_head->fd != fd)
492 error_msg_and_die("corrupt close_me"); 492 bb_error_msg_and_die("corrupt close_me");
493 tmp = close_me_head; 493 tmp = close_me_head;
494 close_me_head = close_me_head->next; 494 close_me_head = close_me_head->next;
495 free(tmp); 495 free(tmp);
@@ -599,7 +599,7 @@ static void checkjobs(struct jobset *j_list)
599 } 599 }
600 600
601 if (childpid == -1 && errno != ECHILD) 601 if (childpid == -1 && errno != ECHILD)
602 perror_msg("waitpid"); 602 bb_perror_msg("waitpid");
603} 603}
604 604
605/* squirrel != NULL means we squirrel away copies of stdin, stdout, 605/* squirrel != NULL means we squirrel away copies of stdin, stdout,
@@ -628,7 +628,7 @@ static int setup_redirects(struct child_prog *prog, int squirrel[])
628 if (openfd < 0) { 628 if (openfd < 0) {
629 /* this could get lost if stderr has been redirected, but 629 /* this could get lost if stderr has been redirected, but
630 bash and ash both lose it as well (though zsh doesn't!) */ 630 bash and ash both lose it as well (though zsh doesn't!) */
631 perror_msg("error opening %s", redir->filename); 631 bb_perror_msg("error opening %s", redir->filename);
632 return 1; 632 return 1;
633 } 633 }
634 634
@@ -803,7 +803,7 @@ static int expand_arguments(char *command)
803 while( command && command[ix]) { 803 while( command && command[ix]) {
804 if (command[ix] == '\\') { 804 if (command[ix] == '\\') {
805 const char *tmp = command+ix+1; 805 const char *tmp = command+ix+1;
806 command[ix] = process_escape_sequence( &tmp ); 806 command[ix] = bb_process_escape_sequence( &tmp );
807 memmove(command+ix + 1, tmp, strlen(tmp)+1); 807 memmove(command+ix + 1, tmp, strlen(tmp)+1);
808 } 808 }
809 ix++; 809 ix++;
@@ -816,7 +816,7 @@ static int expand_arguments(char *command)
816 816
817 /* We need a clean copy, so strsep can mess up the copy while 817 /* We need a clean copy, so strsep can mess up the copy while
818 * we write stuff into the original (in a minute) */ 818 * we write stuff into the original (in a minute) */
819 cmd = cmd_copy = xstrdup(command); 819 cmd = cmd_copy = bb_xstrdup(command);
820 *command = '\0'; 820 *command = '\0';
821 for (ix = 0, tmpcmd = cmd; 821 for (ix = 0, tmpcmd = cmd;
822 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) { 822 (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
@@ -829,13 +829,13 @@ static int expand_arguments(char *command)
829 if (retval == GLOB_NOSPACE) { 829 if (retval == GLOB_NOSPACE) {
830 /* Mem may have been allocated... */ 830 /* Mem may have been allocated... */
831 globfree (&expand_result); 831 globfree (&expand_result);
832 error_msg(out_of_space); 832 bb_error_msg(out_of_space);
833 return FALSE; 833 return FALSE;
834 } else if (retval != 0) { 834 } else if (retval != 0) {
835 /* Some other error. GLOB_NOMATCH shouldn't 835 /* Some other error. GLOB_NOMATCH shouldn't
836 * happen because of the GLOB_NOCHECK flag in 836 * happen because of the GLOB_NOCHECK flag in
837 * the glob call. */ 837 * the glob call. */
838 error_msg("syntax error"); 838 bb_error_msg("syntax error");
839 return FALSE; 839 return FALSE;
840 } else { 840 } else {
841 /* Convert from char** (one word per string) to a simple char*, 841 /* Convert from char** (one word per string) to a simple char*,
@@ -843,7 +843,7 @@ static int expand_arguments(char *command)
843 for (i=0; i < expand_result.gl_pathc; i++) { 843 for (i=0; i < expand_result.gl_pathc; i++) {
844 length=strlen(expand_result.gl_pathv[i]); 844 length=strlen(expand_result.gl_pathv[i]);
845 if (total_length+length+1 >= BUFSIZ) { 845 if (total_length+length+1 >= BUFSIZ) {
846 error_msg(out_of_space); 846 bb_error_msg(out_of_space);
847 return FALSE; 847 return FALSE;
848 } 848 }
849 strcat(command+total_length, " "); 849 strcat(command+total_length, " ");
@@ -930,7 +930,7 @@ static int expand_arguments(char *command)
930 int subst_len = strlen(var); 930 int subst_len = strlen(var);
931 int trail_len = strlen(src); 931 int trail_len = strlen(src);
932 if (dst+subst_len+trail_len >= command+BUFSIZ) { 932 if (dst+subst_len+trail_len >= command+BUFSIZ) {
933 error_msg(out_of_space); 933 bb_error_msg(out_of_space);
934 return FALSE; 934 return FALSE;
935 } 935 }
936 /* Move stuff to the end of the string to accommodate 936 /* Move stuff to the end of the string to accommodate
@@ -1006,7 +1006,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg)
1006 if (*src == '\\') { 1006 if (*src == '\\') {
1007 src++; 1007 src++;
1008 if (!*src) { 1008 if (!*src) {
1009 error_msg("character expected after \\"); 1009 bb_error_msg("character expected after \\");
1010 free_job(job); 1010 free_job(job);
1011 return 1; 1011 return 1;
1012 } 1012 }
@@ -1090,7 +1090,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg)
1090 chptr++; 1090 chptr++;
1091 1091
1092 if (!*chptr) { 1092 if (!*chptr) {
1093 error_msg("file name expected after %c", *(src-1)); 1093 bb_error_msg("file name expected after %c", *(src-1));
1094 free_job(job); 1094 free_job(job);
1095 job->num_progs=0; 1095 job->num_progs=0;
1096 return 1; 1096 return 1;
@@ -1109,7 +1109,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg)
1109 if (*prog->argv[argc_l] || saw_quote) 1109 if (*prog->argv[argc_l] || saw_quote)
1110 argc_l++; 1110 argc_l++;
1111 if (!argc_l) { 1111 if (!argc_l) {
1112 error_msg("empty command in pipe"); 1112 bb_error_msg("empty command in pipe");
1113 free_job(job); 1113 free_job(job);
1114 job->num_progs=0; 1114 job->num_progs=0;
1115 return 1; 1115 return 1;
@@ -1136,7 +1136,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg)
1136 src++; 1136 src++;
1137 1137
1138 if (!*src) { 1138 if (!*src) {
1139 error_msg("empty command in pipe"); 1139 bb_error_msg("empty command in pipe");
1140 free_job(job); 1140 free_job(job);
1141 job->num_progs=0; 1141 job->num_progs=0;
1142 return 1; 1142 return 1;
@@ -1155,7 +1155,7 @@ static int parse_command(char **command_ptr, struct job *job, int *inbg)
1155 case '\\': 1155 case '\\':
1156 src++; 1156 src++;
1157 if (!*src) { 1157 if (!*src) {
1158 error_msg("character expected after \\"); 1158 bb_error_msg("character expected after \\");
1159 free_job(job); 1159 free_job(job);
1160 return 1; 1160 return 1;
1161 } 1161 }
@@ -1217,7 +1217,7 @@ static int pseudo_exec(struct child_prog *child)
1217 /* Check if the command matches any of the forking builtins. */ 1217 /* Check if the command matches any of the forking builtins. */
1218 for (x = bltins_forking; x->cmd; x++) { 1218 for (x = bltins_forking; x->cmd; x++) {
1219 if (strcmp(child->argv[0], x->cmd) == 0) { 1219 if (strcmp(child->argv[0], x->cmd) == 0) {
1220 applet_name=x->cmd; 1220 bb_applet_name=x->cmd;
1221 _exit (x->function(child)); 1221 _exit (x->function(child));
1222 } 1222 }
1223 } 1223 }
@@ -1225,7 +1225,7 @@ static int pseudo_exec(struct child_prog *child)
1225 /* Check if the command matches any busybox internal 1225 /* Check if the command matches any busybox internal
1226 * commands ("applets") here. Following discussions from 1226 * commands ("applets") here. Following discussions from
1227 * November 2000 on busybox@busybox.net, don't use 1227 * November 2000 on busybox@busybox.net, don't use
1228 * get_last_path_component(). This way explicit (with 1228 * bb_get_last_path_component(). This way explicit (with
1229 * slashes) filenames will never be interpreted as an 1229 * slashes) filenames will never be interpreted as an
1230 * applet, just like with builtins. This way the user can 1230 * applet, just like with builtins. This way the user can
1231 * override an applet with an explicit filename reference. 1231 * override an applet with an explicit filename reference.
@@ -1241,7 +1241,7 @@ static int pseudo_exec(struct child_prog *child)
1241 * /bin/cat exists on the filesystem and is _not_ busybox. 1241 * /bin/cat exists on the filesystem and is _not_ busybox.
1242 * Some systems want this, others do not. Choose wisely. :-) 1242 * Some systems want this, others do not. Choose wisely. :-)
1243 */ 1243 */
1244 name = get_last_path_component(name); 1244 name = bb_get_last_path_component(name);
1245#endif 1245#endif
1246 1246
1247 { 1247 {
@@ -1255,7 +1255,7 @@ static int pseudo_exec(struct child_prog *child)
1255 1255
1256 execvp(child->argv[0], child->argv); 1256 execvp(child->argv[0], child->argv);
1257 1257
1258 /* Do not use perror_msg_and_die() here, since we must not 1258 /* Do not use bb_perror_msg_and_die() here, since we must not
1259 * call exit() but should call _exit() instead */ 1259 * call exit() but should call _exit() instead */
1260 fprintf(stderr, "%s: %m\n", child->argv[0]); 1260 fprintf(stderr, "%s: %m\n", child->argv[0]);
1261 _exit(EXIT_FAILURE); 1261 _exit(EXIT_FAILURE);
@@ -1299,7 +1299,7 @@ static void insert_job(struct job *newjob, int inbg)
1299 /* move the new process group into the foreground */ 1299 /* move the new process group into the foreground */
1300 /* suppress messages when run from /linuxrc mag@sysgo.de */ 1300 /* suppress messages when run from /linuxrc mag@sysgo.de */
1301 if (tcsetpgrp(shell_terminal, newjob->pgrp) && errno != ENOTTY) 1301 if (tcsetpgrp(shell_terminal, newjob->pgrp) && errno != ENOTTY)
1302 perror_msg("tcsetpgrp"); 1302 bb_perror_msg("tcsetpgrp");
1303 } 1303 }
1304} 1304}
1305 1305
@@ -1317,7 +1317,7 @@ static int run_command(struct job *newjob, int inbg, int outpipe[2])
1317 child = & (newjob->progs[i]); 1317 child = & (newjob->progs[i]);
1318 1318
1319 if ((i + 1) < newjob->num_progs) { 1319 if ((i + 1) < newjob->num_progs) {
1320 if (pipe(pipefds)<0) perror_msg_and_die("pipe"); 1320 if (pipe(pipefds)<0) bb_perror_msg_and_die("pipe");
1321 nextout = pipefds[1]; 1321 nextout = pipefds[1];
1322 } else { 1322 } else {
1323 if (outpipe[1]!=-1) { 1323 if (outpipe[1]!=-1) {
@@ -1464,7 +1464,7 @@ static int busy_loop(FILE * input)
1464 1464
1465 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) { 1465 if (waitpid(job_list.fg->progs[i].pid, &status, WUNTRACED)<0) {
1466 if (errno != ECHILD) { 1466 if (errno != ECHILD) {
1467 perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid); 1467 bb_perror_msg_and_die("waitpid(%d)",job_list.fg->progs[i].pid);
1468 } 1468 }
1469 } 1469 }
1470 1470
@@ -1496,7 +1496,7 @@ static int busy_loop(FILE * input)
1496 /* move the shell to the foreground */ 1496 /* move the shell to the foreground */
1497 /* suppress messages when run from /linuxrc mag@sysgo.de */ 1497 /* suppress messages when run from /linuxrc mag@sysgo.de */
1498 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY) 1498 if (tcsetpgrp(shell_terminal, getpgrp()) && errno != ENOTTY)
1499 perror_msg("tcsetpgrp"); 1499 bb_perror_msg("tcsetpgrp");
1500 } 1500 }
1501 } 1501 }
1502 } 1502 }
@@ -1504,7 +1504,7 @@ static int busy_loop(FILE * input)
1504 1504
1505 /* return controlling TTY back to parent process group before exiting */ 1505 /* return controlling TTY back to parent process group before exiting */
1506 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY) 1506 if (tcsetpgrp(shell_terminal, parent_pgrp) && errno != ENOTTY)
1507 perror_msg("tcsetpgrp"); 1507 bb_perror_msg("tcsetpgrp");
1508 1508
1509 /* return exit status if called with "-c" */ 1509 /* return exit status if called with "-c" */
1510 if (input == NULL && WIFEXITED(status)) 1510 if (input == NULL && WIFEXITED(status))
@@ -1517,7 +1517,7 @@ static int busy_loop(FILE * input)
1517#ifdef CONFIG_FEATURE_CLEAN_UP 1517#ifdef CONFIG_FEATURE_CLEAN_UP
1518void free_memory(void) 1518void free_memory(void)
1519{ 1519{
1520 if (cwd && cwd!=unknown) { 1520 if (cwd && cwd!=bb_msg_unknown) {
1521 free((char*)cwd); 1521 free((char*)cwd);
1522 } 1522 }
1523 if (local_pending_command) 1523 if (local_pending_command)
@@ -1594,8 +1594,8 @@ int lash_main(int argc_l, char **argv_l)
1594 case 'c': 1594 case 'c':
1595 input = NULL; 1595 input = NULL;
1596 if (local_pending_command != 0) 1596 if (local_pending_command != 0)
1597 error_msg_and_die("multiple -c arguments"); 1597 bb_error_msg_and_die("multiple -c arguments");
1598 local_pending_command = xstrdup(argv[optind]); 1598 local_pending_command = bb_xstrdup(argv[optind]);
1599 optind++; 1599 optind++;
1600 argv = argv+optind; 1600 argv = argv+optind;
1601 break; 1601 break;
@@ -1603,7 +1603,7 @@ int lash_main(int argc_l, char **argv_l)
1603 interactive = TRUE; 1603 interactive = TRUE;
1604 break; 1604 break;
1605 default: 1605 default:
1606 show_usage(); 1606 bb_show_usage();
1607 } 1607 }
1608 } 1608 }
1609 /* A shell is interactive if the `-i' flag was given, or if all of 1609 /* A shell is interactive if the `-i' flag was given, or if all of
@@ -1627,14 +1627,14 @@ int lash_main(int argc_l, char **argv_l)
1627#endif 1627#endif
1628 } else if (local_pending_command==NULL) { 1628 } else if (local_pending_command==NULL) {
1629 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]); 1629 //printf( "optind=%d argv[optind]='%s'\n", optind, argv[optind]);
1630 input = xfopen(argv[optind], "r"); 1630 input = bb_xfopen(argv[optind], "r");
1631 mark_open(fileno(input)); /* be lazy, never mark this closed */ 1631 mark_open(fileno(input)); /* be lazy, never mark this closed */
1632 } 1632 }
1633 1633
1634 /* initialize the cwd -- this is never freed...*/ 1634 /* initialize the cwd -- this is never freed...*/
1635 cwd = xgetcwd(0); 1635 cwd = xgetcwd(0);
1636 if (!cwd) 1636 if (!cwd)
1637 cwd = unknown; 1637 cwd = bb_msg_unknown;
1638 1638
1639#ifdef CONFIG_FEATURE_CLEAN_UP 1639#ifdef CONFIG_FEATURE_CLEAN_UP
1640 atexit(free_memory); 1640 atexit(free_memory);
diff --git a/shell/msh.c b/shell/msh.c
index 53f643de1..aad6bbf35 100644
--- a/shell/msh.c
+++ b/shell/msh.c
@@ -2838,7 +2838,7 @@ char *c, **v, **envp;
2838#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL 2838#ifdef CONFIG_FEATURE_SH_STANDALONE_SHELL
2839 char *name = c; 2839 char *name = c;
2840#ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN 2840#ifdef CONFIG_FEATURE_SH_APPLETS_ALWAYS_WIN
2841 name = get_last_path_component(name); 2841 name = bb_get_last_path_component(name);
2842#endif 2842#endif
2843 optind = 1; 2843 optind = 1;
2844 if (find_applet_by_name(name)) { 2844 if (find_applet_by_name(name)) {
@@ -2876,7 +2876,7 @@ char *c, **v, **envp;
2876 return("no Shell"); 2876 return("no Shell");
2877 2877
2878 case ENOMEM: 2878 case ENOMEM:
2879 return((char*)memory_exhausted); 2879 return((char*)bb_msg_memory_exhausted);
2880 2880
2881 case E2BIG: 2881 case E2BIG:
2882 return("argument list too long"); 2882 return("argument list too long");
@@ -3883,7 +3883,7 @@ int quoted;
3883 ; 3883 ;
3884 if (i < 0) { 3884 if (i < 0) {
3885 closepipe(pf); 3885 closepipe(pf);
3886 err((char*)memory_exhausted); 3886 err((char*)bb_msg_memory_exhausted);
3887 return(0); 3887 return(0);
3888 } 3888 }
3889 if (i != 0) { 3889 if (i != 0) {
diff --git a/sysklogd/klogd.c b/sysklogd/klogd.c
index 4132eb68e..b76a52ff7 100644
--- a/sysklogd/klogd.c
+++ b/sysklogd/klogd.c
@@ -136,12 +136,12 @@ extern int klogd_main(int argc, char **argv)
136 switch (opt) { 136 switch (opt) {
137 case 'c': 137 case 'c':
138 if ((optarg == NULL) || (optarg[1] != '\0')) { 138 if ((optarg == NULL) || (optarg[1] != '\0')) {
139 show_usage(); 139 bb_show_usage();
140 } 140 }
141 /* Valid levels are between 1 and 8 */ 141 /* Valid levels are between 1 and 8 */
142 console_log_level = *optarg - '1'; 142 console_log_level = *optarg - '1';
143 if (console_log_level > 7) { 143 if (console_log_level > 7) {
144 show_usage(); 144 bb_show_usage();
145 } 145 }
146 console_log_level++; 146 console_log_level++;
147 147
@@ -150,16 +150,16 @@ extern int klogd_main(int argc, char **argv)
150 doFork = FALSE; 150 doFork = FALSE;
151 break; 151 break;
152 default: 152 default:
153 show_usage(); 153 bb_show_usage();
154 } 154 }
155 } 155 }
156 156
157 if (doFork) { 157 if (doFork) {
158#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__) 158#if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__)
159 if (daemon(0, 1) < 0) 159 if (daemon(0, 1) < 0)
160 perror_msg_and_die("daemon"); 160 bb_perror_msg_and_die("daemon");
161#else 161#else
162 error_msg_and_die("daemon not supported"); 162 bb_error_msg_and_die("daemon not supported");
163#endif 163#endif
164 } 164 }
165 doKlogd(console_log_level); 165 doKlogd(console_log_level);
diff --git a/sysklogd/logger.c b/sysklogd/logger.c
index bb63975e7..1b55bf589 100644
--- a/sysklogd/logger.c
+++ b/sysklogd/logger.c
@@ -89,14 +89,14 @@ static int pencode(char *s)
89 *s = '\0'; 89 *s = '\0';
90 fac = decode(save, facilitynames); 90 fac = decode(save, facilitynames);
91 if (fac < 0) 91 if (fac < 0)
92 error_msg_and_die("unknown facility name: %s", save); 92 bb_error_msg_and_die("unknown facility name: %s", save);
93 *s++ = '.'; 93 *s++ = '.';
94 } else { 94 } else {
95 s = save; 95 s = save;
96 } 96 }
97 lev = decode(s, prioritynames); 97 lev = decode(s, prioritynames);
98 if (lev < 0) 98 if (lev < 0)
99 error_msg_and_die("unknown priority name: %s", save); 99 bb_error_msg_and_die("unknown priority name: %s", save);
100 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK)); 100 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
101} 101}
102 102
@@ -124,7 +124,7 @@ extern int logger_main(int argc, char **argv)
124 safe_strncpy(name, optarg, sizeof(name)); 124 safe_strncpy(name, optarg, sizeof(name));
125 break; 125 break;
126 default: 126 default:
127 show_usage(); 127 bb_show_usage();
128 } 128 }
129 } 129 }
130 130
diff --git a/sysklogd/logread.c b/sysklogd/logread.c
index ecdf2f579..ae8dbae3a 100644
--- a/sysklogd/logread.c
+++ b/sysklogd/logread.c
@@ -87,7 +87,7 @@ extern int logread_main(int argc, char **argv)
87 87
88 /* no options, no getopt */ 88 /* no options, no getopt */
89 if (argc > 1) 89 if (argc > 1)
90 show_usage(); 90 bb_show_usage();
91 91
92 // handle intrrupt signal 92 // handle intrrupt signal
93 if (setjmp(jmp_env)) goto output_end; 93 if (setjmp(jmp_env)) goto output_end;
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 1c58a64f8..b912f5f8f 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -123,7 +123,7 @@ static int circular_logging = FALSE;
123static inline void sem_up(int semid) 123static inline void sem_up(int semid)
124{ 124{
125 if (semop(semid, SMwup, 1) == -1) { 125 if (semop(semid, SMwup, 1) == -1) {
126 perror_msg_and_die("semop[SMwup]"); 126 bb_perror_msg_and_die("semop[SMwup]");
127 } 127 }
128} 128}
129 129
@@ -133,7 +133,7 @@ static inline void sem_up(int semid)
133static inline void sem_down(int semid) 133static inline void sem_down(int semid)
134{ 134{
135 if (semop(semid, SMwdn, 3) == -1) { 135 if (semop(semid, SMwdn, 3) == -1) {
136 perror_msg_and_die("semop[SMwdn]"); 136 bb_perror_msg_and_die("semop[SMwdn]");
137 } 137 }
138} 138}
139 139
@@ -157,11 +157,11 @@ void ipcsyslog_init(void)
157{ 157{
158 if (buf == NULL) { 158 if (buf == NULL) {
159 if ((shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023)) == -1) { 159 if ((shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023)) == -1) {
160 perror_msg_and_die("shmget"); 160 bb_perror_msg_and_die("shmget");
161 } 161 }
162 162
163 if ((buf = shmat(shmid, NULL, 0)) == NULL) { 163 if ((buf = shmat(shmid, NULL, 0)) == NULL) {
164 perror_msg_and_die("shmat"); 164 bb_perror_msg_and_die("shmat");
165 } 165 }
166 166
167 buf->size = data_size; 167 buf->size = data_size;
@@ -171,10 +171,10 @@ void ipcsyslog_init(void)
171 if ((s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023)) == -1) { 171 if ((s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023)) == -1) {
172 if (errno == EEXIST) { 172 if (errno == EEXIST) {
173 if ((s_semid = semget(KEY_ID, 2, 0)) == -1) { 173 if ((s_semid = semget(KEY_ID, 2, 0)) == -1) {
174 perror_msg_and_die("semget"); 174 bb_perror_msg_and_die("semget");
175 } 175 }
176 } else { 176 } else {
177 perror_msg_and_die("semget"); 177 bb_perror_msg_and_die("semget");
178 } 178 }
179 } 179 }
180 } else { 180 } else {
@@ -471,7 +471,7 @@ static void init_RemoteLog(void)
471 remotefd = socket(AF_INET, SOCK_DGRAM, 0); 471 remotefd = socket(AF_INET, SOCK_DGRAM, 0);
472 472
473 if (remotefd < 0) { 473 if (remotefd < 0) {
474 error_msg_and_die("cannot create socket"); 474 bb_error_msg_and_die("cannot create socket");
475 } 475 }
476 476
477 hostinfo = xgethostbyname(RemoteHost); 477 hostinfo = xgethostbyname(RemoteHost);
@@ -484,7 +484,7 @@ static void init_RemoteLog(void)
484 * for future operations 484 * for future operations
485 */ 485 */
486 if (0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))) { 486 if (0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))) {
487 error_msg_and_die("cannot connect to remote host %s:%d", RemoteHost, 487 bb_error_msg_and_die("cannot connect to remote host %s:%d", RemoteHost,
488 RemotePort); 488 RemotePort);
489 } 489 }
490 490
@@ -521,17 +521,17 @@ static void doSyslogd(void)
521 sunx.sun_family = AF_UNIX; 521 sunx.sun_family = AF_UNIX;
522 strncpy(sunx.sun_path, lfile, sizeof(sunx.sun_path)); 522 strncpy(sunx.sun_path, lfile, sizeof(sunx.sun_path));
523 if ((sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { 523 if ((sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
524 perror_msg_and_die("Couldn't get file descriptor for socket " 524 bb_perror_msg_and_die("Couldn't get file descriptor for socket "
525 _PATH_LOG); 525 _PATH_LOG);
526 } 526 }
527 527
528 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path); 528 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
529 if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0) { 529 if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0) {
530 perror_msg_and_die("Could not connect to socket " _PATH_LOG); 530 bb_perror_msg_and_die("Could not connect to socket " _PATH_LOG);
531 } 531 }
532 532
533 if (chmod(lfile, 0666) < 0) { 533 if (chmod(lfile, 0666) < 0) {
534 perror_msg_and_die("Could not set permission on " _PATH_LOG); 534 bb_perror_msg_and_die("Could not set permission on " _PATH_LOG);
535 } 535 }
536#ifdef CONFIG_FEATURE_IPC_SYSLOG 536#ifdef CONFIG_FEATURE_IPC_SYSLOG
537 if (circular_logging == TRUE) { 537 if (circular_logging == TRUE) {
@@ -557,7 +557,7 @@ static void doSyslogd(void)
557 /* alarm may have happened. */ 557 /* alarm may have happened. */
558 continue; 558 continue;
559 } 559 }
560 perror_msg_and_die("select error"); 560 bb_perror_msg_and_die("select error");
561 } 561 }
562 562
563 if (FD_ISSET(sock_fd, &fds)) { 563 if (FD_ISSET(sock_fd, &fds)) {
@@ -569,7 +569,7 @@ static void doSyslogd(void)
569 if ((i = recv(sock_fd, tmpbuf, BUFSIZ, 0)) > 0) { 569 if ((i = recv(sock_fd, tmpbuf, BUFSIZ, 0)) > 0) {
570 serveConnection(tmpbuf, i); 570 serveConnection(tmpbuf, i);
571 } else { 571 } else {
572 perror_msg_and_die("UNIX socket error"); 572 bb_perror_msg_and_die("UNIX socket error");
573 } 573 }
574 RELEASE_CONFIG_BUFFER(tmpbuf); 574 RELEASE_CONFIG_BUFFER(tmpbuf);
575 } /* FD_ISSET() */ 575 } /* FD_ISSET() */
@@ -598,11 +598,11 @@ extern int syslogd_main(int argc, char **argv)
598 break; 598 break;
599#endif 599#endif
600 case 'O': 600 case 'O':
601 logFilePath = xstrdup(optarg); 601 logFilePath = bb_xstrdup(optarg);
602 break; 602 break;
603#ifdef CONFIG_FEATURE_REMOTE_LOG 603#ifdef CONFIG_FEATURE_REMOTE_LOG
604 case 'R': 604 case 'R':
605 RemoteHost = xstrdup(optarg); 605 RemoteHost = bb_xstrdup(optarg);
606 if ((p = strchr(RemoteHost, ':'))) { 606 if ((p = strchr(RemoteHost, ':'))) {
607 RemotePort = atoi(p + 1); 607 RemotePort = atoi(p + 1);
608 *p = '\0'; 608 *p = '\0';
@@ -619,7 +619,7 @@ extern int syslogd_main(int argc, char **argv)
619 break; 619 break;
620#endif 620#endif
621 default: 621 default:
622 show_usage(); 622 bb_show_usage();
623 } 623 }
624 } 624 }
625 625
@@ -640,7 +640,7 @@ extern int syslogd_main(int argc, char **argv)
640 640
641#if ! defined(__uClinux__) 641#if ! defined(__uClinux__)
642 if ((doFork == TRUE) && (daemon(0, 1) < 0)) { 642 if ((doFork == TRUE) && (daemon(0, 1) < 0)) {
643 perror_msg_and_die("daemon"); 643 bb_perror_msg_and_die("daemon");
644 } 644 }
645#endif 645#endif
646 doSyslogd(); 646 doSyslogd();
diff --git a/util-linux/dmesg.c b/util-linux/dmesg.c
index aa8eb95c9..fb1639ace 100644
--- a/util-linux/dmesg.c
+++ b/util-linux/dmesg.c
@@ -42,7 +42,7 @@ int dmesg_main(int argc, char **argv)
42 int lastc; 42 int lastc;
43 int cmd = 3; 43 int cmd = 3;
44 44
45 while ((i = getopt(argc, argv, "cn:s:")) != EOF) { 45 while ((i = getopt(argc, argv, "cn:s:")) > 0) {
46 switch (i) { 46 switch (i) {
47 case 'c': 47 case 'c':
48 cmd = 4; 48 cmd = 4;
@@ -58,12 +58,12 @@ int dmesg_main(int argc, char **argv)
58 bufsize = bb_xgetlarg(optarg, 10, 4096, 512*1024); 58 bufsize = bb_xgetlarg(optarg, 10, 4096, 512*1024);
59 break; 59 break;
60 default: 60 default:
61 show_usage(); 61 bb_show_usage();
62 } 62 }
63 } 63 }
64 64
65 if (optind < argc) { 65 if (optind < argc) {
66 show_usage(); 66 bb_show_usage();
67 } 67 }
68 68
69 if (cmd == 8) { 69 if (cmd == 8) {
@@ -98,5 +98,5 @@ all_done:
98#endif 98#endif
99 return EXIT_SUCCESS; 99 return EXIT_SUCCESS;
100die_the_death: 100die_the_death:
101 perror_msg_and_die(NULL); 101 bb_perror_nomsg_and_die();
102} 102}
diff --git a/util-linux/fbset.c b/util-linux/fbset.c
index 9423abc09..afd9e64be 100644
--- a/util-linux/fbset.c
+++ b/util-linux/fbset.c
@@ -194,7 +194,7 @@ static int readmode(struct fb_var_screeninfo *base, const char *fn,
194 char buf[256]; 194 char buf[256];
195 char *p = buf; 195 char *p = buf;
196 196
197 f = xfopen(fn, "r"); 197 f = bb_xfopen(fn, "r");
198 while (!feof(f)) { 198 while (!feof(f)) {
199 fgets(buf, sizeof(buf), f); 199 fgets(buf, sizeof(buf), f);
200 if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) { 200 if ((p = strstr(buf, "mode ")) || (p = strstr(buf, "mode\t"))) {
@@ -279,7 +279,7 @@ static int readmode(struct fb_var_screeninfo *base, const char *fn,
279 } 279 }
280 } 280 }
281#else 281#else
282 error_msg( "mode reading not compiled in"); 282 bb_error_msg( "mode reading not compiled in");
283#endif 283#endif
284 return 0; 284 return 0;
285} 285}
@@ -350,7 +350,7 @@ extern int fbset_main(int argc, char **argv)
350 for (i = 0; g_cmdoptions[i].name; i++) { 350 for (i = 0; g_cmdoptions[i].name; i++) {
351 if (!strcmp(thisarg, g_cmdoptions[i].name)) { 351 if (!strcmp(thisarg, g_cmdoptions[i].name)) {
352 if (argc - 1 < g_cmdoptions[i].param_count) 352 if (argc - 1 < g_cmdoptions[i].param_count)
353 show_usage(); 353 bb_show_usage();
354 switch (g_cmdoptions[i].code) { 354 switch (g_cmdoptions[i].code) {
355 case CMD_FB: 355 case CMD_FB:
356 fbdev = argv[1]; 356 fbdev = argv[1];
@@ -399,18 +399,18 @@ extern int fbset_main(int argc, char **argv)
399 mode = *argv; 399 mode = *argv;
400 g_options |= OPT_READMODE; 400 g_options |= OPT_READMODE;
401 } else { 401 } else {
402 show_usage(); 402 bb_show_usage();
403 } 403 }
404 } 404 }
405 } 405 }
406 406
407 if ((fh = open(fbdev, O_RDONLY)) < 0) 407 if ((fh = open(fbdev, O_RDONLY)) < 0)
408 perror_msg_and_die("fbset(open)"); 408 bb_perror_msg_and_die("fbset(open)");
409 if (ioctl(fh, FBIOGET_VSCREENINFO, &var)) 409 if (ioctl(fh, FBIOGET_VSCREENINFO, &var))
410 perror_msg_and_die("fbset(ioctl)"); 410 bb_perror_msg_and_die("fbset(ioctl)");
411 if (g_options & OPT_READMODE) { 411 if (g_options & OPT_READMODE) {
412 if (!readmode(&var, modefile, mode)) { 412 if (!readmode(&var, modefile, mode)) {
413 error_msg("Unknown video mode `%s'", mode); 413 bb_error_msg("Unknown video mode `%s'", mode);
414 return EXIT_FAILURE; 414 return EXIT_FAILURE;
415 } 415 }
416 } 416 }
@@ -418,7 +418,7 @@ extern int fbset_main(int argc, char **argv)
418 setmode(&var, &varset); 418 setmode(&var, &varset);
419 if (g_options & OPT_CHANGE) 419 if (g_options & OPT_CHANGE)
420 if (ioctl(fh, FBIOPUT_VSCREENINFO, &var)) 420 if (ioctl(fh, FBIOPUT_VSCREENINFO, &var))
421 perror_msg_and_die("fbset(ioctl)"); 421 bb_perror_msg_and_die("fbset(ioctl)");
422 showmode(&var); 422 showmode(&var);
423 /* Don't close the file, as exiting will take care of that */ 423 /* Don't close the file, as exiting will take care of that */
424 /* close(fh); */ 424 /* close(fh); */
diff --git a/util-linux/fdflush.c b/util-linux/fdflush.c
index 0756ddfbf..c3fcf3325 100644
--- a/util-linux/fdflush.c
+++ b/util-linux/fdflush.c
@@ -25,6 +25,7 @@
25#include <sys/ioctl.h> 25#include <sys/ioctl.h>
26#include <fcntl.h> 26#include <fcntl.h>
27#include <stdlib.h> 27#include <stdlib.h>
28#include <unistd.h>
28#include "busybox.h" 29#include "busybox.h"
29 30
30/* From <linux/fd.h> */ 31/* From <linux/fd.h> */
@@ -35,22 +36,19 @@ extern int fdflush_main(int argc, char **argv)
35 int fd, result; 36 int fd, result;
36 37
37 if (argc <= 1) 38 if (argc <= 1)
38 show_usage(); 39 bb_show_usage();
39 if ((fd = open(*(++argv), 0)) < 0) 40
40 goto die_the_death; 41 fd = bb_xopen(argv[1], 0);
41 42
42 result = ioctl(fd, FDFLUSH, 0); 43 result = ioctl(fd, FDFLUSH, 0);
43#ifdef CONFIG_FEATURE_CLEAN_UP 44#ifdef CONFIG_FEATURE_CLEAN_UP
44 close(fd); 45 close(fd);
45#endif 46#endif
46 if (result) { 47 if (result) {
47 goto die_the_death; 48 bb_perror_nomsg_and_die();
48 } 49 }
49 50
50 /* Don't bother closing. Exit does 51 /* Don't bother closing. Exit does
51 * that, so we can save a few bytes */ 52 * that, so we can save a few bytes */
52 return EXIT_SUCCESS; 53 return EXIT_SUCCESS;
53
54die_the_death:
55 perror_msg_and_die(NULL);
56} 54}
diff --git a/util-linux/fdisk.c b/util-linux/fdisk.c
index 696e64c7d..71fbe074e 100644
--- a/util-linux/fdisk.c
+++ b/util-linux/fdisk.c
@@ -5655,7 +5655,7 @@ tryprocpt(void) {
5655 char line[100], ptname[100], devname[120], *s; 5655 char line[100], ptname[100], devname[120], *s;
5656 int ma, mi, sz; 5656 int ma, mi, sz;
5657 5657
5658 procpt = wfopen(PROC_PARTITIONS, "r"); 5658 procpt = bb_wfopen(PROC_PARTITIONS, "r");
5659 5659
5660 while (fgets(line, sizeof(line), procpt)) { 5660 while (fgets(line, sizeof(line), procpt)) {
5661 if (sscanf (line, " %d %d %d %[^\n ]", 5661 if (sscanf (line, " %d %d %d %[^\n ]",
@@ -5711,7 +5711,7 @@ int fdisk_main(int argc, char **argv) {
5711 sector_size = atoi(optarg); 5711 sector_size = atoi(optarg);
5712 if (sector_size != 512 && sector_size != 1024 && 5712 if (sector_size != 512 && sector_size != 1024 &&
5713 sector_size != 2048) 5713 sector_size != 2048)
5714 show_usage(); 5714 bb_show_usage();
5715 sector_offset = 2; 5715 sector_offset = 2;
5716 user_set_sector_size = 1; 5716 user_set_sector_size = 1;
5717 break; 5717 break;
@@ -5746,7 +5746,7 @@ int fdisk_main(int argc, char **argv) {
5746 printf("fdisk v" UTIL_LINUX_VERSION "\n"); 5746 printf("fdisk v" UTIL_LINUX_VERSION "\n");
5747 return 0; 5747 return 0;
5748 default: 5748 default:
5749 show_usage(); 5749 bb_show_usage();
5750 } 5750 }
5751 } 5751 }
5752 5752
@@ -5794,7 +5794,7 @@ int fdisk_main(int argc, char **argv) {
5794 5794
5795 opts = argc - optind; 5795 opts = argc - optind;
5796 if (opts <= 0) 5796 if (opts <= 0)
5797 show_usage(); 5797 bb_show_usage();
5798 5798
5799 for (j = optind; j < argc; j++) { 5799 for (j = optind; j < argc; j++) {
5800 disk_device = argv[j]; 5800 disk_device = argv[j];
@@ -5816,7 +5816,7 @@ int fdisk_main(int argc, char **argv) {
5816 if (argc-optind == 1) 5816 if (argc-optind == 1)
5817 disk_device = argv[optind]; 5817 disk_device = argv[optind];
5818 else 5818 else
5819 show_usage(); 5819 bb_show_usage();
5820 5820
5821 get_boot(fdisk); 5821 get_boot(fdisk);
5822 5822
diff --git a/util-linux/freeramdisk.c b/util-linux/freeramdisk.c
index dd7700c06..9276a6c40 100644
--- a/util-linux/freeramdisk.c
+++ b/util-linux/freeramdisk.c
@@ -41,17 +41,17 @@ freeramdisk_main(int argc, char **argv)
41 FILE *f; 41 FILE *f;
42 42
43 if (argc != 2) { 43 if (argc != 2) {
44 show_usage(); 44 bb_show_usage();
45 } 45 }
46 46
47 f = xfopen(argv[1], "r+"); 47 f = bb_xfopen(argv[1], "r+");
48 48
49 result = ioctl(fileno(f), BLKFLSBUF); 49 result = ioctl(fileno(f), BLKFLSBUF);
50#ifdef CONFIG_FEATURE_CLEAN_UP 50#ifdef CONFIG_FEATURE_CLEAN_UP
51 fclose(f); 51 fclose(f);
52#endif 52#endif
53 if (result < 0) { 53 if (result < 0) {
54 perror_msg_and_die("failed ioctl on %s", argv[1]); 54 bb_perror_msg_and_die("failed ioctl on %s", argv[1]);
55 } 55 }
56 56
57 /* Don't bother closing. Exit does 57 /* Don't bother closing. Exit does
diff --git a/util-linux/fsck_minix.c b/util-linux/fsck_minix.c
index d332b6b0a..3a691476e 100644
--- a/util-linux/fsck_minix.c
+++ b/util-linux/fsck_minix.c
@@ -279,7 +279,7 @@ static void leave(int status)
279 279
280static void die(const char *str) 280static void die(const char *str)
281{ 281{
282 error_msg("%s", str); 282 bb_error_msg("%s", str);
283 leave(8); 283 leave(8);
284} 284}
285 285
@@ -1344,7 +1344,7 @@ extern int fsck_minix_main(int argc, char **argv)
1344 argv++; 1344 argv++;
1345 if (argv[0][0] != '-') { 1345 if (argv[0][0] != '-') {
1346 if (device_name) 1346 if (device_name)
1347 show_usage(); 1347 bb_show_usage();
1348 else 1348 else
1349 device_name = argv[0]; 1349 device_name = argv[0];
1350 } else 1350 } else
@@ -1374,11 +1374,11 @@ extern int fsck_minix_main(int argc, char **argv)
1374 force = 1; 1374 force = 1;
1375 break; 1375 break;
1376 default: 1376 default:
1377 show_usage(); 1377 bb_show_usage();
1378 } 1378 }
1379 } 1379 }
1380 if (!device_name) 1380 if (!device_name)
1381 show_usage(); 1381 bb_show_usage();
1382 check_mount(); /* trying to check a mounted filesystem? */ 1382 check_mount(); /* trying to check a mounted filesystem? */
1383 if (repair && !automatic) { 1383 if (repair && !automatic) {
1384 if (!isatty(0) || !isatty(1)) 1384 if (!isatty(0) || !isatty(1))
@@ -1399,7 +1399,7 @@ extern int fsck_minix_main(int argc, char **argv)
1399 * flags and whether or not the -f switch was specified on the 1399 * flags and whether or not the -f switch was specified on the
1400 * command line. 1400 * command line.
1401 */ 1401 */
1402 printf("%s, %s\n", applet_name, program_version); 1402 printf("%s, %s\n", bb_applet_name, program_version);
1403 if (!(Super.s_state & MINIX_ERROR_FS) && 1403 if (!(Super.s_state & MINIX_ERROR_FS) &&
1404 (Super.s_state & MINIX_VALID_FS) && !force) { 1404 (Super.s_state & MINIX_VALID_FS) && !force) {
1405 if (repair) 1405 if (repair)
diff --git a/util-linux/getopt.c b/util-linux/getopt.c
index e126d7b6e..25eeab69b 100644
--- a/util-linux/getopt.c
+++ b/util-linux/getopt.c
@@ -37,7 +37,7 @@
37 * <misiek@misiek.eu.org>) 37 * <misiek@misiek.eu.org>)
38 * Ported to Busybox - Alfred M. Szmidt <ams@trillian.itslinux.org> 38 * Ported to Busybox - Alfred M. Szmidt <ams@trillian.itslinux.org>
39 * Removed --version/-V and --help/-h in 39 * Removed --version/-V and --help/-h in
40 * Removed prase_error(), using error_msg() from Busybox instead 40 * Removed prase_error(), using bb_error_msg() from Busybox instead
41 * Replaced our_malloc with xmalloc and our_realloc with xrealloc 41 * Replaced our_malloc with xmalloc and our_realloc with xrealloc
42 * 42 *
43 */ 43 */
@@ -95,7 +95,7 @@ const char *normalize(const char *arg)
95 free(BUFFER); 95 free(BUFFER);
96 96
97 if (!quote) { /* Just copy arg */ 97 if (!quote) { /* Just copy arg */
98 BUFFER=xstrdup(arg); 98 BUFFER=bb_xstrdup(arg);
99 return BUFFER; 99 return BUFFER;
100 } 100 }
101 101
@@ -224,7 +224,7 @@ void add_longopt(const char *name,int has_arg)
224 long_options[long_options_nr-1].has_arg=has_arg; 224 long_options[long_options_nr-1].has_arg=has_arg;
225 long_options[long_options_nr-1].flag=NULL; 225 long_options[long_options_nr-1].flag=NULL;
226 long_options[long_options_nr-1].val=LONG_OPT; 226 long_options[long_options_nr-1].val=LONG_OPT;
227 long_options[long_options_nr-1].name=xstrdup(name); 227 long_options[long_options_nr-1].name=bb_xstrdup(name);
228 } 228 }
229 long_options_nr++; 229 long_options_nr++;
230} 230}
@@ -254,7 +254,7 @@ void add_long_options(char *options)
254 arg_opt=required_argument; 254 arg_opt=required_argument;
255 } 255 }
256 if (tlen == 0) 256 if (tlen == 0)
257 error_msg("empty long option after -l or --long argument"); 257 bb_error_msg("empty long option after -l or --long argument");
258 } 258 }
259 add_longopt(tokptr,arg_opt); 259 add_longopt(tokptr,arg_opt);
260 } 260 }
@@ -273,7 +273,7 @@ void set_shell(const char *new_shell)
273 else if (!strcmp(new_shell,"csh")) 273 else if (!strcmp(new_shell,"csh"))
274 shell=TCSH; 274 shell=TCSH;
275 else 275 else
276 error_msg("unknown shell after -s or --shell argument"); 276 bb_error_msg("unknown shell after -s or --shell argument");
277} 277}
278 278
279 279
@@ -322,7 +322,7 @@ int getopt_main(int argc, char *argv[])
322 printf(" --\n"); 322 printf(" --\n");
323 return 0; 323 return 0;
324 } else 324 } else
325 error_msg_and_die("missing optstring argument"); 325 bb_error_msg_and_die("missing optstring argument");
326 } 326 }
327 327
328 if (argv[1][0] != '-' || compatible) { 328 if (argv[1][0] != '-' || compatible) {
@@ -340,14 +340,14 @@ int getopt_main(int argc, char *argv[])
340 break; 340 break;
341 case 'o': 341 case 'o':
342 free(optstr); 342 free(optstr);
343 optstr=xstrdup(optarg); 343 optstr=bb_xstrdup(optarg);
344 break; 344 break;
345 case 'l': 345 case 'l':
346 add_long_options(optarg); 346 add_long_options(optarg);
347 break; 347 break;
348 case 'n': 348 case 'n':
349 free(name); 349 free(name);
350 name=xstrdup(optarg); 350 name=bb_xstrdup(optarg);
351 break; 351 break;
352 case 'q': 352 case 'q':
353 quiet_errors=1; 353 quiet_errors=1;
@@ -364,14 +364,14 @@ int getopt_main(int argc, char *argv[])
364 quote=0; 364 quote=0;
365 break; 365 break;
366 default: 366 default:
367 show_usage(); 367 bb_show_usage();
368 } 368 }
369 369
370 if (!optstr) { 370 if (!optstr) {
371 if (optind >= argc) 371 if (optind >= argc)
372 error_msg_and_die("missing optstring argument"); 372 bb_error_msg_and_die("missing optstring argument");
373 else { 373 else {
374 optstr=xstrdup(argv[optind]); 374 optstr=bb_xstrdup(argv[optind]);
375 optind++; 375 optind++;
376 } 376 }
377 } 377 }
diff --git a/util-linux/hexdump.c b/util-linux/hexdump.c
index 33648f918..1858b08d4 100644
--- a/util-linux/hexdump.c
+++ b/util-linux/hexdump.c
@@ -25,115 +25,92 @@
25#include <getopt.h> 25#include <getopt.h>
26#include <stdlib.h> 26#include <stdlib.h>
27#include <string.h> 27#include <string.h>
28#include "dump.h"
29#include "busybox.h" 28#include "busybox.h"
29#include "dump.h"
30 30
31extern off_t skip; /* bytes to skip */ 31static void bb_dump_addfile(char *name)
32
33extern FS *fshead; /* head of format strings */
34extern int blocksize; /* data block size */
35extern int length; /* max bytes to read */
36
37void addfile(char *name)
38{ 32{
39 register char *p; 33 register char *p;
40 FILE *fp; 34 FILE *fp;
41 int ch; 35 char *buf;
42 char buf[2048 + 1];
43 36
44 if (!(fp = fopen(name, "r"))) { 37 fp = bb_xfopen(name, "r");
45 error_msg_and_die("hexdump: can't read %s.\n", name); 38
46 } 39 while ((buf = bb_get_chomped_line_from_file(fp)) != NULL) {
47 while (fgets(buf, sizeof(buf), fp)) { 40 p = (char *) bb_skip_whitespace(buf);
48 if (!(p = index(buf, '\n'))) { 41
49 error_msg("hexdump: line too long.\n"); 42 if (*p && (*p != '#')) {
50 while ((ch = getchar()) != '\n' && ch != EOF); 43 bb_dump_add(p);
51 continue;
52 }
53 *p = '\0';
54 for (p = buf; *p && isspace(*p); ++p);
55 if (!*p || *p == '#') {
56 continue;
57 } 44 }
58 add(p); 45 free(buf);
59 } 46 }
60 (void)fclose(fp); 47 fclose(fp);
61} 48}
62 49
50static const char * const add_strings[] = {
51 "\"%07.7_ax \" 16/1 \"%03o \" \"\\n\"", /* b */
52 "\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\"", /* c */
53 "\"%07.7_ax \" 8/2 \" %05u \" \"\\n\"", /* d */
54 "\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"", /* o */
55 "\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"", /* x */
56};
57
58static const char add_first[] = "\"%07.7_Ax\n\"";
59
60static const char hexdump_opts[] = "bcdoxe:f:n:s:v";
61
62static const struct suffix_mult suffixes[] = {
63 {"b", 512 },
64 {"k", 1024 },
65 {"m", 1024*1024 },
66 {NULL, 0 }
67};
68
63int hexdump_main(int argc, char **argv) 69int hexdump_main(int argc, char **argv)
64{ 70{
65// register FS *tfs; 71// register FS *tfs;
66 char *p; 72 const char *p;
67 int ch; 73 int ch;
68 extern enum _vflag vflag;
69 vflag = FIRST;
70 length = -1;
71 74
72 while ((ch = getopt(argc, argv, "bcde:f:n:os:vx")) != EOF) { 75 bb_dump_vflag = FIRST;
73 switch (ch) { 76 bb_dump_length = -1;
74 case 'b': 77
75 add("\"%07.7_Ax\n\""); 78 while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
76 add("\"%07.7_ax \" 16/1 \"%03o \" \"\\n\""); 79 if ((p = strchr(hexdump_opts, ch)) != NULL) {
77 break; 80 if ((p - hexdump_opts) < 5) {
78 case 'c': 81 bb_dump_add(add_first);
79 add("\"%07.7_Ax\n\""); 82 bb_dump_add(add_strings[(int)(p - hexdump_opts)]);
80 add("\"%07.7_ax \" 16/1 \"%3_c \" \"\\n\""); 83 } else {
81 break; 84 /* Sae a little bit of space below by omitting the 'else's. */
82 case 'd': 85 if (ch == 'e') {
83 add("\"%07.7_Ax\n\""); 86 bb_dump_add(optarg);
84 add("\"%07.7_ax \" 8/2 \" %05u \" \"\\n\""); 87 } /* else */
85 break; 88 if (ch == 'f') {
86 case 'e': 89 bb_dump_addfile(optarg);
87 add(optarg); 90 } /* else */
88 break; 91 if (ch == 'n') {
89 case 'f': 92 bb_dump_length = bb_xgetularg10_bnd(optarg, 0, INT_MAX);
90 addfile(optarg); 93 } /* else */
91 break; 94 if (ch == 's') {
92 case 'n': 95 bb_dump_skip = bb_xgetularg_bnd_sfx(optarg, 10, 0, LONG_MAX, suffixes);
93 if ((length = atoi(optarg)) < 0) { 96 } /* else */
94 error_msg_and_die("hexdump: bad length value.\n"); 97 if (ch == 'v') {
95 } 98 bb_dump_vflag = ALL;
96 break; 99 }
97 case 'o':
98 add("\"%07.7_Ax\n\"");
99 add("\"%07.7_ax \" 8/2 \" %06o \" \"\\n\"");
100 break;
101 case 's':
102 if ((skip = strtol(optarg, &p, 0)) < 0) {
103 error_msg_and_die("hexdump: bad skip value.\n");
104 }
105 switch(*p) {
106 case 'b':
107 skip *= 512;
108 break;
109 case 'k':
110 skip *= 1024;
111 break;
112 case 'm':
113 skip *= 1048576;
114 break;
115 } 100 }
116 break; 101 } else {
117 case 'v': 102 bb_show_usage();
118 vflag = ALL;
119 break;
120 case 'x':
121 add("\"%07.7_Ax\n\"");
122 add("\"%07.7_ax \" 8/2 \" %04x \" \"\\n\"");
123 break;
124 case '?':
125 show_usage();
126 } 103 }
127 } 104 }
128 105
129 if (!fshead) { 106 if (!bb_dump_fshead) {
130 add("\"%07.7_Ax\n\""); 107 bb_dump_add(add_first);
131 add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\""); 108 bb_dump_add("\"%07.7_ax \" 8/2 \"%04x \" \"\\n\"");
132 } 109 }
133 110
134 argv += optind; 111 argv += optind;
135 112
136 return(dump(argv)); 113 return(bb_dump_dump(argv));
137} 114}
138/* 115/*
139 * Copyright (c) 1989 The Regents of the University of California. 116 * Copyright (c) 1989 The Regents of the University of California.
diff --git a/util-linux/hwclock.c b/util-linux/hwclock.c
index 815b60b21..2eee3cfdb 100644
--- a/util-linux/hwclock.c
+++ b/util-linux/hwclock.c
@@ -77,11 +77,11 @@ time_t read_rtc ( int utc )
77 77
78 if (( rtc = open ( "/dev/rtc", O_RDONLY )) < 0 ) { 78 if (( rtc = open ( "/dev/rtc", O_RDONLY )) < 0 ) {
79 if (( rtc = open ( "/dev/misc/rtc", O_RDONLY )) < 0 ) 79 if (( rtc = open ( "/dev/misc/rtc", O_RDONLY )) < 0 )
80 perror_msg_and_die ( "Could not access RTC" ); 80 bb_perror_msg_and_die ( "Could not access RTC" );
81 } 81 }
82 memset ( &tm, 0, sizeof( struct tm )); 82 memset ( &tm, 0, sizeof( struct tm ));
83 if ( ioctl ( rtc, RTC_RD_TIME, &tm ) < 0 ) 83 if ( ioctl ( rtc, RTC_RD_TIME, &tm ) < 0 )
84 perror_msg_and_die ( "Could not read time from RTC" ); 84 bb_perror_msg_and_die ( "Could not read time from RTC" );
85 tm. tm_isdst = -1; // not known 85 tm. tm_isdst = -1; // not known
86 86
87 close ( rtc ); 87 close ( rtc );
@@ -111,7 +111,7 @@ void write_rtc ( time_t t, int utc )
111 111
112 if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 ) { 112 if (( rtc = open ( "/dev/rtc", O_WRONLY )) < 0 ) {
113 if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 ) 113 if (( rtc = open ( "/dev/misc/rtc", O_WRONLY )) < 0 )
114 perror_msg_and_die ( "Could not access RTC" ); 114 bb_perror_msg_and_die ( "Could not access RTC" );
115 } 115 }
116 116
117 printf ( "1\n" ); 117 printf ( "1\n" );
@@ -122,7 +122,7 @@ void write_rtc ( time_t t, int utc )
122 printf ( "2\n") ; 122 printf ( "2\n") ;
123 123
124 if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 ) 124 if ( ioctl ( rtc, RTC_SET_TIME, &tm ) < 0 )
125 perror_msg_and_die ( "Could not set the RTC time" ); 125 bb_perror_msg_and_die ( "Could not set the RTC time" );
126 126
127 close ( rtc ); 127 close ( rtc );
128} 128}
@@ -138,7 +138,7 @@ int show_clock ( int utc )
138 138
139 safe_strncpy ( buffer, ctime ( &t ), sizeof( buffer )); 139 safe_strncpy ( buffer, ctime ( &t ), sizeof( buffer ));
140 if ( buffer [0] ) 140 if ( buffer [0] )
141 buffer [xstrlen ( buffer ) - 1] = 0; 141 buffer [bb_strlen ( buffer ) - 1] = 0;
142 142
143 //printf ( "%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : ( ptm-> tm_isdst ? tzname [1] : tzname [0] )); 143 //printf ( "%s %.6f seconds %s\n", buffer, 0.0, utc ? "" : ( ptm-> tm_isdst ? tzname [1] : tzname [0] ));
144 printf ( "%s %.6f seconds\n", buffer, 0.0 ); 144 printf ( "%s %.6f seconds\n", buffer, 0.0 );
@@ -154,7 +154,7 @@ int to_sys_clock ( int utc )
154 tv. tv_sec = read_rtc ( utc ); 154 tv. tv_sec = read_rtc ( utc );
155 155
156 if ( settimeofday ( &tv, &tz )) 156 if ( settimeofday ( &tv, &tz ))
157 perror_msg_and_die ( "settimeofday() failed" ); 157 bb_perror_msg_and_die ( "settimeofday() failed" );
158 158
159 return 0; 159 return 0;
160} 160}
@@ -165,7 +165,7 @@ int from_sys_clock ( int utc )
165 struct timezone tz = { 0, 0 }; 165 struct timezone tz = { 0, 0 };
166 166
167 if ( gettimeofday ( &tv, &tz )) 167 if ( gettimeofday ( &tv, &tz ))
168 perror_msg_and_die ( "gettimeofday() failed" ); 168 bb_perror_msg_and_die ( "gettimeofday() failed" );
169 169
170 write_rtc ( tv. tv_sec, utc ); 170 write_rtc ( tv. tv_sec, utc );
171 return 0; 171 return 0;
@@ -181,7 +181,7 @@ int check_utc ( void )
181 char buffer [128]; 181 char buffer [128];
182 182
183 while ( fgets ( buffer, sizeof( buffer ), f )) { 183 while ( fgets ( buffer, sizeof( buffer ), f )) {
184 int len = xstrlen ( buffer ); 184 int len = bb_strlen ( buffer );
185 185
186 while ( len && isspace ( buffer [len - 1] )) 186 while ( len && isspace ( buffer [len - 1] ))
187 len--; 187 len--;
@@ -238,7 +238,7 @@ extern int hwclock_main ( int argc, char **argv )
238 utc_arg = 1; 238 utc_arg = 1;
239 break; 239 break;
240 default: 240 default:
241 show_usage(); 241 bb_show_usage();
242 break; 242 break;
243 } 243 }
244 } 244 }
diff --git a/util-linux/losetup.c b/util-linux/losetup.c
index bfeb6b274..e2ea538d5 100644
--- a/util-linux/losetup.c
+++ b/util-linux/losetup.c
@@ -39,16 +39,16 @@ losetup_main (int argc, char **argv)
39 break; 39 break;
40 40
41 case 'o': 41 case 'o':
42 offset = parse_number (optarg, NULL); 42 offset = bb_xparse_number (optarg, NULL);
43 break; 43 break;
44 44
45 default: 45 default:
46 show_usage (); 46 bb_show_usage();
47 } 47 }
48 48
49 if ((delete && (offset || optind + 1 != argc)) 49 if ((delete && (offset || optind + 1 != argc))
50 || (!delete && optind + 2 != argc)) 50 || (!delete && optind + 2 != argc))
51 show_usage (); 51 bb_show_usage();
52 52
53 if (delete) 53 if (delete)
54 return del_loop (argv[optind]) ? EXIT_SUCCESS : EXIT_FAILURE; 54 return del_loop (argv[optind]) ? EXIT_SUCCESS : EXIT_FAILURE;
diff --git a/util-linux/mkfs_minix.c b/util-linux/mkfs_minix.c
index 94f29e55d..75a909e6b 100644
--- a/util-linux/mkfs_minix.c
+++ b/util-linux/mkfs_minix.c
@@ -268,7 +268,7 @@ extern inline void check_mount(void)
268 if (!mnt) 268 if (!mnt)
269 return; 269 return;
270 270
271 error_msg_and_die("%s is mounted; will not make a filesystem here!", device_name); 271 bb_error_msg_and_die("%s is mounted; will not make a filesystem here!", device_name);
272} 272}
273 273
274static long valid_offset(int fd, int offset) 274static long valid_offset(int fd, int offset)
@@ -307,7 +307,7 @@ extern inline int get_size(const char *file)
307 long size; 307 long size;
308 308
309 if ((fd = open(file, O_RDWR)) < 0) 309 if ((fd = open(file, O_RDWR)) < 0)
310 perror_msg_and_die("%s", file); 310 bb_perror_msg_and_die("%s", file);
311 if (ioctl(fd, BLKGETSIZE, &size) >= 0) { 311 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
312 close(fd); 312 close(fd);
313 return (size * 512); 313 return (size * 512);
@@ -325,28 +325,28 @@ extern inline void write_tables(void)
325 Super.s_state &= ~MINIX_ERROR_FS; 325 Super.s_state &= ~MINIX_ERROR_FS;
326 326
327 if (lseek(DEV, 0, SEEK_SET)) 327 if (lseek(DEV, 0, SEEK_SET))
328 error_msg_and_die("seek to boot block failed in write_tables"); 328 bb_error_msg_and_die("seek to boot block failed in write_tables");
329 if (512 != write(DEV, boot_block_buffer, 512)) 329 if (512 != write(DEV, boot_block_buffer, 512))
330 error_msg_and_die("unable to clear boot sector"); 330 bb_error_msg_and_die("unable to clear boot sector");
331 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET)) 331 if (BLOCK_SIZE != lseek(DEV, BLOCK_SIZE, SEEK_SET))
332 error_msg_and_die("seek failed in write_tables"); 332 bb_error_msg_and_die("seek failed in write_tables");
333 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE)) 333 if (BLOCK_SIZE != write(DEV, super_block_buffer, BLOCK_SIZE))
334 error_msg_and_die("unable to write super-block"); 334 bb_error_msg_and_die("unable to write super-block");
335 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE)) 335 if (IMAPS * BLOCK_SIZE != write(DEV, inode_map, IMAPS * BLOCK_SIZE))
336 error_msg_and_die("unable to write inode map"); 336 bb_error_msg_and_die("unable to write inode map");
337 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE)) 337 if (ZMAPS * BLOCK_SIZE != write(DEV, zone_map, ZMAPS * BLOCK_SIZE))
338 error_msg_and_die("unable to write zone map"); 338 bb_error_msg_and_die("unable to write zone map");
339 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE)) 339 if (INODE_BUFFER_SIZE != write(DEV, inode_buffer, INODE_BUFFER_SIZE))
340 error_msg_and_die("unable to write inodes"); 340 bb_error_msg_and_die("unable to write inodes");
341 341
342} 342}
343 343
344static void write_block(int blk, char *buffer) 344static void write_block(int blk, char *buffer)
345{ 345{
346 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET)) 346 if (blk * BLOCK_SIZE != lseek(DEV, blk * BLOCK_SIZE, SEEK_SET))
347 error_msg_and_die("seek failed in write_block"); 347 bb_error_msg_and_die("seek failed in write_block");
348 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE)) 348 if (BLOCK_SIZE != write(DEV, buffer, BLOCK_SIZE))
349 error_msg_and_die("write failed in write_block"); 349 bb_error_msg_and_die("write failed in write_block");
350} 350}
351 351
352static int get_free_block(void) 352static int get_free_block(void)
@@ -354,7 +354,7 @@ static int get_free_block(void)
354 int blk; 354 int blk;
355 355
356 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS) 356 if (used_good_blocks + 1 >= MAX_GOOD_BLOCKS)
357 error_msg_and_die("too many bad blocks"); 357 bb_error_msg_and_die("too many bad blocks");
358 if (used_good_blocks) 358 if (used_good_blocks)
359 blk = good_blocks_table[used_good_blocks - 1] + 1; 359 blk = good_blocks_table[used_good_blocks - 1] + 1;
360 else 360 else
@@ -362,7 +362,7 @@ static int get_free_block(void)
362 while (blk < ZONES && zone_in_use(blk)) 362 while (blk < ZONES && zone_in_use(blk))
363 blk++; 363 blk++;
364 if (blk >= ZONES) 364 if (blk >= ZONES)
365 error_msg_and_die("not enough good blocks"); 365 bb_error_msg_and_die("not enough good blocks");
366 good_blocks_table[used_good_blocks] = blk; 366 good_blocks_table[used_good_blocks] = blk;
367 used_good_blocks++; 367 used_good_blocks++;
368 return blk; 368 return blk;
@@ -428,7 +428,7 @@ extern inline void make_bad_inode(void)
428 goto end_bad; 428 goto end_bad;
429 } 429 }
430 } 430 }
431 error_msg_and_die("too many bad blocks"); 431 bb_error_msg_and_die("too many bad blocks");
432 end_bad: 432 end_bad:
433 if (ind) 433 if (ind)
434 write_block(ind, (char *) ind_block); 434 write_block(ind, (char *) ind_block);
@@ -478,7 +478,7 @@ extern inline void make_bad_inode2(void)
478 } 478 }
479 } 479 }
480 /* Could make triple indirect block here */ 480 /* Could make triple indirect block here */
481 error_msg_and_die("too many bad blocks"); 481 bb_error_msg_and_die("too many bad blocks");
482 end_bad: 482 end_bad:
483 if (ind) 483 if (ind)
484 write_block(ind, (char *) ind_block); 484 write_block(ind, (char *) ind_block);
@@ -579,7 +579,7 @@ extern inline void setup_tables(void)
579 * /sbin/mkfs.minix -i 200 test.fs 579 * /sbin/mkfs.minix -i 200 test.fs
580 * */ 580 * */
581 if (i >= 999) { 581 if (i >= 999) {
582 error_msg_and_die("unable to allocate buffers for maps"); 582 bb_error_msg_and_die("unable to allocate buffers for maps");
583 } 583 }
584 FIRSTZONE = NORM_FIRSTZONE; 584 FIRSTZONE = NORM_FIRSTZONE;
585 inode_map = xmalloc(IMAPS * BLOCK_SIZE); 585 inode_map = xmalloc(IMAPS * BLOCK_SIZE);
@@ -610,7 +610,7 @@ extern inline long do_check(char *buffer, int try, unsigned int current_block)
610 /* Seek to the correct loc. */ 610 /* Seek to the correct loc. */
611 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) != 611 if (lseek(DEV, current_block * BLOCK_SIZE, SEEK_SET) !=
612 current_block * BLOCK_SIZE) { 612 current_block * BLOCK_SIZE) {
613 error_msg_and_die("seek failed during testing of blocks"); 613 bb_error_msg_and_die("seek failed during testing of blocks");
614 } 614 }
615 615
616 616
@@ -650,7 +650,7 @@ static void check_blocks(void)
650 while (currently_testing < ZONES) { 650 while (currently_testing < ZONES) {
651 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) != 651 if (lseek(DEV, currently_testing * BLOCK_SIZE, SEEK_SET) !=
652 currently_testing * BLOCK_SIZE) 652 currently_testing * BLOCK_SIZE)
653 error_msg_and_die("seek failed in check_blocks"); 653 bb_error_msg_and_die("seek failed in check_blocks");
654 try = TEST_BUFFER_BLOCKS; 654 try = TEST_BUFFER_BLOCKS;
655 if (currently_testing + try > ZONES) 655 if (currently_testing + try > ZONES)
656 try = ZONES - currently_testing; 656 try = ZONES - currently_testing;
@@ -659,7 +659,7 @@ static void check_blocks(void)
659 if (got == try) 659 if (got == try)
660 continue; 660 continue;
661 if (currently_testing < FIRSTZONE) 661 if (currently_testing < FIRSTZONE)
662 error_msg_and_die("bad blocks before data-area: cannot make fs"); 662 bb_error_msg_and_die("bad blocks before data-area: cannot make fs");
663 mark_zone(currently_testing); 663 mark_zone(currently_testing);
664 badblocks++; 664 badblocks++;
665 currently_testing++; 665 currently_testing++;
@@ -675,7 +675,7 @@ static void get_list_blocks(char *filename)
675 FILE *listfile; 675 FILE *listfile;
676 unsigned long blockno; 676 unsigned long blockno;
677 677
678 listfile = xfopen(filename, "r"); 678 listfile = bb_xfopen(filename, "r");
679 while (!feof(listfile)) { 679 while (!feof(listfile)) {
680 fscanf(listfile, "%ld\n", &blockno); 680 fscanf(listfile, "%ld\n", &blockno);
681 mark_zone(blockno); 681 mark_zone(blockno);
@@ -696,10 +696,10 @@ extern int mkfs_minix_main(int argc, char **argv)
696 int stopIt=FALSE; 696 int stopIt=FALSE;
697 697
698 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE) 698 if (INODE_SIZE * MINIX_INODES_PER_BLOCK != BLOCK_SIZE)
699 error_msg_and_die("bad inode size"); 699 bb_error_msg_and_die("bad inode size");
700#ifdef CONFIG_FEATURE_MINIX2 700#ifdef CONFIG_FEATURE_MINIX2
701 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE) 701 if (INODE_SIZE2 * MINIX2_INODES_PER_BLOCK != BLOCK_SIZE)
702 error_msg_and_die("bad inode size"); 702 bb_error_msg_and_die("bad inode size");
703#endif 703#endif
704 704
705 /* Parse options */ 705 /* Parse options */
@@ -725,7 +725,7 @@ extern int mkfs_minix_main(int argc, char **argv)
725 } 725 }
726 req_nr_inodes = strtoul(cp, &tmp, 0); 726 req_nr_inodes = strtoul(cp, &tmp, 0);
727 if (*tmp) 727 if (*tmp)
728 show_usage(); 728 bb_show_usage();
729 stopIt=TRUE; 729 stopIt=TRUE;
730 break; 730 break;
731 } 731 }
@@ -749,13 +749,13 @@ extern int mkfs_minix_main(int argc, char **argv)
749 } 749 }
750 i = strtoul(cp, &tmp, 0); 750 i = strtoul(cp, &tmp, 0);
751 if (*tmp) 751 if (*tmp)
752 show_usage(); 752 bb_show_usage();
753 if (i == 14) 753 if (i == 14)
754 magic = MINIX_SUPER_MAGIC; 754 magic = MINIX_SUPER_MAGIC;
755 else if (i == 30) 755 else if (i == 30)
756 magic = MINIX_SUPER_MAGIC2; 756 magic = MINIX_SUPER_MAGIC2;
757 else 757 else
758 show_usage(); 758 bb_show_usage();
759 namelen = i; 759 namelen = i;
760 dirsize = i + 2; 760 dirsize = i + 2;
761 stopIt=TRUE; 761 stopIt=TRUE;
@@ -765,7 +765,7 @@ extern int mkfs_minix_main(int argc, char **argv)
765#ifdef CONFIG_FEATURE_MINIX2 765#ifdef CONFIG_FEATURE_MINIX2
766 version2 = 1; 766 version2 = 1;
767#else 767#else
768 error_msg("%s: not compiled with minix v2 support", 768 bb_error_msg("%s: not compiled with minix v2 support",
769 device_name); 769 device_name);
770 exit(-1); 770 exit(-1);
771#endif 771#endif
@@ -774,7 +774,7 @@ extern int mkfs_minix_main(int argc, char **argv)
774 case 'h': 774 case 'h':
775 default: 775 default:
776goodbye: 776goodbye:
777 show_usage(); 777 bb_show_usage();
778 } 778 }
779 } 779 }
780 } else { 780 } else {
@@ -792,7 +792,7 @@ goodbye:
792 if (device_name && !BLOCKS) 792 if (device_name && !BLOCKS)
793 BLOCKS = get_size(device_name) / 1024; 793 BLOCKS = get_size(device_name) / 1024;
794 if (!device_name || BLOCKS < 10) { 794 if (!device_name || BLOCKS < 10) {
795 show_usage(); 795 bb_show_usage();
796 } 796 }
797#ifdef CONFIG_FEATURE_MINIX2 797#ifdef CONFIG_FEATURE_MINIX2
798 if (version2) { 798 if (version2) {
@@ -816,13 +816,13 @@ goodbye:
816 strcpy(tmp + 2, ".badblocks"); 816 strcpy(tmp + 2, ".badblocks");
817 DEV = open(device_name, O_RDWR); 817 DEV = open(device_name, O_RDWR);
818 if (DEV < 0) 818 if (DEV < 0)
819 error_msg_and_die("unable to open %s", device_name); 819 bb_error_msg_and_die("unable to open %s", device_name);
820 if (fstat(DEV, &statbuf) < 0) 820 if (fstat(DEV, &statbuf) < 0)
821 error_msg_and_die("unable to stat %s", device_name); 821 bb_error_msg_and_die("unable to stat %s", device_name);
822 if (!S_ISBLK(statbuf.st_mode)) 822 if (!S_ISBLK(statbuf.st_mode))
823 check = 0; 823 check = 0;
824 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) 824 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
825 error_msg_and_die("will not try to make filesystem on '%s'", device_name); 825 bb_error_msg_and_die("will not try to make filesystem on '%s'", device_name);
826 setup_tables(); 826 setup_tables();
827 if (check) 827 if (check)
828 check_blocks(); 828 check_blocks();
diff --git a/util-linux/mkswap.c b/util-linux/mkswap.c
index de10ba71f..15db392d2 100644
--- a/util-linux/mkswap.c
+++ b/util-linux/mkswap.c
@@ -87,7 +87,7 @@ static inline void init_signature_page(void)
87 87
88#ifdef PAGE_SIZE 88#ifdef PAGE_SIZE
89 if (pagesize != PAGE_SIZE) 89 if (pagesize != PAGE_SIZE)
90 error_msg("Assuming pages of size %d", pagesize); 90 bb_error_msg("Assuming pages of size %d", pagesize);
91#endif 91#endif
92 signature_page = (int *) xmalloc(pagesize); 92 signature_page = (int *) xmalloc(pagesize);
93 memset(signature_page, 0, pagesize); 93 memset(signature_page, 0, pagesize);
@@ -185,7 +185,7 @@ static inline void page_bad(int page)
185 bit_test_and_clear(signature_page, page); 185 bit_test_and_clear(signature_page, page);
186 else { 186 else {
187 if (badpages == MAX_BADPAGES) 187 if (badpages == MAX_BADPAGES)
188 error_msg_and_die("too many bad pages"); 188 bb_error_msg_and_die("too many bad pages");
189 p->badpages[badpages] = page; 189 p->badpages[badpages] = page;
190 } 190 }
191 badpages++; 191 badpages++;
@@ -206,7 +206,7 @@ static void check_blocks(void)
206 } 206 }
207 if (do_seek && lseek(DEV, current_page * pagesize, SEEK_SET) != 207 if (do_seek && lseek(DEV, current_page * pagesize, SEEK_SET) !=
208 current_page * pagesize) 208 current_page * pagesize)
209 error_msg_and_die("seek failed in check_blocks"); 209 bb_error_msg_and_die("seek failed in check_blocks");
210 if ((do_seek = (pagesize != read(DEV, buffer, pagesize)))) { 210 if ((do_seek = (pagesize != read(DEV, buffer, pagesize)))) {
211 page_bad(current_page++); 211 page_bad(current_page++);
212 continue; 212 continue;
@@ -255,7 +255,7 @@ static long get_size(const char *file)
255 long size; 255 long size;
256 256
257 if ((fd = open(file, O_RDONLY)) < 0) 257 if ((fd = open(file, O_RDONLY)) < 0)
258 perror_msg_and_die("%s", file); 258 bb_perror_msg_and_die("%s", file);
259 if (ioctl(fd, BLKGETSIZE, &size) >= 0) { 259 if (ioctl(fd, BLKGETSIZE, &size) >= 0) {
260 int sectors_per_page = pagesize / 512; 260 int sectors_per_page = pagesize / 512;
261 261
@@ -287,7 +287,7 @@ int mkswap_main(int argc, char **argv)
287 287
288 PAGES = strtol(argv[0], &tmp, 0) / blocks_per_page; 288 PAGES = strtol(argv[0], &tmp, 0) / blocks_per_page;
289 if (*tmp) 289 if (*tmp)
290 show_usage(); 290 bb_show_usage();
291 } else 291 } else
292 device_name = argv[0]; 292 device_name = argv[0];
293 } else { 293 } else {
@@ -302,19 +302,19 @@ int mkswap_main(int argc, char **argv)
302 version = atoi(argv[0] + 2); 302 version = atoi(argv[0] + 2);
303 break; 303 break;
304 default: 304 default:
305 show_usage(); 305 bb_show_usage();
306 } 306 }
307 } 307 }
308 } 308 }
309 if (!device_name) { 309 if (!device_name) {
310 error_msg("error: Nowhere to set up swap on?"); 310 bb_error_msg("error: Nowhere to set up swap on?");
311 show_usage(); 311 bb_show_usage();
312 } 312 }
313 sz = get_size(device_name); 313 sz = get_size(device_name);
314 if (!PAGES) { 314 if (!PAGES) {
315 PAGES = sz; 315 PAGES = sz;
316 } else if (PAGES > sz && !force) { 316 } else if (PAGES > sz && !force) {
317 error_msg("error: size %ld is larger than device size %d", 317 bb_error_msg("error: size %ld is larger than device size %d",
318 PAGES * (pagesize / 1024), sz * (pagesize / 1024)); 318 PAGES * (pagesize / 1024), sz * (pagesize / 1024));
319 return EXIT_FAILURE; 319 return EXIT_FAILURE;
320 } 320 }
@@ -330,13 +330,13 @@ int mkswap_main(int argc, char **argv)
330 version = 1; 330 version = 1;
331 } 331 }
332 if (version != 0 && version != 1) { 332 if (version != 0 && version != 1) {
333 error_msg("error: unknown version %d", version); 333 bb_error_msg("error: unknown version %d", version);
334 show_usage(); 334 bb_show_usage();
335 } 335 }
336 if (PAGES < 10) { 336 if (PAGES < 10) {
337 error_msg("error: swap area needs to be at least %ldkB", 337 bb_error_msg("error: swap area needs to be at least %ldkB",
338 (long) (10 * pagesize / 1024)); 338 (long) (10 * pagesize / 1024));
339 show_usage(); 339 bb_show_usage();
340 } 340 }
341#if 0 341#if 0
342 maxpages = ((version == 0) ? V0_MAX_PAGES : V1_MAX_PAGES); 342 maxpages = ((version == 0) ? V0_MAX_PAGES : V1_MAX_PAGES);
@@ -353,17 +353,17 @@ int mkswap_main(int argc, char **argv)
353#endif 353#endif
354 if (PAGES > maxpages) { 354 if (PAGES > maxpages) {
355 PAGES = maxpages; 355 PAGES = maxpages;
356 error_msg("warning: truncating swap area to %ldkB", 356 bb_error_msg("warning: truncating swap area to %ldkB",
357 PAGES * pagesize / 1024); 357 PAGES * pagesize / 1024);
358 } 358 }
359 359
360 DEV = open(device_name, O_RDWR); 360 DEV = open(device_name, O_RDWR);
361 if (DEV < 0 || fstat(DEV, &statbuf) < 0) 361 if (DEV < 0 || fstat(DEV, &statbuf) < 0)
362 perror_msg_and_die("%s", device_name); 362 bb_perror_msg_and_die("%s", device_name);
363 if (!S_ISBLK(statbuf.st_mode)) 363 if (!S_ISBLK(statbuf.st_mode))
364 check = 0; 364 check = 0;
365 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) 365 else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340)
366 error_msg_and_die("Will not try to make swapdevice on '%s'", device_name); 366 bb_error_msg_and_die("Will not try to make swapdevice on '%s'", device_name);
367 367
368#ifdef __sparc__ 368#ifdef __sparc__
369 if (!force && version == 0) { 369 if (!force && version == 0) {
@@ -372,13 +372,13 @@ int mkswap_main(int argc, char **argv)
372 unsigned short *q, sum; 372 unsigned short *q, sum;
373 373
374 if (read(DEV, buffer, 512) != 512) 374 if (read(DEV, buffer, 512) != 512)
375 error_msg_and_die("fatal: first page unreadable"); 375 bb_error_msg_and_die("fatal: first page unreadable");
376 if (buffer[508] == 0xDA && buffer[509] == 0xBE) { 376 if (buffer[508] == 0xDA && buffer[509] == 0xBE) {
377 q = (unsigned short *) (buffer + 510); 377 q = (unsigned short *) (buffer + 510);
378 for (sum = 0; q >= (unsigned short *) buffer;) 378 for (sum = 0; q >= (unsigned short *) buffer;)
379 sum ^= *q--; 379 sum ^= *q--;
380 if (!sum) { 380 if (!sum) {
381 error_msg("Device '%s' contains a valid Sun disklabel.\n" 381 bb_error_msg("Device '%s' contains a valid Sun disklabel.\n"
382"This probably means creating v0 swap would destroy your partition table\n" 382"This probably means creating v0 swap would destroy your partition table\n"
383"No swap created. If you really want to create swap v0 on that device, use\n" 383"No swap created. If you really want to create swap v0 on that device, use\n"
384"the -f option to force it.", device_name); 384"the -f option to force it.", device_name);
@@ -391,7 +391,7 @@ int mkswap_main(int argc, char **argv)
391 if (version == 0 || check) 391 if (version == 0 || check)
392 check_blocks(); 392 check_blocks();
393 if (version == 0 && !bit_test_and_clear(signature_page, 0)) 393 if (version == 0 && !bit_test_and_clear(signature_page, 0))
394 error_msg_and_die("fatal: first page unreadable"); 394 bb_error_msg_and_die("fatal: first page unreadable");
395 if (version == 1) { 395 if (version == 1) {
396 p->version = version; 396 p->version = version;
397 p->last_page = PAGES - 1; 397 p->last_page = PAGES - 1;
@@ -400,23 +400,23 @@ int mkswap_main(int argc, char **argv)
400 400
401 goodpages = PAGES - badpages - 1; 401 goodpages = PAGES - badpages - 1;
402 if (goodpages <= 0) 402 if (goodpages <= 0)
403 error_msg_and_die("Unable to set up swap-space: unreadable"); 403 bb_error_msg_and_die("Unable to set up swap-space: unreadable");
404 printf("Setting up swapspace version %d, size = %ld bytes\n", 404 printf("Setting up swapspace version %d, size = %ld bytes\n",
405 version, (long) (goodpages * pagesize)); 405 version, (long) (goodpages * pagesize));
406 write_signature((version == 0) ? "SWAP-SPACE" : "SWAPSPACE2"); 406 write_signature((version == 0) ? "SWAP-SPACE" : "SWAPSPACE2");
407 407
408 offset = ((version == 0) ? 0 : 1024); 408 offset = ((version == 0) ? 0 : 1024);
409 if (lseek(DEV, offset, SEEK_SET) != offset) 409 if (lseek(DEV, offset, SEEK_SET) != offset)
410 error_msg_and_die("unable to rewind swap-device"); 410 bb_error_msg_and_die("unable to rewind swap-device");
411 if (write(DEV, (char *) signature_page + offset, pagesize - offset) 411 if (write(DEV, (char *) signature_page + offset, pagesize - offset)
412 != pagesize - offset) 412 != pagesize - offset)
413 error_msg_and_die("unable to write signature page"); 413 bb_error_msg_and_die("unable to write signature page");
414 414
415 /* 415 /*
416 * A subsequent swapon() will fail if the signature 416 * A subsequent swapon() will fail if the signature
417 * is not actually on disk. (This is a kernel bug.) 417 * is not actually on disk. (This is a kernel bug.)
418 */ 418 */
419 if (fsync(DEV)) 419 if (fsync(DEV))
420 error_msg_and_die("fsync failed"); 420 bb_error_msg_and_die("fsync failed");
421 return EXIT_SUCCESS; 421 return EXIT_SUCCESS;
422} 422}
diff --git a/util-linux/more.c b/util-linux/more.c
index c26e4fe54..ff0557d22 100644
--- a/util-linux/more.c
+++ b/util-linux/more.c
@@ -81,7 +81,7 @@ extern int more_main(int argc, char **argv)
81 if(isatty(fileno(stdout))) { 81 if(isatty(fileno(stdout))) {
82 cin = fopen(CURRENT_TTY, "r"); 82 cin = fopen(CURRENT_TTY, "r");
83 if (!cin) 83 if (!cin)
84 cin = xfopen(CONSOLE_DEV, "r"); 84 cin = bb_xfopen(CONSOLE_DEV, "r");
85 please_display_more_prompt = 0; 85 please_display_more_prompt = 0;
86#ifdef CONFIG_FEATURE_USE_TERMIOS 86#ifdef CONFIG_FEATURE_USE_TERMIOS
87 getTermSettings(fileno(cin), &initial_settings); 87 getTermSettings(fileno(cin), &initial_settings);
@@ -105,7 +105,7 @@ extern int more_main(int argc, char **argv)
105 if (argc == 0) { 105 if (argc == 0) {
106 file = stdin; 106 file = stdin;
107 } else 107 } else
108 file = wfopen(*argv, "r"); 108 file = bb_wfopen(*argv, "r");
109 if(file==0) 109 if(file==0)
110 goto loop; 110 goto loop;
111 111
diff --git a/util-linux/mount.c b/util-linux/mount.c
index b3e945ce3..d02005600 100644
--- a/util-linux/mount.c
+++ b/util-linux/mount.c
@@ -86,8 +86,6 @@ extern int umount2(__const char *__special_file, int __flags);
86 86
87extern int sysfs(int option, unsigned int fs_index, char *buf); 87extern int sysfs(int option, unsigned int fs_index, char *buf);
88 88
89extern const char mtab_file[]; /* Defined in utility.c */
90
91struct mount_options { 89struct mount_options {
92 const char *name; 90 const char *name;
93 unsigned long and; 91 unsigned long and;
@@ -136,20 +134,20 @@ do_mount(char *specialfile, char *dir, char *filesystemtype, long flags,
136 134
137 specialfile = find_unused_loop_device(); 135 specialfile = find_unused_loop_device();
138 if (specialfile == NULL) { 136 if (specialfile == NULL) {
139 error_msg_and_die("Could not find a spare loop device"); 137 bb_error_msg_and_die("Could not find a spare loop device");
140 } 138 }
141 if (set_loop(specialfile, lofile, 0, &loro)) { 139 if (set_loop(specialfile, lofile, 0, &loro)) {
142 error_msg_and_die("Could not setup loop device"); 140 bb_error_msg_and_die("Could not setup loop device");
143 } 141 }
144 if (!(flags & MS_RDONLY) && loro) { /* loop is ro, but wanted rw */ 142 if (!(flags & MS_RDONLY) && loro) { /* loop is ro, but wanted rw */
145 error_msg("WARNING: loop device is read-only"); 143 bb_error_msg("WARNING: loop device is read-only");
146 flags |= MS_RDONLY; 144 flags |= MS_RDONLY;
147 } 145 }
148 } 146 }
149#endif 147#endif
150 status = mount(specialfile, dir, filesystemtype, flags, string_flags); 148 status = mount(specialfile, dir, filesystemtype, flags, string_flags);
151 if (status < 0 && errno == EROFS) { 149 if (status < 0 && errno == EROFS) {
152 error_msg("%s is write-protected, mounting read-only", 150 bb_error_msg("%s is write-protected, mounting read-only",
153 specialfile); 151 specialfile);
154 status = mount(specialfile, dir, filesystemtype, flags |= 152 status = mount(specialfile, dir, filesystemtype, flags |=
155 MS_RDONLY, string_flags); 153 MS_RDONLY, string_flags);
@@ -181,7 +179,7 @@ do_mount(char *specialfile, char *dir, char *filesystemtype, long flags,
181#endif 179#endif
182 180
183 if (errno == EPERM) { 181 if (errno == EPERM) {
184 error_msg_and_die("permission denied. Are you root?"); 182 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
185 } 183 }
186 184
187 return (FALSE); 185 return (FALSE);
@@ -268,7 +266,7 @@ static int mount_one(char *blockDevice, char *directory, char *filesystemType,
268 266
269 filesystemType = buf; 267 filesystemType = buf;
270 268
271 if (xstrlen(filesystemType)) { 269 if (bb_strlen(filesystemType)) {
272 status = 270 status =
273 do_mount(blockDevice, directory, filesystemType, 271 do_mount(blockDevice, directory, filesystemType,
274 flags | MS_MGC_VAL, string_flags, 272 flags | MS_MGC_VAL, string_flags,
@@ -284,7 +282,7 @@ static int mount_one(char *blockDevice, char *directory, char *filesystemType,
284 } 282 }
285 283
286 if ((!f || read_proc) && !status) { 284 if ((!f || read_proc) && !status) {
287 f = xfopen("/proc/filesystems", "r"); 285 f = bb_xfopen("/proc/filesystems", "r");
288 286
289 while (fgets(buf, sizeof(buf), f) != NULL) { 287 while (fgets(buf, sizeof(buf), f) != NULL) {
290 filesystemType = buf; 288 filesystemType = buf;
@@ -319,7 +317,7 @@ static int mount_one(char *blockDevice, char *directory, char *filesystemType,
319 317
320 if (!status) { 318 if (!status) {
321 if (whineOnErrors) { 319 if (whineOnErrors) {
322 perror_msg("Mounting %s on %s failed", blockDevice, directory); 320 bb_perror_msg("Mounting %s on %s failed", blockDevice, directory);
323 } 321 }
324 return (FALSE); 322 return (FALSE);
325 } 323 }
@@ -328,7 +326,7 @@ static int mount_one(char *blockDevice, char *directory, char *filesystemType,
328 326
329static void show_mounts(char *onlytype) 327static void show_mounts(char *onlytype)
330{ 328{
331 FILE *mountTable = setmntent(mtab_file, "r"); 329 FILE *mountTable = setmntent(bb_path_mtab_file, "r");
332 330
333 if (mountTable) { 331 if (mountTable) {
334 struct mntent *m; 332 struct mntent *m;
@@ -351,7 +349,7 @@ static void show_mounts(char *onlytype)
351 } 349 }
352 endmntent(mountTable); 350 endmntent(mountTable);
353 } else { 351 } else {
354 perror_msg_and_die("%s", mtab_file); 352 bb_perror_msg_and_die(bb_path_mtab_file);
355 } 353 }
356 exit(EXIT_SUCCESS); 354 exit(EXIT_SUCCESS);
357} 355}
@@ -359,7 +357,7 @@ static void show_mounts(char *onlytype)
359extern int mount_main(int argc, char **argv) 357extern int mount_main(int argc, char **argv)
360{ 358{
361 struct stat statbuf; 359 struct stat statbuf;
362 char *string_flags = xstrdup(""); 360 char *string_flags = bb_xstrdup("");
363 char *extra_opts; 361 char *extra_opts;
364 int flags = 0; 362 int flags = 0;
365 char *filesystemType = "auto"; 363 char *filesystemType = "auto";
@@ -413,7 +411,7 @@ extern int mount_main(int argc, char **argv)
413 if (optind < argc) { 411 if (optind < argc) {
414 /* if device is a filename get its real path */ 412 /* if device is a filename get its real path */
415 if (stat(argv[optind], &statbuf) == 0) { 413 if (stat(argv[optind], &statbuf) == 0) {
416 char *tmp = simplify_path(argv[optind]); 414 char *tmp = bb_simplify_path(argv[optind]);
417 415
418 safe_strncpy(device, tmp, PATH_MAX); 416 safe_strncpy(device, tmp, PATH_MAX);
419 } else { 417 } else {
@@ -422,13 +420,13 @@ extern int mount_main(int argc, char **argv)
422 } 420 }
423 421
424 if (optind + 1 < argc) 422 if (optind + 1 < argc)
425 directory = simplify_path(argv[optind + 1]); 423 directory = bb_simplify_path(argv[optind + 1]);
426 424
427 if (all || optind + 1 == argc) { 425 if (all || optind + 1 == argc) {
428 f = setmntent("/etc/fstab", "r"); 426 f = setmntent("/etc/fstab", "r");
429 427
430 if (f == NULL) 428 if (f == NULL)
431 perror_msg_and_die("\nCannot read /etc/fstab"); 429 bb_perror_msg_and_die("\nCannot read /etc/fstab");
432 430
433 while ((m = getmntent(f)) != NULL) { 431 while ((m = getmntent(f)) != NULL) {
434 if (!all && (optind + 1 == argc) 432 if (!all && (optind + 1 == argc)
@@ -452,7 +450,7 @@ extern int mount_main(int argc, char **argv)
452 450
453 strcpy(device, m->mnt_fsname); 451 strcpy(device, m->mnt_fsname);
454 strcpy(directory, m->mnt_dir); 452 strcpy(directory, m->mnt_dir);
455 filesystemType = xstrdup(m->mnt_type); 453 filesystemType = bb_xstrdup(m->mnt_type);
456 singlemount: 454 singlemount:
457 extra_opts = string_flags; 455 extra_opts = string_flags;
458 rc = EXIT_SUCCESS; 456 rc = EXIT_SUCCESS;
@@ -462,7 +460,7 @@ extern int mount_main(int argc, char **argv)
462 if (nfsmount 460 if (nfsmount
463 (device, directory, &flags, &extra_opts, &string_flags, 461 (device, directory, &flags, &extra_opts, &string_flags,
464 1)) { 462 1)) {
465 perror_msg("nfsmount failed"); 463 bb_perror_msg("nfsmount failed");
466 rc = EXIT_FAILURE; 464 rc = EXIT_FAILURE;
467 } 465 }
468 } 466 }
diff --git a/util-linux/nfsmount.c b/util-linux/nfsmount.c
index ba14aa6bc..0edfdf34a 100644
--- a/util-linux/nfsmount.c
+++ b/util-linux/nfsmount.c
@@ -357,7 +357,7 @@ int nfsmount(const char *spec, const char *node, int *flags,
357 msock = fsock = -1; 357 msock = fsock = -1;
358 mclient = NULL; 358 mclient = NULL;
359 if (strlen(spec) >= sizeof(hostdir)) { 359 if (strlen(spec) >= sizeof(hostdir)) {
360 error_msg("excessively long host:dir argument"); 360 bb_error_msg("excessively long host:dir argument");
361 goto fail; 361 goto fail;
362 } 362 }
363 strcpy(hostdir, spec); 363 strcpy(hostdir, spec);
@@ -369,10 +369,10 @@ int nfsmount(const char *spec, const char *node, int *flags,
369 until they can be fully supported. (mack@sgi.com) */ 369 until they can be fully supported. (mack@sgi.com) */
370 if ((s = strchr(hostdir, ','))) { 370 if ((s = strchr(hostdir, ','))) {
371 *s = '\0'; 371 *s = '\0';
372 error_msg("warning: multiple hostnames not supported"); 372 bb_error_msg("warning: multiple hostnames not supported");
373 } 373 }
374 } else { 374 } else {
375 error_msg("directory to mount not in host:dir format"); 375 bb_error_msg("directory to mount not in host:dir format");
376 goto fail; 376 goto fail;
377 } 377 }
378 378
@@ -382,11 +382,11 @@ int nfsmount(const char *spec, const char *node, int *flags,
382#endif 382#endif
383 { 383 {
384 if ((hp = gethostbyname(hostname)) == NULL) { 384 if ((hp = gethostbyname(hostname)) == NULL) {
385 herror_msg("%s", hostname); 385 bb_herror_msg("%s", hostname);
386 goto fail; 386 goto fail;
387 } else { 387 } else {
388 if (hp->h_length > sizeof(struct in_addr)) { 388 if (hp->h_length > sizeof(struct in_addr)) {
389 error_msg("got bad hp->h_length"); 389 bb_error_msg("got bad hp->h_length");
390 hp->h_length = sizeof(struct in_addr); 390 hp->h_length = sizeof(struct in_addr);
391 } 391 }
392 memcpy(&server_addr.sin_addr, 392 memcpy(&server_addr.sin_addr,
@@ -403,12 +403,12 @@ int nfsmount(const char *spec, const char *node, int *flags,
403 if (!old_opts) 403 if (!old_opts)
404 old_opts = ""; 404 old_opts = "";
405 if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) { 405 if (strlen(old_opts) + strlen(s) + 10 >= sizeof(new_opts)) {
406 error_msg("excessively long option argument"); 406 bb_error_msg("excessively long option argument");
407 goto fail; 407 goto fail;
408 } 408 }
409 sprintf(new_opts, "%s%saddr=%s", 409 sprintf(new_opts, "%s%saddr=%s",
410 old_opts, *old_opts ? "," : "", s); 410 old_opts, *old_opts ? "," : "", s);
411 *extra_opts = xstrdup(new_opts); 411 *extra_opts = bb_xstrdup(new_opts);
412 412
413 /* Set default options. 413 /* Set default options.
414 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to 414 * rsize/wsize (and bsize, for ver >= 3) are left 0 in order to
@@ -476,7 +476,7 @@ int nfsmount(const char *spec, const char *node, int *flags,
476 else if (!strcmp(opt, "mountport")) 476 else if (!strcmp(opt, "mountport"))
477 mountport = val; 477 mountport = val;
478 else if (!strcmp(opt, "mounthost")) 478 else if (!strcmp(opt, "mounthost"))
479 mounthost=xstrndup(opteq+1, 479 mounthost=bb_xstrndup(opteq+1,
480 strcspn(opteq+1," \t\n\r,")); 480 strcspn(opteq+1," \t\n\r,"));
481 else if (!strcmp(opt, "mountprog")) 481 else if (!strcmp(opt, "mountprog"))
482 mountprog = val; 482 mountprog = val;
@@ -563,11 +563,11 @@ int nfsmount(const char *spec, const char *node, int *flags,
563 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0); 563 data.flags |= (nolock ? NFS_MOUNT_NONLM : 0);
564#endif 564#endif
565 if (nfsvers > MAX_NFSPROT) { 565 if (nfsvers > MAX_NFSPROT) {
566 error_msg("NFSv%d not supported!", nfsvers); 566 bb_error_msg("NFSv%d not supported!", nfsvers);
567 return 0; 567 return 0;
568 } 568 }
569 if (mountvers > MAX_NFSPROT) { 569 if (mountvers > MAX_NFSPROT) {
570 error_msg("NFSv%d not supported!", nfsvers); 570 bb_error_msg("NFSv%d not supported!", nfsvers);
571 return 0; 571 return 0;
572 } 572 }
573 if (nfsvers && !mountvers) 573 if (nfsvers && !mountvers)
@@ -627,11 +627,11 @@ int nfsmount(const char *spec, const char *node, int *flags,
627 mount_server_addr.sin_addr.s_addr = inet_addr(hostname); 627 mount_server_addr.sin_addr.s_addr = inet_addr(hostname);
628 } else { 628 } else {
629 if ((hp = gethostbyname(mounthost)) == NULL) { 629 if ((hp = gethostbyname(mounthost)) == NULL) {
630 herror_msg("%s", mounthost); 630 bb_herror_msg("%s", mounthost);
631 goto fail; 631 goto fail;
632 } else { 632 } else {
633 if (hp->h_length > sizeof(struct in_addr)) { 633 if (hp->h_length > sizeof(struct in_addr)) {
634 error_msg("got bad hp->h_length?"); 634 bb_error_msg("got bad hp->h_length?");
635 hp->h_length = sizeof(struct in_addr); 635 hp->h_length = sizeof(struct in_addr);
636 } 636 }
637 mount_server_addr.sin_family = AF_INET; 637 mount_server_addr.sin_family = AF_INET;
@@ -753,7 +753,7 @@ int nfsmount(const char *spec, const char *node, int *flags,
753 if (!bg) 753 if (!bg)
754 goto fail; 754 goto fail;
755 if (!running_bg) { 755 if (!running_bg) {
756 prev_bg_host = xstrdup(hostname); 756 prev_bg_host = bb_xstrdup(hostname);
757 if (retry > 0) 757 if (retry > 0)
758 retval = EX_BG; 758 retval = EX_BG;
759 goto fail; 759 goto fail;
@@ -766,7 +766,7 @@ int nfsmount(const char *spec, const char *node, int *flags,
766 766
767 if (nfsvers == 2) { 767 if (nfsvers == 2) {
768 if (status.nfsv2.fhs_status != 0) { 768 if (status.nfsv2.fhs_status != 0) {
769 error_msg("%s:%s failed, reason given by server: %s", 769 bb_error_msg("%s:%s failed, reason given by server: %s",
770 hostname, pathname, 770 hostname, pathname,
771 nfs_strerror(status.nfsv2.fhs_status)); 771 nfs_strerror(status.nfsv2.fhs_status));
772 goto fail; 772 goto fail;
@@ -784,7 +784,7 @@ int nfsmount(const char *spec, const char *node, int *flags,
784#if NFS_MOUNT_VERSION >= 4 784#if NFS_MOUNT_VERSION >= 4
785 fhandle3 *my_fhandle; 785 fhandle3 *my_fhandle;
786 if (status.nfsv3.fhs_status != 0) { 786 if (status.nfsv3.fhs_status != 0) {
787 error_msg("%s:%s failed, reason given by server: %s", 787 bb_error_msg("%s:%s failed, reason given by server: %s",
788 hostname, pathname, 788 hostname, pathname,
789 nfs_strerror(status.nfsv3.fhs_status)); 789 nfs_strerror(status.nfsv3.fhs_status));
790 goto fail; 790 goto fail;
diff --git a/util-linux/pivot_root.c b/util-linux/pivot_root.c
index ba26b9c58..39453a261 100644
--- a/util-linux/pivot_root.c
+++ b/util-linux/pivot_root.c
@@ -16,10 +16,10 @@ extern int pivot_root(const char * new_root,const char * put_old);
16int pivot_root_main(int argc, char **argv) 16int pivot_root_main(int argc, char **argv)
17{ 17{
18 if (argc != 3) 18 if (argc != 3)
19 show_usage(); 19 bb_show_usage();
20 20
21 if (pivot_root(argv[1],argv[2]) < 0) 21 if (pivot_root(argv[1],argv[2]) < 0)
22 perror_msg_and_die("pivot_root"); 22 bb_perror_msg_and_die("pivot_root");
23 23
24 return EXIT_SUCCESS; 24 return EXIT_SUCCESS;
25 25
diff --git a/util-linux/rdate.c b/util-linux/rdate.c
index df7d7bbc4..3c3b152a2 100644
--- a/util-linux/rdate.c
+++ b/util-linux/rdate.c
@@ -49,7 +49,7 @@ static time_t askremotedate(const char *host)
49 fd = xconnect(host, port); 49 fd = xconnect(host, port);
50 50
51 if (read(fd, (void *)&nett, 4) != 4) /* read time from server */ 51 if (read(fd, (void *)&nett, 4) != 4) /* read time from server */
52 error_msg_and_die("%s did not send the complete time", host); 52 bb_error_msg_and_die("%s did not send the complete time", host);
53 53
54 close(fd); 54 close(fd);
55 55
@@ -83,18 +83,18 @@ int rdate_main(int argc, char **argv)
83 setdate = 0; 83 setdate = 0;
84 break; 84 break;
85 default: 85 default:
86 show_usage(); 86 bb_show_usage();
87 } 87 }
88 } 88 }
89 89
90 if (optind == argc) 90 if (optind == argc)
91 show_usage(); 91 bb_show_usage();
92 92
93 remote_time = askremotedate(argv[optind]); 93 remote_time = askremotedate(argv[optind]);
94 94
95 if (setdate) { 95 if (setdate) {
96 if (stime(&remote_time) < 0) 96 if (stime(&remote_time) < 0)
97 perror_msg_and_die("Could not set time of day"); 97 bb_perror_msg_and_die("Could not set time of day");
98 } 98 }
99 99
100 if (printdate) 100 if (printdate)
diff --git a/util-linux/swaponoff.c b/util-linux/swaponoff.c
index 918a5bc0e..b323cb10a 100644
--- a/util-linux/swaponoff.c
+++ b/util-linux/swaponoff.c
@@ -51,13 +51,13 @@ static int swap_enable_disable(const char *device)
51 struct stat st; 51 struct stat st;
52 52
53 if (stat(device, &st) < 0) { 53 if (stat(device, &st) < 0) {
54 perror_msg_and_die("cannot stat %s"); 54 bb_perror_msg_and_die("cannot stat %s", device);
55 } 55 }
56 56
57 /* test for holes */ 57 /* test for holes */
58 if (S_ISREG(st.st_mode)) { 58 if (S_ISREG(st.st_mode)) {
59 if (st.st_blocks * 512 < st.st_size) { 59 if (st.st_blocks * 512 < st.st_size) {
60 error_msg_and_die("swap file has holes"); 60 bb_error_msg_and_die("swap file has holes");
61 } 61 }
62 } 62 }
63 63
@@ -67,7 +67,7 @@ static int swap_enable_disable(const char *device)
67 status = swapoff(device); 67 status = swapoff(device);
68 68
69 if (status != 0) { 69 if (status != 0) {
70 perror_msg("%s", device); 70 bb_perror_msg("%s", device);
71 return EXIT_FAILURE; 71 return EXIT_FAILURE;
72 } 72 }
73 return EXIT_SUCCESS; 73 return EXIT_SUCCESS;
@@ -80,7 +80,7 @@ static int do_em_all(void)
80 int err = 0; 80 int err = 0;
81 81
82 if (f == NULL) 82 if (f == NULL)
83 perror_msg_and_die("/etc/fstab"); 83 bb_perror_msg_and_die("/etc/fstab");
84 while ((m = getmntent(f)) != NULL) { 84 while ((m = getmntent(f)) != NULL) {
85 if (strcmp(m->mnt_type, MNTTYPE_SWAP)==0) { 85 if (strcmp(m->mnt_type, MNTTYPE_SWAP)==0) {
86 if(swap_enable_disable(m->mnt_fsname) == EXIT_FAILURE) 86 if(swap_enable_disable(m->mnt_fsname) == EXIT_FAILURE)
@@ -94,7 +94,7 @@ static int do_em_all(void)
94 94
95extern int swap_on_off_main(int argc, char **argv) 95extern int swap_on_off_main(int argc, char **argv)
96{ 96{
97 if (applet_name[5] == 'f') { /* "swapoff" */ 97 if (bb_applet_name[5] == 'f') { /* "swapoff" */
98 whichApp = SWAPOFF_APP; 98 whichApp = SWAPOFF_APP;
99 } 99 }
100 100
@@ -113,7 +113,7 @@ extern int swap_on_off_main(int argc, char **argv)
113 struct stat statBuf; 113 struct stat statBuf;
114 114
115 if (stat("/etc/fstab", &statBuf) < 0) 115 if (stat("/etc/fstab", &statBuf) < 0)
116 error_msg_and_die("/etc/fstab file missing"); 116 bb_error_msg_and_die("/etc/fstab file missing");
117 } 117 }
118 return do_em_all(); 118 return do_em_all();
119 break; 119 break;
@@ -124,5 +124,5 @@ extern int swap_on_off_main(int argc, char **argv)
124 return swap_enable_disable(*argv); 124 return swap_enable_disable(*argv);
125 125
126 usage_and_exit: 126 usage_and_exit:
127 show_usage(); 127 bb_show_usage();
128} 128}
diff --git a/util-linux/umount.c b/util-linux/umount.c
index 391d245a8..1d3e89301 100644
--- a/util-linux/umount.c
+++ b/util-linux/umount.c
@@ -67,7 +67,6 @@ static int useMtab = TRUE;
67#endif 67#endif
68static int umountAll = FALSE; 68static int umountAll = FALSE;
69static int doRemount = FALSE; 69static int doRemount = FALSE;
70extern const char mtab_file[]; /* Defined in utility.c */
71 70
72 71
73 72
@@ -87,8 +86,8 @@ static void mtab_read(void)
87 if (mtab_cache != NULL) 86 if (mtab_cache != NULL)
88 return; 87 return;
89 88
90 if ((fp = setmntent(mtab_file, "r")) == NULL) { 89 if ((fp = setmntent(bb_path_mtab_file, "r")) == NULL) {
91 error_msg("Cannot open %s", mtab_file); 90 bb_error_msg("Cannot open %s", bb_path_mtab_file);
92 return; 91 return;
93 } 92 }
94 while ((e = getmntent(fp))) { 93 while ((e = getmntent(fp))) {
@@ -185,7 +184,7 @@ static int do_umount(const char *name)
185 if (status != 0 && doForce) { 184 if (status != 0 && doForce) {
186 status = umount2(blockDevice, MNT_FORCE); 185 status = umount2(blockDevice, MNT_FORCE);
187 if (status != 0) { 186 if (status != 0) {
188 error_msg_and_die("forced umount of %s failed!", blockDevice); 187 bb_error_msg_and_die("forced umount of %s failed!", blockDevice);
189 } 188 }
190 } 189 }
191#endif 190#endif
@@ -193,9 +192,9 @@ static int do_umount(const char *name)
193 status = mount(blockDevice, name, NULL, 192 status = mount(blockDevice, name, NULL,
194 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL); 193 MS_MGC_VAL | MS_REMOUNT | MS_RDONLY, NULL);
195 if (status == 0) { 194 if (status == 0) {
196 error_msg("%s busy - remounted read-only", blockDevice); 195 bb_error_msg("%s busy - remounted read-only", blockDevice);
197 } else { 196 } else {
198 error_msg("Cannot remount %s read-only", blockDevice); 197 bb_error_msg("Cannot remount %s read-only", blockDevice);
199 } 198 }
200 } 199 }
201 if (status == 0) { 200 if (status == 0) {
@@ -221,7 +220,7 @@ static int umount_all(void)
221 if (!do_umount(mountpt)) { 220 if (!do_umount(mountpt)) {
222 /* Don't bother retrying the umount on busy devices */ 221 /* Don't bother retrying the umount on busy devices */
223 if (errno == EBUSY) { 222 if (errno == EBUSY) {
224 perror_msg("%s", mountpt); 223 bb_perror_msg("%s", mountpt);
225 status = FALSE; 224 status = FALSE;
226 continue; 225 continue;
227 } 226 }
@@ -241,7 +240,7 @@ extern int umount_main(int argc, char **argv)
241 char path[PATH_MAX]; 240 char path[PATH_MAX];
242 241
243 if (argc < 2) { 242 if (argc < 2) {
244 show_usage(); 243 bb_show_usage();
245 } 244 }
246#ifdef CONFIG_FEATURE_CLEAN_UP 245#ifdef CONFIG_FEATURE_CLEAN_UP
247 atexit(mtab_free); 246 atexit(mtab_free);
@@ -275,7 +274,7 @@ extern int umount_main(int argc, char **argv)
275 case 'v': 274 case 'v':
276 break; /* ignore -v */ 275 break; /* ignore -v */
277 default: 276 default:
278 show_usage(); 277 bb_show_usage();
279 } 278 }
280 } 279 }
281 280
@@ -287,9 +286,9 @@ extern int umount_main(int argc, char **argv)
287 return EXIT_FAILURE; 286 return EXIT_FAILURE;
288 } 287 }
289 if (realpath(*argv, path) == NULL) 288 if (realpath(*argv, path) == NULL)
290 perror_msg_and_die("%s", path); 289 bb_perror_msg_and_die("%s", path);
291 if (do_umount(path)) 290 if (do_umount(path))
292 return EXIT_SUCCESS; 291 return EXIT_SUCCESS;
293 perror_msg_and_die("%s", *argv); 292 bb_perror_msg_and_die("%s", *argv);
294} 293}
295 294