ALPHASORT(3P)
π ALPHASORT(3P) β bestie edition π
okay so this page lives in the POSIX Programmer's Manual (that's the manual name, she's fancy) π
πΈ PROLOG πΈ
Real talk first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β like, go check the corresponding Linux manual page for the deets on Linux behavior β or honestly the interface may not even be implemented on Linux at all. π
β¨ NAME β¨
alphasort, scandir β scan a directory π¦
π SYNOPSIS π
#include <dirent.h>
int alphasort(const struct dirent **d1, const struct dirent **d2);
int scandir(const char *dir, struct dirent ***namelist,
int (*sel)(const struct dirent *),
int (*compar)(const struct dirent **, const struct dirent **));
π DESCRIPTION π
ok so alphasort() is literally built to be the comparison function for scandir(), and its whole job is sorting the directory entries d1 and d2 into alphabetical order. The sorting happens as if you called strcoll() on the d_name element of the dirent structures you passed in as the two parameters. And if strcoll() fails? Then the return value of alphasort() is unspecified β no promises, bestie. π
Here's the tea on errors: alphasort() shall not change the setting of errno if successful. And since there's no return value reserved to signal an error, if you're an application wanting to check for error situations you gotta set errno to 0, then call alphasort(), then check errno. That's the whole ritual. π―οΈ
Now scandir() β she's the main character. She shall scan the directory dir, calling the function referenced by sel on each directory entry. Entries where the function referenced by sel returns non-zero get stored in strings allocated as if by a call to malloc(), and sorted as if by a call to qsort() with the comparison function compar β with one little caveat: compar need not provide total ordering. The strings get collected in the array namelist, which shall be allocated as if by a call to malloc(). If sel is a null pointer, then all entries shall be selected. And if the comparison function compar doesn't provide total ordering, the order in which the directory entries are stored is unspecified. iykyk π
π RETURN VALUE π
Upon successful completion, alphasort() shall return an integer greater than, equal to, or less than 0 β depending on whether the name of the directory entry pointed to by d1 is lexically greater than, equal to, or less than the directory pointed to by d2, when both are interpreted as appropriate to the current locale. And heads up: there is no return value reserved to indicate an error. π
Upon successful completion, scandir() shall return the number of entries in the array, and hand you a pointer to the array through the parameter namelist. Otherwise? scandir() shall return -1. periodt π
π₯ ERRORS π₯
The scandir() function shall fail if:
EACCESβ Search permission is denied for the component of the path prefix ofdir, or read permission is denied fordir.ELOOPβ A loop exists in symbolic links encountered during resolution of thedirargument.ENAMETOOLONGβ The length of a component of a pathname is longer than{NAME_MAX}.ENOENTβ A component ofdirdoes not name an existing directory, ordiris an empty string.ENOMEMβ Insufficient storage space is available.ENOTDIRβ A component ofdirnames an existing file that is neither a directory nor a symbolic link to a directory.EOVERFLOWβ One of the values to be returned or passed to a callback function cannot be represented correctly.
The scandir() function may fail if:
ELOOPβ More than{SYMLOOP_MAX}symbolic links were encountered during resolution of thedirargument.EMFILEβ All file descriptors available to the process are currently open.ENAMETOOLONGβ 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}.ENFILEβ Too many files are currently open in the system.
The following sections are informative. (so like, not the binding rules, just the lore β¨)
π¦ EXAMPLES π¦
An example to print the files in the current directory:
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
...
struct dirent **namelist;
int i,n;
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);
...
π APPLICATION USAGE π
If dir contains filenames that do not form character strings, or which contain characters outside the domain of the collating sequence of the current locale, then alphasort() need not provide a total ordering. But good news: this condition is not possible if all filenames within the directory consist only of characters from the portable filename character set. π«Ά
scandir() may allocate dynamic storage during its operation. So if scandir() gets forcibly terminated β like by longjmp() or siglongjmp() being executed by the function pointed to by sel or compar, or by an interrupt routine β then scandir() doesn't get 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 scandir() returns to act on the interrupt. That's the move.
For functions that allocate memory as if by malloc(), the application should release such memory when it's no longer required, by a call to free(). For scandir(), that's namelist β including all of the individual strings in namelist. π§
π RATIONALE π
None.
β FUTURE DIRECTIONS β
None.
πΈ SEE ALSO πΈ
qsort(), strcoll()
The Base Definitions volume of POSIX.1β2017, <dirent.h>
π COPYRIGHT π
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 .
and that's the whole page, bestie!! πβ¨ this one's brought to you by IEEE/The Open Group, dated 2017, and she ate, no cap. go forth and scandir() responsibly π
π
π¦ where this page comes from
- alpine 3.22: man-pages-posix 2017a-r0 Β· custom
- alpine 3.23: man-pages-posix 2017a-r0 Β· custom
- alpine 3.24: man-pages-posix 2017a-r0 Β· custom
- arch rolling: man-pages 6.19-1
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages-posix 2017a
- opensuse-leap 16.0: man-pages-posix 2017a-160000.4.1 Β· SUSE-IEEE
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms