- if (var) {
- int offset = dst-src;
-#warning I have a memory leak which needs to be plugged somehow
- src = (char*)xmalloc(strlen(src)-strlen(dst)+strlen(var)+1);
- strncpy(src, prog->argv[argc_l -1], offset);
- safe_strncpy(src+offset, var, strlen(var)+1);
- /* If there are any remaining $ variables in the src string, put them back */
- if ((dst = strchr(prog->argv[argc_l -1]+offset+1,'$')) != NULL) {
- offset=strlen(src);
- safe_strncpy(src+strlen(src), dst, strlen(dst)+1);
+ 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);