man-pages/man3/scandir.3.html
2021-03-31 01:06:50 +01:00

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">&nbsp;</A>
<H2>NAME</H2>
scandir, scandirat, alphasort, versionsort - scan
a directory for matching entries
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/dirent.h">dirent.h</A>&gt;</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 &lt;<A HREF="file:///usr/include/fcntl.h">fcntl.h</A>&gt;</B> /* Definition of AT_* constants */
<B>#include &lt;<A HREF="file:///usr/include/dirent.h">dirent.h</A>&gt;</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&nbsp;&gt;=&nbsp;200809L
<BR>&nbsp;&nbsp;&nbsp;&nbsp;||&nbsp;/*&nbsp;Glibc&nbsp;versions&nbsp;&lt;=&nbsp;2.19:&nbsp;*/&nbsp;_BSD_SOURCE&nbsp;||&nbsp;_SVID_SOURCE
</DL>
<P>
<B>versionsort</B>():
_GNU_SOURCE
<P>
<B>scandirat</B>():
_GNU_SOURCE
<A NAME="lbAD">&nbsp;</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)-&gt;d_name</I> and <I>(*b)-&gt;d_name</I>.
<A NAME="lbAE">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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">&nbsp;</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&nbsp;*</I>.
When
<B>alphasort</B>()
was standardized in POSIX.1-2008,
the argument type was specified as the type-safe
<I>const struct dirent&nbsp;**,</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">&nbsp;</A>
<H2>EXAMPLE</H2>
The program below prints a list of the files in the current directory
in reverse order.
<A NAME="lbAM">&nbsp;</A>
<H3>Program source</H3>
#define _DEFAULT_SOURCE
#include &lt;<A HREF="file:///usr/include/dirent.h">dirent.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
<P>
int
main(void)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;dirent&nbsp;**namelist;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;n;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;=&nbsp;scandir(&quot;.&quot;,&nbsp;&amp;namelist,&nbsp;NULL,&nbsp;alphasort);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(n&nbsp;==&nbsp;-1)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;perror(&quot;scandir&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(n--)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%s\n&quot;,&nbsp;namelist[n]-&gt;d_name);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;free(namelist[n]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<BR>&nbsp;&nbsp;&nbsp;&nbsp;free(namelist);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAN">&nbsp;</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">&nbsp;</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">&nbsp;</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>