1SCANDIR(3)                 Linux Programmer's Manual                SCANDIR(3)
2
3
4

NAME

6       scandir, alphasort, versionsort - scan a directory for matching entries
7

SYNOPSIS

9       #include <dirent.h>
10
11       int scandir(const char *dirp, struct dirent ***namelist,
12              int (*filter)(const struct dirent *),
13              int (*compar)(const struct dirent **, const struct dirent **));
14
15       int alphasort(const void *a, const void *b);
16
17       int versionsort(const void *a, const void *b);
18
19   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21       scandir(), alphasort(): _BSD_SOURCE || _SVID_SOURCE
22       versionsort(): _GNU_SOURCE
23

DESCRIPTION

25       The  scandir()  function  scans the directory dirp, calling filter() on
26       each directory entry.  Entries for which filter() returns  nonzero  are
27       stored  in  strings allocated via malloc(3), sorted using qsort(3) with
28       the comparison function compar(), and collected in array namelist which
29       is  allocated  via  malloc(3).   If  filter  is  NULL,  all entries are
30       selected.
31
32       The alphasort() and versionsort() functions can be used as the compari‐
33       son  function  compar().  The former sorts directory entries using str‐
34       coll(3), the latter using strverscmp(3) on the strings (*a)->d_name and
35       (*b)->d_name.
36

RETURN VALUE

38       The   scandir()  function  returns  the  number  of  directory  entries
39       selected.  On error, -1 is returned, with errno  set  to  indicate  the
40       cause of the error.
41
42       The  alphasort()  and  versionsort()  functions  return an integer less
43       than, equal to, or greater than zero if the first argument  is  consid‐
44       ered  to  be respectively less than, equal to, or greater than the sec‐
45       ond.
46

ERRORS

48       ENOENT The path in dirp does not exist.
49
50       ENOMEM Insufficient memory to complete the operation.
51
52       ENOTDIR
53              The path in dirp is not a directory.
54

VERSIONS

56       versionsort() was added to glibc in version 2.1.
57

CONFORMING TO

59       alphasort() and scandir() are specified in POSIX.1-2008, and are widely
60       available.  versionsort() is a GNU extension.
61
62       The  functions scandir() and alphasort() are from 4.3BSD, and have been
63       available under Linux since libc4.  Libc4 and libc5 use the  more  pre‐
64       cise prototype
65
66           int alphasort(const struct dirent ** a,
67                         const struct dirent **b);
68
69       but glibc 2.0 returns to the imprecise BSD prototype.
70
71       The  function  versionsort()  is a GNU extension, available since glibc
72       2.1.
73
74       Since glibc 2.1, alphasort() calls strcoll(3);  earlier  it  used  str‐
75       cmp(3).
76

EXAMPLE

78       #define _SVID_SOURCE
79       /* print files in current directory in reverse order */
80       #include <dirent.h>
81
82       int
83       main(void)
84       {
85           struct dirent **namelist;
86           int n;
87
88           n = scandir(".", &namelist, NULL, alphasort);
89           if (n < 0)
90               perror("scandir");
91           else {
92               while (n--) {
93                   printf("%s\n", namelist[n]->d_name);
94                   free(namelist[n]);
95               }
96               free(namelist);
97           }
98       }
99

SEE ALSO

101       closedir(3),  fnmatch(3), opendir(3), readdir(3), rewinddir(3), scandi‐
102       rat(3), seekdir(3), strcmp(3), strcoll(3), strverscmp(3), telldir(3)
103

COLOPHON

105       This page is part of release 3.53 of the Linux  man-pages  project.   A
106       description  of  the project, and information about reporting bugs, can
107       be found at http://www.kernel.org/doc/man-pages/.
108
109
110
111GNU                               2013-04-19                        SCANDIR(3)
Impressum