Standard C Library Functions wordexp(3C)
NAME
wordexp, wordfree - perform word expansions
SYNOPSIS
#include
int wordexp(const char *restrict words, wordexpt *restrict pwordexp,
int flags);
void wordfree(wordexpt *pwordexp);
DESCRIPTION
The wordexp() function performs word expansions, subject to
quoting, and places the list of expanded words into the
structure pointed to by pwordexp.
The wordfree() function frees any memory allocated by wor-
dexp() associated with pwordexp.
words Argument
The words argument is a pointer to a string containing one
or more words to be expanded. The expansions will be the
same as would be performed by the shell if words were the
part of a command line representing the arguments to a util-
ity. Therefore, words must not contain an unquoted NEWLINE
or any of the unquoted shell special characters:
& ; < >
except in the context of command substitution. It also must
not contain unquoted parentheses or braces, except in the
context of command or variable substitution. If the argument
words contains an unquoted comment character (number sign)
that is the beginning of a token, wordexp() may treat the
comment character as a regular character, or may interpret
it as a comment indicator and ignore the remainder of words.
pwordexp Argument
The structure type wordexpt is defined in the header
and includes at least the following members:
sizet wewordc Count of words matched by words.
char **wewordv Pointer to list of expanded words.
SunOS 5.11 Last change: 1 Nov 2003 1
Standard C Library Functions wordexp(3C)
sizet weoffs Slots to reserve at the beginning of
pwordexp->wewordv.
The wordexp() function stores the number of generated words
into pwordexp->wewordc and a pointer to a list of pointers
to words in pwordexp->wewordv. Each individual field
created during field splitting is a separate word in the
pwordexp->wewordv list. The words are in order. The first
pointer after the last word pointer will be a null pointer.
It is the caller's responsibility to allocate the storage
pointed to by pwordexp. The wordexp() function allocates
other space as needed, including memory pointed to by
pwordexp->wewordv. The wordfree() function frees any memory
associated with pwordexp from a previous call to wordexp().
flags Argument
The flags argument is used to control the behavior of wor-
dexp(). The value of flags is the bitwise inclusive OR of
zero or more of the following constants, which are defined
in :
WRDEAPEND Append words generated to the ones from a
previous call to wordexp().
WRDEDOFS Make use of pwordexp->weoffs. If this flag
is set, pwordexp->weoffs is used to specify
how many NUL pointers to add to the begin-
ning of pwordexp->wewordv. In other words,
pwordexp->wewordv will point to
pwordexp->weoffs NUL pointers, followed by
pwordexp->wewordc word pointers, followed
by a NUL pointer.
WRDENOCMD Fail if command substitution is requested.
WRDEREUSE The pwordexp argument was passed to a previ-
ous successful call to wordexp(), and has
not been passed to wordfree(). The result
will be the same as if the application had
called wordfree() and then called wordexp()
without WRDEREUSE.
WRDESHOWER Do not redirect stderr to /dev/null.
SunOS 5.11 Last change: 1 Nov 2003 2
Standard C Library Functions wordexp(3C)
WRDEUNDEF Report error on an attempt to expand an
undefined shell variable.
The WRDEAPEND flag can be used to append a new set of
words to those generated by a previous call to wordexp().
The following rules apply when two or more calls to wor-
dexp() are made with the same value of pwordexp and without
intervening calls to wordfree():
1. The first such call must not set WRDEAPEND. All
subsequent calls must set it.
2. All of the calls must set WRDEDOFS, or all must
not set it.
3. After the second and each subsequent call,
pwordexp->wewordv will point to a list containing
the following:
a. zero or more NUL pointers, as specified by
WRDEDOFS and pwordexp->weoffs.
b. pointers to the words that were in the
pwordexp->wewordv list before the call, in the
same order as before.
c. pointers to the new words generated by the
latest call, in the specified order.
4. The count returned in pwordexp->wewordc will be
the total number of words from all of the calls.
5. The application can change any of the fields after
a call to wordexp(), but if it does it must reset
them to the original value before a subsequent
call, using the same pwordexp value, to wordfree()
or wordexp() with the WRDEAPEND or WRDEREUSE
flag.
If words contains an unquoted:
NEWLINE & ; < > ( ) { }
in an inappropriate context, wordexp() will fail, and the
number of expanded words will be zero.
SunOS 5.11 Last change: 1 Nov 2003 3
Standard C Library Functions wordexp(3C)
Unless WRDESHOWER is set in flags, wordexp() will redirect
stderr to /dev/null for any utilities executed as a result
of command substitution while expanding words.
If WRDESHOWER is set, wordexp() may write messages to
stderr if syntax errors are detected while expanding words.
If WRDEDOFS is set, then pwordexp-> weoffs must have the
same value for each wordexp() call and wordfree() call using
a given pwordexp.
The following constants are defined as error return values:
WRDEBADCHAR One of the unquoted characters:
NEWLINE & ; < > ( ) { }
appears in words in an inappropriate con-
text.
WRDEBADVAL Reference to undefined shell variable when
WRDEUNDEF is set in flags.
WRDECMDSUB Command substitution requested when
WRDENOCMD was set in flags.
WRDENOSPACE Attempt to allocate memory failed.
WRDESYNTAX Shell syntax error, such as unbalanced
parentheses or unterminated string.
RETURN VALUES
On successful completion, wordexp() returns 0.
Otherwise, a non-zero value as described in is
returned to indicate an error. If wordexp() returns the
value WRDENOSPACE, then pwordexp->wewordc and
pwordexp->wewordv will be updated to reflect any words that
were successfully expanded. In other cases, they will not be
modified.
The wordfree() function returns no value.
SunOS 5.11 Last change: 1 Nov 2003 4
Standard C Library Functions wordexp(3C)
ERORS
No errors are defined.
USAGE
This function is intended to be used by an application that
wants to do all of the shell's expansions on a word or words
obtained from a user. For example, if the application
prompts for a filename (or list of filenames) and then uses
wordexp() to process the input, the user could respond with
anything that would be valid as input to the shell.
The WRDENOCMD flag is provided for applications that, for
security or other reasons, want to prevent a user from exe-
cuting shell command. Disallowing unquoted shell special
characters also prevents unwanted side effects such as exe-
cuting a command or writing a file.
ATRIBUTES
See attributes(5) for descriptions of the following attri-
butes:
ATRIBUTE TYPE ATRIBUTE VALUE
Interface Stability Standard
MT-Level MT-Safe
SEE ALSO
fnmatch(3C), glob(3C), attributes(5), standards(5)
SunOS 5.11 Last change: 1 Nov 2003 5
|