blob: 7dc873dd42d09996f8750562fe2d65e4100a52f2 [file] [log] [blame]
<html lang="en">
<head>
<title>Scanning Directory Content - The GNU C Library</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="The GNU C Library">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Accessing-Directories.html#Accessing-Directories" title="Accessing Directories">
<link rel="prev" href="Random-Access-Directory.html#Random-Access-Directory" title="Random Access Directory">
<link rel="next" href="Simple-Directory-Lister-Mark-II.html#Simple-Directory-Lister-Mark-II" title="Simple Directory Lister Mark II">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<!--
This file documents the GNU C library.
This is Edition 0.12, last updated 2007-10-27,
of `The GNU C Library Reference Manual', for version
2.8 (Sourcery G++ Lite 2011.03-41).
Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002,
2003, 2007, 2008, 2010 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being ``Free Software Needs Free Documentation''
and ``GNU Lesser General Public License'', the Front-Cover texts being
``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A
copy of the license is included in the section entitled "GNU Free
Documentation License".
(a) The FSF's Back-Cover Text is: ``You have the freedom to
copy and modify this GNU manual. Buying copies from the FSF
supports it in developing GNU and promoting software freedom.''-->
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
pre.display { font-family:inherit }
pre.format { font-family:inherit }
pre.smalldisplay { font-family:inherit; font-size:smaller }
pre.smallformat { font-family:inherit; font-size:smaller }
pre.smallexample { font-size:smaller }
pre.smalllisp { font-size:smaller }
span.sc { font-variant:small-caps }
span.roman { font-family:serif; font-weight:normal; }
span.sansserif { font-family:sans-serif; font-weight:normal; }
--></style>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="Scanning-Directory-Content"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Simple-Directory-Lister-Mark-II.html#Simple-Directory-Lister-Mark-II">Simple Directory Lister Mark II</a>,
Previous:&nbsp;<a rel="previous" accesskey="p" href="Random-Access-Directory.html#Random-Access-Directory">Random Access Directory</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Accessing-Directories.html#Accessing-Directories">Accessing Directories</a>
<hr>
</div>
<h4 class="subsection">14.2.6 Scanning the Content of a Directory</h4>
<p>A higher-level interface to the directory handling functions is the
<code>scandir</code> function. With its help one can select a subset of the
entries in a directory, possibly sort them and get a list of names as
the result.
<!-- dirent.h -->
<!-- BSD/SVID -->
<div class="defun">
&mdash; Function: int <b>scandir</b> (<var>const char *dir, struct dirent ***namelist, int </var>(<var>*selector</var>) (<var>const struct dirent *</var>)<var>, int </var>(<var>*cmp</var>) (<var>const void *, const void *</var>))<var><a name="index-scandir-1426"></a></var><br>
<blockquote>
<p>The <code>scandir</code> function scans the contents of the directory selected
by <var>dir</var>. The result in *<var>namelist</var> is an array of pointers to
structure of type <code>struct dirent</code> which describe all selected
directory entries and which is allocated using <code>malloc</code>. Instead
of always getting all directory entries returned, the user supplied
function <var>selector</var> can be used to decide which entries are in the
result. Only the entries for which <var>selector</var> returns a non-zero
value are selected.
<p>Finally the entries in *<var>namelist</var> are sorted using the
user-supplied function <var>cmp</var>. The arguments passed to the <var>cmp</var>
function are of type <code>struct dirent **</code>, therefore one cannot
directly use the <code>strcmp</code> or <code>strcoll</code> functions; instead see
the functions <code>alphasort</code> and <code>versionsort</code> below.
<p>The return value of the function is the number of entries placed in
*<var>namelist</var>. If it is <code>-1</code> an error occurred (either the
directory could not be opened for reading or the malloc call failed) and
the global variable <code>errno</code> contains more information on the error.
</p></blockquote></div>
<p>As described above the fourth argument to the <code>scandir</code> function
must be a pointer to a sorting function. For the convenience of the
programmer the GNU C library contains implementations of functions which
are very helpful for this purpose.
<!-- dirent.h -->
<!-- BSD/SVID -->
<div class="defun">
&mdash; Function: int <b>alphasort</b> (<var>const void *a, const void *b</var>)<var><a name="index-alphasort-1427"></a></var><br>
<blockquote><p>The <code>alphasort</code> function behaves like the <code>strcoll</code> function
(see <a href="String_002fArray-Comparison.html#String_002fArray-Comparison">String/Array Comparison</a>). The difference is that the arguments
are not string pointers but instead they are of type
<code>struct dirent **</code>.
<p>The return value of <code>alphasort</code> is less than, equal to, or greater
than zero depending on the order of the two entries <var>a</var> and <var>b</var>.
</p></blockquote></div>
<!-- dirent.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: int <b>versionsort</b> (<var>const void *a, const void *b</var>)<var><a name="index-versionsort-1428"></a></var><br>
<blockquote><p>The <code>versionsort</code> function is like <code>alphasort</code> except that it
uses the <code>strverscmp</code> function internally.
</p></blockquote></div>
<p>If the filesystem supports large files we cannot use the <code>scandir</code>
anymore since the <code>dirent</code> structure might not able to contain all
the information. The LFS provides the new type <code>struct&nbsp;dirent64</code><!-- /@w -->. To use this we need a new function.
<!-- dirent.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: int <b>scandir64</b> (<var>const char *dir, struct dirent64 ***namelist, int </var>(<var>*selector</var>) (<var>const struct dirent64 *</var>)<var>, int </var>(<var>*cmp</var>) (<var>const void *, const void *</var>))<var><a name="index-scandir64-1429"></a></var><br>
<blockquote><p>The <code>scandir64</code> function works like the <code>scandir</code> function
except that the directory entries it returns are described by elements
of type <code>struct&nbsp;dirent64</code><!-- /@w -->. The function pointed to by
<var>selector</var> is again used to select the desired entries, except that
<var>selector</var> now must point to a function which takes a
<code>struct&nbsp;dirent64&nbsp;*</code><!-- /@w --> parameter.
<p>Similarly the <var>cmp</var> function should expect its two arguments to be
of type <code>struct dirent64 **</code>.
</p></blockquote></div>
<p>As <var>cmp</var> is now a function of a different type, the functions
<code>alphasort</code> and <code>versionsort</code> cannot be supplied for that
argument. Instead we provide the two replacement functions below.
<!-- dirent.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: int <b>alphasort64</b> (<var>const void *a, const void *b</var>)<var><a name="index-alphasort64-1430"></a></var><br>
<blockquote><p>The <code>alphasort64</code> function behaves like the <code>strcoll</code> function
(see <a href="String_002fArray-Comparison.html#String_002fArray-Comparison">String/Array Comparison</a>). The difference is that the arguments
are not string pointers but instead they are of type
<code>struct dirent64 **</code>.
<p>Return value of <code>alphasort64</code> is less than, equal to, or greater
than zero depending on the order of the two entries <var>a</var> and <var>b</var>.
</p></blockquote></div>
<!-- dirent.h -->
<!-- GNU -->
<div class="defun">
&mdash; Function: int <b>versionsort64</b> (<var>const void *a, const void *b</var>)<var><a name="index-versionsort64-1431"></a></var><br>
<blockquote><p>The <code>versionsort64</code> function is like <code>alphasort64</code>, excepted that it
uses the <code>strverscmp</code> function internally.
</p></blockquote></div>
<p>It is important not to mix the use of <code>scandir</code> and the 64-bit
comparison functions or vice versa. There are systems on which this
works but on others it will fail miserably.
</body></html>