*: shrink by using [f]open_or_warn_stdin where appropriate

function                                             old     new   delta
lsattr_main                                           62     143     +81
open_or_warn_stdin                                     -      36     +36
fclose_if_not_stdin                                   20      47     +27
xfopen_stdin                                           -      20     +20
tac_main                                             336     356     +20
cksum_main                                           249     259     +10
bb_argv_dash                                           -       8      +8
su_main                                              448     455      +7
cmp_main                                             630     633      +3
passwd_main                                         1072    1074      +2
uudecode_main                                        317     315      -2
text_yank                                            110     108      -2
handle_incoming_and_exit                            2653    2651      -2
flags                                                  5       1      -4
write_leases                                         235     230      -5
fopen_or_warn_stdin                                   48      42      -6
fold_main                                            648     642      -6
static.argv_dash                                       8       -      -8
sum_main                                             142     128     -14
tail_main                                           1237    1221     -16
sed_main                                             711     695     -16
cmp_xfopen_input                                      17       -     -17
bb_cat                                               113      96     -17
catv_main                                            328     306     -22
strings_main                                         457     434     -23
hash_file                                            298     274     -24
sum_file                                             353     325     -28
sort_main                                            904     859     -45
expand_main                                          736     686     -50
cut_main                                            1116    1065     -51
md5_sha1_sum_main                                    549     493     -56
lsattr_args                                           90       -     -90
read_stduu                                           408     255    -153
------------------------------------------------------------------------------
(add/remove: 3/3 grow/shrink: 7/20 up/down: 214/-657)        Total: -443 bytes
   text    data     bss     dec     hex filename
 797417     658    7428  805503   c4a7f busybox_old
 796973     658    7428  805059   c48c3 busybox_unstripped
This commit is contained in:
Denis Vlasenko 2008-03-17 09:07:36 +00:00
parent d02db89244
commit 62a90cdd74
26 changed files with 214 additions and 219 deletions

View file

