1SCANDIR(3)                 Linux Programmer's Manual                SCANDIR(3)
2
3
4

NAME

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

SYNOPSIS

9       #include <dirent.h>
10
11       int scandir(const char *dir, 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       int versionsort(const void *a, const void *b);
17

DESCRIPTION

19       The  scandir()  function  scans  the directory dir, calling filter() on
20       each directory entry.  Entries for which filter() returns non-zero  are
21       stored in strings allocated via malloc(), sorted using qsort() with the
22       comparison function compar(), and collected in array namelist which  is
23       allocated via malloc().  If filter is NULL, all entries are selected.
24
25       The alphasort() and versionsort() functions can be used as the compari‐
26       son function compar().  The former sorts directory entries  using  str‐
27       coll(3), the latter using strverscmp(3) on the strings (*a)->d_name and
28       (*b)->d_name.
29

RETURN VALUE

31       The scandir() function returns the number of directory entries selected
32       or -1 if an error occurs.
33
34       The  alphasort()  and  versionsort()  functions  return an integer less
35       than, equal to, or greater than zero if the first argument  is  consid‐
36       ered  to  be respectively less than, equal to, or greater than the sec‐
37       ond.
38

ERRORS

40       ENOMEM Insufficient memory to complete the operation.
41

CONFORMING TO

43       None of these functions is in POSIX.1-2001, but alphasort()  and  scan‐
44       dir() are under consideration for a future revision to POSIX.1.
45
46       The  functions scandir() and alphasort() are from 4.3BSD, and have been
47       available under Linux since libc4.  Libc4 and libc5 use the  more  pre‐
48       cise prototype
49
50           int alphasort(const struct dirent ** a,
51                         const struct dirent **b);
52
53       but glibc 2.0 returns to the imprecise BSD prototype.
54
55       The  function  versionsort()  is a GNU extension, available since glibc
56       2.1.
57
58       Since glibc 2.1, alphasort() calls strcoll(3);  earlier  it  used  str‐
59       cmp(3).
60

EXAMPLE

62       /* print files in current directory in reverse order */
63       #include <dirent.h>
64       main(){
65           struct dirent **namelist;
66           int n;
67
68           n = scandir(".", &namelist, 0, alphasort);
69           if (n < 0)
70               perror("scandir");
71           else {
72               while(n--) {
73                   printf("%s\n", namelist[n]->d_name);
74                   free(namelist[n]);
75               }
76               free(namelist);
77           }
78       }
79

SEE ALSO

81       closedir(3),    fnmatch(3),   opendir(3),   readdir(3),   rewinddir(3),
82       seekdir(3), strcmp(3), strcoll(3), strverscmp(3), telldir(3)
83
84
85
86GNU                               2001-12-26                        SCANDIR(3)
Impressum