GLOB(3)                    POSIX Programmer's Manual                   GLOB(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
       glob, globfree -- generate pathnames matching a pattern

SSYYNNOOPPSSIISS
       #include <glob.h>

       int glob(const char *restrict _p_a_t_t_e_r_n, int _f_l_a_g_s,
           int(*_e_r_r_f_u_n_c)(const char *_e_p_a_t_h, int _e_e_r_r_n_o),
           glob_t *restrict _p_g_l_o_b);
       void globfree(glob_t *_p_g_l_o_b);

DDEESSCCRRIIPPTTIIOONN
       The _g_l_o_b() function is a pathname generator that shall implement the
       rules defined in the Shell and Utilities volume of POSIX.1-2008,
       _S_e_c_t_i_o_n _2_._1_3, _P_a_t_t_e_r_n _M_a_t_c_h_i_n_g _N_o_t_a_t_i_o_n, with optional support for rule
       3 in the Shell and Utilities volume of POSIX.1-2008, _S_e_c_t_i_o_n _2_._1_3_._3,
       _P_a_t_t_e_r_n_s _U_s_e_d _f_o_r _F_i_l_e_n_a_m_e _E_x_p_a_n_s_i_o_n.

       The structure type gglloobb__tt is defined in _<_g_l_o_b_._h_> and includes at least
       the following members:

       +--------------+--------------+-----------------------------------------+
       |MMeemmbbeerr TTyyppee   | MMeemmbbeerr NNaammee  |               DDeessccrriippttiioonn               |
       +--------------+--------------+-----------------------------------------+
       |ssiizzee__tt        |_g_l___p_a_t_h_c      | Count of paths matched by _p_a_t_t_e_r_n.      |
       |cchhaarr ****       |_g_l___p_a_t_h_v      | Pointer to a list of matched pathnames. |
       |ssiizzee__tt        |_g_l___o_f_f_s       | Slots to reserve at the beginning of    |
       |              |              | _g_l___p_a_t_h_v.                               |
       +--------------+--------------+-----------------------------------------+
       The argument _p_a_t_t_e_r_n is a pointer to a pathname pattern to be expanded.
       The _g_l_o_b() function shall match all accessible pathnames against this
       pattern and develop a list of all pathnames that match. In order to
       have access to a pathname, _g_l_o_b() requires search permission on every
       component of a path except the last, and read permission on each
       directory of any filename component of _p_a_t_t_e_r_n that contains any of the
       following special characters: ''**'', ''??'', and ''[[''.

       The _g_l_o_b() function shall store the number of matched pathnames into
       _p_g_l_o_b->_g_l___p_a_t_h_c and a pointer to a list of pointers to pathnames into
       _p_g_l_o_b->_g_l___p_a_t_h_v. The pathnames shall be in sort order as defined by the
       current setting of the _L_C___C_O_L_L_A_T_E category; see the Base Definitions
       volume of POSIX.1-2008, _S_e_c_t_i_o_n _7_._3_._2, _L_C___C_O_L_L_A_T_E.  The first pointer
       after the last pathname shall be a null pointer. If the pattern does
       not match any pathnames, the returned number of matched paths is set to
       0, and the contents of _p_g_l_o_b->_g_l___p_a_t_h_v are implementation-defined.

       It is the caller's responsibility to create the structure pointed to by
       _p_g_l_o_b.  The _g_l_o_b() function shall allocate other space as needed,
       including the memory pointed to by _g_l___p_a_t_h_v.  The _g_l_o_b_f_r_e_e() function
       shall free any space associated with _p_g_l_o_b from a previous call to
       _g_l_o_b().

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

       GLOB_APPEND   Append pathnames generated to the ones from a previous
                     call to _g_l_o_b().

       GLOB_DOOFFS   Make use of _p_g_l_o_b->_g_l___o_f_f_s. If this flag is set,
                     _p_g_l_o_b->_g_l___o_f_f_s is used to specify how many null pointers
                     to add to the beginning of _p_g_l_o_b->_g_l___p_a_t_h_v. In other
                     words, _p_g_l_o_b->_g_l___p_a_t_h_v shall point to _p_g_l_o_b->_g_l___o_f_f_s null
                     pointers, followed by _p_g_l_o_b->_g_l___p_a_t_h_c pathname pointers,
                     followed by a null pointer.

       GLOB_ERR      Cause _g_l_o_b() to return when it encounters a directory
                     that it cannot open or read.  Ordinarily, _g_l_o_b()
                     continues to find matches.

       GLOB_MARK     Each pathname that is a directory that matches _p_a_t_t_e_r_n
                     shall have a <slash> appended.

       GLOB_NOCHECK  Supports rule 3 in the Shell and Utilities volume of
                     POSIX.1-2008, _S_e_c_t_i_o_n _2_._1_3_._3, _P_a_t_t_e_r_n_s _U_s_e_d _f_o_r _F_i_l_e_n_a_m_e
                     _E_x_p_a_n_s_i_o_n.  If _p_a_t_t_e_r_n does not match any pathname, then
                     _g_l_o_b() shall return a list consisting of only _p_a_t_t_e_r_n,
                     and the number of matched pathnames is 1.

       GLOB_NOESCAPE Disable backslash escaping.

       GLOB_NOSORT   Ordinarily, _g_l_o_b() sorts the matching pathnames according
                     to the current setting of the _L_C___C_O_L_L_A_T_E category; see
                     the Base Definitions volume of POSIX.1-2008, _S_e_c_t_i_o_n
                     _7_._3_._2, _L_C___C_O_L_L_A_T_E.  When this flag is used, the order of
                     pathnames returned is unspecified.

       The GLOB_APPEND flag can be used to append a new set of pathnames to
       those found in a previous call to _g_l_o_b().  The following rules apply to
       applications when two or more calls to _g_l_o_b() are made with the same
       value of _p_g_l_o_b and without intervening calls to _g_l_o_b_f_r_e_e():

        1. The first such call shall not set GLOB_APPEND. All subsequent calls
           shall set it.

        2. All the calls shall set GLOB_DOOFFS, or all shall not set it.

        3. After the second call, _p_g_l_o_b->_g_l___p_a_t_h_v points to a list containing
           the following:

            a. Zero or more null pointers, as specified by GLOB_DOOFFS and
               _p_g_l_o_b->_g_l___o_f_f_s.

            b. Pointers to the pathnames that were in the _p_g_l_o_b->_g_l___p_a_t_h_v list
               before the call, in the same order as before.

            c. Pointers to the new pathnames generated by the second call, in
               the specified order.

        4. The count returned in _p_g_l_o_b->_g_l___p_a_t_h_c shall be the total number of
           pathnames from the two calls.

        5. The application can change any of the fields after a call to
           _g_l_o_b().  If it does, the application shall reset them to the
           original value before a subsequent call, using the same _p_g_l_o_b
           value, to _g_l_o_b_f_r_e_e() or _g_l_o_b() with the GLOB_APPEND flag.

       If, during the search, a directory is encountered that cannot be opened
       or read and _e_r_r_f_u_n_c is not a null pointer, _g_l_o_b() calls _(()*errfunc )
       with two arguments:

        1. The _e_p_a_t_h argument is a pointer to the path that failed.

        2. The _e_e_r_r_n_o argument is the value of _e_r_r_n_o from the failure, as set
           by _o_p_e_n_d_i_r(), _r_e_a_d_d_i_r(), or _s_t_a_t().  (Other values may be used to
           report other errors not explicitly documented for those functions.)

       If _(()*errfunc ) is called and returns non-zero, or if the GLOB_ERR
       flag is set in _f_l_a_g_s, _g_l_o_b() shall stop the scan and return
       GLOB_ABORTED after setting _g_l___p_a_t_h_c and _g_l___p_a_t_h_v in _p_g_l_o_b to reflect
       the paths already scanned. If GLOB_ERR is not set and either _e_r_r_f_u_n_c is
       a null pointer or _(()*errfunc ) returns 0, the error shall be ignored.

       The _g_l_o_b() function shall not fail because of large files.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, _g_l_o_b() shall return 0. The argument
       _p_g_l_o_b->_g_l___p_a_t_h_c shall return the number of matched pathnames and the
       argument _p_g_l_o_b->_g_l___p_a_t_h_v shall contain a pointer to a null-terminated
       list of matched and sorted pathnames. However, if _p_g_l_o_b->_g_l___p_a_t_h_c is 0,
       the content of _p_g_l_o_b->_g_l___p_a_t_h_v is undefined.

       The _g_l_o_b_f_r_e_e() function shall not return a value.

       If _g_l_o_b() terminates due to an error, it shall return one of the non-
       zero constants defined in _<_g_l_o_b_._h_>.  The arguments _p_g_l_o_b->_g_l___p_a_t_h_c and
       _p_g_l_o_b->_g_l___p_a_t_h_v are still set as defined above.

EERRRROORRSS
       The _g_l_o_b() function shall fail and return the corresponding value if:

       GLOB_ABORTED  The scan was stopped because GLOB_ERR was set or
                     _(()*errfunc ) returned non-zero.

       GLOB_NOMATCH  The pattern does not match any existing pathname, and
                     GLOB_NOCHECK was not set in flags.

       GLOB_NOSPACE  An attempt to allocate memory failed.

       _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
       One use of the GLOB_DOOFFS flag is by applications that build an
       argument list for use with _e_x_e_c_v(), _e_x_e_c_v_e(), or _e_x_e_c_v_p().  Suppose,
       for example, that an application wants to do the equivalent of:

           llss --ll **..cc

       but for some reason:

           ssyysstteemm((""llss --ll **..cc""))

       is not acceptable. The application could obtain approximately the same
       result using the sequence:

           gglloobbbbuuff..ggll__ooffffss == 22;;
           gglloobb((""**..cc"",, GGLLOOBB__DDOOOOFFFFSS,, NNUULLLL,, &&gglloobbbbuuff));;
           gglloobbbbuuff..ggll__ppaatthhvv[[00]] == ""llss"";;
           gglloobbbbuuff..ggll__ppaatthhvv[[11]] == ""--ll"";;
           eexxeeccvvpp((""llss"",, &&gglloobbbbuuff..ggll__ppaatthhvv[[00]]));;

       Using the same example:

           llss --ll **..cc **..hh

       could be approximately simulated using GLOB_APPEND as follows:

           gglloobbbbuuff..ggll__ooffffss == 22;;
           gglloobb((""**..cc"",, GGLLOOBB__DDOOOOFFFFSS,, NNUULLLL,, &&gglloobbbbuuff));;
           gglloobb((""**..hh"",, GGLLOOBB__DDOOOOFFFFSS||GGLLOOBB__AAPPPPEENNDD,, NNUULLLL,, &&gglloobbbbuuff));;
           ......

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       This function is not provided for the purpose of enabling utilities to
       perform pathname expansion on their arguments, as this operation is
       performed by the shell, and utilities are explicitly not expected to
       redo this. Instead, it is provided for applications that need to do
       pathname expansion on strings obtained from other sources, such as a
       pattern typed by a user or read from a file.

       If a utility needs to see if a pathname matches a given pattern, it can
       use _f_n_m_a_t_c_h().

       Note that _g_l___p_a_t_h_c and _g_l___p_a_t_h_v have meaning even if _g_l_o_b() fails. This
       allows _g_l_o_b() to report partial results in the event of an error.
       However, if _g_l___p_a_t_h_c is 0, _g_l___p_a_t_h_v is unspecified even if _g_l_o_b() did
       not return an error.

       The GLOB_NOCHECK option could be used when an application wants to
       expand a pathname if wildcards are specified, but wants to treat the
       pattern as just a string otherwise. The _s_h utility might use this for
       option-arguments, for example.

       The new pathnames generated by a subsequent call with GLOB_APPEND are
       not sorted together with the previous pathnames. This mirrors the way
       that the shell handles pathname expansion when multiple expansions are
       done on a command line.

       Applications that need tilde and parameter expansion should use
       _w_o_r_d_e_x_p().

RRAATTIIOONNAALLEE
       It was claimed that the GLOB_DOOFFS flag is unnecessary because it
       could be simulated using:

           nneeww == ((cchhaarr ****))mmaalllloocc((((nn ++ ppgglloobb-->>ggll__ppaatthhcc ++ 11))
                  ** ssiizzeeooff((cchhaarr **))));;
           ((vvooiidd)) mmeemmccppyy((nneeww++nn,, ppgglloobb-->>ggll__ppaatthhvv,,
                  ppgglloobb-->>ggll__ppaatthhcc ** ssiizzeeooff((cchhaarr **))));;
           ((vvooiidd)) mmeemmsseett((nneeww,, 00,, nn ** ssiizzeeooff((cchhaarr **))));;
           ffrreeee((ppgglloobb-->>ggll__ppaatthhvv));;
           ppgglloobb-->>ggll__ppaatthhvv == nneeww;;

       However, this assumes that the memory pointed to by _g_l___p_a_t_h_v is a block
       that was separately created using _m_a_l_l_o_c().  This is not necessarily
       the case. An application should make no assumptions about how the
       memory referenced by fields in _p_g_l_o_b was allocated. It might have been
       obtained from _m_a_l_l_o_c() in a large chunk and then carved up within
       _g_l_o_b(), or it might have been created using a different memory
       allocator. It is not the intent of the standard developers to specify
       or imply how the memory used by _g_l_o_b() is managed.

       The GLOB_APPEND flag would be used when an application wants to expand
       several different patterns into a single list.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _e_x_e_c, _f_d_o_p_e_n_d_i_r(), _f_n_m_a_t_c_h(), _f_s_t_a_t_a_t(), _r_e_a_d_d_i_r(), _S_e_c_t_i_o_n _2_._6, _W_o_r_d
       _E_x_p_a_n_s_i_o_n_s

       The Base Definitions volume of POSIX.1-2008, _S_e_c_t_i_o_n _7_._3_._2, _L_C___C_O_L_L_A_T_E,
       <<gglloobb..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) 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.unix.org/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                  2013                              GLOB(3)
