1WORDEXP(3P)                POSIX Programmer's Manual               WORDEXP(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10

NAME

12       wordexp, wordfree - perform word expansions
13

SYNOPSIS

15       #include <wordexp.h>
16
17       int wordexp(const char *restrict words, wordexp_t *restrict pwordexp,
18              int flags);
19       void wordfree(wordexp_t *pwordexp);
20
21

DESCRIPTION

23       The wordexp() function shall perform word expansions  as  described  in
24       the  Shell  and  Utilities volume of IEEE Std 1003.1-2001, Section 2.6,
25       Word Expansions, subject to quoting as in the Shell and Utilities  vol‐
26       ume  of  IEEE Std 1003.1-2001, Section 2.2, Quoting, and place the list
27       of expanded words into the structure pointed to by pwordexp.
28
29       The words argument is a pointer to a  string  containing  one  or  more
30       words to be expanded. The expansions shall be the same as would be per‐
31       formed by the command line interpreter if words were the part of a com‐
32       mand  line  representing  the  arguments  to  a utility. Therefore, the
33       application shall ensure that words does not contain an unquoted  <new‐
34       line>  or  any  of the unquoted shell special characters '|', '&', ';',
35       '<', '>' except in the context of command substitution as specified  in
36       the  Shell and Utilities volume of IEEE Std 1003.1-2001, Section 2.6.3,
37       Command Substitution.  It also shall not contain  unquoted  parentheses
38       or  braces,  except in the context of command or variable substitution.
39       The application shall ensure  that  every  member  of  words  which  it
40       expects to have expanded by wordexp() does not contain an unquoted ini‐
41       tial comment character. The application  shall  also  ensure  that  any
42       words  which it intends to be ignored (because they begin or continue a
43       comment) are deleted from words. If  the  argument  words  contains  an
44       unquoted  comment  character  (number  sign) that is the beginning of a
45       token, wordexp() shall either treat the comment character as a  regular
46       character,  or  interpret  it  as  a  comment  indicator and ignore the
47       remainder of words.
48
49       The structure type wordexp_t is defined in the <wordexp.h>  header  and
50       includes at least the following members:
51
52          Member Type Member Name Description
53          size_t      we_wordc    Count of words matched by words.
54          char **     we_wordv    Pointer to list of expanded words.
55          size_t      we_offs     Slots to reserve at the beginning of
56                                  pwordexp->we_wordv.
57
58       The wordexp() function shall store the number of generated  words  into
59       pwordexp->we_wordc  and  a  pointer  to  a list of pointers to words in
60       pwordexp->we_wordv.  Each individual field created during field  split‐
61       ting  (see the Shell and Utilities volume of IEEE Std 1003.1-2001, Sec‐
62       tion 2.6.5, Field Splitting) or pathname expansion (see the  Shell  and
63       Utilities  volume  of  IEEE Std 1003.1-2001,  Section  2.6.6,  Pathname
64       Expansion) shall be a separate word in the pwordexp->we_wordv list. The
65       words  shall be in order as described in the Shell and Utilities volume
66       of  IEEE Std 1003.1-2001,  Section  2.6,  Word  Expansions.  The  first
67       pointer after the last word pointer shall be a null pointer. The expan‐
68       sion of special parameters described in the Shell and Utilities  volume
69       of  IEEE Std 1003.1-2001, Section 2.5.2, Special Parameters is unspeci‐
70       fied.
71
72       It is the caller's responsibility to allocate the storage pointed to by
73       pwordexp.  The wordexp() function shall allocate other space as needed,
74       including memory pointed  to  by  pwordexp->we_wordv.   The  wordfree()
75       function frees any memory associated with pwordexp from a previous call
76       to wordexp().
77
78       The flags argument is used to control the behavior of  wordexp().   The
79       value  of flags is the bitwise-inclusive OR of zero or more of the fol‐
80       lowing constants, which are defined in <wordexp.h>:
81
82       WRDE_APPEND
83              Append words generated to the ones from a previous call to word‐
84              exp().
85
86       WRDE_DOOFFS
87              Make  use  of  pwordexp->we_offs.  If  this  flag is set, pword‐
88              exp->we_offs is used to specify how many null pointers to add to
89              the  beginning  of  pwordexp->we_wordv.   In other words, pword‐
90              exp->we_wordv shall point to  pwordexp->we_offs  null  pointers,
91              followed by pwordexp->we_wordc word pointers, followed by a null
92              pointer.
93
94       WRDE_NOCMD
95              If the implementation supports  the  utilities  defined  in  the
96              Shell and Utilities volume of IEEE Std 1003.1-2001, fail if com‐
97              mand substitution, as specified in the Shell and Utilities  vol‐
98              ume  of  IEEE Std 1003.1-2001,  Section 2.6.3, Command Substitu‐
99              tion, is requested.
100
101       WRDE_REUSE
102              The pwordexp argument was passed to a previous  successful  call
103              to  wordexp(), and has not been passed to wordfree(). The result
104              shall be the same as if the application  had  called  wordfree()
105              and then called wordexp() without WRDE_REUSE.
106
107       WRDE_SHOWERR
108              Do not redirect stderr to /dev/null.
109
110       WRDE_UNDEF
111              Report  error  on  an attempt to expand an undefined shell vari‐
112              able.
113
114
115       The WRDE_APPEND flag can be used to append a new set of words to  those
116       generated by a previous call to wordexp(). The following rules apply to
117       applications when two or more calls to wordexp() are made with the same
118       value of pwordexp and without intervening calls to wordfree():
119
120        1. The first such call shall not set WRDE_APPEND. All subsequent calls
121           shall set it.
122
123        2. All of the calls shall set WRDE_DOOFFS, or all shall not set it.
124
125        3. After the second and each subsequent call, pwordexp->we_wordv shall
126           point to a list containing the following:
127
128            a. Zero  or  more  null  pointers, as specified by WRDE_DOOFFS and
129               pwordexp->we_offs
130
131            b. Pointers to the words that were in the pwordexp->we_wordv  list
132               before the call, in the same order as before
133
134            c. Pointers  to the new words generated by the latest call, in the
135               specified order
136
137        4. The count returned in pwordexp->we_wordc shall be the total  number
138           of words from all of the calls.
139
140        5. The  application can change any of the fields after a call to word‐
141           exp(), but if it does it shall reset them  to  the  original  value
142           before  a  subsequent call, using the same pwordexp value, to word‐
143           free() or wordexp() with the WRDE_APPEND or WRDE_REUSE flag.
144
145       If the implementation supports the utilities defined in the  Shell  and
146       Utilities   volume  of  IEEE Std 1003.1-2001,  and  words  contains  an
147       unquoted character- <newline>, '|', '&', ';', '<', '>', '(', ')',  '{',
148       '}' - in an inappropriate context, wordexp() shall fail, and the number
149       of expanded words shall be 0.
150
151       Unless WRDE_SHOWERR is set in flags, wordexp() shall redirect stderr to
152       /dev/null  for  any utilities executed as a result of command substitu‐
153       tion while expanding words.  If  WRDE_SHOWERR  is  set,  wordexp()  may
154       write  messages to stderr if syntax errors are detected while expanding
155       words.
156
157       The application shall ensure that if WRDE_DOOFFS is  set,  then  pword‐
158       exp->we_offs  has the same value for each wordexp() call and wordfree()
159       call using a given pwordexp.
160
161       The following constants are defined as error return values:
162
163       WRDE_BADCHAR
164              One of the unquoted characters- <newline>, '|', '&',  ';',  '<',
165              '>',  '(',  ')', '{', '}' - appears in words in an inappropriate
166              context.
167
168       WRDE_BADVAL
169              Reference to undefined shell variable when WRDE_UNDEF is set  in
170              flags.
171
172       WRDE_CMDSUB
173              Command substitution requested when WRDE_NOCMD was set in flags.
174
175       WRDE_NOSPACE
176              Attempt to allocate memory failed.
177
178       WRDE_SYNTAX
179              Shell  syntax  error, such as unbalanced parentheses or untermi‐
180              nated string.
181
182

