hush: fix "export a=a b=b" (was not setting and exporting b)
authorDenis Vlasenko <vda.linux@googlemail.com>
Mon, 6 Apr 2009 11:33:07 +0000 (11:33 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Mon, 6 Apr 2009 11:33:07 +0000 (11:33 -0000)
function                                             old     new   delta
builtin_export                                       103     117     +14

shell/hush.c

index 119518bd15fb42ce4f189ae76da7229d8d230dfa..3cf651b33d3075b9a3292069ad10c9a6d1b07bd7 100644 (file)
@@ -5442,8 +5442,8 @@ static int builtin_eval(char **argv)
 {
        int rcode = EXIT_SUCCESS;
 
-       if (argv[1]) {
-               char *str = expand_strvec_to_string(argv + 1);
+       if (*++argv) {
+               char *str = expand_strvec_to_string(argv);
                /* bash:
                 * eval "echo Hi; done" ("done" is syntax error):
                 * "echo Hi" will not execute too.
@@ -5458,13 +5458,14 @@ static int builtin_eval(char **argv)
 static int builtin_cd(char **argv)
 {
        const char *newdir;
-       if (argv[1] == NULL) {
+       if (*++argv == NULL) {
                /* bash does nothing (exitcode 0) if HOME is ""; if it's unset,
                 * bash says "bash: cd: HOME not set" and does nothing (exitcode 1)
                 */
                newdir = getenv("HOME") ? : "/";
-       } else
-               newdir = argv[1];
+       } else {
+               newdir = *argv;
+       }
        if (chdir(newdir)) {
                printf("cd: %s: %s\n", newdir, strerror(errno));
                return EXIT_FAILURE;
@@ -5475,14 +5476,14 @@ static int builtin_cd(char **argv)
 
 static int builtin_exec(char **argv)
 {
-       if (argv[1] == NULL)
+       if (*++argv == NULL)
                return EXIT_SUCCESS; /* bash does this */
        {
 #if !BB_MMU
                nommu_save_t dummy;
 #endif
 // FIXME: if exec fails, bash does NOT exit! We do...
-               pseudo_exec_argv(&dummy, argv + 1, 0, NULL);
+               pseudo_exec_argv(&dummy, argv, 0, NULL);
                /* never returns */
        }
 }
@@ -5493,20 +5494,17 @@ static int builtin_exit(char **argv)
        //puts("exit"); /* bash does it */
 // TODO: warn if we have background jobs: "There are stopped jobs"
 // On second consecutive 'exit', exit anyway.
-       if (argv[1] == NULL)
+       if (*++argv == NULL)
                hush_exit(G.last_return_code);
        /* mimic bash: exit 123abc == exit 255 + error msg */
        xfunc_error_retval = 255;
        /* bash: exit -2 == exit 254, no error msg */
-       hush_exit(xatoi(argv[1]) & 0xff);
+       hush_exit(xatoi(*argv) & 0xff);
 }
 
 static int builtin_export(char **argv)
 {
-       const char *value;
-       char *name = argv[1];
-
-       if (name == NULL) {
+       if (*++argv == NULL) {
                // TODO:
                // ash emits: export VAR='VAL'
                // bash: declare -x VAR="VAL"
@@ -5518,23 +5516,28 @@ static int builtin_export(char **argv)
                return EXIT_SUCCESS;
        }
 
-       value = strchr(name, '=');
-       if (!value) {
-               /* They are exporting something without a =VALUE */
-               struct variable *var;
-
-               var = get_local_var(name);
-               if (var) {
-                       var->flg_export = 1;
-                       debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr);
-                       putenv(var->varstr);
+       do {
+               const char *value;
+               char *name = *argv;
+
+               value = strchr(name, '=');
+               if (!value) {
+                       /* They are exporting something without a =VALUE */
+                       struct variable *var;
+
+                       var = get_local_var(name);
+                       if (var) {
+                               var->flg_export = 1;
+                               debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr);
+                               putenv(var->varstr);
+                       }
+                       /* bash does not return an error when trying to export
+                        * an undefined variable.  Do likewise. */
+                       continue;
                }
-               /* bash does not return an error when trying to export
-                * an undefined variable.  Do likewise. */
-               return EXIT_SUCCESS;
-       }
+               set_local_var(xstrdup(name), 1, 0);
+       } while (*++argv);
 
-       set_local_var(xstrdup(name), 1, 0);
        return EXIT_SUCCESS;
 }