# wordexp

## PROLOG



           This manual page is part of the POSIX Programmer's Manual.  The
           Linux implementation of this interface may differ (consult the
           corresponding Linux manual page for details of Linux behavior), or
           the interface may not be implemented on Linux.
    


## NAME



           wordexp, wordfree — perform word expansions
    


## SYNOPSIS



           #include <wordexp.h>
    
           int wordexp(const char *restrict _words_ , wordexp_t *restrict _pwordexp_ ,
               int _flags_);
           void wordfree(wordexp_t *_pwordexp_);
    


## DESCRIPTION



           The _wordexp_() function shall perform word expansions as described
           in the Shell and Utilities volume of POSIX.1‐2017, _Section 2.6_ ,
           _Word Expansions_ , subject to quoting as described in the Shell and
           Utilities volume of POSIX.1‐2017, _Section 2.2_ , _Quoting_ , and place
           the list of expanded words into the structure pointed to by
           _pwordexp_.
    
           The _words_ argument is a pointer to a string containing one or more
           words to be expanded. The expansions shall be the same as would be
           performed by the command line interpreter if _words_ were the part
           of a command line representing the arguments to a utility.
           Therefore, the application shall ensure that _words_ does not
           contain an unquoted <newline> character or any of the unquoted
           shell special characters **'|'** , **' &'**, **';'** , **' <'**, **' >' **except in the
           context of command substitution as specified in the Shell and
           Utilities volume of POSIX.1‐2017, _Section 2.6.3_ , _Command_
           _Substitution_.  It also shall not contain unquoted parentheses or
           braces, except in the context of command or variable substitution.
           The application shall ensure that every member of _words_ which it
           expects to have expanded by _wordexp_() does not contain an unquoted
           initial comment character. The application shall also ensure that
           any words which it intends to be ignored (because they begin or
           continue a comment) are deleted from _words_.  If the argument _words_
           contains an unquoted comment character (<number-sign>) that is the
           beginning of a token, _wordexp_() shall either treat the comment
           character as a regular character, or interpret it as a comment
           indicator and ignore the remainder of _words_.
    
           The structure type **wordexp_t** is defined in the _< wordexp.h>_ header
           and includes at least the following members:
         ┌───────────────┬──────────────┬────────────────────────────────────┐
         │ **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 shall store 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 Shell and Utilities volume of
           POSIX.1‐2017, _Section 2.6.5_ , _Field Splitting_) or pathname
           expansion (see the Shell and Utilities volume of POSIX.1‐2017,
           _Section 2.6.6_ , _Pathname Expansion_) shall be a separate word in the
           _pwordexp_ ->_we_wordv_ list. The words shall be in order as described
           in the Shell and Utilities volume of POSIX.1‐2017, _Section 2.6_ ,
           _Word Expansions_.  The first pointer after the last word pointer
           shall be a null pointer.  The expansion of special parameters
           described in the Shell and Utilities volume of POSIX.1‐2017,
           _Section 2.5.2_ , _Special Parameters_ is unspecified.
    
           It is the caller's responsibility to allocate the storage pointed
           to by _pwordexp_.  The _wordexp_() function shall allocate 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>_:
    
           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_ shall point to _pwordexp_ ->_we_offs_
                         null pointers, followed by _pwordexp_ ->_we_wordc_ word
                         pointers, followed by a null pointer.
    
           WRDE_NOCMD    If the implementation supports the utilities defined
                         in the Shell and Utilities volume of POSIX.1‐2017,
                         fail if command substitution, as specified in the
                         Shell and Utilities volume of POSIX.1‐2017, _Section_
                         _2.6.3_ , _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 shall 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 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 to applications when two or more calls to _wordexp_()
           are made with the same value of _pwordexp_ and without intervening
           calls to _wordfree_():
    
            1. The first such call shall not set WRDE_APPEND. All subsequent
               calls shall set it.
    
            2. All of the calls shall set WRDE_DOOFFS, or all shall not set
               it.
    
            3. After the second and each subsequent call, _pwordexp_ ->_we_wordv_
               shall point to a list containing the following:
    
                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
    
            4. The count returned in _pwordexp_ ->_we_wordc_ shall 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 shall reset them to the original
               value before a subsequent call, using the same _pwordexp_ value,
               to _wordfree_() or _wordexp_() with the WRDE_APPEND or WRDE_REUSE
               flag.
    
           If the implementation supports the utilities defined in the Shell
           and Utilities volume of POSIX.1‐2017, and _words_ contains an
           unquoted character—<newline>, **'|'** , **' &'**, **';'** , **' <'**, **' >'**, **'('** , **')'** ,
           **'{'** , **'}'** —in an inappropriate context, _wordexp_() shall fail, and
           the number of expanded words shall be 0.
    
           Unless WRDE_SHOWERR is set in _flags_ , _wordexp_() shall 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_ , unless the _stderr_ stream has wide
           orientation in which case the behavior is undefined.  It is
           unspecified whether any write errors encountered while outputting
           such messages will affect the _stderr_ error indicator or the value
           of _errno_.
    
           The application shall ensure that if WRDE_DOOFFS is set, then
           _pwordexp_ ->_we_offs_ has the same value for each _wordexp_() call and
           _wordfree_() call using a given _pwordexp_.
    
           The results are unspecified if WRDE_APPEND and WRDE_REUSE are both
           specified.
    
           The following constants are defined as error return values:
    
           WRDE_BADCHAR  One of the unquoted characters—<newline>, **'|'** , **' &'**,
                         **';'** , **' <'**, **' >'**, **'('** , **')'** , **'{'** , **'}'** —appears in _words_
                         in an inappropriate context.
    
           WRDE_BADVAL   Reference to undefined shell variable when
                         WRDE_UNDEF is set in _flags_.
    
           WRDE_CMDSUB   Command substitution requested when WRDE_NOCMD was
                         set in _flags_.
    
           WRDE_NOSPACE  Attempt to allocate memory failed.
    
           WRDE_SYNTAX   Shell syntax error, such as unbalanced parentheses
                         or unterminated string.
    


