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, struct dirent ***namelist,
24              int (*filter)(const struct dirent *),
25              int (*compar)(const struct dirent **, const struct dirent **));
26
27   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
28
29       scandir(), alphasort():
30           /* Since glibc 2.10: */ _POSIX_C_SOURCE >= 200809L
31               || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
32
33       versionsort(): _GNU_SOURCE
34
35       scandirat(): _GNU_SOURCE
36

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

VERSIONS

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

ATTRIBUTES

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

CONFORMING TO

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

NOTES

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

EXAMPLE

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

SEE ALSO

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

COLOPHON

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