1 .\" Copyright (c) 2003 Andries Brouwer (aeb@cwi.nl)
3 .\" This is free documentation; you can redistribute it and/or
4 .\" modify it under the terms of the GNU General Public License as
5 .\" published by the Free Software Foundation; either version 2 of
6 .\" the License, or (at your option) any later version.
8 .\" The GNU General Public License's references to "object code"
9 .\" and "executables" are to be interpreted as the output of any
10 .\" document formatting or typesetting system, including
11 .\" intermediate and printed output.
13 .\" This manual is distributed in the hope that it will be useful,
14 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
15 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 .\" GNU General Public License for more details.
18 .\" You should have received a copy of the GNU General Public
19 .\" License along with this manual; if not, write to the Free
20 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
23 .TH WORDEXP 3 2003-11-11 "" "Linux Programmer's Manual"
25 wordexp, wordfree \- perform word expansion like a posix-shell
28 .B "#include <wordexp.h>"
30 .BI "int wordexp(const char *" s ", wordexp_t *" p ", int " flags );
32 .BI "void wordfree(wordexp_t *" p );
37 performs a shell-like expansion of the string
39 and returns the result in the structure pointed to by
43 is a structure that at least has the fields
52 that gives the number of words in the expansion of
58 that points to the array of words found.
63 is sometimes (depending on
65 see below) used to indicate the number of initial elements in the
67 array that should be filled with NULLs.
71 frees the allocated memory again. More precisely, it does not free
72 its argument, but it frees the array
74 and the strings that points to.
77 First a small example. The output is approximately that of "ls [a-c]*.c".
83 int main(int argc, char **argv) {
88 wordexp("[a-c]*.c", &p, 0);
90 for (i=0; i<p.we_wordc; i++)
91 printf("%s\en", w[i]);
97 .SS "The string argument"
98 Since the expansion is the same as the expansion by the shell (see
100 of the parameters to a command, the string
102 must not contain characters that would be illegal in shell command
103 parameters. In particular, there must not be any non-escaped
104 newline or |, &, ;, <, >, (, ), {, } characters
105 outside a command substitution or parameter substitution context.
109 contains a word that starts with an unquoted comment character #,
110 then it is unspecified whether that word and all following words
111 are ignored, or the # is treated as a non-comment character.
114 The expansion done consists of the following stages:
115 tilde expansion (replacing ~user by user's home directory),
116 variable substitution (replacing $FOO by the value of the environment
117 variable FOO), command substitution (replacing $(command) or `command`
118 by the output of command), arithmetic expansion, field splitting,
119 wildcard expansion, quote removal.
121 The result of expansion of special parameters
122 ($@, $*, $#, $?, $-, $$, $!, $0) is unspecified.
124 Field splitting is done using the environment variable $IFS.
125 If it is not set, the field separators are space, tab and newline.
127 .SS "The output array"
130 contains the words found, followed by a NULL.
132 .SS "The flags argument"
135 argument is a bitwise inclusive OR of the following values:
138 Append the words found to the array resulting from a previous call.
143 initial NULLs in the array
145 (These are not counted in the returned
149 Don't do command substitution.
154 resulted from a previous call to
158 was not called. Reuse the allocated storage.
161 Normally during command substitution
165 This flag specifies that
167 is not to be redirected.
170 Consider it an error if an undefined shell variable is expanded.
172 In case of success 0 is returned. In case of error
173 one of the following five values is returned.
176 Illegal occurrence of newline or one of |, &, ;, <, >, (, ), {, }.
179 An undefined shell variable was referenced, and the WRDE_UNDEF flag
180 told us to consider this an error.
183 Command substitution occurred, and the WRDE_NOCMD flag
184 told us to consider this an error.
190 Shell syntax error, such as unbalanced parentheses or
194 XPG4, POSIX 1003.1-2003