1SCANDIR(3) Linux Programmer's Manual SCANDIR(3)
2
3
4
6 scandir, scandirat, alphasort, versionsort - scan a directory for
7 matching entries
8
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
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
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
83 EBADF (scandirat()) dirp is relative but dirfd is neither AT_FDCWD nor
84 a valid file descriptor.
85
86 ENOENT The path in dirp does not exist.
87
88 ENOMEM Insufficient memory to complete the operation.
89
90 ENOTDIR
91 The path in dirp is not a directory.
92
93 ENOTDIR
94 (scandirat()) dirp is a relative pathname and dirfd is a file
95 descriptor referring to a file other than a directory.
96
98 versionsort() was added to glibc in version 2.1.
99
100 scandirat() was added to glibc in version 2.15.
101
103 For an explanation of the terms used in this section, see at‐
104 tributes(7).
105
106 ┌─────────────────────────────────────┬───────────────┬────────────────┐
107 │Interface │ Attribute │ Value │
108 ├─────────────────────────────────────┼───────────────┼────────────────┤
109 │scandir(), scandirat() │ Thread safety │ MT-Safe │
110 ├─────────────────────────────────────┼───────────────┼────────────────┤
111 │alphasort(), versionsort() │ Thread safety │ MT-Safe locale │
112 └─────────────────────────────────────┴───────────────┴────────────────┘
113
115 alphasort(), scandir(): 4.3BSD, POSIX.1-2008.
116
117 versionsort() and scandirat() are GNU extensions.
118
120 Since glibc 2.1, alphasort() calls strcoll(3); earlier it used str‐
121 cmp(3).
122
123 Before glibc 2.10, the two arguments of alphasort() and versionsort()
124 were typed as const void *. When alphasort() was standardized in
125 POSIX.1-2008, the argument type was specified as the type-safe const
126 struct dirent **, and glibc 2.10 changed the definition of alphasort()
127 (and the nonstandard versionsort()) to match the standard.
128
130 The program below prints a list of the files in the current directory
131 in reverse order.
132
133 Program source
134
135 #define _DEFAULT_SOURCE
136 #include <dirent.h>
137 #include <stdio.h>
138 #include <stdlib.h>
139
140 int
141 main(void)
142 {
143 struct dirent **namelist;
144 int n;
145
146 n = scandir(".", &namelist, NULL, alphasort);
147 if (n == -1) {
148 perror("scandir");
149 exit(EXIT_FAILURE);
150 }
151
152 while (n--) {
153 printf("%s\n", namelist[n]->d_name);
154 free(namelist[n]);
155 }
156 free(namelist);
157
158 exit(EXIT_SUCCESS);
159 }
160
162 closedir(3), fnmatch(3), opendir(3), readdir(3), rewinddir(3),
163 seekdir(3), strcmp(3), strcoll(3), strverscmp(3), telldir(3)
164
166 This page is part of release 5.13 of the Linux man-pages project. A
167 description of the project, information about reporting bugs, and the
168 latest version of this page, can be found at
169 https://www.kernel.org/doc/man-pages/.
170
171
172
173GNU 2021-08-27 SCANDIR(3)