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

NAME

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

SYNOPSIS

10       #include <dirent.h>
11
12       int scandir(const char *restrict dirp,
13                   struct dirent ***restrict namelist,
14                   int (*filter)(const struct dirent *),
15                   int (*compar)(const struct dirent **,
16                                 const struct dirent **));
17
18       int alphasort(const struct dirent **a, const struct dirent **b);
19       int versionsort(const struct dirent **a, const struct dirent **b);
20
21       #include <fcntl.h>          /* Definition of AT_* constants */
22       #include <dirent.h>
23
24       int scandirat(int dirfd, const char *restrict dirp,
25                   struct dirent ***restrict namelist,
26                   int (*filter)(const struct dirent *),
27                   int (*compar)(const struct dirent **,
28                                 const struct dirent **));
29
30   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
31
32       scandir(), alphasort():
33           /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200809L
34               || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
35
36       versionsort():
37           _GNU_SOURCE
38
39       scandirat():
40           _GNU_SOURCE
41

DESCRIPTION

43       The scandir() function scans the directory dirp,  calling  filter()  on
44       each  directory  entry.  Entries for which filter() returns nonzero are
45       stored in strings allocated via malloc(3), sorted using  qsort(3)  with
46       the comparison function compar(), and collected in array namelist which
47       is allocated via malloc(3).  If filter is NULL,  all  entries  are  se‐
48       lected.
49
50       The alphasort() and versionsort() functions can be used as the compari‐
51       son function compar().  The former sorts directory entries  using  str‐
52       coll(3), the latter using strverscmp(3) on the strings (*a)->d_name and
53       (*b)->d_name.
54
55   scandirat()
56       The scandirat() function operates in exactly the same way as scandir(),
57       except for the differences described here.
58
59       If the pathname given in dirp is relative, then it is interpreted rela‐
60       tive to the directory referred to by the file descriptor dirfd  (rather
61       than  relative to the current working directory of the calling process,
62       as is done by scandir() for a relative pathname).
63
64       If dirp is relative and dirfd is the special value AT_FDCWD, then  dirp
65       is interpreted relative to the current working directory of the calling
66       process (like scandir()).
67
68       If dirp is absolute, then dirfd is ignored.
69
70       See openat(2) for an explanation of the need for scandirat().
71

RETURN VALUE

73       The scandir() function returns the  number  of  directory  entries  se‐
74       lected.   On  error, -1 is returned, with errno set to indicate the er‐
75       ror.
76
77       The alphasort() and versionsort()  functions  return  an  integer  less
78       than,  equal  to, or greater than zero if the first argument is consid‐
79       ered to be respectively less than, equal to, or greater than  the  sec‐
80       ond.
81

ERRORS

83       ENOENT The path in dirp does not exist.
84
85       ENOMEM Insufficient memory to complete the operation.
86
87       ENOTDIR
88              The path in dirp is not a directory.
89
90       The following additional errors can occur for scandirat():
91
92       EBADF  dirfd is not a valid file descriptor.
93
94       ENOTDIR
95              dirp is a relative path and dirfd is a file descriptor referring
96              to a file other than a directory.
97

VERSIONS

99       versionsort() was added to glibc in version 2.1.
100
101       scandirat() was added to glibc in version 2.15.
102

ATTRIBUTES

104       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
105       tributes(7).
106
107       ┌─────────────────────────────────────┬───────────────┬────────────────┐
108Interface                            Attribute     Value          
109       ├─────────────────────────────────────┼───────────────┼────────────────┤
110scandir(), scandirat()               │ Thread safety │ MT-Safe        │
111       ├─────────────────────────────────────┼───────────────┼────────────────┤
112alphasort(), versionsort()           │ Thread safety │ MT-Safe locale │
113       └─────────────────────────────────────┴───────────────┴────────────────┘
114

CONFORMING TO

116       alphasort(), scandir(): 4.3BSD, POSIX.1-2008.
117
118       versionsort() and scandirat() are GNU extensions.
119

NOTES

121       Since  glibc  2.1,  alphasort()  calls strcoll(3); earlier it used str‐
122       cmp(3).
123
124       Before glibc 2.10, the two arguments of alphasort()  and  versionsort()
125       were  typed  as  const  void *.   When  alphasort() was standardized in
126       POSIX.1-2008, the argument type was specified as  the  type-safe  const
127       struct  dirent **, and glibc 2.10 changed the definition of alphasort()
128       (and the nonstandard versionsort()) to match the standard.
129

EXAMPLES

131       The program below prints a list of the files in the  current  directory
132       in reverse order.
133
134   Program source
135
136       #define _DEFAULT_SOURCE
137       #include <dirent.h>
138       #include <stdio.h>
139       #include <stdlib.h>
140
141       int
142       main(void)
143       {
144           struct dirent **namelist;
145           int n;
146
147           n = scandir(".", &namelist, NULL, alphasort);
148           if (n == -1) {
149               perror("scandir");
150               exit(EXIT_FAILURE);
151           }
152
153           while (n--) {
154               printf("%s\n", namelist[n]->d_name);
155               free(namelist[n]);
156           }
157           free(namelist);
158
159           exit(EXIT_SUCCESS);
160       }
161

SEE ALSO

163       closedir(3),    fnmatch(3),   opendir(3),   readdir(3),   rewinddir(3),
164       seekdir(3), strcmp(3), strcoll(3), strverscmp(3), telldir(3)
165

COLOPHON

167       This page is part of release 5.12 of the Linux  man-pages  project.   A
168       description  of  the project, information about reporting bugs, and the
169       latest    version    of    this    page,    can     be     found     at
170       https://www.kernel.org/doc/man-pages/.
171
172
173
174GNU                               2021-03-22                        SCANDIR(3)
Impressum