{"id":4998,"date":"2022-12-20T18:37:02","date_gmt":"2022-12-20T21:37:02","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/alphasort-man3p\/"},"modified":"2022-12-20T18:37:02","modified_gmt":"2022-12-20T21:37:02","slug":"alphasort-man3p","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/alphasort-man3p\/","title":{"rendered":"ALPHASORT (man3p)"},"content":{"rendered":"<h1 align=\"center\">ALPHASORT<\/h1>\n<p> <a href=\"#PROLOG\">PROLOG<\/a><br \/> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#SYNOPSIS\">SYNOPSIS<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#RETURN VALUE\">RETURN VALUE<\/a><br \/> <a href=\"#ERRORS\">ERRORS<\/a><br \/> <a href=\"#EXAMPLES\">EXAMPLES<\/a><br \/> <a href=\"#APPLICATION USAGE\">APPLICATION USAGE<\/a><br \/> <a href=\"#RATIONALE\">RATIONALE<\/a><br \/> <a href=\"#FUTURE DIRECTIONS\">FUTURE DIRECTIONS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#COPYRIGHT\">COPYRIGHT<\/a> <\/p>\n<hr>\n<h2>PROLOG <a name=\"PROLOG\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This manual page is part of the POSIX Programmer\u2019s 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.<\/p>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">alphasort, scandir \u2014 scan a directory<\/p>\n<h2>SYNOPSIS <a name=\"SYNOPSIS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">#include <dirent.h><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">int alphasort(const struct dirent **<i>d1<\/i>, const struct dirent **<i>d2<\/i>); <br \/> int scandir(const char *<i>dir<\/i>, struct dirent ***<i>namelist<\/i>, <br \/> int (*<i>sel<\/i>)(const struct dirent *), <br \/> int (*<i>compar<\/i>)(const struct dirent **, const struct dirent **));<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>alphasort<\/i>() function can be used as the comparison function for the <i>scandir<\/i>() function to sort the directory entries, <i>d1<\/i> and <i>d2<\/i>, into alphabetical order. Sorting happens as if by calling the <i>strcoll<\/i>() function on the <i>d_name<\/i> element of the <b>dirent<\/b> structures passed as the two parameters. If the <i>strcoll<\/i>() function fails, the return value of <i>alphasort<\/i>() is unspecified.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>alphasort<\/i>() function shall not change the setting of <i>errno<\/i> if successful. Since no return value is reserved to indicate an error, an application wishing to check for error situations should set <i>errno<\/i> to 0, then call <i>alphasort<\/i>(), then check <i>errno<\/i>.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>scandir<\/i>() function shall scan the directory <i>dir<\/i>, calling the function referenced by <i>sel<\/i> on each directory entry. Entries for which the function referenced by <i>sel<\/i> returns non-zero shall be stored in strings allocated as if by a call to <i>malloc<\/i>(), and sorted as if by a call to <i>qsort<\/i>() with the comparison function <i>compar<\/i>, except that <i>compar<\/i> need not provide total ordering. The strings are collected in array <i>namelist<\/i> which shall be allocated as if by a call to <i>malloc<\/i>(). If <i>sel<\/i> is a null pointer, all entries shall be selected. If the comparison function <i>compar<\/i> does not provide total ordering, the order in which the directory entries are stored is unspecified.<\/p>\n<h2>RETURN VALUE <a name=\"RETURN VALUE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Upon successful completion, the <i>alphasort<\/i>() 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 <i>d1<\/i> is lexically greater than, equal to, or less than the directory pointed to by <i>d2<\/i> when both are interpreted as appropriate to the current locale. There is no return value reserved to indicate an error.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Upon successful completion, the <i>scandir<\/i>() function shall return the number of entries in the array and a pointer to the array through the parameter <i>namelist<\/i>. Otherwise, the <i>scandir<\/i>() function shall return \u22121.<\/p>\n<h2>ERRORS <a name=\"ERRORS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>scandir<\/i>() function shall fail if:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EACCES<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Search permission is denied for the component of the path prefix of <i>dir<\/i> or read permission is denied for <i>dir<\/i>.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ELOOP<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>A loop exists in symbolic links encountered during resolution of the <i>dir<\/i> argument.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENAMETOOLONG<\/b><\/p>\n<p style=\"margin-left:22%;\">The length of a component of a pathname is longer than {NAME_MAX}.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENOENT<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>A component of <i>dir<\/i> does not name an existing directory or <i>dir<\/i> is an empty string.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENOMEM<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>Insufficient storage space is available.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENOTDIR<\/b><\/p>\n<p style=\"margin-left:22%;\">A component of <i>dir<\/i> names an existing file that is neither a directory nor a symbolic link to a directory.<\/p>\n<p style=\"margin-left:11%;\"><b>EOVERFLOW<\/b><\/p>\n<p style=\"margin-left:22%;\">One of the values to be returned or passed to a callback function cannot be represented correctly.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>scandir<\/i>() function may fail if:<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ELOOP<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>More than {SYMLOOP_MAX} symbolic links were encountered during resolution of the <i>dir<\/i> argument.<\/p>\n<\/td>\n<\/tr>\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>EMFILE<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"78%\">\n<p>All file descriptors available to the process are currently open.<\/p>\n<\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%;\"><b>ENAMETOOLONG<\/b><\/p>\n<p style=\"margin-left:22%;\">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}.<\/p>\n<table width=\"100%\" border=\"0\" rules=\"none\" frame=\"void\" cellspacing=\"0\" cellpadding=\"0\">\n<tr valign=\"top\" align=\"left\">\n<td width=\"11%\"><\/td>\n<td width=\"9%\">\n<p><b>ENFILE<\/b><\/p>\n<\/td>\n<td width=\"2%\"><\/td>\n<td width=\"73%\">\n<p>Too many files are currently open in the system.<\/p>\n<\/td>\n<td width=\"5%\"> <\/td>\n<\/tr>\n<\/table>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>The following sections are informative.<\/i><\/p>\n<h2>EXAMPLES <a name=\"EXAMPLES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">An example to print the files in the current directory:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">#include <dirent.h> <br \/> #include <stdio.h> <br \/> #include <stdlib.h> <br \/> &#8230; <br \/> struct dirent **namelist; <br \/> int i,n;<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">n = scandir(&#8220;.&#8221;, &#038;namelist, 0, alphasort); <br \/> if (n < 0) <br \/> perror(&#8220;scandir&#8221;); <br \/> else { <br \/> for (i = 0; i < n; i++) { <br \/> printf(&#8220;%sn&#8221;, namelist[i]->d_name); <br \/> free(namelist[i]); <br \/> } <br \/> } <br \/> free(namelist); <br \/> &#8230;<\/p>\n<h2>APPLICATION USAGE <a name=\"APPLICATION USAGE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">If <i>dir<\/i> contains filenames that do not form character strings, or which contain characters outside the domain of the collating sequence of the current locale, the <i>alphasort<\/i>() 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.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The <i>scandir<\/i>() function may allocate dynamic storage during its operation. If <i>scandir<\/i>() is forcibly terminated, such as by <i>longjmp<\/i>() or <i>siglongjmp<\/i>() being executed by the function pointed to by <i>sel<\/i> or <i>compar<\/i>, or by an interrupt routine, <i>scandir<\/i>() 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 <i>scandir<\/i>() returns to act on the interrupt.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">For functions that allocate memory as if by <i>malloc<\/i>(), the application should release such memory when it is no longer required by a call to <i>free<\/i>(). For <i>scandir<\/i>(), this is <i>namelist<\/i> (including all of the individual strings in <i>namelist<\/i>).<\/p>\n<h2>RATIONALE <a name=\"RATIONALE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>FUTURE DIRECTIONS <a name=\"FUTURE DIRECTIONS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">None.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><i>qsort<\/i>(), <i>strcoll<\/i>()<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">The Base Definitions volume of POSIX.1-2017, <b><dirent.h><\/b><\/p>\n<h2>COPYRIGHT <a name=\"COPYRIGHT\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology &#8212; 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 .<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">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 .<\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  alphasort, scandir \u2014 scan a directory <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3779,1],"tags":[1892,1594],"class_list":["post-4998","post","type-post","status-publish","format-standard","hentry","category-3pm-perl-llamadas-de-bibliotecas","category-sin-categoria","tag-alphasort","tag-man3p"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=4998"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/4998\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=4998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=4998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=4998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}