diff options
author | Manuel Novoa III <mjn3@codepoet.org> | 2003-03-19 09:13:01 +0000 |
---|---|---|
committer | Manuel Novoa III <mjn3@codepoet.org> | 2003-03-19 09:13:01 +0000 |
commit | cad5364599eb5062d59e0c397ed638ddd61a8d5d (patch) | |
tree | a318d0f03aa076c74b576ea45dc543a5669e8e91 /libbb | |
parent | e01f9662a5bd5d91be4f6b3941b57fff73cd5af1 (diff) | |
download | busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.gz busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.bz2 busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.zip |
Major coreutils update.
Diffstat (limited to 'libbb')
78 files changed, 1823 insertions, 1049 deletions
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 | ||
49 | LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC)) | 53 | LIBBB_OBJS=$(patsubst %.c,$(LIBBB_DIR)%.o, $(LIBBB_SRC)) |
50 | 54 | ||
51 | LIBBB_MSRC:=$(LIBBB_DIR)messages.c | 55 | LIBBB_MSRC0:=$(LIBBB_DIR)messages.c |
52 | LIBBB_MOBJ:=full_version.o name_too_long.o omitting_directory.o not_a_directory.o \ | 56 | LIBBB_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 \ |
57 | LIBBB_MOBJS=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ)) | 61 | securetty_file.o motd_file.o \ |
62 | msg_standard_input.o msg_standard_output.o | ||
63 | |||
64 | LIBBB_MSRC1:=$(LIBBB_DIR)xfuncs.c | ||
65 | LIBBB_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 | |||
69 | LIBBB_MSRC2:=$(LIBBB_DIR)printf.c | ||
70 | LIBBB_MOBJ2:=vfprintf.o vprintf.o fprintf.o printf.o | ||
71 | |||
72 | LIBBB_MSRC3:=$(LIBBB_DIR)xgetularg.c | ||
73 | LIBBB_MOBJ3:=xgetularg_bnd_sfx.o xgetlarg_bnd_sfx.o getlarg10_sfx.o \ | ||
74 | xgetularg_bnd.o xgetularg10_bnd.o xgetularg10.o | ||
75 | |||
76 | LIBBB_MOBJS0=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ0)) | ||
77 | LIBBB_MOBJS1=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ1)) | ||
78 | LIBBB_MOBJS2=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ2)) | ||
79 | LIBBB_MOBJS3=$(patsubst %,$(LIBBB_DIR)%, $(LIBBB_MOBJ3)) | ||
58 | 80 | ||
59 | libraries-y+=$(LIBBB_DIR)$(LIBBB_AR) | 81 | libraries-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 | 31 | int bb_ask_confirmation(void) | |
28 | int 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 | } |
44 | Local Variables: | ||
45 | c-file-style: "linux" | ||
46 | c-basic-offset: 4 | ||
47 | tab-width: 4 | ||
48 | End: | ||
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 | 10 | void bb_xasprintf(char **string_ptr, const char *format, ...) | |
13 | void 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 @@ | |||
43 | void change_identity ( const struct passwd *pw ) | 43 | void 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, ×) < 0) | 258 | if (utime(dest, ×) < 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. */ | ||
30 | extern 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 |
28 | extern 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 */ | ||
33 | extern 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 | /* | ||
81 | Local Variables: | ||
82 | c-file-style: "linux" | ||
83 | c-basic-offset: 4 | ||
84 | tab-width: 4 | ||
85 | End: | ||
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 | |||
32 | int 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 | ||
33 | extern | ||
30 | char *dirname(char *path) | 34 | char *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 | ||
31 | enum _vflag vflag = FIRST; | 31 | enum _vflag bb_dump_vflag = FIRST; |
32 | FS *fshead; /* head of format strings */ | 32 | FS *bb_dump_fshead; /* head of format strings */ |
33 | extern FS *fshead; /* head of format strings */ | ||
34 | extern int blocksize; | ||
35 | static FU *endfu; | 33 | static FU *endfu; |
36 | static char **_argv; | 34 | static char **_argv; |
37 | static off_t savaddress; /* saved address/offset in stream */ | 35 | static off_t savaddress; /* saved address/offset in stream */ |
38 | static off_t eaddress; /* end address */ | 36 | static off_t eaddress; /* end address */ |
39 | static off_t address; /* address/offset in stream */ | 37 | static off_t address; /* address/offset in stream */ |
40 | off_t skip; /* bytes to skip */ | 38 | off_t bb_dump_skip; /* bytes to skip */ |
41 | off_t saveaddress; | 39 | static int exitval; /* final exit value */ |
42 | int exitval; /* final exit value */ | 40 | int bb_dump_blocksize; /* data block size */ |
43 | int blocksize; /* data block size */ | 41 | int bb_dump_length = -1; /* max bytes to read */ |
44 | int length = -1; /* max bytes to read */ | 42 | |
43 | static const char index_str[] = ".#-+ 0123456789"; | ||
45 | 44 | ||
45 | static const char size_conv_str[] = | ||
46 | "\x1\x4\x4\x4\x4\x4\x4\x8\x8\x8\x8\010cdiouxXeEfgG"; | ||
46 | 47 | ||
47 | int size(FS * fs) | 48 | static const char lcc[] = "diouxX"; |
49 | |||
50 | int 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 | ||
109 | void rewrite(FS * fs) | 94 | static 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 | ||
372 | static void doskip(char *fname, int statok) | 289 | static 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 | ||
396 | int next(char **argv) | 313 | static 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 | ||
514 | void conv_c(PR * pr, u_char * p) | 432 | static 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 | |||
444 | static 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 | ||
558 | void conv_u(PR * pr, u_char * p) | 469 | static 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 | ||
583 | void display(void) | 493 | static 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 | ||
732 | int dump(char **argv) | 642 | int 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 | ||
754 | void add(char *fmt) | 664 | void 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 | ||
28 | extern void error_msg(const char *s, ...) | 28 | extern 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 | ||
28 | extern void error_msg_and_die(const char *s, ...) | 28 | extern 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 | |||
31 | int 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 | |||
31 | void 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 | */ |
33 | int full_read(int fd, char *buf, int len) | 32 | ssize_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 | */ |
31 | int full_write(int fd, const char *buf, int len) | 31 | ssize_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 | ||
27 | char *get_last_path_component(char *path) | 27 | char *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) |
31 | extern char *get_line_from_file(FILE *file) | 30 | * is removed. In event of a read error or EOF, NULL is returned. */ |
31 | |||
32 | static 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 | ||
65 | extern char *bb_get_line_from_file(FILE *file) | ||
66 | { | ||
67 | return private_get_line_from_file(file, 0); | ||
68 | } | ||
69 | |||
70 | extern 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 | 27 | unsigned 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 | */ | ||
33 | const 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 | /* | ||
57 | Local Variables: | ||
58 | c-file-style: "linux" | ||
59 | c-basic-offset: 4 | ||
60 | tab-width: 4 | ||
61 | End: | ||
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 | ||
27 | extern void herror_msg(const char *s, ...) | 27 | extern 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 | ||
27 | extern void herror_msg_and_die(const char *s, ...) | 27 | extern 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. */ |
1512 | static int do_ppp(int fd) | 1512 | static 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. | 42 | int bb_make_directory (char *path, long mode, int flags) |
38 | * Also create parent directories as necessary if flags contains | ||
39 | * FILEUTILS_RECUR. */ | ||
40 | |||
41 | int 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" |
72 | const char * const passwd_file = PASSWD_FILE; | 61 | const 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" |
77 | const char * const shadow_file = SHADOW_FILE; | 66 | const 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" |
82 | const char * const group_file = GROUP_FILE; | 71 | const 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" |
87 | const char * const gshadow_file = GSHADOW_FILE; | 76 | const 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" |
92 | const char * const nologin_file = NOLOGIN_FILE; | 81 | const 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" |
97 | const char * const securetty_file = SECURETTY_FILE; | 86 | const 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" |
102 | const char * const motd_file = MOTD_FILE; | 91 | const 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 */ | 44 | static const mode_t mode_flags[] = { |
33 | static 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 |
40 | static 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 | ||
46 | static const char MODE1[] = "rwxrwxrwx"; | 54 | /* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C', |
47 | static const char MODE0[] = "---------"; | 55 | * and 'B' types don't appear to be available on linux. So I removed them. */ |
48 | static const char SMODE1[] = "..s..s..t"; | 56 | static const char type_chars[16] = "?pc?d?b?-?l?s???"; |
49 | static const char SMODE0[] = "..S..S..T"; | 57 | /* 0123456789abcdef */ |
58 | static const char mode_chars[7] = "rwxSTst"; | ||
50 | 59 | ||
51 | /* | 60 | const 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 | */ | ||
55 | const 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 | |
73 | Local Variables: | 94 | /* The previous version used "0pcCd?bB-?l?s???". However, the '0', 'C', |
74 | c-file-style: "linux" | 95 | * and 'B' types don't appear to be available on linux. So I removed them. */ |
75 | c-basic-offset: 4 | 96 | static const char type_chars[16] = "?pc?d?b?-?l?s???"; |
76 | tab-width: 4 | 97 | /* 0123456789abcdef */ |
77 | End: | 98 | static const char mode_chars[7] = "rwxSTst"; |
78 | */ | 99 | |
100 | const 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... |
52 | int query_module(const char *name, int which, void *buf, size_t bufsize, size_t *ret) | 52 | int 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 | ||
30 | extern const char mtab_file[]; /* Defined in utility.c */ | ||
31 | static const int MS_RDONLY = 1; /* Mount read-only. */ | 30 | static const int MS_RDONLY = 1; /* Mount read-only. */ |
32 | 31 | ||
33 | void erase_mtab(const char *name) | 32 | void 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 | ||
75 | void write_mtab(char *blockDevice, char *directory, | 74 | void 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 |
29 | const char mtab_file[] = CONFIG_FEATURE_MTAB_FILENAME; | 29 | const char bb_path_mtab_file[] = CONFIG_FEATURE_MTAB_FILENAME; |
30 | #else | 30 | #else |
31 | const char mtab_file[] = "/proc/mounts"; | 31 | const 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 | 32 | extern 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. */ | ||
32 | extern 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 | /* | ||
129 | Local Variables: | ||
130 | c-file-style: "linux" | ||
131 | c-basic-offset: 4 | ||
132 | tab-width: 4 | ||
133 | End: | ||
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 | 30 | extern | |
30 | unsigned long parse_number(const char *numstr, | 31 | unsigned 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 | /* | ||
65 | Local Variables: | ||
66 | c-file-style: "linux" | ||
67 | c-basic-offset: 4 | ||
68 | tab-width: 4 | ||
69 | End: | ||
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 | ||
28 | extern void perror_msg(const char *s, ...) | 28 | extern 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 | ||
28 | extern void perror_msg_and_die(const char *s, ...) | 28 | extern 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 | |||
26 | extern 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 | |||
26 | extern 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 | 26 | extern void bb_xprint_and_close_file(FILE *file) | |
28 | extern 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 | ||
37 | extern 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 | |||
47 | extern 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 | /* |
59 | Local Variables: | 70 | Local 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 | ||
129 | extern 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 | ||
144 | extern 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 | ||
151 | extern 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 | ||
166 | extern 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 | 29 | char bb_process_escape_sequence(const char **ptr) | |
30 | |||
31 | char 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 | /* |
75 | Local Variables: | 76 | Local 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 @@ | |||
45 | static void xsetenv ( const char *key, const char *value ) | 45 | static 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 | ||
51 | void setup_environment ( const char *shell, int loginshell, int changeenv, const struct passwd *pw ) | 51 | void 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 | ||
27 | char *simplify_path(const char *path) | 26 | char *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 | |||
26 | extern 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 | |||
26 | struct speed_map { | ||
27 | unsigned short speed; | ||
28 | unsigned short value; | ||
29 | }; | ||
30 | |||
31 | static 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 | |||
70 | static const int NUM_SPEEDS = (sizeof(speeds) / sizeof(struct speed_map)); | ||
71 | |||
72 | unsigned 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 | |||
88 | speed_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 | |||
105 | int 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 | ||
33 | void syslog_msg(int facility, int pri, const char *msg) | 33 | void 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 | ||
28 | extern void verror_msg(const char *s, va_list p) | 28 | extern 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 | ||
29 | extern void vherror_msg(const char *s, va_list p) | 29 | extern 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 | ||
28 | extern void vperror_msg(const char *s, va_list p) | 28 | extern 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 | |||
25 | extern 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 | ||
26 | FILE *wfopen(const char *path, const char *mode) | 26 | FILE *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 | |||
34 | FILE *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 | ||
33 | extern void *xmalloc(size_t size) | 34 | extern 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 | ||
41 | extern void *xrealloc(void *ptr, size_t size) | 44 | extern 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 | ||
49 | extern void *xcalloc(size_t nmemb, size_t size) | 54 | extern 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 | ||
57 | extern char * xstrdup (const char *s) { | 63 | #ifdef L_xstrdup |
64 | extern 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 | ||
72 | extern char * xstrndup (const char *s, int n) { | 80 | #ifdef L_xstrndup |
81 | extern 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 | ||
83 | FILE *xfopen(const char *path, const char *mode) | 93 | #ifdef L_xfopen |
94 | FILE *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 | ||
91 | extern int xopen(const char *pathname, int flags) | 103 | #ifdef L_xopen |
104 | extern 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 | ||
102 | extern ssize_t xread(int fd, void *buf, size_t count) | 116 | #ifdef L_xread |
117 | extern 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 | ||
113 | extern void xread_all(int fd, void *buf, size_t count) | 129 | #ifdef L_xread_all |
130 | extern 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 | ||
124 | extern unsigned char xread_char(int fd) | 144 | #ifdef L_xread_char |
145 | extern 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 | ||
156 | extern 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 | ||
165 | extern void bb_xferror_stdout(void) | ||
166 | { | ||
167 | bb_xferror(stdout, bb_msg_standard_output); | ||
168 | } | ||
169 | #endif | ||
170 | |||
171 | #ifdef L_xfflush_stdout | ||
172 | extern 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 |
135 | size_t xstrlen(const char *string) | 183 | size_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 | ||
32 | extern | ||
33 | unsigned 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 | ||
95 | extern | ||
96 | long 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 | ||
127 | extern | ||
128 | long 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 | ||
135 | extern | ||
136 | unsigned 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 | ||
145 | extern | ||
146 | unsigned 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 | ||
155 | extern | ||
156 | unsigned 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 | ||