@ -17,18 +17,14 @@
int bb_cat(char **argv) int bb_cat(char **argv)
{ {
static const char *const argv_dash[] = { "-", NULL };
int fd; int fd;
int retval = EXIT_SUCCESS; int retval = EXIT_SUCCESS;
if (!*argv) if (!*argv)
argv = (char**) &argv_dash; argv = (char**) &bb_argv_dash;
do { do {
fd = STDIN_FILENO; fd = open_or_warn_stdin(*argv);
if (!LONE_DASH(*argv))
fd = open_or_warn(*argv, O_RDONLY);
if (fd >= 0) { if (fd >= 0) {
/* This is not a xfunc - never exits */ /* This is not a xfunc - never exits */
off_t r = bb_copyfd_eof(fd, STDOUT_FILENO); off_t r = bb_copyfd_eof(fd, STDOUT_FILENO);

View file

@ -27,18 +27,14 @@ int catv_main(int argc ATTRIBUTE_UNUSED, char **argv)
argv += optind; argv += optind;
/* Read from stdin if there's nothing else to do. */ /* Read from stdin if there's nothing else to do. */
fd = 0; if (!argv[0])
if (!argv[0]) { *--argv = (char*)"-";
argv--;
goto jump_in;
}
do { do {
fd = open_or_warn(*argv, O_RDONLY); fd = open_or_warn_stdin(*argv);
if (fd < 0) { if (fd < 0) {
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
continue; continue;
} }
jump_in:
for (;;) { for (;;) {
int i, res; int i, res;

View file

@ -9,46 +9,48 @@
#include "libbb.h" #include "libbb.h"
int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int cksum_main(int argc, char **argv) int cksum_main(int argc ATTRIBUTE_UNUSED, char **argv)
{ {
uint32_t *crc32_table = crc32_filltable(NULL, 1); uint32_t *crc32_table = crc32_filltable(NULL, 1);
FILE *fp;
uint32_t crc; uint32_t crc;
long length, filesize; long length, filesize;
int bytes_read; int bytes_read;
char *cp; uint8_t *cp;
int inp_stdin = (argc == optind) ? 1 : 0; #if ENABLE_DESKTOP
getopt32(argv, ""); /* coreutils 6.9 compat */
argv += optind;
#else
argv++;
#endif
do { do {
fp = fopen_or_warn_stdin((inp_stdin) ? bb_msg_standard_input : *++argv); int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input);
if (fd < 0)
continue;
crc = 0; crc = 0;
length = 0; length = 0;
#define read_buf bb_common_bufsiz1 #define read_buf bb_common_bufsiz1
while ((bytes_read = fread(read_buf, 1, BUFSIZ, fp)) > 0) { while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) {
cp = read_buf; cp = read_buf;
length += bytes_read; length += bytes_read;
while (bytes_read--) do {
crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ (*cp++)) & 0xffL]; crc = (crc << 8) ^ crc32_table[(crc >> 24) ^ *cp++];
} while (--bytes_read);
} }
close(fd);
filesize = length; filesize = length;
for (; length; length >>= 8) for (; length; length >>= 8)
crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ length) & 0xffL]; crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ length) & 0xff];
crc ^= 0xffffffffL; crc ^= 0xffffffffL;
if (inp_stdin) { printf((*argv ? "%" PRIu32 " %li %s\n" : "%" PRIu32 " %li\n"),
printf("%" PRIu32 " %li\n", crc, filesize); crc, filesize, *argv);
break; } while (*argv && *++argv);
}
printf("%" PRIu32 " %li %s\n", crc, filesize, *argv);
fclose(fp);
} while (*(argv + 1));
fflush_stdout_and_exit(EXIT_SUCCESS); fflush_stdout_and_exit(EXIT_SUCCESS);
} }

View file

@ -205,7 +205,7 @@ int cut_main(int argc ATTRIBUTE_UNUSED, char **argv)
char *ntok; char *ntok;
int s = 0, e = 0; int s = 0, e = 0;
/* take apart the lists, one by one (they are separated with commas */ /* take apart the lists, one by one (they are separated with commas) */
while ((ltok = strsep(&sopt, ",")) != NULL) { while ((ltok = strsep(&sopt, ",")) != NULL) {
/* it's actually legal to pass an empty list */ /* it's actually legal to pass an empty list */
@ -258,25 +258,18 @@ int cut_main(int argc ATTRIBUTE_UNUSED, char **argv)
{ {
int retval = EXIT_SUCCESS; int retval = EXIT_SUCCESS;
FILE *file = stdin;
if (!*argv) { if (!*argv)
argv--; *--argv = (char *)"-";
goto jump_in;
}
do { do {
file = stdin; FILE *file = fopen_or_warn_stdin(*argv);
if (NOT_LONE_DASH(*argv))
file = fopen_or_warn(*argv, "r");
if (!file) { if (!file) {
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
continue; continue;
} }
jump_in:
cut_file(file, delim); cut_file(file, delim);
if (NOT_LONE_DASH(*argv)) fclose_if_not_stdin(file);
fclose(file);
} while (*++argv); } while (*++argv);
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)

View file

@ -154,7 +154,7 @@ int expand_main(int argc ATTRIBUTE_UNUSED, char **argv)
if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) { if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) {
USE_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts); USE_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts);
opt = getopt32(argv, "it:", &opt_t); opt = getopt32(argv, "it:", &opt_t);
} else if (ENABLE_UNEXPAND) { } else {
USE_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts); USE_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts);
/* -t NUM sets also -a */ /* -t NUM sets also -a */
opt_complementary = "ta"; opt_complementary = "ta";
@ -166,32 +166,23 @@ int expand_main(int argc ATTRIBUTE_UNUSED, char **argv)
argv += optind; argv += optind;
/* If no args are given, read from stdin */
if (!*argv) { if (!*argv) {
*--argv = (char*)bb_msg_standard_input; *--argv = (char*)bb_msg_standard_input;
goto use_stdin;
} }
do { do {
if (NOT_LONE_CHAR(*argv, '-')) { file = fopen_or_warn_stdin(*argv);
file = fopen_or_warn(*argv, "r");
if (!file) { if (!file) {
exit_status = EXIT_FAILURE; exit_status = EXIT_FAILURE;
continue; continue;
} }
} else {
use_stdin:
file = stdin;
}
if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e'))
USE_EXPAND(expand(file, tab_size, opt)); USE_EXPAND(expand(file, tab_size, opt));
else if (ENABLE_UNEXPAND) else
USE_UNEXPAND(unexpand(file, tab_size, opt)); USE_UNEXPAND(unexpand(file, tab_size, opt));
/* Check and close the file */ /* Check and close the file */
/* We do want all of them to execute, thus | instead of || */ if (fclose_if_not_stdin(file)) {
if (ferror(file) | fclose_if_not_stdin(file)) {
bb_simple_perror_msg(*argv); bb_simple_perror_msg(*argv);
exit_status = EXIT_FAILURE; exit_status = EXIT_FAILURE;
} }

View file

@ -12,7 +12,7 @@
#include "libbb.h" #include "libbb.h"
static unsigned long flags; /* Must match getopt32 call */
#define FLAG_COUNT_BYTES 1 #define FLAG_COUNT_BYTES 1
#define FLAG_BREAK_SPACES 2 #define FLAG_BREAK_SPACES 2
#define FLAG_WIDTH 4 #define FLAG_WIDTH 4
@ -20,10 +20,9 @@ static unsigned long flags;
/* Assuming the current column is COLUMN, return the column that /* Assuming the current column is COLUMN, return the column that
printing C will move the cursor to. printing C will move the cursor to.
The first column is 0. */ The first column is 0. */
static int adjust_column(int column, char c) static int adjust_column(int column, char c)
{ {
if (!(flags & FLAG_COUNT_BYTES)) { if (!(option_mask32 & FLAG_COUNT_BYTES)) {
if (c == '\b') { if (c == '\b') {
if (column > 0) if (column > 0)
column--; column--;
@ -54,29 +53,27 @@ int fold_main(int argc, char **argv)
char const *a = argv[i]; char const *a = argv[i];
if (*a++ == '-') { if (*a++ == '-') {
if (*a == '-' && !a[1]) if (*a == '-' && !a[1]) /* "--" */
break; break;
if (isdigit(*a)) { if (isdigit(*a))
argv[i] = xasprintf("-w%s", a); argv[i] = xasprintf("-w%s", a);
} }
} }
} }
}
flags = getopt32(argv, "bsw:", &w_opt); getopt32(argv, "bsw:", &w_opt);
if (flags & FLAG_WIDTH) if (option_mask32 & FLAG_WIDTH)
width = xatoul_range(w_opt, 1, 10000); width = xatoul_range(w_opt, 1, 10000);
argv += optind; argv += optind;
if (!*argv) { if (!*argv)
*--argv = (char*)"-"; *--argv = (char*)"-";
}
do { do {
FILE *istream = fopen_or_warn_stdin(*argv); FILE *istream = fopen_or_warn_stdin(*argv);
int c; int c;
int column = 0; /* Screen column where next char will go. */ int column = 0; /* Screen column where next char will go. */
int offset_out = 0; /* Index in `line_out' for next char. */ int offset_out = 0; /* Index in 'line_out' for next char. */
if (istream == NULL) { if (istream == NULL) {
errs |= EXIT_FAILURE; errs |= EXIT_FAILURE;
@ -102,7 +99,7 @@ int fold_main(int argc, char **argv)
/* This character would make the line too long. /* This character would make the line too long.
Print the line plus a newline, and make this character Print the line plus a newline, and make this character
start the next line. */ start the next line. */
if (flags & FLAG_BREAK_SPACES) { if (option_mask32 & FLAG_BREAK_SPACES) {
/* Look for the last blank. */ /* Look for the last blank. */
int logical_end; int logical_end;
@ -144,7 +141,7 @@ int fold_main(int argc, char **argv)
fwrite(line_out, sizeof(char), (size_t) offset_out, stdout); fwrite(line_out, sizeof(char), (size_t) offset_out, stdout);
} }
if (ferror(istream) || fclose_if_not_stdin(istream)) { if (fclose_if_not_stdin(istream)) {
bb_simple_perror_msg(*argv); /* Avoid multibyte problems. */ bb_simple_perror_msg(*argv); /* Avoid multibyte problems. */
errs |= EXIT_FAILURE; errs |= EXIT_FAILURE;
} }

View file

@ -91,19 +91,19 @@ int head_main(int argc, char **argv)
} }
} }
argc -= optind;
argv += optind; argv += optind;
if (!*argv) { if (!*argv)
*--argv = (char*)"-"; *--argv = (char*)"-";
}
fmt = header_fmt_str + 1; fmt = header_fmt_str + 1;
#if ENABLE_FEATURE_FANCY_HEAD #if ENABLE_FEATURE_FANCY_HEAD
if (argc - optind <= header_threshhold) { if (argc <= header_threshhold) {
header_threshhold = 0; header_threshhold = 0;
} }
#else #else
if (argc <= optind + 1) { if (argc <= 1) {
fmt += 11; fmt += 11; /* "" */
} }
/* Now define some things here to avoid #ifdefs in the code below. /* Now define some things here to avoid #ifdefs in the code below.
* These should optimize out of the if conditions below. */ * These should optimize out of the if conditions below. */

View file

@ -36,13 +36,10 @@ static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
void (*update)(const void*, size_t, void*); void (*update)(const void*, size_t, void*);
void (*final)(void*, void*); void (*final)(void*, void*);
src_fd = STDIN_FILENO; src_fd = open_or_warn_stdin(filename);
if (NOT_LONE_DASH(filename)) {
src_fd = open_or_warn(filename, O_RDONLY);
if (src_fd < 0) { if (src_fd < 0) {
return NULL; return NULL;
} }
}
/* figure specific hash algorithims */ /* figure specific hash algorithims */
if (ENABLE_MD5SUM && hash_algo==HASH_MD5) { if (ENABLE_MD5SUM && hash_algo==HASH_MD5) {
@ -78,7 +75,7 @@ static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo)
} }
int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int md5_sha1_sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int md5_sha1_sum_main(int argc, char **argv) int md5_sha1_sum_main(int argc ATTRIBUTE_UNUSED, char **argv)
{ {
int return_value = EXIT_SUCCESS; int return_value = EXIT_SUCCESS;
uint8_t *hash_value; uint8_t *hash_value;
@ -90,6 +87,10 @@ int md5_sha1_sum_main(int argc, char **argv)
if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK) if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK)
flags = getopt32(argv, "scw"); flags = getopt32(argv, "scw");
else optind = 1; else optind = 1;
argv += optind;
//argc -= optind;
if (!*argv)
*--argv = (char*)"-";
if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) { if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && !(flags & FLAG_CHECK)) {
if (flags & FLAG_SILENT) { if (flags & FLAG_SILENT) {
@ -101,26 +102,18 @@ int md5_sha1_sum_main(int argc, char **argv)
} }
} }
if (argc == optind) {
argv[argc++] = (char*)"-";
}
if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) { if (ENABLE_FEATURE_MD5_SHA1_SUM_CHECK && (flags & FLAG_CHECK)) {
FILE *pre_computed_stream; FILE *pre_computed_stream;
int count_total = 0; int count_total = 0;
int count_failed = 0; int count_failed = 0;
char *file_ptr = argv[optind];
char *line; char *line;
if (optind + 1 != argc) { if (argv[1]) {
bb_error_msg_and_die bb_error_msg_and_die
("only one argument may be specified when using -c"); ("only one argument may be specified when using -c");
} }
pre_computed_stream = stdin; pre_computed_stream = xfopen_stdin(argv[0]);
if (NOT_LONE_DASH(file_ptr)) {
pre_computed_stream = xfopen(file_ptr, "r");
}
while ((line = xmalloc_getline(pre_computed_stream)) != NULL) { while ((line = xmalloc_getline(pre_computed_stream)) != NULL) {
char *filename_ptr; char *filename_ptr;
@ -168,17 +161,15 @@ int md5_sha1_sum_main(int argc, char **argv)
} }
*/ */
} else { } else {
while (optind < argc) { do {
char *file_ptr = argv[optind++]; hash_value = hash_file(*argv, hash_algo);
hash_value = hash_file(file_ptr, hash_algo);
if (hash_value == NULL) { if (hash_value == NULL) {
return_value = EXIT_FAILURE; return_value = EXIT_FAILURE;
} else { } else {
printf("%s %s\n", hash_value, file_ptr); printf("%s %s\n", hash_value, *argv);
free(hash_value); free(hash_value);
} }
} } while (*++argv);
} }
return return_value; return return_value;
} }

