1 /* Copyright (C) 1992 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
20 #include <sys/types.h>
28 /* We do word expansion with a pipe to the shell.
29 The shell command `sh [-P] [-u] -w "words ..."' expands words.
30 If -P, command substitution is an error.
31 If -u, reference to an undefined variable is an error.
32 The shell writes on its stdout:
34 %u\0 Number of bytes in all words together (not counting \0s).
41 #define SHELL_PATH "/bin/sh"
42 #define SHELL_NAME "sh"
46 DEFUN(wordexp, (string, pwordexp, flags),
47 CONST char *string AND wordexp_t *pwordexp AND int flags)
55 size_t wordc, start, buflen;
58 /* Create the pipe through which we will communicate to the shell. */
68 /* Child. Run the shell. */
72 close (d[STDIN_FILENO]);
73 dup2 (d[STDOUT_FILENO], STDOUT_FILENO);
74 if (!(flags & WRDE_SHOWERR))
75 close (STDERR_FILENO);
78 argv[i++] = SHELL_NAME;
79 if (flags & WRDE_NOCMD)
81 if (flags & WRDE_UNDEF)
87 execv (SHELL_PATH, argv);
96 close (d[STDOUT_FILENO]);
97 f = fdopen (d[STDIN_FILENO]);
101 /* Read the number of words and number of bytes from the shell. */
102 if (fscanf (f, "%u", &wordc) != 1 || getc (f) != '\0' ||
103 fscanf (f, "%u", &buflen) != 1 || getc (f) != '\0')
106 /* Read the words from the shell, and wait for it to return. */
108 buf = malloc (buflen);
110 fread (buf, buflen, 1, f) != 1 ||
111 waitpid (pid, &status, 0) != pid)
114 if (WIFEXITED (status))
116 if (WEXITSTATUS (status) != 0)
118 error = WEXITSTATUS (status);
125 /* Pack the structure. */
128 if (flags & WRDE_DOOFFS)
129 start += pwordexp->we_offs;
130 if (flags & WRDE_APPEND)
131 start += pwordexp->we_wordc;
132 wordc = start + wordc + 1;
134 if (flags & WRDE_APPEND)
135 wordv = (char **) realloc ((PTR) pwordexp->we_wordv,
136 wordc * sizeof (char *));
138 wordv = (char **) malloc (wordc * sizeof (char *));
142 if (flags & WRDE_DOOFFS)
143 for (i = 0; i < pwordexp->we_offs; ++i)
146 for (i = start; i < wordc; ++i)
148 pwordexp->we_wordv[i] = buf;
149 buf = strchr (buf, '\0') + 1;
153 if (flags & WRDE_REUSE)
155 free (pwordexp->we_wordv[0]);
156 if (!(flags & WRDE_APPEND))
157 free (pwordexp->we_wordv);
160 pwordexp->we_wordc = wordc;
161 pwordexp->we_wordv = wordv;
169 (void) kill (pid, SIGKILL);
171 (void) waitpid (pid, (int *) NULL, 0);
179 DEFUN(wordexp, (pwordexp), wordexp_t *pwordexp)
181 /* All the other elts point into the first. */
182 free (pwordexp->we_wordv[0]);
183 free (pwordexp->we_wordv);