From 9f739445cd3deddd0343c3a8d5a981ede26bef30 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Sat, 16 Dec 2006 23:49:13 +0000 Subject: inline strcmp(s, "-") [actually macro-ize it for now - gcc is too stupid] --- archival/bunzip2.c | 2 +- archival/gunzip.c | 2 +- archival/gzip.c | 2 +- archival/tar.c | 2 +- archival/uncompress.c | 2 +- archival/unlzma.c | 2 +- archival/unzip.c | 3 +-- coreutils/cut.c | 13 +++++++------ coreutils/diff.c | 10 +++++----- coreutils/env.c | 2 +- coreutils/md5_sha1_sum.c | 4 ++-- coreutils/sort.c | 2 +- coreutils/sum.c | 11 ++++------- coreutils/tail.c | 4 ++-- coreutils/uudecode.c | 2 +- e2fsprogs/fsck.c | 4 ++-- editors/sed.c | 4 +--- include/libbb.h | 8 ++++++++ libbb/wfopen_input.c | 2 +- loginutils/getty.c | 2 +- loginutils/su.c | 18 +++++++++++------- miscutils/crontab.c | 2 +- networking/ftpgetput.c | 7 +++---- networking/libiproute/ipaddress.c | 2 +- networking/tftp.c | 2 +- networking/wget.c | 2 +- shell/ash.c | 14 +++++++------- shell/msh.c | 2 +- 28 files changed, 69 insertions(+), 63 deletions(-) diff --git a/archival/bunzip2.c b/archival/bunzip2.c index 757001fe4..a6cd17611 100644 --- a/archival/bunzip2.c +++ b/archival/bunzip2.c @@ -23,7 +23,7 @@ int bunzip2_main(int argc, char **argv) /* Set input filename and number */ filename = argv[optind]; - if ((filename) && (filename[0] != '-') && (filename[1] != '\0')) { + if (filename && NOT_LONE_DASH(filename)) { /* Open input file */ src_fd = xopen(filename, O_RDONLY); } else { diff --git a/archival/gunzip.c b/archival/gunzip.c index e24401c71..c5deec3ce 100644 --- a/archival/gunzip.c +++ b/archival/gunzip.c @@ -58,7 +58,7 @@ int gunzip_main(int argc, char **argv) optind++; - if (old_path == NULL || strcmp(old_path, "-") == 0) { + if (old_path == NULL || LONE_DASH(old_path)) { src_fd = STDIN_FILENO; opt |= GUNZIP_OPT_STDOUT; USE_DESKTOP(opt &= ~GUNZIP_OPT_VERBOSE;) diff --git a/archival/gzip.c b/archival/gzip.c index c9b67538e..7124e9bd4 100644 --- a/archival/gzip.c +++ b/archival/gzip.c @@ -1201,7 +1201,7 @@ int gzip_main(int argc, char **argv) char *path = NULL; clear_bufs(); - if (strcmp(argv[i], "-") == 0) { + if (LONE_DASH(argv[i])) { time_stamp = 0; inFileNum = STDIN_FILENO; outFileNum = STDOUT_FILENO; diff --git a/archival/tar.c b/archival/tar.c index 79c3595ac..ba7cb0f8c 100644 --- a/archival/tar.c +++ b/archival/tar.c @@ -851,7 +851,7 @@ int tar_main(int argc, char **argv) flags = O_RDONLY; } - if (tar_filename[0] == '-' && !tar_filename[1]) { + if (LONE_DASH(tar_filename)) { tar_handle->src_fd = fileno(tar_stream); tar_handle->seek = seek_by_read; } else { diff --git a/archival/uncompress.c b/archival/uncompress.c index 3c78961d5..461179016 100644 --- a/archival/uncompress.c +++ b/archival/uncompress.c @@ -25,7 +25,7 @@ int uncompress_main(int argc, char **argv) int src_fd; int dst_fd; - if (strcmp(compressed_file, "-") == 0) { + if (LONE_DASH(compressed_file)) { src_fd = STDIN_FILENO; flags |= GUNZIP_TO_STDOUT; } else { diff --git a/archival/unlzma.c b/archival/unlzma.c index 46fbefdc0..20c4ff2a7 100644 --- a/archival/unlzma.c +++ b/archival/unlzma.c @@ -26,7 +26,7 @@ int unlzma_main(int argc, char **argv) /* Set input filename and number */ filename = argv[optind]; - if (filename && (filename[0] != '-') && (filename[1] != '\0')) { + if (filename && NOT_LONE_DASH(filename)) { /* Open input file */ src_fd = xopen(filename, O_RDONLY); } else { diff --git a/archival/unzip.c b/archival/unzip.c index 8b1c281c4..f553eefa2 100644 --- a/archival/unzip.c +++ b/archival/unzip.c @@ -187,11 +187,10 @@ int unzip_main(int argc, char **argv) } /* Open input file */ - if (strcmp("-", src_fn) == 0) { + if (LONE_DASH(src_fn)) { src_fd = STDIN_FILENO; /* Cannot use prompt mode since zip data is arriving on STDIN */ overwrite = (overwrite == o_prompt) ? o_never : overwrite; - } else { static const char *const extn[] = {"", ".zip", ".ZIP"}; int orig_src_fn_len = strlen(src_fn); diff --git a/coreutils/cut.c b/coreutils/cut.c index a538e3d20..a72b2c29a 100644 --- a/coreutils/cut.c +++ b/coreutils/cut.c @@ -168,6 +168,8 @@ int cut_main(int argc, char **argv) opt_complementary = "b--bcf:c--bcf:f--bcf"; getopt32(argc, argv, optstring, &sopt, &sopt, &sopt, <ok); +// argc -= optind; + argv += optind; if (!(option_mask32 & (CUT_OPT_BYTE_FLGS | CUT_OPT_CHAR_FLGS | CUT_OPT_FIELDS_FLGS))) bb_error_msg_and_die("expected a list of bytes, characters, or fields"); if (option_mask32 & BB_GETOPT_ERROR) @@ -262,22 +264,21 @@ int cut_main(int argc, char **argv) qsort(cut_lists, nlists, sizeof(struct cut_list), cmpfunc); } - /* argv[(optind)..(argc-1)] should be names of file to process. If no + /* argv[0..argc-1] should be names of file to process. If no * files were specified or '-' was specified, take input from stdin. * Otherwise, we process all the files specified. */ - if (argv[optind] == NULL - || (argv[optind][0] == '-' && argv[optind][1] == '\0')) { + if (argv[0] == NULL || LONE_DASH(argv[0])) { cut_file(stdin); } else { FILE *file; - for (; optind < argc; optind++) { - file = fopen_or_warn(argv[optind], "r"); + do { + file = fopen_or_warn(argv[0], "r"); if (file) { cut_file(file); fclose(file); } - } + } while (*++argv); } if (ENABLE_FEATURE_CLEAN_UP) free(cut_lists); diff --git a/coreutils/diff.c b/coreutils/diff.c index 0df9989b7..887679a0a 100644 --- a/coreutils/diff.c +++ b/coreutils/diff.c @@ -904,19 +904,19 @@ static int diffreg(char *ofile1, char *ofile2, int flags) if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode)) return (S_ISDIR(stb1.st_mode) ? D_MISMATCH1 : D_MISMATCH2); - if (strcmp(file1, "-") == 0 && strcmp(file2, "-") == 0) + if (LONE_DASH(file1) && LONE_DASH(file2)) goto closem; f1 = stdin; if (flags & D_EMPTY1) f1 = xfopen(bb_dev_null, "r"); - else if (file1[0] != '-' || file1[1]) /* not "-" */ + else if (NOT_LONE_DASH(file1)) f1 = xfopen(file1, "r"); f2 = stdin; if (flags & D_EMPTY2) f2 = xfopen(bb_dev_null, "r"); - else if (file2[0] != '-' || file2[1]) /* not "-" */ + else if (NOT_LONE_DASH(file2)) f2 = xfopen(file2, "r"); i = files_differ(f1, f2, flags); @@ -1212,12 +1212,12 @@ int diff_main(int argc, char **argv) bb_error_msg("missing filename"); bb_show_usage(); } - if (argv[0][0] == '-' && !argv[0][1]) { /* "-" */ + if (LONE_DASH(argv[0])) { fstat(STDIN_FILENO, &stb1); gotstdin = 1; } else xstat(argv[0], &stb1); - if (argv[1][0] == '-' && !argv[1][1]) { /* "-" */ + if (LONE_DASH(argv[1])) { fstat(STDIN_FILENO, &stb2); gotstdin = 1; } else diff --git a/coreutils/env.c b/coreutils/env.c index 2ce99b0ad..e4cad271b 100644 --- a/coreutils/env.c +++ b/coreutils/env.c @@ -58,7 +58,7 @@ int env_main(int argc, char** argv) opt = getopt32(argc, argv, "+iu:", &unset_env); argv += optind; - if (*argv && (argv[0][0] == '-') && !argv[0][1]) { + if (*argv && LONE_DASH(argv[0])) { opt |= 1; ++argv; } diff --git a/coreutils/md5_sha1_sum.c b/coreutils/md5_sha1_sum.c index e8d3a1509..6fe1b0286 100644 --- a/coreutils/md5_sha1_sum.c +++ b/coreutils/md5_sha1_sum.c @@ -39,7 +39,7 @@ static uint8_t *hash_file(const char *filename, hash_algo_t hash_algo) void (*final)(void*, void*); src_fd = STDIN_FILENO; - if (filename[0] != '-' || filename[1]) { /* not "-" */ + if (NOT_LONE_DASH(filename)) { src_fd = open(filename, O_RDONLY); if (src_fd < 0) { bb_perror_msg("%s", filename); @@ -120,7 +120,7 @@ int md5_sha1_sum_main(int argc, char **argv) } pre_computed_stream = stdin; - if (file_ptr[0] != '-' || file_ptr[1]) { /* not "-" */ + if (NOT_LONE_DASH(file_ptr)) { pre_computed_stream = xfopen(file_ptr, "r"); } diff --git a/coreutils/sort.c b/coreutils/sort.c index c23bf9c60..c37810f1a 100644 --- a/coreutils/sort.c +++ b/coreutils/sort.c @@ -310,7 +310,7 @@ int sort_main(int argc, char **argv) /* Open input files and read data */ for (i = argv[optind] ? optind : optind-1; argv[i]; i++) { fp = stdin; - if (i >= optind && (argv[i][0] != '-' || argv[i][1])) + if (i >= optind && NOT_LONE_DASH(argv[i])) fp = xfopen(argv[i], "r"); for (;;) { line = GET_LINE(fp); diff --git a/coreutils/sum.c b/coreutils/sum.c index 93f4e22eb..68a857816 100644 --- a/coreutils/sum.c +++ b/coreutils/sum.c @@ -18,9 +18,6 @@ /* 1 if any of the files read were the standard input */ static int have_read_stdin; -/* make a little more readable and avoid using strcmp for just 2 bytes */ -#define IS_STDIN(s) (s[0] == '-' && s[1] == '\0') - /* Calculate and print the rotated checksum and the size in 1K blocks of file FILE, or of the standard input if FILE is "-". If PRINT_NAME is >1, print FILE next to the checksum and size. @@ -34,7 +31,7 @@ static int bsd_sum_file(const char *file, int print_name) int ch; /* Each character read. */ int ret = 0; - if (IS_STDIN(file)) { + if (LONE_DASH(file)) { fp = stdin; have_read_stdin++; } else { @@ -84,7 +81,7 @@ static int sysv_sum_file(const char *file, int print_name) /* The sum of all the input bytes, modulo (UINT_MAX + 1). */ unsigned int s = 0; - if (IS_STDIN(file)) { + if (LONE_DASH(file)) { fd = 0; have_read_stdin = 1; } else { @@ -103,7 +100,7 @@ static int sysv_sum_file(const char *file, int print_name) release_and_ret: bb_perror_msg(file); RELEASE_CONFIG_BUFFER(buf); - if (!IS_STDIN(file)) + if (NOT_LONE_DASH(file)) close(fd); return 0; } @@ -113,7 +110,7 @@ release_and_ret: s += buf[bytes_read]; } - if (!IS_STDIN(file) && close(fd) == -1) + if (NOT_LONE_DASH(file) && close(fd) == -1) goto release_and_ret; else RELEASE_CONFIG_BUFFER(buf); diff --git a/coreutils/tail.c b/coreutils/tail.c index ed5ea1467..4c3c3b901 100644 --- a/coreutils/tail.c +++ b/coreutils/tail.c @@ -176,8 +176,8 @@ int tail_main(int argc, char **argv) } do { - if ((argv[i][0] == '-') && !argv[i][1]) { - DO_STDIN: + if (LONE_DASH(argv[i])) { + DO_STDIN: fds[nfiles] = STDIN_FILENO; } else if ((fds[nfiles] = open(argv[i], O_RDONLY)) < 0) { bb_perror_msg("%s", argv[i]); diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c index 8b7de74ff..06512119e 100644 --- a/coreutils/uudecode.c +++ b/coreutils/uudecode.c @@ -166,7 +166,7 @@ int uudecode_main(int argc, char **argv) } outname++; } - if (strcmp(outname, "-") == 0) { + if (LONE_DASH(outname)) { dst_stream = stdout; } else { dst_stream = xfopen(outname, "w"); diff --git a/e2fsprogs/fsck.c b/e2fsprogs/fsck.c index f0c1316a9..3b01c1021 100644 --- a/e2fsprogs/fsck.c +++ b/e2fsprogs/fsck.c @@ -1249,8 +1249,8 @@ static void PRS(int argc, char *argv[]) progress_fd = 0; else goto next_arg; - } else if ((i+1) < argc && - !strncmp(argv[i+1], "-", 1) == 0) { + } else if ((i+1) < argc + && argv[i+1][0] != '-') { progress_fd = string_to_int(argv[i]); if (progress_fd < 0) progress_fd = 0; diff --git a/editors/sed.c b/editors/sed.c index ac3d8d463..8d372abe4 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -1235,9 +1235,7 @@ int sed_main(int argc, char **argv) struct stat statbuf; int nonstdoutfd; - if (argv[i][0] == '-' && !argv[i][1] - && !(opt & OPT_in_place) - ) { + if (LONE_DASH(argv[i]) && !(opt & OPT_in_place)) { add_input_file(stdin); process_files(); continue; diff --git a/include/libbb.h b/include/libbb.h index 1d91a0a72..fcd0dfa31 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -255,6 +255,14 @@ extern char *xstrdup(const char *s); extern char *xstrndup(const char *s, int n); extern char *safe_strncpy(char *dst, const char *src, size_t size); extern char *xasprintf(const char *format, ...) __attribute__ ((format (printf, 1, 2))); +// gcc-4.1.1 still isn't good enough at optimizing it +// (+200 bytes compared to macro) +//static ATTRIBUTE_ALWAYS_INLINE +//int LONE_DASH(const char *s) { return s[0] == '-' && !s[1]; } +//static ATTRIBUTE_ALWAYS_INLINE +//int NOT_LONE_DASH(const char *s) { return s[0] != '-' || s[1]; } +#define LONE_DASH(s) ((s)[0] == '-' && !(s)[1]) +#define NOT_LONE_DASH(s) ((s)[0] != '-' || (s)[1]) /* dmalloc will redefine these to it's own implementation. It is safe * to have the prototypes here unconditionally. */ diff --git a/libbb/wfopen_input.c b/libbb/wfopen_input.c index 3da855fe6..7a11dacd7 100644 --- a/libbb/wfopen_input.c +++ b/libbb/wfopen_input.c @@ -22,7 +22,7 @@ FILE *fopen_or_warn_stdin(const char *filename) if (filename != bb_msg_standard_input && filename[0] - && (filename[0] != '-' || filename[1]) + && NOT_LONE_DASH(filename) ) { fp = fopen_or_warn(filename, "r"); } diff --git a/loginutils/getty.c b/loginutils/getty.c index a85e52306..5ceaefcac 100644 --- a/loginutils/getty.c +++ b/loginutils/getty.c @@ -239,7 +239,7 @@ static void open_tty(char *tty, struct termios *tp, int local) /* Set up new standard input, unless we are given an already opened port. */ - if (strcmp(tty, "-")) { + if (NOT_LONE_DASH(tty)) { struct stat st; int fd; diff --git a/loginutils/su.c b/loginutils/su.c index a23ee932b..046457b6f 100644 --- a/loginutils/su.c +++ b/loginutils/su.c @@ -14,25 +14,29 @@ int su_main(int argc, char **argv) char *opt_shell = 0; char *opt_command = 0; char *opt_username = "root"; - char **opt_args = 0; struct passwd *pw; uid_t cur_uid = getuid(); const char *tty; char *old_user; flags = getopt32(argc, argv, "mplc:s:", &opt_command, &opt_shell); + argc -= optind; + argv -= optind; #define SU_OPT_mp (3) #define SU_OPT_l (4) - if (optind < argc && argv[optind][0] == '-' && argv[optind][1] == 0) { + if (argc && LONE_DASH(argv[0])) { flags |= SU_OPT_l; - ++optind; + argc--; + argv++; } /* get user if specified */ - if (optind < argc) opt_username = argv [optind++]; - - if (optind < argc) opt_args = argv + optind; + if (argc) { + opt_username = argv[0]; +// argc--; + argv++; + } if (ENABLE_SU_SYSLOG) { /* The utmp entry (via getlogin) is probably the best way to identify @@ -84,7 +88,7 @@ int su_main(int argc, char **argv) USE_SELINUX(set_current_security_context(NULL);) /* Never returns */ - run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)opt_args); + run_shell(opt_shell, flags & SU_OPT_l, opt_command, (const char**)argv); return EXIT_FAILURE; } diff --git a/miscutils/crontab.c b/miscutils/crontab.c index 39d3aae41..d442272f0 100644 --- a/miscutils/crontab.c +++ b/miscutils/crontab.c @@ -51,7 +51,7 @@ int crontab_main(int ac, char **av) i = 1; if (ac > 1) { - if (av[1][0] == '-' && av[1][1] == 0) { + if (LONE_DASH(av[1])) { option = REPLACE; ++i; } else if (av[1][0] != '-') { diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c index 223d2435c..dff894468 100644 --- a/networking/ftpgetput.c +++ b/networking/ftpgetput.c @@ -132,7 +132,7 @@ int ftp_receive(ftp_host_info_t *server, FILE *control_stream, do_continue = 0; } - if ((local_path[0] == '-') && (local_path[1] == '\0')) { + if (LONE_DASH(local_path)) { fd_local = STDOUT_FILENO; do_continue = 0; } @@ -212,9 +212,8 @@ int ftp_send(ftp_host_info_t *server, FILE *control_stream, fd_data = xconnect_ftpdata(server, buf); /* get the local file */ - if ((local_path[0] == '-') && (local_path[1] == '\0')) { - fd_local = STDIN_FILENO; - } else { + fd_local = STDIN_FILENO; + if (NOT_LONE_DASH(local_path)) { fd_local = xopen(local_path, O_RDONLY); fstat(fd_local, &sbuf); diff --git a/networking/libiproute/ipaddress.c b/networking/libiproute/ipaddress.c index 2a267fef6..9fb08e6ba 100644 --- a/networking/libiproute/ipaddress.c +++ b/networking/libiproute/ipaddress.c @@ -681,7 +681,7 @@ static int ipaddr_modify(int cmd, int argc, char **argv) if (strcmp(*argv, "+") == 0) { brd_len = -1; } - else if (strcmp(*argv, "-") == 0) { + else if (LONE_DASH(*argv)) { brd_len = -2; } else { get_addr(&addr, *argv, req.ifa.ifa_family); diff --git a/networking/tftp.c b/networking/tftp.c index 64d376fa7..a62c5d8cc 100644 --- a/networking/tftp.c +++ b/networking/tftp.c @@ -530,7 +530,7 @@ int tftp_main(int argc, char **argv) if ((localfile == NULL && remotefile == NULL) || (argv[optind] == NULL)) bb_show_usage(); - if (localfile == NULL || strcmp(localfile, "-") == 0) { + if (localfile == NULL || LONE_DASH(localfile)) { fd = (cmd == tftp_cmd_get) ? STDOUT_FILENO : STDIN_FILENO; } else { fd = open(localfile, flags, 0644); /* fail below */ diff --git a/networking/wget.c b/networking/wget.c index 19bf8f887..fbdbf62f6 100644 --- a/networking/wget.c +++ b/networking/wget.c @@ -217,7 +217,7 @@ int wget_main(int argc, char **argv) /* * Determine where to start transfer. */ - if (fname_out[0] == '-' && !fname_out[1]) { + if (LONE_DASH(fname_out)) { output_fd = 1; opt &= ~WGET_OPT_CONTINUE; } diff --git a/shell/ash.c b/shell/ash.c index 3a9998fc0..8ba4cb8a0 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -2315,7 +2315,7 @@ cdcmd(int argc, char **argv) dest = *argptr; if (!dest) dest = bltinlookup(homestr); - else if (dest[0] == '-' && dest[1] == '\0') { + else if (LONE_DASH(dest)) { dest = bltinlookup("OLDPWD"); flags |= CD_PRINT; } @@ -8889,7 +8889,7 @@ options(int cmdline) argptr++; if ((c = *p++) == '-') { val = 1; - if (p[0] == '\0' || (p[0] == '-' && p[1] == '\0')) { + if (p[0] == '\0' || LONE_DASH(p)) { if (!cmdline) { /* "-" means turn off -x and -v */ if (p[0] == '\0') @@ -9114,7 +9114,7 @@ atend: goto out; } optnext++; - if (p[0] == '-' && p[1] == '\0') /* check for "--" */ + if (LONE_DASH(p)) /* check for "--" */ goto atend; } @@ -9232,7 +9232,7 @@ nextopt(const char *optstring) if (p == NULL || *p != '-' || *++p == '\0') return '\0'; argptr++; - if (p[0] == '-' && p[1] == '\0') /* check for "--" */ + if (LONE_DASH(p)) /* check for "--" */ return '\0'; } c = *p++; @@ -9825,7 +9825,7 @@ void fixredir(union node *n, const char *text, int err) if (is_digit(text[0]) && text[1] == '\0') n->ndup.dupfd = digit_val(text[0]); - else if (text[0] == '-' && text[1] == '\0') + else if (LONE_DASH(text)) n->ndup.dupfd = -1; else { @@ -11650,7 +11650,7 @@ trapcmd(int argc, char **argv) sh_error("%s: bad trap", *ap); INTOFF; if (action) { - if (action[0] == '-' && action[1] == '\0') + if (LONE_DASH(action)) action = NULL; else action = savestr(action); @@ -12257,7 +12257,7 @@ static void mklocal(char *name) INTOFF; lvp = ckmalloc(sizeof (struct localvar)); - if (name[0] == '-' && name[1] == '\0') { + if (LONE_DASH(name)) { char *p; p = ckmalloc(sizeof(optlist)); lvp->text = memcpy(p, optlist, sizeof(optlist)); diff --git a/shell/msh.c b/shell/msh.c index 492d3cf43..45ca3df81 100644 --- a/shell/msh.c +++ b/shell/msh.c @@ -1001,7 +1001,7 @@ static int newfile(char *s) DBGPRINTF7(("NEWFILE: opening %s\n", s)); - if (strcmp(s, "-") != 0) { + if (NOT_LONE_DASH(s)) { DBGPRINTF(("NEWFILE: s is %s\n", s)); f = open(s, 0); if (f < 0) { -- cgit v1.2.3