View file

@ -351,10 +351,13 @@ int sort_main(int argc ATTRIBUTE_UNUSED, char **argv)
if (option_mask32 & FLAG_b) option_mask32 |= FLAG_bb; if (option_mask32 & FLAG_b) option_mask32 |= FLAG_bb;
/* Open input files and read data */ /* Open input files and read data */
for (i = argv[optind] ? optind : optind-1; argv[i]; i++) { argv += optind;
fp = stdin; if (!*argv)
if (i >= optind && NOT_LONE_DASH(argv[i])) *--argv = (char*)"-";
fp = xfopen(argv[i], "r"); do {
/* coreutils 6.9 compat: abort on first open error,
* do not continue to next file: */
fp = xfopen_stdin(*argv);
for (;;) { for (;;) {
line = GET_LINE(fp); line = GET_LINE(fp);
if (!line) break; if (!line) break;
@ -362,8 +365,9 @@ int sort_main(int argc ATTRIBUTE_UNUSED, char **argv)
lines = xrealloc(lines, sizeof(char *) * (linecount + 64)); lines = xrealloc(lines, sizeof(char *) * (linecount + 64));
lines[linecount++] = line; lines[linecount++] = line;
} }
fclose(fp); fclose_if_not_stdin(fp);
} } while (*++argv);
#if ENABLE_FEATURE_SORT_BIG #if ENABLE_FEATURE_SORT_BIG
/* if no key, perform alphabetic sort */ /* if no key, perform alphabetic sort */
if (!key_list) if (!key_list)

