1WORDEXP(3)                 Linux Programmer's Manual                WORDEXP(3)
2
3
4

NAME

6       wordexp, wordfree - perform word expansion like a posix-shell
7

SYNOPSIS

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

DESCRIPTION

17       The  function wordexp() performs a shell-like expansion of the string s
18       and returns the result in the structure pointed to by p.  The data type
19       wordexp_t  is  a  structure  that  at  least  has  the fields we_wordc,
20       we_wordv, and we_offs.  The field we_wordc is a size_t that  gives  the
21       number of words in the expansion of s.  The field we_wordv is a char **
22       that points to the array of words found.  The  field  we_offs  of  type
23       size_t  is  sometimes  (depending on flags, see below) used to indicate
24       the number of initial elements in the we_wordv  array  that  should  be
25       filled with NULLs.
26
27       The  function  wordfree()  frees  the allocated memory again. More pre‐
28       cisely, it does not free its argument, but it frees the array  we_wordv
29       and the strings that points to.
30
31

EXAMPLE

33       First  a  small  example.  The  output is approximately that of "ls [a-
34       c]*.c".
35
36       #include <stdio.h>
37       #include <wordexp.h>
38
39       int main(int argc, char **argv) {
40               wordexp_t p;
41               char **w;
42               int i;
43
44               wordexp("[a-c]*.c", &p, 0);
45               w = p.we_wordv;
46               for (i=0; i<p.we_wordc; i++)
47                       printf("%s\n", w[i]);
48               wordfree(&p);
49               return 0;
50       }
51

DETAILS

53   The string argument
54       Since the expansion is the same as the  expansion  by  the  shell  (see
55       sh(1))  of  the  parameters to a command, the string s must not contain
56       characters that would be illegal in shell command parameters.  In  par‐
57       ticular, there must not be any non-escaped newline or |, &, ;, <, >, (,
58       ), {, } characters outside a command substitution or parameter  substi‐
59       tution context.
60
61       If  the argument s contains a word that starts with an unquoted comment
62       character #, then it is unspecified whether that word and all following
63       words are ignored, or the # is treated as a non-comment character.
64
65
66   The expansion
67       The  expansion  done  consists of the following stages: tilde expansion
68       (replacing ~user  by  user's  home  directory),  variable  substitution
69       (replacing  $FOO by the value of the environment variable FOO), command
70       substitution (replacing $(command) or `command` by the output  of  com‐
71       mand), arithmetic expansion, field splitting, wildcard expansion, quote
72       removal.
73
74       The result of expansion of special parameters ($@, $*, $#, $?, $-,  $$,
75       $!, $0) is unspecified.
76
77       Field  splitting is done using the environment variable $IFS.  If it is
78       not set, the field separators are space, tab and newline.
79
80
81   The output array
82       The array we_wordv contains the words found, followed by a NULL.
83
84
85   The flags argument
86       The flag argument is a bitwise inclusive OR of the following values:
87
88       WRDE_APPEND
89              Append the words found to the array resulting  from  a  previous
90              call.
91
92       WRDE_DOOFFS
93              Insert  we_offs initial NULLs in the array we_wordv.  (These are
94              not counted in the returned we_wordc.)
95
96       WRDE_NOCMD
97              Don't do command substitution.
98
99       WRDE_REUSE
100              The parameter p resulted from a previous call to wordexp(),  and
101              wordfree() was not called. Reuse the allocated storage.
102
103       WRDE_SHOWERR
104              Normally  during  command  substitution  stderr is redirected to
105              /dev/null.  This flag specifies that stderr is not to  be  redi‐
106              rected.
107
108       WRDE_UNDEF
109              Consider it an error if an undefined shell variable is expanded.
110

RETURN VALUE

112       In case of success 0 is returned. In case of error one of the following
113       five values is returned.
114
115       WRDE_BADCHAR
116              Illegal occurrence of newline or one of |, &, ;, <, >, (, ),  {,
117              }.
118
119       WRDE_BADVAL
120              An  undefined  shell variable was referenced, and the WRDE_UNDEF
121              flag told us to consider this an error.
122
123       WRDE_CMDSUB
124              Command substitution occurred, and the WRDE_NOCMD flag  told  us
125              to consider this an error.
126
127       WRDE_NOSPACE
128              Out of memory.
129
130       WRDE_SYNTAX
131              Shell  syntax error, such as unbalanced parentheses or unmatched
132              quotes.
133
134

CONFORMING TO

136       POSIX.1-2001
137
138

SEE ALSO

140       fnmatch(3), glob(3)
141
142
143
144                                  2003-11-11                        WORDEXP(3)
Impressum