+
+#ifdef BB_FEATURE_SH_ENVIRONMENT
+
+
+#ifdef BB_FEATURE_SH_WORDEXP
+ /* This first part uses wordexp() which is a wonderful C lib
+ * function which expands nearly everything. */
+ retval = wordexp (command, &expand_result, WRDE_SHOWERR);
+ if (retval == WRDE_NOSPACE) {
+ /* Mem may have been allocated... */
+ wordfree (&expand_result);
+ error_msg(out_of_space);
+ return FALSE;
+ }
+ if (retval < 0) {
+ /* Some other error. */
+ error_msg("syntax error");
+ return FALSE;
+ }
+
+ if (expand_result.we_wordc > 0) {
+ /* Convert from char** (one word per string) to a simple char*,
+ * but don't overflow command which is BUFSIZ in length */
+ *command = '\0';
+ while (i < expand_result.we_wordc && total_length < BUFSIZ) {
+ length=strlen(expand_result.we_wordv[i])+1;
+ if (BUFSIZ-total_length-length <= 0) {
+ error_msg(out_of_space);
+ return FALSE;
+ }
+ strcat(command+total_length, expand_result.we_wordv[i++]);
+ strcat(command+total_length, " ");
+ total_length+=length;
+ }
+ wordfree (&expand_result);
+ }
+#else
+
+ /* Ok. They don't have a recent glibc and they don't have uClibc. Chances
+ * are about 100% they don't have wordexp(). So instead the best we can do
+ * is use glob and then fixup environment variables and such ourselves.
+ * This is better then nothing, but certainly not perfect */
+
+ /* It turns out that glob is very stupid. We have to feed it one word at a
+ * time since it can't cope with a full string. Here we convert command
+ * (char*) into cmd (char**, one word per string) */
+ {
+
+ int flags = GLOB_NOCHECK
+#ifdef GLOB_BRACE
+ | GLOB_BRACE
+#endif
+#ifdef GLOB_TILDE
+ | GLOB_TILDE
+#endif
+ ;
+ char *tmpcmd, *cmd, *cmd_copy;
+ /* We need a clean copy, so strsep can mess up the copy while
+ * we write stuff into the original (in a minute) */
+ cmd = cmd_copy = strdup(command);
+ *command = '\0';
+ for (index = 0, tmpcmd = cmd;
+ (tmpcmd = strsep_space(cmd, &index)) != NULL; cmd += index, index=0) {
+ if (*tmpcmd == '\0')
+ break;
+ retval = glob(tmpcmd, flags, NULL, &expand_result);
+ free(tmpcmd); /* Free mem allocated by strsep_space */
+ if (retval == GLOB_NOSPACE) {
+ /* Mem may have been allocated... */
+ globfree (&expand_result);
+ error_msg(out_of_space);
+ return FALSE;
+ } else if (retval != 0) {
+ /* Some other error. GLOB_NOMATCH shouldn't
+ * happen because of the GLOB_NOCHECK flag in
+ * the glob call. */
+ error_msg("syntax error");
+ return FALSE;
+ } else {
+ /* Convert from char** (one word per string) to a simple char*,
+ * but don't overflow command which is BUFSIZ in length */
+ for (i=0; i < expand_result.gl_pathc; i++) {
+ length=strlen(expand_result.gl_pathv[i]);
+ if (total_length+length+1 >= BUFSIZ) {
+ error_msg(out_of_space);
+ return FALSE;
+ }
+ if (i>0) {
+ strcat(command+total_length, " ");
+ total_length+=1;
+ }
+ strcat(command+total_length, expand_result.gl_pathv[i]);
+ total_length+=length;
+ }
+ globfree (&expand_result);
+ }
+ }
+ free(cmd_copy);
+ trim(command);
+ }
+
+#endif
+
+ /* Now do the shell variable substitutions which
+ * wordexp can't do for us, namely $? and $! */
+ src = command;
+ while((dst = strchr(src,'$')) != NULL){
+ var = NULL;
+ switch(*(dst+1)) {
+ case '?':
+ var = itoa(last_return_code);
+ break;
+ case '!':
+ if (last_bg_pid==-1)
+ *(var)='\0';
+ else
+ var = itoa(last_bg_pid);
+ break;
+ /* Everything else like $$, $#, $[0-9], etc should all be
+ * expanded by wordexp(), so we can in theory skip that stuff
+ * here, but just to be on the safe side (i.e. since uClibc
+ * wordexp doesn't do this stuff yet), lets leave it in for
+ * now. */
+ case '$':
+ var = itoa(getpid());
+ break;
+ case '#':
+ var = itoa(argc-1);
+ break;
+ case '0':case '1':case '2':case '3':case '4':
+ case '5':case '6':case '7':case '8':case '9':
+ {
+ int index=*(dst + 1)-48;
+ if (index >= argc) {
+ var='\0';
+ } else {
+ var = argv[index];
+ }
+ }
+ break;
+
+ }
+ if (var) {
+ /* a single character construction was found, and
+ * already handled in the case statement */
+ src=dst+2;
+ } else {
+ /* Looks like an environment variable */
+ char delim_hold;
+ int num_skip_chars=0;
+ int dstlen = strlen(dst);
+ /* Is this a ${foo} type variable? */
+ if (dstlen >=2 && *(dst+1) == '{') {
+ src=strchr(dst+1, '}');
+ num_skip_chars=1;
+ } else {
+ src=dst+1;
+ while(isalnum(*src) || *src=='_') src++;
+ }
+ if (src == NULL) {
+ src = dst+dstlen;
+ }
+ delim_hold=*src;
+ *src='\0'; /* temporary */
+ var = getenv(dst + 1 + num_skip_chars);
+ *src=delim_hold;
+ src += num_skip_chars;
+ }
+ if (var == NULL) {
+ /* Seems we got an un-expandable variable. So delete it. */
+ var = "";
+ }
+ {
+ int subst_len = strlen(var);
+ int trail_len = strlen(src);
+ if (dst+subst_len+trail_len >= command+BUFSIZ) {
+ error_msg(out_of_space);
+ return FALSE;
+ }
+ /* Move stuff to the end of the string to accommodate
+ * filling the created gap with the new stuff */
+ memmove(dst+subst_len, src, trail_len+1);
+ /* Now copy in the new stuff */
+ memcpy(dst, var, subst_len);
+ src = dst+subst_len;