View file

@ -21,20 +21,17 @@ enum { SUM_BSD, PRINT_NAME, SUM_SYSV };
The checksum varies depending on sizeof (int). */ The checksum varies depending on sizeof (int). */
/* SYSV: calculate and print the checksum and the size in 512-byte blocks */ /* SYSV: calculate and print the checksum and the size in 512-byte blocks */
/* Return 1 if successful. */ /* Return 1 if successful. */
static unsigned sum_file(const char *file, const unsigned type) static unsigned sum_file(const char *file, unsigned type)
{ {
#define buf bb_common_bufsiz1 #define buf bb_common_bufsiz1
unsigned long long total_bytes = 0; unsigned long long total_bytes = 0;
int fd = 0, r; int fd, r;
/* The sum of all the input bytes, modulo (UINT_MAX + 1). */ /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
unsigned s = 0; unsigned s = 0;
if (NOT_LONE_DASH(file)) { fd = open_or_warn_stdin(file);
fd = open(file, O_RDONLY);
if (fd == -1) if (fd == -1)
goto ret_bad; return 0;
}
while (1) { while (1) {
size_t bytes_read = safe_read(fd, buf, BUFSIZ); size_t bytes_read = safe_read(fd, buf, BUFSIZ);
@ -44,7 +41,6 @@ static unsigned sum_file(const char *file, const unsigned type)
if (!bytes_read && !r) if (!bytes_read && !r)
/* no error */ /* no error */
break; break;
ret_bad:
bb_perror_msg(file); bb_perror_msg(file);
return 0; return 0;
} }
@ -75,26 +71,29 @@ static unsigned sum_file(const char *file, const unsigned type)
} }
int sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int sum_main(int argc, char **argv) int sum_main(int argc ATTRIBUTE_UNUSED, char **argv)
{ {
unsigned n; unsigned n;
unsigned type = SUM_BSD; unsigned type = SUM_BSD;
n = getopt32(argv, "sr"); n = getopt32(argv, "sr");
argv += optind;
if (n & 1) type = SUM_SYSV; if (n & 1) type = SUM_SYSV;
/* give the bsd priority over sysv func */ /* give the bsd priority over sysv func */
if (n & 2) type = SUM_BSD; if (n & 2) type = SUM_BSD;
if (argc == optind) { if (!argv[0]) {
/* Do not print the name */ /* Do not print the name */
n = sum_file("-", type); n = sum_file("-", type);
} else { } else {
/* Need to print the name if either /* Need to print the name if either
- more than one file given - more than one file given
- doing sysv */ - doing sysv */
type += argc - 1 > optind || type == SUM_SYSV; type += (argv[1] || type == SUM_SYSV);
for (n = 1; optind < argc; optind++) n = 1;
n &= sum_file(argv[optind], type); do {
n &= sum_file(*argv, type);
} while (*++argv);
} }
return !n; return !n;
} }

View file

