introduce LONE_CHAR (optimized strcmp with one-char string)
authorDenis Vlasenko <vda.linux@googlemail.com>
Thu, 21 Dec 2006 13:23:14 +0000 (13:23 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Thu, 21 Dec 2006 13:23:14 +0000 (13:23 -0000)
17 files changed:
coreutils/Kbuild
coreutils/cat.c
coreutils/diff.c
coreutils/echo.c
coreutils/expr.c
coreutils/test.c
e2fsprogs/blkid/devname.c
e2fsprogs/chattr.c
e2fsprogs/ext2fs/ismounted.c
e2fsprogs/fsck.c
include/libbb.h
init/init.c
libbb/correct_password.c
miscutils/last.c
networking/inetd.c
networking/libiproute/ipaddress.c
shell/ash.c

index cfb508d816761e68a0507757b58f7775e5960a2e..55f19b4ca214faac16203d01cadab5014f46f540 100644 (file)
@@ -10,6 +10,7 @@ lib-y:=
 lib-$(CONFIG_BASENAME)  += basename.o
 lib-$(CONFIG_CAL)       += cal.o
 lib-$(CONFIG_CAT)       += cat.o
+lib-$(CONFIG_LESS)      += cat.o # less uses it if stdout isn't a tty
 lib-$(CONFIG_CATV)      += catv.o
 lib-$(CONFIG_CHGRP)     += chgrp.o chown.o
 lib-$(CONFIG_CHMOD)     += chmod.o
@@ -28,6 +29,7 @@ lib-$(CONFIG_DIRNAME)   += dirname.o
 lib-$(CONFIG_DOS2UNIX)  += dos2unix.o
 lib-$(CONFIG_DU)        += du.o
 lib-$(CONFIG_ECHO)      += echo.o
+lib-$(CONFIG_ASH)       += echo.o # used by ash
 lib-$(CONFIG_ENV)       += env.o
 lib-$(CONFIG_EXPR)      += expr.o
 lib-$(CONFIG_FALSE)     += false.o
@@ -65,6 +67,7 @@ lib-$(CONFIG_SYNC)      += sync.o
 lib-$(CONFIG_TAIL)      += tail.o
 lib-$(CONFIG_TEE)       += tee.o
 lib-$(CONFIG_TEST)      += test.o
+lib-$(CONFIG_ASH)       += test.o # used by ash
 lib-$(CONFIG_TOUCH)     += touch.o
 lib-$(CONFIG_TR)        += tr.o
 lib-$(CONFIG_TRUE)      += true.o
index a95980552aa8661e1fb2fa56f1d9305394e9b4ea..db4d33dc59ecaa41f804c9250d13098815f872c7 100644 (file)
 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cat.html */
 
 #include "busybox.h"
-#include <unistd.h>
 
-int cat_main(int argc, char **argv)
+int bb_cat(char **argv)
 {
        FILE *f;
        int retval = EXIT_SUCCESS;
 
-       getopt32(argc, argv, "u");
-
-       argv += optind;
-       if (!*argv) {
-               *--argv = "-";
-       }
-
        do {
                f = fopen_or_warn_stdin(*argv);
                if (f) {
@@ -39,3 +31,15 @@ int cat_main(int argc, char **argv)
 
        return retval;
 }
+
+int cat_main(int argc, char **argv)
+{
+       getopt32(argc, argv, "u");
+
+       argv += optind;
+       if (!*argv) {
+               *--argv = "-";
+       }
+
+       return bb_cat(argv);
+}
index 887679a0a57f0d13e5c01f14df1c2cd26afe92e6..a49d5195a7fa6a7db1e6065249f933bd607fc1dd 100644 (file)
@@ -1079,7 +1079,7 @@ static char **get_dir(char *path)
 
                dp = warn_opendir(path);
                while ((ep = readdir(dp))) {
-                       if ((!strcmp(ep->d_name, "..")) || (!strcmp(ep->d_name, ".")))
+                       if (!strcmp(ep->d_name, "..") || LONE_CHAR(ep->d_name, '.'))
                                continue;
                        add_to_dirlist(ep->d_name, NULL, NULL, 0);
                }
index 99063ae5295a19b9daba4b064fdc0d91432b6567..0c8eac3bc07cf3dff27385f45dd63792947bcc18 100644 (file)
@@ -29,7 +29,7 @@
 #include <stdlib.h>
 #include "busybox.h"
 
-int bb_echo(int ATTRIBUTE_UNUSED argc, char **argv)
+int bb_echo(char **argv)
 {
 #ifndef CONFIG_FEATURE_FANCY_ECHO
 #define eflag '\\'
@@ -114,7 +114,7 @@ just_echo:
 
 int echo_main(int argc, char** argv)
 {
-       (void)bb_echo(argc, argv);
+       (void)bb_echo(argv);
        fflush_stdout_and_exit(EXIT_SUCCESS);
 }
 
index 19147344609fe8ed0e2af00713f428e6d7634ab4..51e553dc6baaab5a21e30b9df937815f6d5085d7 100644 (file)
@@ -136,7 +136,7 @@ static int null(VALUE * v)
        if (v->type == integer)
                return v->u.i == 0;
        else                            /* string: */
-               return v->u.s[0] == '\0' || strcmp(v->u.s, "0") == 0;
+               return v->u.s[0] == '\0' || LONE_CHAR(v->u.s, '0');
 }
 
 /* Coerce V to a string value (can't fail).  */
index ebb4f908660d653da8c481de61169792cc80ce00..d3d7604671fa264726cee90f1543321f80f74f52 100644 (file)
@@ -175,14 +175,16 @@ int bb_test(int argc, char **argv)
 {
        int res;
 
-       if (strcmp(argv[0], "[") == 0) {
-               if (strcmp(argv[--argc], "]")) {
+       if (LONE_CHAR(argv[0], '[')) {
+               --argc;
+               if (NOT_LONE_CHAR(argv[argc], ']')) {
                        bb_error_msg("missing ]");
                        return 2;
                }
                argv[argc] = NULL;
        } else if (strcmp(argv[0], "[[") == 0) {
-               if (strcmp(argv[--argc], "]]")) {
+               --argc;
+               if (strcmp(argv[argc], "]]")) {
                        bb_error_msg("missing ]]");
                        return 2;
                }
@@ -578,6 +580,6 @@ static int is_a_group_member(gid_t gid)
 
 int test_main(int argc, char **argv)
 {
-       exit(bb_test(argc, argv));
+       return bb_test(argc, argv);
 }
 
index d69000be779c1d0de2aceeda7e9f181fbf1869a3..3d11734d5b05d2ff787fddbea5eb60a799b06c2c 100644 (file)
@@ -189,7 +189,7 @@ static void lvm_probe_all(blkid_cache cache)
                struct dirent   *lv_iter;
 
                vg_name = vg_iter->d_name;
-               if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
+               if (LONE_CHAR(vg_name, '.') || !strcmp(vg_name, ".."))
                        continue;
                vdirname = xmalloc(vg_len + strlen(vg_name) + 8);
                sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
@@ -203,7 +203,7 @@ static void lvm_probe_all(blkid_cache cache)
                        char            *lv_name, *lvm_device;
 
                        lv_name = lv_iter->d_name;
-                       if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
+                       if (LONE_CHAR(lv_name, '.') || !strcmp(lv_name, ".."))
                                continue;
 
                        lvm_device = xmalloc(vg_len + strlen(vg_name) +
index 4c341627edd05fe1fa018f450851b0d08e2acbb8..4848e1e1af9780a98a80a5a316e5dc4d15a8436a 100644 (file)
@@ -157,9 +157,10 @@ skip_setflags:
 static int chattr_dir_proc(const char *dir_name, struct dirent *de,
                           void *private EXT2FS_ATTR((unused)))
 {
-       /*if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {*/
-       if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || \
-          (de->d_name[1] == '.' && de->d_name[2] == '\0'))) {
+       /*if (strcmp(de->d_name, ".") || strcmp(de->d_name, "..")) {*/
+       if (de->d_name[0] == '.'
+        && (!de->d_name[1] || (de->d_name[1] == '.' && !de->d_name[2]))
+       ) {
                char *path = concat_subpath_file(dir_name, de->d_name);
                if (path) {
                        change_attributes(path);
index cace7715c94471a3abad172be853db3135520649..d943f1185bbb068475d451b7d4c5694334ae3dad 100644 (file)
@@ -144,7 +144,7 @@ static errcode_t check_mntent_file(const char *mtab_file, const char *file,
         * read/write, since if the root is mounted read/only, the
         * contents of /etc/mtab may not be accurate.
         */
-       if (!strcmp(mnt->mnt_dir, "/")) {
+       if (LONE_CHAR(mnt->mnt_dir, '/')) {
 is_root:
 #define TEST_FILE "/.ismount-test-file"
                *mount_flags |= EXT2_MF_ISROOT;
index 3b01c1021ba207cccdfbac60942cba6dfa1a3612..da66250f1b9d4a8189720ccb42c09b1eccf0bfc0 100644 (file)
@@ -1080,7 +1080,7 @@ static int check_all(void)
         */
        if (!parallel_root) {
                for (fs = filesys_info; fs; fs = fs->next) {
-                       if (!strcmp(fs->mountpt, "/"))
+                       if (LONE_CHAR(fs->mountpt, '/'))
                                break;
                }
                if (fs) {
@@ -1099,7 +1099,7 @@ static int check_all(void)
         */
        if (skip_root)
                for (fs = filesys_info; fs; fs = fs->next)
-                       if (!strcmp(fs->mountpt, "/"))
+                       if (LONE_CHAR(fs->mountpt, '/'))
                                fs->flags |= FLAG_DONE;
 
        while (not_done_yet) {
index 7dc7abd7f08e77a95e637e38adb9943fc864b387..2fd54e789f2ff16ab4c926f2137741f09861609d 100644 (file)
@@ -263,6 +263,8 @@ extern char *xasprintf(const char *format, ...) __attribute__ ((format (printf,
 //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])
+#define LONE_CHAR(s,c) ((s)[0] == (c) && !(s)[1])
+#define NOT_LONE_CHAR(s,c) ((s)[0] != (c) || (s)[1])
 
 /* dmalloc will redefine these to it's own implementation. It is safe
  * to have the prototypes here unconditionally.  */
@@ -386,7 +388,9 @@ extern void bb_vperror_msg(const char *s, va_list p);
 extern void bb_vinfo_msg(const char *s, va_list p);
 
 
-extern int bb_echo(int argc, char** argv);
+/* applets which are useful from another applets */
+extern int bb_cat(char** argv);
+extern int bb_echo(char** argv);
 extern int bb_test(int argc, char** argv);
 
 #ifndef BUILD_INDIVIDUAL
index 213a5c149ccc36f83c0e236f6cbb12cf68979e1a..bc53feeae246416cee2c543ebacdb06c27d0b121 100644 (file)
@@ -1047,8 +1047,9 @@ int init_main(int argc, char **argv)
        }
 
        /* Check if we are supposed to be in single user mode */
-       if (argc > 1 && (!strcmp(argv[1], "single") ||
-                                        !strcmp(argv[1], "-s") || !strcmp(argv[1], "1"))) {
+       if (argc > 1
+        && (!strcmp(argv[1], "single") || !strcmp(argv[1], "-s") || LONE_CHAR(argv[1], '1'))
+       ) {
                /* Start a shell on console */
                new_init_action(RESPAWN, bb_default_login_shell, "");
        } else {
index fd7e0b56c8d85e26796dc93635c0d2a7fafadd71..d031b2109a6e9f8d8e655324f2f181255b409dfd 100644 (file)
@@ -40,15 +40,14 @@ int correct_password(const struct passwd *pw)
        char *unencrypted, *encrypted, *correct;
 
 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
-       if (!strcmp(pw->pw_passwd, "x") || !strcmp(pw->pw_passwd, "*")) {
+       if (LONE_CHAR(pw->pw_passwd, 'x') || LONE_CHAR(pw->pw_passwd, '*')) {
                struct spwd *sp = getspnam(pw->pw_name);
 
                if (!sp)
                        bb_error_msg_and_die("no valid shadow password");
 
                correct = sp->sp_pwdp;
-       }
-       else
+       } else
 #endif
                correct = pw->pw_passwd;
 
index 668f0c17dc5d5f36731483066394361cc50c91fb..fd1033bf7c493a651ad54a0afb3343e63491c5ea 100644 (file)
@@ -44,7 +44,7 @@ int last_main(int argc, char **argv)
                        bb_perror_msg_and_die("short read");
                }
 
-               if (strncmp(ut.ut_line, "~", 1) == 0) {
+               if (ut.ut_line[0] == '~') {
                        if (strncmp(ut.ut_user, "shutdown", 8) == 0)
                                ut.ut_type = SHUTDOWN_TIME;
                        else if (strncmp(ut.ut_user, "reboot", 6) == 0)
index 4856b11ae5c9cf84b0dfe506a38fa6a0f54ae7a7..7c89be28f11a807e82c67c18e9b02dff981ecaee 100644 (file)
@@ -759,7 +759,7 @@ static servtab_t *getconfigent(void)
        while (nsep != NULL) {
                nsep->se_checked = 1;
                if (nsep->se_family == AF_INET) {
-                       if (!strcmp(nsep->se_hostaddr, "*"))
+                       if (LONE_CHAR(nsep->se_hostaddr, '*'))
                                nsep->se_ctrladdr_in.sin_addr.s_addr = INADDR_ANY;
                        else if (!inet_aton(nsep->se_hostaddr, &nsep->se_ctrladdr_in.sin_addr)) {
                                struct hostent *hp;
index 9fb08e6bae1ee79dd052772c3da62e3efc8e3612..e6130e4d7f11adb857269aafee664d836c0b63a3 100644 (file)
@@ -678,7 +678,7 @@ static int ipaddr_modify(int cmd, int argc, char **argv)
                                if (brd_len) {
                                        duparg("broadcast", *argv);
                                }
-                               if (strcmp(*argv, "+") == 0) {
+                               if (LONE_CHAR(*argv, '+')) {
                                        brd_len = -1;
                                }
                                else if (LONE_DASH(*argv)) {
index 97f0d6befce48949d3c5bbf83fd84e94ca1ea658..591e0a658e3b0252d464a1a87178df2c654e0693 100644 (file)
@@ -8172,7 +8172,7 @@ exitcmd(int argc, char **argv)
 static int
 echocmd(int argc, char **argv)
 {
-       return bb_echo(argc, argv);
+       return bb_echo(argv);
 }
 #endif