ALPHASORT(3)               POSIX Programmer's Manual              ALPHASORT(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
       alphasort, scandir -- scan a directory

SSYYNNOOPPSSIISS
       #include <dirent.h>

       int alphasort(const struct dirent **_d_1, const struct dirent **_d_2);
       int scandir(const char *_d_i_r, struct dirent ***_n_a_m_e_l_i_s_t,
           int (*_s_e_l)(const struct dirent *),
           int (*_c_o_m_p_a_r)(const struct dirent **, const struct dirent **));

DDEESSCCRRIIPPTTIIOONN
       The _a_l_p_h_a_s_o_r_t() function can be used as the comparison function for the
       _s_c_a_n_d_i_r() function to sort the directory entries, _d_1 and _d_2, into
       alphabetical order. Sorting happens as if by calling the _s_t_r_c_o_l_l()
       function on the _d___n_a_m_e element of the ddiirreenntt structures passed as the
       two parameters. If the _s_t_r_c_o_l_l() function fails, the return value of
       _a_l_p_h_a_s_o_r_t() is unspecified.

       The _a_l_p_h_a_s_o_r_t() function shall not change the setting of _e_r_r_n_o if
       successful. Since no return value is reserved to indicate an error, an
       application wishing to check for error situations should set _e_r_r_n_o to
       0, then call _a_l_p_h_a_s_o_r_t(), then check _e_r_r_n_o.

       The _s_c_a_n_d_i_r() function shall scan the directory _d_i_r, calling the
       function referenced by _s_e_l on each directory entry. Entries for which
       the function referenced by _s_e_l returns non-zero shall be stored in
       strings allocated as if by a call to _m_a_l_l_o_c(), and sorted as if by a
       call to _q_s_o_r_t() with the comparison function _c_o_m_p_a_r, except that _c_o_m_p_a_r
       need not provide total ordering. The strings are collected in array
       _n_a_m_e_l_i_s_t which shall be allocated as if by a call to _m_a_l_l_o_c().  If _s_e_l
       is a null pointer, all entries shall be selected.  If the comparison
       function _c_o_m_p_a_r does not provide total ordering, the order in which the
       directory entries are stored is unspecified.

RREETTUURRNN VVAALLUUEE
       Upon successful completion, the _a_l_p_h_a_s_o_r_t() function shall return an
       integer greater than, equal to, or less than 0, according to whether
       the name of the directory entry pointed to by _d_1 is lexically greater
       than, equal to, or less than the directory pointed to by _d_2 when both
       are interpreted as appropriate to the current locale. There is no
       return value reserved to indicate an error.

       Upon successful completion, the _s_c_a_n_d_i_r() function shall return the
       number of entries in the array and a pointer to the array through the
       parameter _n_a_m_e_l_i_s_t.  Otherwise, the _s_c_a_n_d_i_r() function shall return -1.

EERRRROORRSS
       The _s_c_a_n_d_i_r() function shall fail if:

       EEAACCCCEESS Search permission is denied for the component of the path prefix
              of _d_i_r or read permission is denied for _d_i_r.

       EELLOOOOPP  A loop exists in symbolic links encountered during resolution of
              the _d_i_r argument.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a component of a pathname is longer than
              {NAME_MAX}.

       EENNOOEENNTT A component of _d_i_r does not name an existing directory or _d_i_r is
              an empty string.

       EENNOOMMEEMM Insufficient storage space is available.

       EENNOOTTDDIIRR
              A component of _d_i_r names an existing file that is neither a
              directory nor a symbolic link to a directory.

       EEOOVVEERRFFLLOOWW
              One of the values to be returned or passed to a callback
              function cannot be represented correctly.

       The _s_c_a_n_d_i_r() function may fail if:

       EELLOOOOPP  More than {SYMLOOP_MAX} symbolic links were encountered during
              resolution of the _d_i_r argument.

       EEMMFFIILLEE All file descriptors available to the process are currently
              open.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a pathname exceeds {PATH_MAX}, or pathname
              resolution of a symbolic link produced an intermediate result
              with a length that exceeds {PATH_MAX}.

       EENNFFIILLEE Too many files are currently open in the system.

       _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
       An example to print the files in the current directory:

           ##iinncclluuddee <<ddiirreenntt..hh>>
           ##iinncclluuddee <<ssttddiioo..hh>>
           ##iinncclluuddee <<ssttddlliibb..hh>>
           ......
           ssttrruucctt ddiirreenntt ****nnaammeelliisstt;;
           iinntt ii,,nn;;

               n = scandir(".", &namelist, 0, alphasort);
               if (n < 0)
                   perror("scandir");
               else {
                   for (i = 0; i < n; i++) {
                       printf("%s\n", namelist[i]->d_name);
                       free(namelist[i]);
                       }
                   }
               free(namelist);
           ...

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       If _d_i_r contains filenames that do not form character strings, or which
       contain characters outside the domain of the collating sequence of the
       current locale, the _a_l_p_h_a_s_o_r_t() function need not provide a total
       ordering. This condition is not possible if all filenames within the
       directory consist only of characters from the portable filename
       character set.

       The _s_c_a_n_d_i_r() function may allocate dynamic storage during its
       operation. If _s_c_a_n_d_i_r() is forcibly terminated, such as by _l_o_n_g_j_m_p() or
       _s_i_g_l_o_n_g_j_m_p() being executed by the function pointed to by _s_e_l or
       _c_o_m_p_a_r, or by an interrupt routine, _s_c_a_n_d_i_r() does not have a chance to
       free that storage, so it remains permanently allocated. A safe way to
       handle interrupts is to store the fact that an interrupt has occurred,
       then wait until _s_c_a_n_d_i_r() returns to act on the interrupt.

       For functions that allocate memory as if by _m_a_l_l_o_c(), the application
       should release such memory when it is no longer required by a call to
       _f_r_e_e().  For _s_c_a_n_d_i_r(), this is _n_a_m_e_l_i_s_t (including all of the
       individual strings in _n_a_m_e_l_i_s_t).

RRAATTIIOONNAALLEE
       None.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _q_s_o_r_t(), _s_t_r_c_o_l_l()

       The Base Definitions volume of POSIX.1-2008, <<ddiirreenntt..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                         ALPHASORT(3)