@ -22,7 +22,7 @@
struct lstring { struct lstring {
int size; int size;
char buf[]; char buf[1];
}; };
int tac_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int tac_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
@ -34,7 +34,21 @@ int tac_main(int argc ATTRIBUTE_UNUSED, char **argv)
llist_t *list = NULL; llist_t *list = NULL;
int retval = EXIT_SUCCESS; int retval = EXIT_SUCCESS;
#if ENABLE_DESKTOP
/* tac from coreutils 6.9 supports:
-b, --before
attach the separator before instead of after
-r, --regex
interpret the separator as a regular expression
-s, --separator=STRING
use STRING as the separator instead of newline
We support none, but at least we will complain or handle "--":
*/
getopt32(argv, "");
argv += optind;
#else
argv++; argv++;
#endif
if (!*argv) if (!*argv)
*--argv = (char *)"-"; *--argv = (char *)"-";
/* We will read from last file to first */ /* We will read from last file to first */
@ -48,6 +62,7 @@ int tac_main(int argc ATTRIBUTE_UNUSED, char **argv)
name--; name--;
f = fopen_or_warn_stdin(*name); f = fopen_or_warn_stdin(*name);
if (f == NULL) { if (f == NULL) {
/* error message is printed by fopen_or_warn_stdin */
retval = EXIT_FAILURE; retval = EXIT_FAILURE;
continue; continue;
} }
@ -61,7 +76,7 @@ int tac_main(int argc ATTRIBUTE_UNUSED, char **argv)
line = xrealloc(line, i + 0x7f + sizeof(int) + 1); line = xrealloc(line, i + 0x7f + sizeof(int) + 1);
line->buf[i++] = ch; line->buf[i++] = ch;
} }
if ((ch == '\n' || ch == EOF) && i) { if (ch == '\n' || (ch == EOF && i != 0)) {
line = xrealloc(line, i + sizeof(int)); line = xrealloc(line, i + sizeof(int));
line->size = i; line->size = i;
llist_add_to(&list, line); llist_add_to(&list, line);
@ -69,9 +84,8 @@ int tac_main(int argc ATTRIBUTE_UNUSED, char **argv)
i = 0; i = 0;
} }
} while (ch != EOF); } while (ch != EOF);
/* fgetc sets errno to ENOENT on EOF, but */ /* fgetc sets errno to ENOENT on EOF, we don't want
/* fopen_or_warn_stdin would catch this error */ * to warn on this non-error! */
/* so we can filter it out here. */
if (errno && errno != ENOENT) { if (errno && errno != ENOENT) {
bb_simple_perror_msg(*name); bb_simple_perror_msg(*name);
retval = EXIT_FAILURE; retval = EXIT_FAILURE;

View file

@ -101,16 +101,12 @@ int tail_main(int argc, char **argv)
#if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
/* Allow legacy syntax of an initial numeric option without -n. */ /* Allow legacy syntax of an initial numeric option without -n. */
if (argc >= 2 && (argv[1][0] == '+' || argv[1][0] == '-') if (argv[1] && (argv[1][0] == '+' || argv[1][0] == '-')
&& isdigit(argv[1][1]) && isdigit(argv[1][1])
) { ) {
/* replacing arg[0] with "-n" can segfault, so... */ count = eat_num(&argv[1][1]);
argv[1] = xasprintf("-n%s", argv[1]); argv++;
#if 0 /* If we ever decide to make tail NOFORK */ argc--;
char *s = alloca(strlen(argv[1]) + 3);
sprintf(s, "-n%s", argv[1]);
argv[1] = s;
#endif
} }
#endif #endif
@ -133,8 +129,7 @@ int tail_main(int argc, char **argv)
/* open all the files */ /* open all the files */
fds = xmalloc(sizeof(int) * (argc + 1)); fds = xmalloc(sizeof(int) * (argc + 1));
nfiles = i = 0; if (!argv[0]) {
if (argc == 0) {
struct stat statbuf; struct stat statbuf;
if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) { if (!fstat(STDIN_FILENO, &statbuf) && S_ISFIFO(statbuf.st_mode)) {
@ -142,13 +137,14 @@ int tail_main(int argc, char **argv)
} }
*argv = (char *) bb_msg_standard_input; *argv = (char *) bb_msg_standard_input;
} }
nfiles = i = 0;
do { do {
FILE* fil = fopen_or_warn_stdin(argv[i]); int fd = open_or_warn_stdin(argv[i]);
if (!fil) { if (fd < 0) {
G.status = EXIT_FAILURE; G.status = EXIT_FAILURE;
continue; continue;
} }
fds[nfiles] = fileno(fil); fds[nfiles] = fd;
argv[nfiles++] = argv[i]; argv[nfiles++] = argv[i];
} while (++i < argc); } while (++i < argc);

View file

@ -19,48 +19,57 @@ static void read_stduu(FILE *src_stream, FILE *dst_stream)
char *line; char *line;
while ((line = xmalloc_getline(src_stream)) != NULL) { while ((line = xmalloc_getline(src_stream)) != NULL) {
int length; int encoded_len, str_len;
char *line_ptr = line; char *line_ptr, *dst;
if (strcmp(line, "end") == 0) { if (strcmp(line, "end") == 0) {
return; return; /* the only non-error exit */
} }
length = ((*line_ptr - 0x20) & 0x3f)* 4 / 3;
if (length <= 0) { line_ptr = line;
while (*line_ptr) {
*line_ptr = (*line_ptr - 0x20) & 0x3f;
line_ptr++;
}
str_len = line_ptr - line;
encoded_len = line[0] * 4 / 3;
/* Check that line is not too short. (we tolerate
* overly _long_ line to accomodate possible extra '`').
* Empty line case is also caught here. */
if (str_len <= encoded_len) {
break; /* go to bb_error_msg_and_die("short file"); */
}
if (encoded_len <= 0) {
/* Ignore the "`\n" line, why is it even in the encode file ? */ /* Ignore the "`\n" line, why is it even in the encode file ? */
free(line);
continue; continue;
} }
if (length > 60) { if (encoded_len > 60) {
bb_error_msg_and_die("line too long"); bb_error_msg_and_die("line too long");
} }
line_ptr++; dst = line;
/* Tolerate an overly long line to accomodate a possible exta '`' */ line_ptr = line + 1;
if (strlen(line_ptr) < (size_t)length) { do {
bb_error_msg_and_die("short file");
}
while (length > 0) {
/* Merge four 6 bit chars to three 8 bit chars */ /* Merge four 6 bit chars to three 8 bit chars */
fputc(((line_ptr[0] - 0x20) & 077) << 2 | ((line_ptr[1] - 0x20) & 077) >> 4, dst_stream); *dst++ = line_ptr[0] << 2 | line_ptr[1] >> 4;
line_ptr++; encoded_len--;
length--; if (encoded_len == 0) {
if (length == 0) {
break; break;
} }
fputc(((line_ptr[0] - 0x20) & 077) << 4 | ((line_ptr[1] - 0x20) & 077) >> 2, dst_stream); *dst++ = line_ptr[1] << 4 | line_ptr[2] >> 2;
line_ptr++; encoded_len--;
length--; if (encoded_len == 0) {
if (length == 0) {
break; break;
} }
fputc(((line_ptr[0] - 0x20) & 077) << 6 | ((line_ptr[1] - 0x20) & 077), dst_stream); *dst++ = line_ptr[2] << 6 | line_ptr[3];
line_ptr += 2; line_ptr += 4;
length -= 2; encoded_len -= 2;
} } while (encoded_len > 0);
fwrite(line, 1, dst - line, dst_stream);
free(line); free(line);
} }
bb_error_msg_and_die("short file"); bb_error_msg_and_die("short file");
@ -129,7 +138,7 @@ static void read_base64(FILE *src_stream, FILE *dst_stream)
int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int uudecode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int uudecode_main(int argc ATTRIBUTE_UNUSED, char **argv) int uudecode_main(int argc ATTRIBUTE_UNUSED, char **argv)
{ {
FILE *src_stream = stdin; FILE *src_stream;
char *outname = NULL; char *outname = NULL;
char *line; char *line;
@ -137,8 +146,9 @@ int uudecode_main(int argc ATTRIBUTE_UNUSED, char **argv)
getopt32(argv, "o:", &outname); getopt32(argv, "o:", &outname);
argv += optind; argv += optind;
if (argv[0]) if (!*argv)
src_stream = xfopen(argv[0], "r"); *--argv = (char*)"-";
src_stream = xfopen_stdin(*argv);
/* Search for the start of the encoding */ /* Search for the start of the encoding */
while ((line = xmalloc_getline(src_stream)) != NULL) { while ((line = xmalloc_getline(src_stream)) != NULL) {
@ -159,7 +169,7 @@ int uudecode_main(int argc ATTRIBUTE_UNUSED, char **argv)
} }
/* begin line found. decode and exit */ /* begin line found. decode and exit */
mode = strtoul(line_ptr, NULL, 8); mode = bb_strtou(line_ptr, NULL, 8);
if (outname == NULL) { if (outname == NULL) {
outname = strchr(line_ptr, ' '); outname = strchr(line_ptr, ' ');
if ((outname == NULL) || (*outname == '\0')) { if ((outname == NULL) || (*outname == '\0')) {
@ -170,7 +180,7 @@ int uudecode_main(int argc ATTRIBUTE_UNUSED, char **argv)
dst_stream = stdout; dst_stream = stdout;
if (NOT_LONE_DASH(outname)) { if (NOT_LONE_DASH(outname)) {
dst_stream = xfopen(outname, "w"); dst_stream = xfopen(outname, "w");
chmod(outname, mode & (S_IRWXU | S_IRWXG | S_IRWXO)); fchmod(fileno(dst_stream), mode & (S_IRWXU | S_IRWXG | S_IRWXO));
} }
free(line); free(line);
decode_fn_ptr(src_stream, dst_stream); decode_fn_ptr(src_stream, dst_stream);

View file

@ -21,22 +21,21 @@
int yes_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int yes_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int yes_main(int argc, char **argv) int yes_main(int argc, char **argv)
{ {
char **first_arg; char **pp;
argv[0] = (char*)"y"; argv[0] = (char*)"y";
if (argc != 1) { if (argc != 1) {
++argv; ++argv;
} }
first_arg = argv;
do { do {
pp = argv;
while (1) { while (1) {
fputs(*argv, stdout); fputs(*pp, stdout);
if (!*++argv) if (!*++pp)
break; break;
putchar(' '); putchar(' ');
} }
argv = first_arg;
} while (putchar('\n') != EOF); } while (putchar('\n') != EOF);
bb_perror_nomsg_and_die(); bb_perror_nomsg_and_die();

View file

@ -102,10 +102,8 @@ int lsattr_main(int argc ATTRIBUTE_UNUSED, char **argv)
argv += optind; argv += optind;
if (!*argv) if (!*argv)
lsattr_args("."); *--argv = (char*)".";
else {
do lsattr_args(*argv++); while (*argv); do lsattr_args(*argv++); while (*argv);
}
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View file

@ -23,16 +23,6 @@
#include "libbb.h" #include "libbb.h"
static FILE *cmp_xfopen_input(const char *filename)
{
FILE *fp;
fp = fopen_or_warn_stdin(filename);
if (fp)
return fp;
xfunc_die(); /* We already output an error message. */
}
static const char fmt_eof[] ALIGN1 = "cmp: EOF on %s\n"; static const char fmt_eof[] ALIGN1 = "cmp: EOF on %s\n";
static const char fmt_differ[] ALIGN1 = "%s %s differ: char %"OFF_FMT"d, line %d\n"; static const char fmt_differ[] ALIGN1 = "%s %s differ: char %"OFF_FMT"d, line %d\n";
// This fmt_l_opt uses gnu-isms. SUSv3 would be "%.0s%.0s%"OFF_FMT"d %o %o\n" // This fmt_l_opt uses gnu-isms. SUSv3 would be "%.0s%.0s%"OFF_FMT"d %o %o\n"
@ -65,7 +55,7 @@ int cmp_main(int argc ATTRIBUTE_UNUSED, char **argv)
argv += optind; argv += optind;
filename1 = *argv; filename1 = *argv;
fp1 = cmp_xfopen_input(filename1); fp1 = xfopen_stdin(filename1);
if (*++argv) { if (*++argv) {
filename2 = *argv; filename2 = *argv;
@ -79,7 +69,7 @@ int cmp_main(int argc ATTRIBUTE_UNUSED, char **argv)
#endif #endif
} }
fp2 = cmp_xfopen_input(filename2); fp2 = xfopen_stdin(filename2);
if (fp1 == fp2) { /* Paranoia check... stdin == stdin? */ if (fp1 == fp2) { /* Paranoia check... stdin == stdin? */
/* Note that we don't bother reading stdin. Neither does gnu wc. /* Note that we don't bother reading stdin. Neither does gnu wc.
* But perhaps we should, so that other apps down the chain don't * But perhaps we should, so that other apps down the chain don't

View file

@ -1231,7 +1231,7 @@ static void add_cmd_block(char *cmdstr)
} }
int sed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int sed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int sed_main(int argc, char **argv) int sed_main(int argc ATTRIBUTE_UNUSED, char **argv)
{ {
enum { enum {
OPT_in_place = 1 << 0, OPT_in_place = 1 << 0,
@ -1246,7 +1246,7 @@ int sed_main(int argc, char **argv)
if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff); if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff);
/* Lie to autoconf when it starts asking stupid questions. */ /* Lie to autoconf when it starts asking stupid questions. */
if (argc == 2 && !strcmp(argv[1], "--version")) { if (argv[1] && !strcmp(argv[1], "--version")) {
puts("This is not GNU sed version 4.0"); puts("This is not GNU sed version 4.0");
return 0; return 0;
} }
@ -1257,7 +1257,7 @@ int sed_main(int argc, char **argv)
"nn"; /* count -n */ "nn"; /* count -n */
opt = getopt32(argv, "irne:f:", &opt_e, &opt_f, opt = getopt32(argv, "irne:f:", &opt_e, &opt_f,
&G.be_quiet); /* counter for -n */ &G.be_quiet); /* counter for -n */
argc -= optind; //argc -= optind;
argv += optind; argv += optind;
if (opt & OPT_in_place) { // -i if (opt & OPT_in_place) { // -i
atexit(cleanup_outname); atexit(cleanup_outname);
@ -1283,10 +1283,9 @@ int sed_main(int argc, char **argv)
} }
/* if we didn't get a pattern from -e or -f, use argv[0] */ /* if we didn't get a pattern from -e or -f, use argv[0] */
if (!(opt & 0x18)) { if (!(opt & 0x18)) {
if (!argc) if (!*argv)
bb_show_usage(); bb_show_usage();
add_cmd_block(*argv++); add_cmd_block(*argv++);
argc--;
} }
/* Flush any unfinished commands. */ /* Flush any unfinished commands. */
add_cmd(""); add_cmd("");
@ -1306,7 +1305,7 @@ int sed_main(int argc, char **argv)
int i; int i;
FILE *file; FILE *file;
for (i = 0; i < argc; i++) { for (i = 0; argv[i]; i++) {
struct stat statbuf; struct stat statbuf;
int nonstdoutfd; int nonstdoutfd;

View file

@ -333,6 +333,7 @@ int xopen(const char *pathname, int flags);
int xopen3(const char *pathname, int flags, int mode); int xopen3(const char *pathname, int flags, int mode);
int open_or_warn(const char *pathname, int flags); int open_or_warn(const char *pathname, int flags);
int open3_or_warn(const char *pathname, int flags, int mode); int open3_or_warn(const char *pathname, int flags, int mode);
int open_or_warn_stdin(const char *pathname);
void xrename(const char *oldpath, const char *newpath); void xrename(const char *oldpath, const char *newpath);
int rename_or_warn(const char *oldpath, const char *newpath); int rename_or_warn(const char *oldpath, const char *newpath);
off_t xlseek(int fd, off_t offset, int whence); off_t xlseek(int fd, off_t offset, int whence);
@ -559,6 +560,7 @@ extern FILE *xfopen(const char *filename, const char *mode);
/* Prints warning to stderr and returns NULL on failure: */ /* Prints warning to stderr and returns NULL on failure: */
extern FILE *fopen_or_warn(const char *filename, const char *mode); extern FILE *fopen_or_warn(const char *filename, const char *mode);
/* "Opens" stdin if filename is special, else just opens file: */ /* "Opens" stdin if filename is special, else just opens file: */
extern FILE *xfopen_stdin(const char *filename);
extern FILE *fopen_or_warn_stdin(const char *filename); extern FILE *fopen_or_warn_stdin(const char *filename);
int bb_pstrcmp(const void *a, const void *b); int bb_pstrcmp(const void *a, const void *b);
@ -741,6 +743,7 @@ void bb_sanitize_stdio(void);
int sanitize_env_if_suid(void); int sanitize_env_if_suid(void);
extern const char *const bb_argv_dash[]; /* "-", NULL */
extern const char *opt_complementary; extern const char *opt_complementary;
#if ENABLE_GETOPT_LONG #if ENABLE_GETOPT_LONG
#define No_argument "\0" #define No_argument "\0"

View file

@ -16,8 +16,10 @@
int fclose_if_not_stdin(FILE *f) int fclose_if_not_stdin(FILE *f)
{ {
if (f != stdin) { /* Some more paranoid applets want ferror() check too */
return fclose(f); int r = ferror(f); /* NB: does NOT set errno! */
} if (r) errno = EIO; /* so we'll help it */
return 0; if (f != stdin)
return (r | fclose(f)); /* fclose does set errno on error */
return r;
} }

View file

@ -281,6 +281,8 @@ Special characters:
/* Code here assumes that 'unsigned' is at least 32 bits wide */ /* Code here assumes that 'unsigned' is at least 32 bits wide */
const char *const bb_argv_dash[] = { "-", NULL };
const char *opt_complementary; const char *opt_complementary;
enum { enum {

View file

@ -10,8 +10,6 @@
/* A number of applets need to open a file for reading, where the filename /* A number of applets need to open a file for reading, where the filename
* is a command line arg. Since often that arg is '-' (meaning stdin), * is a command line arg. Since often that arg is '-' (meaning stdin),
* we avoid testing everywhere by consolidating things in this routine. * we avoid testing everywhere by consolidating things in this routine.
*
* Note: we also consider "" to mean stdin (for 'cmp' at least).
*/ */
#include "libbb.h" #include "libbb.h"
@ -21,11 +19,30 @@ FILE *fopen_or_warn_stdin(const char *filename)
FILE *fp = stdin; FILE *fp = stdin;
if (filename != bb_msg_standard_input if (filename != bb_msg_standard_input
&& filename[0]
&& NOT_LONE_DASH(filename) && NOT_LONE_DASH(filename)
) { ) {
fp = fopen_or_warn(filename, "r"); fp = fopen_or_warn(filename, "r");
} }
return fp; return fp;
} }
FILE *xfopen_stdin(const char *filename)
{
FILE *fp = fopen_or_warn_stdin(filename);
if (fp)
return fp;
xfunc_die(); /* We already output an error message. */
}
int open_or_warn_stdin(const char *filename)
{
int fd = STDIN_FILENO;
if (filename != bb_msg_standard_input
&& NOT_LONE_DASH(filename)
) {
fd = open_or_warn(filename, O_RDONLY);
}
return fd;
}

View file

@ -12,7 +12,7 @@
#define SU_OPT_l (4) #define SU_OPT_l (4)
int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int su_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int su_main(int argc, char **argv) int su_main(int argc ATTRIBUTE_UNUSED, char **argv)
{ {
unsigned flags; unsigned flags;
char *opt_shell = NULL; char *opt_shell = NULL;
@ -24,19 +24,17 @@ int su_main(int argc, char **argv)
char *old_user; char *old_user;
flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell); flags = getopt32(argv, "mplc:s:", &opt_command, &opt_shell);
argc -= optind; //argc -= optind;
argv += optind; argv += optind;
if (argc && LONE_DASH(argv[0])) { if (argv[0] && LONE_DASH(argv[0])) {
flags |= SU_OPT_l; flags |= SU_OPT_l;
argc--;
argv++; argv++;
} }
/* get user if specified */ /* get user if specified */
if (argc) { if (argv[0]) {
opt_username = argv[0]; opt_username = argv[0];
//argc--; - not used below anyway
argv++; argv++;
} }

View file

@ -23,7 +23,7 @@ int strings_main(int argc ATTRIBUTE_UNUSED, char **argv)
unsigned opt; unsigned opt;
unsigned count; unsigned count;
off_t offset; off_t offset;
FILE *file = stdin; FILE *file;
char *string; char *string;
const char *fmt = "%s: "; const char *fmt = "%s: ";
const char *n_arg = "4"; const char *n_arg = "4";
@ -40,16 +40,14 @@ int strings_main(int argc ATTRIBUTE_UNUSED, char **argv)
if (!*argv) { if (!*argv) {
fmt = "{%s}: "; fmt = "{%s}: ";
*--argv = (char *)bb_msg_standard_input; *--argv = (char *)bb_msg_standard_input;
goto PIPE;
} }
do { do {
file = fopen_or_warn(*argv, "r"); file = fopen_or_warn_stdin(*argv);
if (!file) { if (!file) {
status = EXIT_FAILURE; status = EXIT_FAILURE;
continue; continue;
} }
PIPE:
offset = 0; offset = 0;
count = 0; count = 0;
do { do {

View file

@ -377,7 +377,7 @@ void write_leases(void)
time_t curr = time(0); time_t curr = time(0);
unsigned long tmp_time; unsigned long tmp_time;
fp = open3_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666); fp = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
if (fp < 0) { if (fp < 0) {
return; return;
} }

View file

@ -566,7 +566,7 @@ static int setup_redirects(struct child_prog *prog, int squirrel[])
break; break;
} }
openfd = open3_or_warn(redir->filename, mode, 0666); openfd = open_or_warn(redir->filename, mode);
if (openfd < 0) { if (openfd < 0) {
/* this could get lost if stderr has been redirected, but /* this could get lost if stderr has been redirected, but
bash and ash both lose it as well (though zsh doesn't!) */ bash and ash both lose it as well (though zsh doesn't!) */

View file

@ -73,7 +73,7 @@ int hexdump_main(int argc, char **argv)
} }
/* We cannot use getopt32: in hexdump options are cumulative. /* We cannot use getopt32: in hexdump options are cumulative.
* E.g. hexdump -C -C file should dump each line twice */ * E.g. "hexdump -C -C file" should dump each line twice */
while ((ch = getopt(argc, argv, hexdump_opts)) > 0) { while ((ch = getopt(argc, argv, hexdump_opts)) > 0) {
p = strchr(hexdump_opts, ch); p = strchr(hexdump_opts, ch);
if (!p) if (!p)