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

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

VERSIONS

95       versionsort() was added to glibc in version 2.1.
96
97       scandirat() was added to glibc in version 2.15.
98

ATTRIBUTES

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

CONFORMING TO

112       alphasort(), scandir(): 4.3BSD, POSIX.1-2008.
113
114       versionsort() and scandirat() are GNU extensions.
115

NOTES

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

EXAMPLES

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

SEE ALSO

159       closedir(3),    fnmatch(3),   opendir(3),   readdir(3),   rewinddir(3),
160       seekdir(3), strcmp(3), strcoll(3), strverscmp(3), telldir(3)
161

COLOPHON

163       This page is part of release 5.10 of the Linux  man-pages  project.   A
164       description  of  the project, information about reporting bugs, and the
165       latest    version    of    this    page,    can     be     found     at
166       https://www.kernel.org/doc/man-pages/.
167
168
169
170GNU                               2020-06-09                        SCANDIR(3)
Impressum