RETURN VALUE

184       Upon successful completion, wordexp() shall return 0. Otherwise, a non-
185       zero  value, as described in <wordexp.h>, shall be returned to indicate
186       an error. If wordexp() returns  the  value  WRDE_NOSPACE,  then  pword‐
187       exp->we_wordc  and  pwordexp->we_wordv  shall be updated to reflect any
188       words that were successfully expanded. In other cases, they  shall  not
189       be modified.
190
191       The wordfree() function shall not return a value.
192

ERRORS

194       No errors are defined.
195
196       The following sections are informative.
197

EXAMPLES

199       None.
200

APPLICATION USAGE

202       The  wordexp()  function  is intended to be used by an application that
203       wants to do all of the shell's expansions on a word or  words  obtained
204       from a user. For example, if the application prompts for a filename (or
205       list of filenames) and then uses wordexp() to process  the  input,  the
206       user  could  respond  with anything that would be valid as input to the
207       shell.
208
209       The WRDE_NOCMD flag is provided for applications that, for security  or
210       other  reasons,  want  to prevent a user from executing shell commands.
211       Disallowing unquoted shell special characters  also  prevents  unwanted
212       side effects, such as executing a command or writing a file.
213

RATIONALE

215       This  function was included as an alternative to glob(). There had been
216       continuing controversy over exactly what features should be included in
217       glob().  It is hoped that by providing wordexp() (which provides all of
218       the shell word expansions, but which may be slow to execute) and glob()
219       (which  is  faster, but which only performs pathname expansion, without
220       tilde or parameter expansion) this will satisfy the majority of  appli‐
221       cations.
222
223       While  wordexp() could be implemented entirely as a library routine, it
224       is expected that most implementations run a shell in a subprocess to do
225       the expansion.
226
227       Two different approaches have been proposed for how the required infor‐
228       mation might be presented to the shell and the results  returned.  They
229       are presented here as examples.
230
231       One  proposal is to extend the echo utility by adding a -q option. This
232       option would cause echo to add a backslash before  each  backslash  and
233       <blank>  that  occurs  within an argument. The wordexp() function could
234       then invoke the shell as follows:
235
236
237              (void) strcpy(buffer, "echo -q");
238              (void) strcat(buffer, words);
239              if ((flags & WRDE_SHOWERR) == 0)
240                  (void) strcat(buffer, "2>/dev/null");
241              f = popen(buffer, "r");
242
243       The wordexp() function would read the resulting output, remove unquoted
244       backslashes,  and  break  into  words  at  unquoted  <blank>s.  If  the
245       WRDE_NOCMD flag was set, wordexp() would  have  to  scan  words  before
246       starting  the subshell to make sure that there would be no command sub‐
247       stitution. In any case, it would have to scan words for  unquoted  spe‐
248       cial characters.
249
250       Another proposal is to add the following options to sh:
251
252       -w wordlist
253
254              This  option  provides  a wordlist expansion service to applica‐
255              tions.  The words in wordlist shall be expanded and the  follow‐
256              ing written to standard output:
257
258               1. The  count  of the number of words after expansion, in deci‐
259                  mal, followed by a null byte
260
261               2. The number of bytes needed to represent the  expanded  words
262                  (not  including  null separators), in decimal, followed by a
263                  null byte
264
265               3. The expanded words, each terminated by a null byte
266
267       If an error is encountered during word expansion, sh exits with a  non-
268       zero  status  after writing the former to report any words successfully
269       expanded
270
271       -P     Run in "protected" mode. If specified with  the  -w  option,  no
272              command substitution shall be performed.
273
274
275       With  these  options,  wordexp()  could be implemented fairly simply by
276       creating a subprocess using fork() and executing sh using the line:
277
278
279              execl(<shell path>, "sh", "-P", "-w", words, (char *)0);
280
281       after directing standard error to /dev/null.
282
283       It seemed objectionable for a library  routine  to  write  messages  to
284       standard  error,  unless explicitly requested, so wordexp() is required
285       to redirect standard error to /dev/null to ensure that no messages  are
286       generated,  even  for  commands  executed for command substitution. The
287       WRDE_SHOWERR flag can be specified to request that  error  messages  be
288       written.
289
290       The  WRDE_REUSE  flag allows the implementation to avoid the expense of
291       freeing and reallocating memory, if that is possible. A minimal  imple‐
292       mentation can call wordfree() when WRDE_REUSE is set.
293

FUTURE DIRECTIONS

295       None.
296

SEE ALSO

298       fnmatch(), glob(), the Base Definitions volume of IEEE Std 1003.1-2001,
299       <wordexp.h>, the Shell and Utilities  volume  of  IEEE Std 1003.1-2001,
300       Chapter 2, Shell Command Language
301
303       Portions  of  this text are reprinted and reproduced in electronic form
304       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
305       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
306       Specifications Issue 6, Copyright (C) 2001-2003  by  the  Institute  of
307       Electrical  and  Electronics  Engineers, Inc and The Open Group. In the
308       event of any discrepancy between this version and the original IEEE and
309       The  Open Group Standard, the original IEEE and The Open Group Standard
310       is the referee document. The original Standard can be  obtained  online
311       at http://www.opengroup.org/unix/online.html .
312
313
314
315IEEE/The Open Group                  2003                          WORDEXP(3P)
Impressum