441 lines
9.5 KiB
HTML
441 lines
9.5 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of SCANDIR</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>SCANDIR</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2017-09-15<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
scandir, scandirat, alphasort, versionsort - scan
|
|
a directory for matching entries
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/dirent.h">dirent.h</A>></B>
|
|
|
|
<B>int scandir(const char *</B><I>dirp</I><B>, struct dirent ***</B><I>namelist</I><B>,</B>
|
|
<DL COMPACT><DT id="1"><DD><B>int (*</B><I>filter</I><B>)(const struct dirent *),</B>
|
|
<B>int (*</B><I>compar</I><B>)(const struct dirent **, const struct dirent **));</B>
|
|
</DL>
|
|
|
|
<B>int alphasort(const struct dirent **</B><I>a</I><B>, const struct dirent **</B><I>b</I><B>);</B>
|
|
|
|
<B>int versionsort(const struct dirent **</B><I>a</I><B>, const struct dirent **</B><I>b</I><B>);</B>
|
|
|
|
<B>#include <<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>></B> /* Definition of AT_* constants */
|
|
<B>#include <<A HREF="file:///usr/include/dirent.h">dirent.h</A>></B>
|
|
|
|
</PRE>
|
|
|
|
<B>int scandirat(int </B><I>dirfd</I><B>, const char *</B><I>dirp</I><B>,</B>
|
|
|
|
<B>struct dirent ***</B><I>namelist</I><B>,</B>
|
|
|
|
<PRE>
|
|
<DL COMPACT><DT id="2"><DD><B>int (*</B><I>filter</I><B>)(const struct dirent *),</B>
|
|
<B>int (*</B><I>compar</I><B>)(const struct dirent **, const struct dirent **));</B>
|
|
</DL>
|
|
</PRE>
|
|
|
|
<P>
|
|
|
|
|
|
Feature Test Macro Requirements for glibc (see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
|
|
|
|
|
|
<P>
|
|
|
|
<B>scandir</B>(),
|
|
|
|
<B>alphasort</B>():
|
|
|
|
<BR>
|
|
|
|
<DL COMPACT><DT id="3"><DD>
|
|
|
|
|
|
/* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200809L
|
|
<BR> || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
|
|
|
|
</DL>
|
|
|
|
<P>
|
|
|
|
<B>versionsort</B>():
|
|
|
|
_GNU_SOURCE
|
|
<P>
|
|
|
|
<B>scandirat</B>():
|
|
|
|
_GNU_SOURCE
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>scandir</B>()
|
|
|
|
function scans the directory <I>dirp</I>, calling
|
|
<I>filter</I>() on each directory entry.
|
|
Entries for which
|
|
<I>filter</I>() returns nonzero are stored in strings allocated via
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3),
|
|
|
|
sorted using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+qsort">qsort</A></B>(3)
|
|
|
|
with the comparison
|
|
function <I>compar</I>(), and collected in array <I>namelist</I>
|
|
which is allocated via
|
|
<B><A HREF="/cgi-bin/man/man2html?3+malloc">malloc</A></B>(3).
|
|
|
|
If <I>filter</I> is NULL, all entries are selected.
|
|
<P>
|
|
|
|
The
|
|
<B>alphasort</B>()
|
|
|
|
and
|
|
<B>versionsort</B>()
|
|
|
|
functions can be used as the comparison function
|
|
<I>compar</I>().
|
|
|
|
The former sorts directory entries using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strcoll">strcoll</A></B>(3),
|
|
|
|
the latter using
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strverscmp">strverscmp</A></B>(3)
|
|
|
|
on the strings <I>(*a)->d_name</I> and <I>(*b)->d_name</I>.
|
|
<A NAME="lbAE"> </A>
|
|
<H3>scandirat()</H3>
|
|
|
|
The
|
|
<B>scandirat</B>()
|
|
|
|
function operates in exactly the same way as
|
|
<B>scandir</B>(),
|
|
|
|
except for the differences described here.
|
|
<P>
|
|
|
|
If the pathname given in
|
|
<I>dirp</I>
|
|
|
|
is relative, then it is interpreted relative to the directory
|
|
referred to by the file descriptor
|
|
<I>dirfd</I>
|
|
|
|
(rather than relative to the current working directory of
|
|
the calling process, as is done by
|
|
<B>scandir</B>()
|
|
|
|
for a relative pathname).
|
|
<P>
|
|
|
|
If
|
|
<I>dirp</I>
|
|
|
|
is relative and
|
|
<I>dirfd</I>
|
|
|
|
is the special value
|
|
<B>AT_FDCWD</B>,
|
|
|
|
then
|
|
<I>dirp</I>
|
|
|
|
is interpreted relative to the current working
|
|
directory of the calling process (like
|
|
<B>scandir</B>()).
|
|
|
|
<P>
|
|
|
|
If
|
|
<I>dirp</I>
|
|
|
|
is absolute, then
|
|
<I>dirfd</I>
|
|
|
|
is ignored.
|
|
<P>
|
|
|
|
See
|
|
<B><A HREF="/cgi-bin/man/man2html?2+openat">openat</A></B>(2)
|
|
|
|
for an explanation of the need for
|
|
<B>scandirat</B>().
|
|
|
|
<A NAME="lbAF"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
The
|
|
<B>scandir</B>()
|
|
|
|
function returns the number of directory entries
|
|
selected.
|
|
On error, -1 is returned, with
|
|
<I>errno</I>
|
|
|
|
set to indicate the cause of the error.
|
|
<P>
|
|
|
|
The
|
|
<B>alphasort</B>()
|
|
|
|
and
|
|
<B>versionsort</B>()
|
|
|
|
functions return an integer less than, equal to,
|
|
or greater than zero if the first argument is considered to be
|
|
respectively less than, equal to, or greater than the second.
|
|
<A NAME="lbAG"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="4"><B>ENOENT</B>
|
|
|
|
<DD>
|
|
The path in <I>dirp</I> does not exist.
|
|
<DT id="5"><B>ENOMEM</B>
|
|
|
|
<DD>
|
|
Insufficient memory to complete the operation.
|
|
<DT id="6"><B>ENOTDIR</B>
|
|
|
|
<DD>
|
|
The path in <I>dirp</I> is not a directory.
|
|
</DL>
|
|
<P>
|
|
|
|
The following additional errors can occur for
|
|
<B>scandirat</B>():
|
|
|
|
<DL COMPACT>
|
|
<DT id="7"><B>EBADF</B>
|
|
|
|
<DD>
|
|
<I>dirfd</I>
|
|
|
|
is not a valid file descriptor.
|
|
<DT id="8"><B>ENOTDIR</B>
|
|
|
|
<DD>
|
|
<I>dirp</I>
|
|
|
|
is a relative path and
|
|
<I>dirfd</I>
|
|
|
|
is a file descriptor referring to a file other than a directory.
|
|
</DL>
|
|
<A NAME="lbAH"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
<B>versionsort</B>()
|
|
|
|
was added to glibc in version 2.1.
|
|
<P>
|
|
|
|
<B>scandirat</B>()
|
|
|
|
was added to glibc in version 2.15.
|
|
<A NAME="lbAI"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>scandir</B>(),
|
|
|
|
<B>scandirat</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>alphasort</B>(),
|
|
|
|
<B>versionsort</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe locale<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<P>
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
<B>alphasort</B>(),
|
|
|
|
<B>scandir</B>():
|
|
|
|
4.3BSD, POSIX.1-2008.
|
|
<P>
|
|
|
|
<B>versionsort</B>()
|
|
|
|
and
|
|
<B>scandirat</B>()
|
|
|
|
are GNU extensions.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
Since glibc 2.1,
|
|
<B>alphasort</B>()
|
|
|
|
calls
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strcoll">strcoll</A></B>(3);
|
|
|
|
earlier it used
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strcmp">strcmp</A></B>(3).
|
|
|
|
<P>
|
|
|
|
Before glibc 2.10, the two arguments of
|
|
<B>alphasort</B>()
|
|
|
|
and
|
|
<B>versionsort</B>()
|
|
|
|
were typed as
|
|
<I>const void *</I>.
|
|
|
|
When
|
|
<B>alphasort</B>()
|
|
|
|
was standardized in POSIX.1-2008,
|
|
the argument type was specified as the type-safe
|
|
<I>const struct dirent **,</I>
|
|
|
|
and glibc 2.10 changed the definition of
|
|
<B>alphasort</B>()
|
|
|
|
(and the nonstandard
|
|
<B>versionsort</B>())
|
|
|
|
to match the standard.
|
|
<A NAME="lbAL"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below prints a list of the files in the current directory
|
|
in reverse order.
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
#define _DEFAULT_SOURCE
|
|
#include <<A HREF="file:///usr/include/dirent.h">dirent.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
<P>
|
|
int
|
|
main(void)
|
|
{
|
|
<BR> struct dirent **namelist;
|
|
<BR> int n;
|
|
<P>
|
|
<BR> n = scandir(".", &namelist, NULL, alphasort);
|
|
<BR> if (n == -1) {
|
|
<BR> perror("scandir");
|
|
<BR> exit(EXIT_FAILURE);
|
|
<BR> }
|
|
<P>
|
|
<BR> while (n--) {
|
|
<BR> printf("%s\n", namelist[n]->d_name);
|
|
<BR> free(namelist[n]);
|
|
<BR> }
|
|
<BR> free(namelist);
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS);
|
|
}
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+closedir">closedir</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+fnmatch">fnmatch</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+opendir">opendir</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+readdir">readdir</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+rewinddir">rewinddir</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+seekdir">seekdir</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strcmp">strcmp</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strcoll">strcoll</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+strverscmp">strverscmp</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+telldir">telldir</A></B>(3)
|
|
|
|
<A NAME="lbAO"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="9"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="10"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="11"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DL>
|
|
<DT id="12"><A HREF="#lbAE">scandirat()</A><DD>
|
|
</DL>
|
|
<DT id="13"><A HREF="#lbAF">RETURN VALUE</A><DD>
|
|
<DT id="14"><A HREF="#lbAG">ERRORS</A><DD>
|
|
<DT id="15"><A HREF="#lbAH">VERSIONS</A><DD>
|
|
<DT id="16"><A HREF="#lbAI">ATTRIBUTES</A><DD>
|
|
<DT id="17"><A HREF="#lbAJ">CONFORMING TO</A><DD>
|
|
<DT id="18"><A HREF="#lbAK">NOTES</A><DD>
|
|
<DT id="19"><A HREF="#lbAL">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="20"><A HREF="#lbAM">Program source</A><DD>
|
|
</DL>
|
|
<DT id="21"><A HREF="#lbAN">SEE ALSO</A><DD>
|
|
<DT id="22"><A HREF="#lbAO">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:54 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|