Updates from both Vladimir and Larry
[oweals/busybox.git] / lash.c
diff --git a/lash.c b/lash.c
index 243ba8139d7fa03da14e998ca13aee166a58014a..f1200ba951f5a530f7d9b9e66f080096d9915494 100644 (file)
--- a/lash.c
+++ b/lash.c
@@ -64,6 +64,8 @@
 #include <sys/wait.h>
 #include <unistd.h>
 #include <getopt.h>
+#include "busybox.h"
+#include "cmdedit.h"
 
 #ifdef BB_LOCALE_SUPPORT
 #include <locale.h>
@@ -79,8 +81,6 @@
 #include <glob.h>
 #define expand_t       glob_t
 #endif 
-#include "busybox.h"
-#include "cmdedit.h"
 
 
 static const int MAX_READ = 128;       /* size of input buffer for `read' builtin */
@@ -216,7 +216,7 @@ unsigned int shell_context;  /* Used in cmdedit.c to reset the
 
 
 /* Globals that are static to this file */
-static char *cwd;
+static const char *cwd;
 static char *local_pending_command = NULL;
 static struct jobset job_list = { NULL, NULL };
 static int argc;
@@ -296,8 +296,9 @@ static int builtin_cd(struct child_prog *child)
                printf("cd: %s: %m\n", newdir);
                return EXIT_FAILURE;
        }
-       cwd = xgetcwd(cwd);
-
+       cwd = xgetcwd((char *)cwd);
+       if (!cwd)
+               cwd = unknown;
        return EXIT_SUCCESS;
 }
 
@@ -412,7 +413,10 @@ static int builtin_jobs(struct child_prog *child)
 /* built-in 'pwd' handler */
 static int builtin_pwd(struct child_prog *dummy)
 {
-       printf( "%s\n", cwd);
+       cwd = xgetcwd((char *)cwd);
+       if (!cwd)
+               cwd = unknown;
+       puts(cwd);
        return EXIT_SUCCESS;
 }
 
@@ -425,14 +429,14 @@ static int builtin_export(struct child_prog *child)
        if (v == NULL) {
                char **e;
                for (e = environ; *e; e++) {
-                       printf( "%s\n", *e);
+                       puts(*e);
                }
                return 0;
        }
        res = putenv(v);
        if (res)
                fprintf(stderr, "export: %m\n");
-#ifndef BB_FEATURE_SH_SIMPLE_PROMPT
+#ifdef BB_FEATURE_SH_FANCY_PROMPT
        if (strncmp(v, "PS1=", 4)==0)
                PS1 = getenv("PS1");
 #endif
@@ -805,7 +809,7 @@ static void restore_redirects(int squirrel[])
 
 static inline void cmdedit_set_initial_prompt(void)
 {
-#ifdef BB_FEATURE_SH_SIMPLE_PROMPT
+#ifndef BB_FEATURE_SH_FANCY_PROMPT
        PS1 = NULL;
 #else
        PS1 = getenv("PS1");
@@ -816,7 +820,7 @@ static inline void cmdedit_set_initial_prompt(void)
 
 static inline void setup_prompt_string(char **prompt_str)
 {
-#ifdef BB_FEATURE_SH_SIMPLE_PROMPT
+#ifndef BB_FEATURE_SH_FANCY_PROMPT
        /* Set up the prompt */
        if (shell_context == 0) {
                if (PS1)
@@ -1019,6 +1023,8 @@ static int expand_arguments(char *command)
                                (tmpcmd = strsep_space(cmd, &ix)) != NULL; cmd += ix, ix=0) {
                        if (*tmpcmd == '\0')
                                break;
+                       /* we need to trim() the result for glob! */
+                       trim(tmpcmd);
                        retval = glob(tmpcmd, flags, NULL, &expand_result);
                        free(tmpcmd); /* Free mem allocated by strsep_space */
                        if (retval == GLOB_NOSPACE) {
@@ -1041,10 +1047,8 @@ static int expand_arguments(char *command)
                                                error_msg(out_of_space);
                                                return FALSE;
                                        }
-                                       if (i>0) {
-                                               strcat(command+total_length, " ");
-                                               total_length+=1;
-                                       }
+                                       strcat(command+total_length, " ");
+                                       total_length+=1;
                                        strcat(command+total_length, expand_result.gl_pathv[i]);
                                        total_length+=length;
                                }
@@ -1803,7 +1807,7 @@ static int busy_loop(FILE * input)
                        if (!job_list.fg) {
                                /* move the shell to the foreground */
                                /* suppress messages when run from /linuxrc mag@sysgo.de */
-                               if (tcsetpgrp(0, getpid()) && errno != ENOTTY)
+                               if (tcsetpgrp(0, getpgrp()) && errno != ENOTTY)
                                        perror_msg("tcsetpgrp"); 
                        }
                }
@@ -1827,7 +1831,6 @@ void free_memory(void)
 {
        if (cwd) {
                free(cwd);
-               cwd = NULL;
        }
        if (local_pending_command)
                free(local_pending_command);
@@ -1919,6 +1922,8 @@ int shell_main(int argc_l, char **argv_l)
 
        /* initialize the cwd -- this is never freed...*/
        cwd = xgetcwd(0);
+       if (!cwd)
+               cwd = unknown;
 
 #ifdef BB_FEATURE_CLEAN_UP
        atexit(free_memory);