1WORDEXP(3) Linux Programmer's Manual WORDEXP(3)
2
3
4
6 wordexp, wordfree - perform word expansion like a posix-shell
7
9 #include <wordexp.h>
10
11 int wordexp(const char *s, wordexp_t *p, int flags);
12
13 void wordfree(wordexp_t *p);
14
15 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17 wordexp(), wordfree(): _XOPEN_SOURCE
18
20 The function wordexp() performs a shell-like expansion of the string s
21 and returns the result in the structure pointed to by p. The data type
22 wordexp_t is a structure that at least has the fields we_wordc,
23 we_wordv, and we_offs. The field we_wordc is a size_t that gives the
24 number of words in the expansion of s. The field we_wordv is a char **
25 that points to the array of words found. The field we_offs of type
26 size_t is sometimes (depending on flags, see below) used to indicate
27 the number of initial elements in the we_wordv array that should be
28 filled with NULLs.
29
30 The function wordfree() frees the allocated memory again. More pre‐
31 cisely, it does not free its argument, but it frees the array we_wordv
32 and the strings that points to.
33
34 The string argument
35 Since the expansion is the same as the expansion by the shell (see
36 sh(1)) of the parameters to a command, the string s must not contain
37 characters that would be illegal in shell command parameters. In par‐
38 ticular, there must not be any unescaped newline or |, &, ;, <, >, (,
39 ), {, } characters outside a command substitution or parameter substi‐
40 tution context.
41
42 If the argument s contains a word that starts with an unquoted comment
43 character #, then it is unspecified whether that word and all following
44 words are ignored, or the # is treated as a non-comment character.
45
46 The expansion
47 The expansion done consists of the following stages: tilde expansion
48 (replacing ~user by user's home directory), variable substitution
49 (replacing $FOO by the value of the environment variable FOO), command
50 substitution (replacing $(command) or `command` by the output of com‐
51 mand), arithmetic expansion, field splitting, wildcard expansion, quote
52 removal.
53
54 The result of expansion of special parameters ($@, $*, $#, $?, $-, $$,
55 $!, $0) is unspecified.
56
57 Field splitting is done using the environment variable $IFS. If it is
58 not set, the field separators are space, tab and newline.
59
60 The output array
61 The array we_wordv contains the words found, followed by a NULL.
62
63 The flags argument
64 The flag argument is a bitwise inclusive OR of the following values:
65
66 WRDE_APPEND
67 Append the words found to the array resulting from a previous
68 call.
69
70 WRDE_DOOFFS
71 Insert we_offs initial NULLs in the array we_wordv. (These are
72 not counted in the returned we_wordc.)
73
74 WRDE_NOCMD
75 Don't do command substitution.
76
77 WRDE_REUSE
78 The argument p resulted from a previous call to wordexp(), and
79 wordfree() was not called. Reuse the allocated storage.
80
81 WRDE_SHOWERR
82 Normally during command substitution stderr is redirected to
83 /dev/null. This flag specifies that stderr is not to be redi‐
84 rected.
85
86 WRDE_UNDEF
87 Consider it an error if an undefined shell variable is expanded.
88
90 In case of success 0 is returned. In case of error one of the follow‐
91 ing five values is returned.
92
93 WRDE_BADCHAR
94 Illegal occurrence of newline or one of |, &, ;, <, >, (, ), {,
95 }.
96
97 WRDE_BADVAL
98 An undefined shell variable was referenced, and the WRDE_UNDEF
99 flag told us to consider this an error.
100
101 WRDE_CMDSUB
102 Command substitution occurred, and the WRDE_NOCMD flag told us
103 to consider this an error.
104
105 WRDE_NOSPACE
106 Out of memory.
107
108 WRDE_SYNTAX
109 Shell syntax error, such as unbalanced parentheses or unmatched
110 quotes.
111
113 wordexp() and wordfree() are provided in glibc since version 2.1.
114
116 POSIX.1-2001.
117
119 The output of the following example program is approximately that of
120 "ls [a-c]*.c".
121
122 #include <stdio.h>
123 #include <stdlib.h>
124 #include <wordexp.h>
125
126 int
127 main(int argc, char **argv)
128 {
129 wordexp_t p;
130 char **w;
131 int i;
132
133 wordexp("[a-c]*.c", &p, 0);
134 w = p.we_wordv;
135 for (i = 0; i < p.we_wordc; i++)
136 printf("%s\n", w[i]);
137 wordfree(&p);
138 exit(EXIT_SUCCESS);
139 }
140
142 fnmatch(3), glob(3)
143
145 This page is part of release 3.25 of the Linux man-pages project. A
146 description of the project, and information about reporting bugs, can
147 be found at http://www.kernel.org/doc/man-pages/.
148
149
150
151 2008-07-14 WORDEXP(3)