#include <wordexp.h>
int wordexp(const char *words, wordexp_t *pwordexp, int flags);
void wordfree(wordexp_t *pwordexp);
The wordfree() function frees any memory allocated by wordexp() associated with pwordexp.
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 utility. 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.
The structure type wordexp_t is defined in the header <wordexp.h> and includes at least the following members:
- 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 is a separate word in the pwordexp->we_wordv 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->we_wordv. The wordfree() function frees any memory associated with pwordexp from a previous call to wordexp().
The flags argument is used to control the behavior of wordexp(). The value of flags is the bitwise inclusive OR of zero or more of the following constants, which are defined in <wordexp.h>:
The WRDE_APPEND 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 wordexp() are made with the same value of pwordexp and without intervening calls to wordfree():
- a.
- zero or more NULL pointers, as specified by WRDE_DOOFFS and pwordexp->we_offs.
- b.
- pointers to the words that were in the pwordexp->we_wordv 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.
If words contains an unquoted:
NEWLINE | & ; < > ( ) { }
in an inappropriate context, wordexp() will fail, and the number of expanded words will be zero.
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.
The following constants are defined as error return values:
NEWLINE | & ; < > ( ) { }appears in words in an inappropriate context.
The wordfree() function returns no value.
The WRDE_NOCMD flag is provided for applications that, for security or other reasons, want to prevent a user from executing shell commands. Disallowing unquoted shell special characters also prevents unwanted side effects such as executing a command or writing a file.