1scandir(3)                 Library Functions Manual                 scandir(3)
2
3
4

NAME

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

LIBRARY

10       Standard C library (libc, -lc)
11

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

86       EBADF  (scandirat()) dirp is relative but dirfd is neither AT_FDCWD nor
87              a valid file descriptor.
88
89       ENOENT The path in dirp does not exist.
90
91       ENOMEM Insufficient memory to complete the operation.
92
93       ENOTDIR
94              The path in dirp is not a directory.
95
96       ENOTDIR
97              (scandirat()) dirp is a relative pathname and dirfd  is  a  file
98              descriptor referring to a file other than a directory.
99

ATTRIBUTES

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

STANDARDS

113       alphasort()
114       scandir()
115              POSIX.1-2008.
116
117       versionsort()
118       scandirat()
119              GNU.
120

HISTORY

122       alphasort()
123       scandir()
124              4.3BSD, POSIX.1-2008.
125
126       versionsort()
127              glibc 2.1.
128
129       scandirat()
130              glibc 2.15.
131

NOTES

133       Since glibc 2.1, alphasort() calls strcoll(3);  earlier  it  used  str‐
134       cmp(3).
135
136       Before  glibc  2.10, the two arguments of alphasort() and versionsort()
137       were typed as const  void *.   When  alphasort()  was  standardized  in
138       POSIX.1-2008,  the  argument  type was specified as the type-safe const
139       struct dirent **, and glibc 2.10 changed the definition of  alphasort()
140       (and the nonstandard versionsort()) to match the standard.
141

EXAMPLES

143       The  program  below prints a list of the files in the current directory
144       in reverse order.
145
146   Program source
147
148       #define _DEFAULT_SOURCE
149       #include <dirent.h>
150       #include <stdio.h>
151       #include <stdlib.h>
152
153       int
154       main(void)
155       {
156           struct dirent **namelist;
157           int n;
158
159           n = scandir(".", &namelist, NULL, alphasort);
160           if (n == -1) {
161               perror("scandir");
162               exit(EXIT_FAILURE);
163           }
164
165           while (n--) {
166               printf("%s\n", namelist[n]->d_name);
167               free(namelist[n]);
168           }
169           free(namelist);
170
171           exit(EXIT_SUCCESS);
172       }
173

SEE ALSO

175       closedir(3),   fnmatch(3),   opendir(3),   readdir(3),    rewinddir(3),
176       seekdir(3), strcmp(3), strcoll(3), strverscmp(3), telldir(3)
177
178
179
180Linux man-pages 6.04              2023-03-30                        scandir(3)
Impressum