|
Packit |
7cfc04 |
'\" et
|
|
Packit |
7cfc04 |
.TH ALPHASORT "3P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
|
|
Packit |
7cfc04 |
.SH PROLOG
|
|
Packit |
7cfc04 |
This manual page is part of the POSIX Programmer's Manual.
|
|
Packit |
7cfc04 |
The Linux implementation of this interface may differ (consult
|
|
Packit |
7cfc04 |
the corresponding Linux manual page for details of Linux behavior),
|
|
Packit |
7cfc04 |
or the interface may not be implemented on Linux.
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
.SH NAME
|
|
Packit |
7cfc04 |
alphasort, scandir
|
|
Packit |
7cfc04 |
\(em scan a directory
|
|
Packit |
7cfc04 |
.SH SYNOPSIS
|
|
Packit |
7cfc04 |
.LP
|
|
Packit |
7cfc04 |
.nf
|
|
Packit |
7cfc04 |
#include <dirent.h>
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
int alphasort(const struct dirent **\fId1\fP, const struct dirent **\fId2\fP);
|
|
Packit |
7cfc04 |
int scandir(const char *\fIdir\fP, struct dirent ***\fInamelist\fP,
|
|
Packit |
7cfc04 |
int (*\fIsel\fP)(const struct dirent *),
|
|
Packit |
7cfc04 |
int (*\fIcompar\fP)(const struct dirent **, const struct dirent **));
|
|
Packit |
7cfc04 |
.fi
|
|
Packit |
7cfc04 |
.SH DESCRIPTION
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
\fIalphasort\fR()
|
|
Packit |
7cfc04 |
function can be used as the comparison function for the
|
|
Packit |
7cfc04 |
\fIscandir\fR()
|
|
Packit |
7cfc04 |
function to sort the directory entries,
|
|
Packit |
7cfc04 |
.IR d1
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.IR d2 ,
|
|
Packit |
7cfc04 |
into alphabetical order. Sorting happens as if by calling the
|
|
Packit |
7cfc04 |
\fIstrcoll\fR()
|
|
Packit |
7cfc04 |
function on the
|
|
Packit |
7cfc04 |
.IR d_name
|
|
Packit |
7cfc04 |
element of the
|
|
Packit |
7cfc04 |
.BR dirent
|
|
Packit |
7cfc04 |
structures passed as the two parameters. If the
|
|
Packit |
7cfc04 |
\fIstrcoll\fR()
|
|
Packit |
7cfc04 |
function fails, the return value of
|
|
Packit |
7cfc04 |
\fIalphasort\fR()
|
|
Packit |
7cfc04 |
is unspecified.
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
\fIalphasort\fR()
|
|
Packit |
7cfc04 |
function shall not change the setting of
|
|
Packit |
7cfc04 |
.IR errno
|
|
Packit |
7cfc04 |
if successful. Since no return value is reserved to indicate an error,
|
|
Packit |
7cfc04 |
an application wishing to check for error situations should set
|
|
Packit |
7cfc04 |
.IR errno
|
|
Packit |
7cfc04 |
to 0, then call
|
|
Packit |
7cfc04 |
\fIalphasort\fR(),
|
|
Packit |
7cfc04 |
then check
|
|
Packit |
7cfc04 |
.IR errno .
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
\fIscandir\fR()
|
|
Packit |
7cfc04 |
function shall scan the directory
|
|
Packit |
7cfc04 |
.IR dir ,
|
|
Packit |
7cfc04 |
calling the function referenced by
|
|
Packit |
7cfc04 |
.IR sel
|
|
Packit |
7cfc04 |
on each directory entry. Entries for which the function referenced by
|
|
Packit |
7cfc04 |
.IR sel
|
|
Packit |
7cfc04 |
returns non-zero shall be stored in strings allocated as if by
|
|
Packit |
7cfc04 |
a call to
|
|
Packit |
7cfc04 |
\fImalloc\fR(),
|
|
Packit |
7cfc04 |
and sorted as if by a call to
|
|
Packit |
7cfc04 |
\fIqsort\fR()
|
|
Packit |
7cfc04 |
with the comparison function
|
|
Packit |
7cfc04 |
.IR compar ,
|
|
Packit |
7cfc04 |
except that
|
|
Packit |
7cfc04 |
.IR compar
|
|
Packit |
7cfc04 |
need not provide total ordering. The strings are collected in
|
|
Packit |
7cfc04 |
array
|
|
Packit |
7cfc04 |
.IR namelist
|
|
Packit |
7cfc04 |
which shall be allocated as if by a call to
|
|
Packit |
7cfc04 |
\fImalloc\fR().
|
|
Packit |
7cfc04 |
If
|
|
Packit |
7cfc04 |
.IR sel
|
|
Packit |
7cfc04 |
is a null pointer, all entries shall be selected.
|
|
Packit |
7cfc04 |
If the comparison function
|
|
Packit |
7cfc04 |
.IR compar
|
|
Packit |
7cfc04 |
does not provide total ordering, the order in which the directory
|
|
Packit |
7cfc04 |
entries are stored is unspecified.
|
|
Packit |
7cfc04 |
.SH "RETURN VALUE"
|
|
Packit |
7cfc04 |
Upon successful completion, the
|
|
Packit |
7cfc04 |
\fIalphasort\fR()
|
|
Packit |
7cfc04 |
function shall return an integer greater than, equal to, or less than 0,
|
|
Packit |
7cfc04 |
according to whether the name of the directory entry pointed to by
|
|
Packit |
7cfc04 |
.IR d1
|
|
Packit |
7cfc04 |
is lexically greater than, equal to, or less than the directory pointed
|
|
Packit |
7cfc04 |
to by
|
|
Packit |
7cfc04 |
.IR d2
|
|
Packit |
7cfc04 |
when both are interpreted as appropriate to the current locale. There
|
|
Packit |
7cfc04 |
is no return value reserved to indicate an error.
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
Upon successful completion, the
|
|
Packit |
7cfc04 |
\fIscandir\fR()
|
|
Packit |
7cfc04 |
function shall return the number of entries in the array and a pointer
|
|
Packit |
7cfc04 |
to the array through the parameter
|
|
Packit |
7cfc04 |
.IR namelist .
|
|
Packit |
7cfc04 |
Otherwise, the
|
|
Packit |
7cfc04 |
\fIscandir\fR()
|
|
Packit |
7cfc04 |
function shall return \(mi1.
|
|
Packit |
7cfc04 |
.SH ERRORS
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
\fIscandir\fR()
|
|
Packit |
7cfc04 |
function shall fail if:
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EACCES
|
|
Packit |
7cfc04 |
Search permission is denied for the component of the path prefix of
|
|
Packit |
7cfc04 |
.IR dir
|
|
Packit |
7cfc04 |
or read permission is denied for
|
|
Packit |
7cfc04 |
.IR dir .
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ELOOP
|
|
Packit |
7cfc04 |
A loop exists in symbolic links encountered during resolution of the
|
|
Packit |
7cfc04 |
.IR dir
|
|
Packit |
7cfc04 |
argument.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENAMETOOLONG
|
|
Packit |
7cfc04 |
.br
|
|
Packit |
7cfc04 |
The length of a component of a pathname is longer than
|
|
Packit |
7cfc04 |
{NAME_MAX}.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENOENT
|
|
Packit |
7cfc04 |
A component of
|
|
Packit |
7cfc04 |
.IR dir
|
|
Packit |
7cfc04 |
does not name an existing directory or
|
|
Packit |
7cfc04 |
.IR dir
|
|
Packit |
7cfc04 |
is an empty string.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENOMEM
|
|
Packit |
7cfc04 |
Insufficient storage space is available.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENOTDIR
|
|
Packit |
7cfc04 |
A component of
|
|
Packit |
7cfc04 |
.IR dir
|
|
Packit |
7cfc04 |
names an existing file that is neither a directory nor a symbolic link
|
|
Packit |
7cfc04 |
to a directory.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EOVERFLOW
|
|
Packit |
7cfc04 |
One of the values to be returned or passed to a callback function cannot
|
|
Packit |
7cfc04 |
be represented correctly.
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
\fIscandir\fR()
|
|
Packit |
7cfc04 |
function may fail if:
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ELOOP
|
|
Packit |
7cfc04 |
More than
|
|
Packit |
7cfc04 |
{SYMLOOP_MAX}
|
|
Packit |
7cfc04 |
symbolic links were encountered during resolution of the
|
|
Packit |
7cfc04 |
.IR dir
|
|
Packit |
7cfc04 |
argument.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR EMFILE
|
|
Packit |
7cfc04 |
All file descriptors available to the process are currently open.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENAMETOOLONG
|
|
Packit |
7cfc04 |
.br
|
|
Packit |
7cfc04 |
The length of a pathname exceeds
|
|
Packit |
7cfc04 |
{PATH_MAX},
|
|
Packit |
7cfc04 |
or pathname resolution of a symbolic link produced an intermediate
|
|
Packit |
7cfc04 |
result with a length that exceeds
|
|
Packit |
7cfc04 |
{PATH_MAX}.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR ENFILE
|
|
Packit |
7cfc04 |
Too many files are currently open in the system.
|
|
Packit |
7cfc04 |
.LP
|
|
Packit |
7cfc04 |
.IR "The following sections are informative."
|
|
Packit |
7cfc04 |
.SH EXAMPLES
|
|
Packit |
7cfc04 |
An example to print the files in the current directory:
|
|
Packit |
7cfc04 |
.sp
|
|
Packit |
7cfc04 |
.RS 4
|
|
Packit |
7cfc04 |
.nf
|
|
Packit |
7cfc04 |
\fB
|
|
Packit |
7cfc04 |
#include <dirent.h>
|
|
Packit |
7cfc04 |
#include <stdio.h>
|
|
Packit |
7cfc04 |
#include <stdlib.h>
|
|
Packit |
7cfc04 |
\&...
|
|
Packit |
7cfc04 |
struct dirent **namelist;
|
|
Packit |
7cfc04 |
int i,n;
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
n = scandir(".", &namelist, 0, alphasort);
|
|
Packit |
7cfc04 |
if (n < 0)
|
|
Packit |
7cfc04 |
perror("scandir");
|
|
Packit |
7cfc04 |
else {
|
|
Packit |
7cfc04 |
for (i = 0; i < n; i++) {
|
|
Packit |
7cfc04 |
printf("%s\en", namelist[i]->d_name);
|
|
Packit |
7cfc04 |
free(namelist[i]);
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
free(namelist);
|
|
Packit |
7cfc04 |
\&...
|
|
Packit |
7cfc04 |
.fi \fR
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
.RE
|
|
Packit |
7cfc04 |
.SH "APPLICATION USAGE"
|
|
Packit |
7cfc04 |
If
|
|
Packit |
7cfc04 |
.IR dir
|
|
Packit |
7cfc04 |
contains filenames that do not form character strings, or which contain
|
|
Packit |
7cfc04 |
characters outside the domain of the collating sequence of the current
|
|
Packit |
7cfc04 |
locale, the
|
|
Packit |
7cfc04 |
\fIalphasort\fR()
|
|
Packit |
7cfc04 |
function need not provide a total ordering. This condition is not possible
|
|
Packit |
7cfc04 |
if all filenames within the directory consist only of characters from
|
|
Packit |
7cfc04 |
the portable filename character set.
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
\fIscandir\fR()
|
|
Packit |
7cfc04 |
function may allocate dynamic storage during its operation. If
|
|
Packit |
7cfc04 |
\fIscandir\fR()
|
|
Packit |
7cfc04 |
is forcibly terminated, such as by
|
|
Packit |
7cfc04 |
\fIlongjmp\fR()
|
|
Packit |
7cfc04 |
or
|
|
Packit |
7cfc04 |
\fIsiglongjmp\fR()
|
|
Packit |
7cfc04 |
being executed by the function pointed to by
|
|
Packit |
7cfc04 |
.IR sel
|
|
Packit |
7cfc04 |
or
|
|
Packit |
7cfc04 |
.IR compar ,
|
|
Packit |
7cfc04 |
or by an interrupt routine,
|
|
Packit |
7cfc04 |
\fIscandir\fR()
|
|
Packit |
7cfc04 |
does not have a chance to free that storage, so it remains permanently
|
|
Packit |
7cfc04 |
allocated. A safe way to handle interrupts is to store the fact that
|
|
Packit |
7cfc04 |
an interrupt has occurred, then wait until
|
|
Packit |
7cfc04 |
\fIscandir\fR()
|
|
Packit |
7cfc04 |
returns to act on the interrupt.
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
For functions that allocate memory as if by
|
|
Packit |
7cfc04 |
\fImalloc\fR(),
|
|
Packit |
7cfc04 |
the application should release such memory when it is no longer
|
|
Packit |
7cfc04 |
required by a call to
|
|
Packit |
7cfc04 |
\fIfree\fR().
|
|
Packit |
7cfc04 |
For
|
|
Packit |
7cfc04 |
\fIscandir\fR(),
|
|
Packit |
7cfc04 |
this is
|
|
Packit |
7cfc04 |
.IR namelist
|
|
Packit |
7cfc04 |
(including all of the individual strings in
|
|
Packit |
7cfc04 |
.IR namelist ).
|
|
Packit |
7cfc04 |
.SH RATIONALE
|
|
Packit |
7cfc04 |
None.
|
|
Packit |
7cfc04 |
.SH "FUTURE DIRECTIONS"
|
|
Packit |
7cfc04 |
None.
|
|
Packit |
7cfc04 |
.SH "SEE ALSO"
|
|
Packit |
7cfc04 |
.IR "\fIqsort\fR\^(\|)",
|
|
Packit |
7cfc04 |
.IR "\fIstrcoll\fR\^(\|)"
|
|
Packit |
7cfc04 |
.P
|
|
Packit |
7cfc04 |
The Base Definitions volume of POSIX.1\(hy2008,
|
|
Packit |
7cfc04 |
.IR "\fB<dirent.h>\fP"
|
|
Packit |
7cfc04 |
.SH COPYRIGHT
|
|
Packit |
7cfc04 |
Portions of this text are reprinted and reproduced in electronic form
|
|
Packit |
7cfc04 |
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
|
|
Packit |
7cfc04 |
-- Portable Operating System Interface (POSIX), The Open Group Base
|
|
Packit |
7cfc04 |
Specifications Issue 7, Copyright (C) 2013 by the Institute of
|
|
Packit |
7cfc04 |
Electrical and Electronics Engineers, Inc and The Open Group.
|
|
Packit |
7cfc04 |
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
|
|
Packit |
7cfc04 |
event of any discrepancy between this version and the original IEEE and
|
|
Packit |
7cfc04 |
The Open Group Standard, the original IEEE and The Open Group Standard
|
|
Packit |
7cfc04 |
is the referee document. The original Standard can be obtained online at
|
|
Packit |
7cfc04 |
http://www.unix.org/online.html .
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
Any typographical or formatting errors that appear
|
|
Packit |
7cfc04 |
in this page are most likely
|
|
Packit |
7cfc04 |
to have been introduced during the conversion of the source files to
|
|
Packit |
7cfc04 |
man page format. To report such errors, see
|
|
Packit |
7cfc04 |
https://www.kernel.org/doc/man-pages/reporting_bugs.html .
|