WORDEXP(3)                 POSIX Programmer's Manual                WORDEXP(3)



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

NNAAMMEE
       wordexp, wordfree -- perform word expansions

SSYYNNOOPPSSIISS
       #include <wordexp.h>

       int wordexp(const char *restrict _w_o_r_d_s, wordexp_t *restrict _p_w_o_r_d_e_x_p,
           int _f_l_a_g_s);
       void wordfree(wordexp_t *_p_w_o_r_d_e_x_p);

DDEESSCCRRIIPPTTIIOONN
       The _w_o_r_d_e_x_p() function shall perform word expansions as described in
       the Shell and Utilities volume of POSIX.1-2017, _S_e_c_t_i_o_n _2_._6, _W_o_r_d
       _E_x_p_a_n_s_i_o_n_s, subject to quoting as described in the Shell and Utilities
       volume of POSIX.1-2017, _S_e_c_t_i_o_n _2_._2, _Q_u_o_t_i_n_g, and place the list of
       expanded words into the structure pointed to by _p_w_o_r_d_e_x_p.

       The _w_o_r_d_s 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 _w_o_r_d_s were the part of a
       command line representing the arguments to a utility. Therefore, the
       application shall ensure that _w_o_r_d_s 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, _S_e_c_t_i_o_n
       _2_._6_._3, _C_o_m_m_a_n_d _S_u_b_s_t_i_t_u_t_i_o_n.  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 _w_o_r_d_s
       which it expects to have expanded by _w_o_r_d_e_x_p() 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 _w_o_r_d_s.  If the argument _w_o_r_d_s
       contains an unquoted comment character (<number-sign>) that is the
       beginning of a token, _w_o_r_d_e_x_p() shall either treat the comment
       character as a regular character, or interpret it as a comment
       indicator and ignore the remainder of _w_o_r_d_s.

       The structure type wwoorrddeexxpp__tt is defined in the _<_w_o_r_d_e_x_p_._h_> header and
       includes at least the following members:

         +--------------+--------------+------------------------------------+
         |MMeemmbbeerr TTyyppee   | MMeemmbbeerr NNaammee  |            DDeessccrriippttiioonn             |
         +--------------+--------------+------------------------------------+
         |ssiizzee__tt        |_w_e___w_o_r_d_c      | Count of words matched by _w_o_r_d_s.   |
         |cchhaarr ****       |_w_e___w_o_r_d_v      | Pointer to list of expanded words. |
         |ssiizzee__tt        |_w_e___o_f_f_s       | Slots to reserve at the beginning  |
         |              |              | of _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_v.             |
         +--------------+--------------+------------------------------------+
       The _w_o_r_d_e_x_p() function shall store the number of generated words into
       _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_c and a pointer to a list of pointers to words in
       _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_v. Each individual field created during field
       splitting (see the Shell and Utilities volume of POSIX.1-2017, _S_e_c_t_i_o_n
       _2_._6_._5, _F_i_e_l_d _S_p_l_i_t_t_i_n_g) or pathname expansion (see the Shell and
       Utilities volume of POSIX.1-2017, _S_e_c_t_i_o_n _2_._6_._6, _P_a_t_h_n_a_m_e _E_x_p_a_n_s_i_o_n)
       shall be a separate word in the _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_v list. The words
       shall be in order as described in the Shell and Utilities volume of
       POSIX.1-2017, _S_e_c_t_i_o_n _2_._6, _W_o_r_d _E_x_p_a_n_s_i_o_n_s.  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, _S_e_c_t_i_o_n _2_._5_._2, _S_p_e_c_i_a_l _P_a_r_a_m_e_t_e_r_s is unspecified.

       It is the caller's responsibility to allocate the storage pointed to by
       _p_w_o_r_d_e_x_p.  The _w_o_r_d_e_x_p() function shall allocate other space as needed,
       including memory pointed to by _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_v. The _w_o_r_d_f_r_e_e()
       function frees any memory associated with _p_w_o_r_d_e_x_p from a previous call
       to _w_o_r_d_e_x_p().

       The _f_l_a_g_s argument is used to control the behavior of _w_o_r_d_e_x_p().  The
       value of _f_l_a_g_s is the bitwise-inclusive OR of zero or more of the
       following constants, which are defined in _<_w_o_r_d_e_x_p_._h_>:

       WRDE_APPEND   Append words generated to the ones from a previous call
                     to _w_o_r_d_e_x_p().

       WRDE_DOOFFS   Make use of _p_w_o_r_d_e_x_p->_w_e___o_f_f_s. If this flag is set,
                     _p_w_o_r_d_e_x_p->_w_e___o_f_f_s is used to specify how many null
                     pointers to add to the beginning of _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_v.
                     In other words, _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_v shall point to
                     _p_w_o_r_d_e_x_p->_w_e___o_f_f_s null pointers, followed by
                     _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_c 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, _S_e_c_t_i_o_n _2_._6_._3, _C_o_m_m_a_n_d
                     _S_u_b_s_t_i_t_u_t_i_o_n, is requested.

       WRDE_REUSE    The _p_w_o_r_d_e_x_p argument was passed to a previous successful
                     call to _w_o_r_d_e_x_p(), and has not been passed to _w_o_r_d_f_r_e_e().
                     The result shall be the same as if the application had
                     called _w_o_r_d_f_r_e_e() and then called _w_o_r_d_e_x_p() without
                     WRDE_REUSE.

       WRDE_SHOWERR  Do not redirect _s_t_d_e_r_r to //ddeevv//nnuullll.

       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 _w_o_r_d_e_x_p().  The following rules apply
       to applications when two or more calls to _w_o_r_d_e_x_p() are made with the
       same value of _p_w_o_r_d_e_x_p and without intervening calls to _w_o_r_d_f_r_e_e():

        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, _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_v shall
           point to a list containing the following:

            a. Zero or more null pointers, as specified by WRDE_DOOFFS and
               _p_w_o_r_d_e_x_p->_w_e___o_f_f_s

            b. Pointers to the words that were in the _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_v 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 _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_c 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
           _w_o_r_d_e_x_p(), but if it does it shall reset them to the original value
           before a subsequent call, using the same _p_w_o_r_d_e_x_p value, to
           _w_o_r_d_f_r_e_e() or _w_o_r_d_e_x_p() 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 _w_o_r_d_s contains an unquoted
       character--<newline>, ''||'', ''&&'', '';;'', ''<<'', ''>>'', ''(('', ''))'', ''{{'', ''}}''--in
       an inappropriate context, _w_o_r_d_e_x_p() shall fail, and the number of
       expanded words shall be 0.

       Unless WRDE_SHOWERR is set in _f_l_a_g_s, _w_o_r_d_e_x_p() shall redirect _s_t_d_e_r_r to
       //ddeevv//nnuullll for any utilities executed as a result of command
       substitution while expanding _w_o_r_d_s.  If WRDE_SHOWERR is set, _w_o_r_d_e_x_p()
       may write messages to _s_t_d_e_r_r if syntax errors are detected while
       expanding _w_o_r_d_s, unless the _s_t_d_e_r_r 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
       _s_t_d_e_r_r error indicator or the value of _e_r_r_n_o.

       The application shall ensure that if WRDE_DOOFFS is set, then
       _p_w_o_r_d_e_x_p->_w_e___o_f_f_s has the same value for each _w_o_r_d_e_x_p() call and
       _w_o_r_d_f_r_e_e() call using a given _p_w_o_r_d_e_x_p.

       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 _w_o_r_d_s in an
                     inappropriate context.

       WRDE_BADVAL   Reference to undefined shell variable when WRDE_UNDEF is
                     set in _f_l_a_g_s.

       WRDE_CMDSUB   Command substitution requested when WRDE_NOCMD was set in
                     _f_l_a_g_s.

       WRDE_NOSPACE  Attempt to allocate memory failed.

       WRDE_SYNTAX   Shell syntax error, such as unbalanced parentheses or
                     unterminated string.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, _w_o_r_d_e_x_p() shall return 0. Otherwise, a non-
       zero value, as described in _<_w_o_r_d_e_x_p_._h_>, shall be returned to indicate
       an error. If _w_o_r_d_e_x_p() returns the value WRDE_NOSPACE, then
       _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_c and _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_v shall be updated to reflect
       any words that were successfully expanded. In other error cases, if the
       WRDE_APPEND flag was specified, _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_c and
       _p_w_o_r_d_e_x_p->_w_e___w_o_r_d_v shall not be modified.

       The _w_o_r_d_f_r_e_e() function shall not return a value.

EERRRROORRSS
       No errors are defined.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
       None.

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The _w_o_r_d_e_x_p() 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 _w_o_r_d_e_x_p() 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 _w_o_r_d_e_x_p() function to be thread-safe
       if passed an expression referencing an environment variable while any
       other thread is concurrently modifying any environment variable; see
       _e_x_e_c.

       Even though the WRDE_SHOWERR flag allows the implementation to write
       messages to _s_t_d_e_r_r 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 _s_t_d_e_r_r
       should ensure that any calls to _w_o_r_d_e_x_p() do not write to _s_t_d_e_r_r, by
       avoiding use of the WRDE_SHOWERR flag.

RRAATTIIOONNAALLEE
       This function was included as an alternative to _g_l_o_b().  There had been
       continuing controversy over exactly what features should be included in
       _g_l_o_b().  It is hoped that by providing _w_o_r_d_e_x_p() (which provides all of
       the shell word expansions, but which may be slow to execute) and _g_l_o_b()
       (which is faster, but which only performs pathname expansion, without
       tilde or parameter expansion) this will satisfy the majority of
       applications.

       While _w_o_r_d_e_x_p() 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 _e_c_h_o utility by adding a --qq option. This
       option would cause _e_c_h_o to add a <backslash> before each <backslash>
       and <blank> that occurs within an argument. The _w_o_r_d_e_x_p() function
       could then invoke the shell as follows:


           (void) strcpy(buffer, "echo -q");
           (void) strcat(buffer, _w_o_r_d_s);
           if ((flags & WRDE_SHOWERR) == 0)
               (void) strcat(buffer, "2>/dev/null");
           f = popen(buffer, "r");

       The _w_o_r_d_e_x_p() 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, _w_o_r_d_e_x_p() would have to
       scan _w_o_r_d_s before starting the subshell to make sure that there would
       be no command substitution. In any case, it would have to scan _w_o_r_d_s
       for unquoted special characters.

       Another proposal is to add the following options to _s_h:

       --ww _w_o_r_d_l_i_s_t
             This option provides a wordlist expansion service to
             applications. The words in _w_o_r_d_l_i_s_t 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, _s_h exits with a
             non-zero status after writing the former to report any words
             successfully expanded

       --PP    Run in ``protected'' mode. If specified with the --ww option, no
             command substitution shall be performed.

       With these options, _w_o_r_d_e_x_p() could be implemented fairly simply by
       creating a subprocess using _f_o_r_k() and executing _s_h using the line:


           execl(<_s_h_e_l_l _p_a_t_h>, "sh", "-P", "-w", _w_o_r_d_s, (char *)0);

       after directing standard error to //ddeevv//nnuullll.

       It seemed objectionable for a library routine to write messages to
       standard error, unless explicitly requested, so _w_o_r_d_e_x_p() is required
       to redirect standard error to //ddeevv//nnuullll 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 _w_o_r_d_f_r_e_e() when WRDE_REUSE is set.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _e_x_e_c, _f_n_m_a_t_c_h(), _g_l_o_b()

       The Base Definitions volume of POSIX.1-2017, <<wwoorrddeexxpp..hh>>

       The Shell and Utilities volume of POSIX.1-2017, _C_h_a_p_t_e_r _2, _S_h_e_l_l
       _C_o_m_m_a_n_d _L_a_n_g_u_a_g_e

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2017                           WORDEXP(3)
