1 /* Copyright (C) 2002 by Red Hat, Incorporated. All rights reserved.
3 * Permission to use, copy, modify, and distribute this software
4 * is freely granted, provided that this notice is preserved.
24 #define MAXLINELEN 500
26 /* Note: This implementation of wordexp requires a version of bash
27 that supports the --wordexp and --protected arguments to be present
28 on the system. It does not support the WRDE_UNDEF flag. */
30 wordexp(const char *words, wordexp_t *pwordexp, int flags)
43 int err = WRDE_NOSPACE;
53 if (flags & WRDE_REUSE)
56 if ((flags & WRDE_APPEND) == 0)
58 pwordexp->we_wordc = 0;
59 pwordexp->we_wordv = NULL;
62 if (flags & WRDE_DOOFFS)
64 offs = pwordexp->we_offs;
66 wordv = (char **)realloc(pwordexp->we_wordv, (pwordexp->we_wordc + offs + 1) * sizeof(char *));
69 pwordexp->we_wordv = wordv;
71 for (i = 0; i < offs; i++)
72 pwordexp->we_wordv[i] = NULL;
87 /* In "parent" process, but fork failed */
96 /* In parent process. */
98 /* Close write end of parent's pipe. */
102 /* f_err is the standard error from the shell command. */
103 if (!(f_err = fdopen(fd_err[0], "r")))
106 /* Check for errors. */
107 if (fgets(tmp, MAXLINELEN, f_err))
109 if (strstr(tmp, "EOF"))
111 else if (strstr(tmp, "`\n'") || strstr(tmp, "`|'")
112 || strstr(tmp, "`&'") || strstr(tmp, "`;'")
113 || strstr(tmp, "`<'") || strstr(tmp, "`>'")
114 || strstr(tmp, "`('") || strstr(tmp, "`)'")
115 || strstr(tmp, "`{'") || strstr(tmp, "`}'"))
117 else if (strstr(tmp, "command substitution"))
122 if (flags & WRDE_SHOWERR)
124 fprintf(stderr, tmp);
125 while(fgets(tmp, MAXLINELEN, f_err))
126 fprintf(stderr, tmp);
132 /* f is the standard output from the shell command. */
133 if (!(f = fdopen(fd[0], "r")))
136 /* Get number of words expanded by shell. */
137 if (!fgets(tmp, MAXLINELEN, f))
140 if((iter = strchr(tmp, '\n')))
143 num_words = atoi(tmp);
145 wordv = (char **)realloc(pwordexp->we_wordv,
146 (pwordexp->we_wordc + num_words + offs + 1) * sizeof(char *));
149 pwordexp->we_wordv = wordv;
151 /* Get number of bytes required for storage of all num_words words. */
152 if (!fgets(tmp, MAXLINELEN, f))
155 if((iter = strchr(tmp, '\n')))
158 num_bytes = atoi(tmp);
160 /* Get expansion from the shell output. */
161 if (!(ewords = (char *)malloc(num_bytes + num_words + 1)))
163 if (!fread(ewords, 1, num_bytes + num_words, f))
165 ewords[num_bytes + num_words] = 0;
167 /* Store each entry in pwordexp's we_wordv vector. */
169 for(i = 0; i < num_words; i++)
171 if (eword && (iter = strchr(eword, '\n')))
175 !(pwordexp->we_wordv[pwordexp->we_wordc + offs + i] = strdup(eword)))
177 pwordexp->we_wordv[pwordexp->we_wordc + offs + i] = NULL;
178 pwordexp->we_wordc += i;
181 eword = iter ? iter + 1 : iter;
184 pwordexp->we_wordv[pwordexp->we_wordc + offs + i] = NULL;
185 pwordexp->we_wordc += num_words;
199 /* Wait for child to finish. */
200 waitpid (pid, NULL, 0);
206 /* In child process. */
208 /* Close read end of child's pipe. */
212 /* Pipe standard output to parent process via fd. */
213 if (fd[1] != STDOUT_FILENO)
215 if (dup2(fd[1], STDOUT_FILENO) == -1)
217 /* fd[1] no longer required. */
221 /* Pipe standard error to parent process via fd_err. */
222 if (fd_err[1] != STDERR_FILENO)
224 if (dup2(fd_err[1], STDERR_FILENO) == -1)
226 /* fd_err[1] no longer required. */
230 if (flags & WRDE_NOCMD)
231 execl("/bin/bash", "bash", "--protected", "--wordexp", words, (char *)0);
233 execl("/bin/bash", "bash", "--wordexp", words, (char *)0);
238 #endif /* !_NO_WORDEXP */