1SCANDIR(3) Linux Programmer's Manual SCANDIR(3)
2
3
4
6 scandir, alphasort, versionsort - scan a directory for matching entries
7
9 #include <dirent.h>
10
11 int scandir(const char *dirp, struct dirent ***namelist,
12 int (*filter)(const struct dirent *),
13 int (*compar)(const struct dirent **, const struct dirent **));
14
15 int alphasort(const void *a, const void *b);
16
17 int versionsort(const void *a, const void *b);
18
19 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21 scandir(), alphasort(): _BSD_SOURCE || _SVID_SOURCE
22 versionsort(): _GNU_SOURCE
23
25 The scandir() function scans the directory dirp, calling filter() on
26 each directory entry. Entries for which filter() returns nonzero are
27 stored in strings allocated via malloc(3), sorted using qsort(3) with
28 the comparison function compar(), and collected in array namelist which
29 is allocated via malloc(3). If filter is NULL, all entries are
30 selected.
31
32 The alphasort() and versionsort() functions can be used as the compari‐
33 son function compar(). The former sorts directory entries using str‐
34 coll(3), the latter using strverscmp(3) on the strings (*a)->d_name and
35 (*b)->d_name.
36
38 The scandir() function returns the number of directory entries selected
39 or -1 if an error occurs.
40
41 The alphasort() and versionsort() functions return an integer less
42 than, equal to, or greater than zero if the first argument is consid‐
43 ered to be respectively less than, equal to, or greater than the sec‐
44 ond.
45
47 ENOMEM Insufficient memory to complete the operation.
48
50 versionsort() was added to glibc in version 2.1.
51
53 alphasort() and scandir() are specified in POSIX.1-2008, and are widely
54 available. versionsort() is a GNU extension.
55
56 The functions scandir() and alphasort() are from 4.3BSD, and have been
57 available under Linux since libc4. Libc4 and libc5 use the more pre‐
58 cise prototype
59
60 int alphasort(const struct dirent ** a,
61 const struct dirent **b);
62
63 but glibc 2.0 returns to the imprecise BSD prototype.
64
65 The function versionsort() is a GNU extension, available since glibc
66 2.1.
67
68 Since glibc 2.1, alphasort() calls strcoll(3); earlier it used str‐
69 cmp(3).
70
72 #define _SVID_SOURCE
73 /* print files in current directory in reverse order */
74 #include <dirent.h>
75
76 int
77 main(void)
78 {
79 struct dirent **namelist;
80 int n;
81
82 n = scandir(".", &namelist, 0, alphasort);
83 if (n < 0)
84 perror("scandir");
85 else {
86 while (n--) {
87 printf("%s\n", namelist[n]->d_name);
88 free(namelist[n]);
89 }
90 free(namelist);
91 }
92 }
93
95 closedir(3), fnmatch(3), opendir(3), readdir(3), rewinddir(3),
96 seekdir(3), strcmp(3), strcoll(3), strverscmp(3), telldir(3)
97
99 This page is part of release 3.25 of the Linux man-pages project. A
100 description of the project, and information about reporting bugs, can
101 be found at http://www.kernel.org/doc/man-pages/.
102
103
104
105GNU 2009-02-10 SCANDIR(3)