## RETURN VALUE



           Upon successful completion, _wordexp_() shall return 0. Otherwise, a
           non-zero value, as described in _< wordexp.h>_, shall be returned to
           indicate an error. If _wordexp_() returns the value WRDE_NOSPACE,
           then _pwordexp_ ->_we_wordc_ and _pwordexp_ ->_we_wordv_ shall be updated to
           reflect any words that were successfully expanded. In other error
           cases, if the WRDE_APPEND flag was specified, _pwordexp_ ->_we_wordc_
           and _pwordexp_ ->_we_wordv_ shall not be modified.
    
           The _wordfree_() function shall not return a value.
    


## ERRORS



           No errors are defined.
    
           _The following sections are informative._
    


## EXAMPLES



           None.
    


## APPLICATION USAGE



           The _wordexp_() 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 pathname (or list of pathnames) and then uses _wordexp_() to
           process the input, the user could respond with anything that would
           be valid as input to the shell.
    
           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.
    
           POSIX.1‐2008 does not require the _wordexp_() function to be thread-
           safe if passed an expression referencing an environment variable
           while any other thread is concurrently modifying any environment
           variable; see exec(1p).
    
           Even though the WRDE_SHOWERR flag allows the implementation to
           write messages to _stderr_ during command substitution or syntax
           errors, this standard does not provide any way to detect write
           failures during the output of such messages.
    
           Applications which use wide-character output functions with _stderr_
           should ensure that any calls to _wordexp_() do not write to _stderr_ ,
           by avoiding use of the WRDE_SHOWERR flag.
    


## RATIONALE



           This function was included as an alternative to _glob_().  There had
           been continuing controversy over exactly what features should be
           included in _glob_().  It is hoped that by providing _wordexp_()
           (which provides all of the shell word expansions, but which may be
           slow to execute) and _glob_() (which is faster, but which only
           performs pathname expansion, without tilde or parameter expansion)
           this will satisfy the majority of applications.
    
           While _wordexp_() could be implemented entirely as a library
           routine, it is expected that most implementations run a shell in a
           subprocess to do the expansion.
    
           Two different approaches have been proposed for how the required
           information might be presented to the shell and the results
           returned.  They are presented here as examples.
    
           One proposal is to extend the _echo_ utility by adding a **-q** option.
           This option would cause _echo_ to add a <backslash> before each
           <backslash> and <blank> that occurs within an argument. The
           _wordexp_() function could then invoke the shell as follows:
    
               (void) strcpy(buffer, "echo -q");
               (void) strcat(buffer, _words_);
               if ((flags & WRDE_SHOWERR) == 0)
                   (void) strcat(buffer, "2>/dev/null");
               f = popen(buffer, "r");
    
           The _wordexp_() function would read the resulting output, remove
           unquoted <backslash> characters, and break into words at unquoted
           <blank> characters. If the WRDE_NOCMD flag was set, _wordexp_()
           would have to scan _words_ before starting the subshell to make sure
           that there would be no command substitution. In any case, it would
           have to scan _words_ for unquoted special characters.
    
           Another proposal is to add the following options to _sh_ :
    
           **-w** _wordlist_
                 This option provides a wordlist expansion service to
                 applications. The words in _wordlist_ shall be expanded and
                 the following written to standard output:
    
                  1. The count of the number of words after expansion, in
                     decimal, followed by a null byte
    
                  2. The number of bytes needed to represent the expanded
                     words (not including null separators), in decimal,
                     followed by a null byte
    
                  3. The expanded words, each terminated by a null byte
    
                 If an error is encountered during word expansion, _sh_ exits
                 with a non-zero status after writing the former to report
                 any words successfully expanded
    
           **-P** Run in ``protected'' mode. If specified with the **-w** option,
                 no command substitution shall be performed.
    
           With these options, _wordexp_() could be implemented fairly simply
           by creating a subprocess using _fork_() and executing _sh_ using the
           line:
    
               execl(<_shell path_ >, "sh", "-P", "-w", _words_ , (char *)0);
    
           after directing standard error to **/dev/null**.
    
           It seemed objectionable for a library routine to write messages to
           standard error, unless explicitly requested, so _wordexp_() is
           required to redirect standard error to **/dev/null** to ensure that no
           messages are generated, even for commands executed for command
           substitution. The WRDE_SHOWERR flag can be specified to request
           that error messages be written.
    
           The WRDE_REUSE flag allows the implementation to avoid the expense
           of freeing and reallocating memory, if that is possible. A minimal
           implementation can call _wordfree_() when WRDE_REUSE is set.
    


## FUTURE DIRECTIONS



           None.
    


* * *