1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
.\"
.\" Copyright (c) 2002 Tim J. Robbins
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd December 27, 2002
.Dt WORDEXP 3
.Os
.Sh NAME
.Nm wordexp
.Nd "perform shell-style word expansions"
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In sys/types.h
.In wordexp.h
.Ft int
.Fo wordexp
.Fa "const char * restrict words"
.Fa "wordexp_t * restrict we"
.Fa "int flags"
.Fc
.Fo wordfree
.Fa "wordexp_t *we"
.Fc
.Sh DESCRIPTION
The
.Fn wordexp
function performs shell-style word expansion on
.Fa words
and places the list of words into the
.Va we_wordv
member of
.Va we ,
and the number of words into
.Va we_wordc .
.Pp
The
.Va flags
argument is the bitwise inclusive OR of any of the following constants:
.Bl -tag -width ".Dv WRDE_SHOWERR"
.It Dv WRDE_APPEND
Append the words to those generate by a previous call to
.Fn wordexp .
.It Dv WRDE_DOOFS
As many
.Dv NULL
pointers as are specified by the
.Va we_offs
member of
.Va we
are added to the front of
.Va we_wordv .
.It Dv WRDE_NOCMD
Disallow command substitution in
.Fa words .
See the note in
.Sx BUGS
before using this.
.It Dv WRDE_REUSE
The
.Va we
argument was passed to a previous successful call to
.Fn wordexp
but has not been passed to
.Fn wordfree .
The implementation may reuse the space allocated to it.
.It Dv WRDE_SHOWERR
Do not redirect shell error messages to
.Pa /dev/null .
.It Dv WRDE_UNDEF
Report error on an attempt to expand an undefined shell variable.
.El
.Pp
The
.Vt wordexp_t
structure is defined in
.Pa wordexp.h
as:
.Bd -literal -offset indent
typedef struct {
size_t we_wordc; /* count of words matched */
char **we_wordv; /* pointer to list of words */
size_t we_offs; /* slots to reserve in we_wordv */
} wordexp_t;
.Ed
.Pp
The
.Fn wordfree
function frees the memory allocated by
.Fn wordexp .
.Sh IMPLEMENTATION NOTES
The
.Fn wordexp
function is implemented as a wrapper around the undocumented
.Ic wordexp
shell built-in command.
.Sh RETURN VALUES
The
.Fn wordexp
function returns zero if successful, otherwise it returns one of the following
error codes:
.Bl -tag -width ".Dv WRDE_NOSPACE"
.It Dv WRDE_BADCHAR
The
.Fa words
argument contains one of the following unquoted characters:
<newline>,
.Ql | ,
.Ql & ,
.Ql \&; ,
.Ql < ,
.Ql > ,
.Ql \&( ,
.Ql \&) ,
.Ql { ,
.Ql } .
.It Dv WRDE_BADVAL
An attempt was made to expand an undefined shell variable and
.Dv WRDE_UNDEF
is set in
.Fa flags .
.It Dv WRDE_CMDSUB
An attempt was made to use command substitution and
.Dv WRDE_NOCMD
is set in
.Fa flags .
.It Dv WRDE_NOSPACE
Not enough memory to store the result.
.It Dv WRDE_SYNTAX
Shell syntax error in
.Fa words .
.El
.Pp
The
.Fn wordfree
function returns no value.
.Sh ENVIRONMENT
.Bl -tag -width ".Ev IFS"
.It Ev IFS
Field separator.
.El
.Sh EXAMPLES
Invoke the editor on all
.Dq Li \&.c
files in the current directory,
and
.Pa /etc/motd
(error checking omitted):
.Bd -literal -offset indent
wordexp_t we;
wordexp("${EDITOR:-vi} *.c /etc/motd", &we, 0);
execvp(we->we_wordv[0], we->we_wordv);
.Ed
.Sh DIAGNOSTICS
Diagnostic messages from the shell are written to the standard error output
if
.Dv WRDE_SHOWERR
is set in
.Fa flags .
.Sh SEE ALSO
.Xr sh 1 ,
.Xr fnmatch 3 ,
.Xr glob 3 ,
.Xr popen 3 ,
.Xr system 3
.Sh STANDARDS
The
.Fn wordexp
and
.Fn wordfree
functions conform to
.St -p1003.1-2001 .
.Sh BUGS
Do not pass untrusted user data to
.Fn wordexp ,
regardless of whether the
.Dv WRDE_NOCMD
flag is set.
The
.Fn wordexp
function attempts to detect input that would cause commands to be
executed before passing it to the shell
but it does not use the same parser so it may be fooled.
|