wordexp() — Perform shell word expansions
Standards
Standards / Extensions | C or C++ | Dependencies |
---|---|---|
XPG4 |
both | POSIX(ON) |
Format
#define _XOPEN_SOURCE
#include <wordexp.h>
int wordexp(const char *__restrict__ words,
wordexp_t *__restrict__ pwordexp, int flags);
void wordfree(wordexp_t *pwordexp);
General description
The wordexp() function performs word expansions as described in X/Open CAE Specification, Commands and Utilities, Issue 4, Version 2 (XCU) Topic, Word Expansions, subject to quoting as in the XCU specification, topic, Quoting, and places the list of expanded words into the structure pointed to by pwordexp.
| & ; < >
except
in the context of command substitution as specified in the XCU specification, topic,
Command Substitution. It also must not contain unquoted parentheses
or braces, except in the context of command or variable substitution. Member Type Member Name Description
size_t we_wordc Count of words matched by words.
char ** we_wordv Pointer to list of expanded words.
size_t we_offs Slots to reserve at the beginning
of pwordexp->we_wordv.
The
wordexp() function stores the number of generated words into pwordexp->we_wordc
and a pointer to a list of pointers to words in pwordexp->we_wordv.
Each individual field created during field splitting (see the XCU specification, topic,
Field Splitting) or pathname expansion (see the XCU specification, topic ,
Pathname Expansion) is a separate word in the pwordexp->we_wordv
list. The words are in order as described in the XCU specification, topic,
Word Expansions. The first pointer after the last word pointer
will be a NULL pointer. The expansion of special parameters described
in the XCU specification, topic, Special Parameters is
unspecified.It is the caller's responsibility to allocate the storage pointed to by pwordexp. The wordexp() function allocates the other space as needed, including memory pointed to by pwordexp->we_wordv. The wordfree() function frees any memory associated with pwordexp from a previous call to wordexp().
- WRDE_APPEND
- Append words generated to the ones from a previous call to wordexp().
- WRDE_DOOFFS
- Make use of pwordexp->we_offs. If this flag is set, pwordexp->we_offs is used to specify how many NULL pointers to add to the beginning of pwordexp->we_wordv. In other words, pwordexp->we_wordv will point to pwordexp->we_offs NULL pointers followed by pwordexp->we_wordc word pointers, followed by a NULL pointer.
- WRDE_NOCMD
- Fail if command substitution, as specified in the XCU specification, topic, Command Substitution, is requested.
- WRDE_REUSE
- The pwordexp argument was passed to a previous 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 WRDE_REUSE.
- WRDE_SHOWERR
- Do not redirect stderr to /dev/null.
- WRDE_UNDEF
- Report error on an attempt to expand an undefined shell variable.
- The first such call must not set WRDE_APPEND. All subsequent calls must set it.
- All of the calls must set WRDE_DOOFFS, or all must not set it.
- After the second and each subsequent call, pwordexp->we_wordv
will point to a list containing the following:
- zero or more NULL pointers, as specified by WRDE_DOOFFS and pwordexp->we_offs
- pointers to the words that were in the pwordexp->we_wordv list before the call, in the same order as before
- pointers to the new words generated by the latest call, in the specified order
- The count returned in pwordexp->we_wordc will be the total number of words from all of the calls.
- 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 wordexp value, to wordfree() or wordexp() with the WRDE_APPEND or WRDE_REUSE flag.
<newline> | & ; < > ( ) { }
in
an inappropriate context, wordexp() will fail, and the number of expanded
words will be 0.Unless WRDE_SHOWERR 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 WRDE_SHOWERR is set, wordexp() may write messages to stderr if syntax errors are detected while expanding words.
If WRDE_DOOFFS is set, then pwordexp->we_offs must have the same value for each wordexp() call and wordfree() call using a given pwordexp.
- WRDE_BADCHAR
- One of the unquoted characters:
appears in words in an inappropriate context.<newline> | & ; < > ( ) { }
- WRDE_BADVAL
- Reference to undefined shell variable when WRDE_UNDEF is set in flags.
- WRDE_CMDSUB
- Command substitution requested when WRDE_NOCMD is set in flags.
- WRDE_NOSPACE
- Attempt to allocate memory failed.
- WRDE_SYNTAX
- Shell syntax error, such as unbalanced parentheses or unterminated string.
- WRDE_NOSYS
- POSIX shell not available.
- WRDE_EPOPEN
- wordexp() was invoked in an environment that does not supported a multi-threaded fork, or wordexp() was invoked from a multithreaded process in TSO or MVS™ batch.
- WRDE_INTRUPT
- wordexp() was interrupted by a signal, such as an alarm. In this event, the caller may re-issue wordexp().
For further information about the cause of a failure, please refer to __errno2() and errno in addition to the above error return values.
Returned value
If successful, wordexp() returns 0.
If unsuccessful, wordexp() returns a nonzero value, as defined in <wordexp.h> and described above, to indicate an error. If wordexp() returns the value WRDE_NOSPACE, then pwordexp->we_wordc, and pwordexp->we_wordv will be updated to reflect any words that were successfully expanded. In other cases, they will not be modified.