Cygwin: access: Fix X_OK behaviour for backup operators and admins
[newlib-cygwin.git] / newlib / libc / sys / sysvi386 / scandir.c
blobb6ba1a906680f0b851cfdefab75ae4d5fece6094
1 /*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)scandir.c 5.10 (Berkeley) 2/23/91";
32 #endif /* LIBC_SCCS and not lint */
35 * Scan the directory dirname calling select to make a list of selected
36 * directory entries then sort using qsort and compare routine dcomp.
37 * Returns the number of entries and a pointer to a list of pointers to
38 * struct dirent (through namelist). Returns -1 if there were any errors.
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <dirent.h>
44 #include <stdlib.h>
45 #include <string.h>
48 * The DIRSIZ macro gives the minimum record length which will hold
49 * the directory entry. This requires the amount of space in struct dirent
50 * without the d_name field, plus enough space for the name with a terminating
51 * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
53 #undef DIRSIZ
54 #define DIRSIZ(dp) \
55 ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
57 int
58 scandir(dirname, namelist, select, dcomp)
59 const char *dirname;
60 struct dirent ***namelist;
61 int (*select) __P((struct dirent *));
62 int (*dcomp) __P((const void *, const void *));
64 register struct dirent *d, *p, **names;
65 register size_t nitems;
66 struct stat stb;
67 long arraysz;
68 DIR *dirp;
70 if ((dirp = opendir(dirname)) == NULL)
71 return(-1);
72 if (fstat(dirp->dd_fd, &stb) < 0)
73 return(-1);
76 * estimate the array size by taking the size of the directory file
77 * and dividing it by a multiple of the minimum size entry.
79 arraysz = (stb.st_size / 24);
80 names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
81 if (names == NULL)
82 return(-1);
84 nitems = 0;
85 while ((d = readdir(dirp)) != NULL) {
86 if (select != NULL && !(*select)(d))
87 continue; /* just selected names */
89 * Make a minimum size copy of the data
91 p = (struct dirent *)malloc(DIRSIZ(d));
92 if (p == NULL)
93 return(-1);
94 p->d_ino = d->d_ino;
95 p->d_reclen = d->d_reclen;
96 p->d_namlen = d->d_namlen;
97 bcopy(d->d_name, p->d_name, p->d_namlen + 1);
99 * Check to make sure the array has space left and
100 * realloc the maximum size.
102 if (++nitems >= arraysz) {
103 if (fstat(dirp->dd_fd, &stb) < 0)
104 return(-1); /* just might have grown */
105 arraysz = stb.st_size / 12;
106 names = (struct dirent **)realloc((char *)names,
107 arraysz * sizeof(struct dirent *));
108 if (names == NULL)
109 return(-1);
111 names[nitems-1] = p;
113 closedir(dirp);
114 if (nitems && dcomp != NULL)
115 qsort(names, nitems, sizeof(struct dirent *), dcomp);
116 *namelist = names;
117 return(nitems);
121 * Alphabetic order comparison routine for those who want it.
124 alphasort(d1, d2)
125 const void *d1;
126 const void *d2;
128 return(strcmp((*(struct dirent **)d1)->d_name,
129 (*(struct dirent **)d2)->d_name));