| 1 |
207 |
jeremybenn |
#ifndef HAVE_OPENDIR
|
| 2 |
|
|
|
| 3 |
|
|
/*
|
| 4 |
|
|
* Copyright (c) 1983 Regents of the University of California.
|
| 5 |
|
|
* All rights reserved.
|
| 6 |
|
|
*
|
| 7 |
|
|
* Redistribution and use in source and binary forms, with or without
|
| 8 |
|
|
* modification, are permitted provided that the following conditions
|
| 9 |
|
|
* are met:
|
| 10 |
|
|
* 1. Redistributions of source code must retain the above copyright
|
| 11 |
|
|
* notice, this list of conditions and the following disclaimer.
|
| 12 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
| 13 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
| 14 |
|
|
* documentation and/or other materials provided with the distribution.
|
| 15 |
|
|
* 4. Neither the name of the University nor the names of its contributors
|
| 16 |
|
|
* may be used to endorse or promote products derived from this software
|
| 17 |
|
|
* without specific prior written permission.
|
| 18 |
|
|
*
|
| 19 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
| 20 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 21 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
| 22 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
| 23 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 24 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
| 25 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
| 26 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
| 27 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
| 28 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
| 29 |
|
|
* SUCH DAMAGE.
|
| 30 |
|
|
*/
|
| 31 |
|
|
|
| 32 |
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
| 33 |
|
|
static char sccsid[] = "@(#)scandir.c 5.10 (Berkeley) 2/23/91";
|
| 34 |
|
|
#endif /* LIBC_SCCS and not lint */
|
| 35 |
|
|
|
| 36 |
|
|
/*
|
| 37 |
|
|
* Scan the directory dirname calling select to make a list of selected
|
| 38 |
|
|
* directory entries then sort using qsort and compare routine dcomp.
|
| 39 |
|
|
* Returns the number of entries and a pointer to a list of pointers to
|
| 40 |
|
|
* struct dirent (through namelist). Returns -1 if there were any errors.
|
| 41 |
|
|
*/
|
| 42 |
|
|
|
| 43 |
|
|
#include <sys/types.h>
|
| 44 |
|
|
#include <sys/stat.h>
|
| 45 |
|
|
#include <dirent.h>
|
| 46 |
|
|
#include <stdlib.h>
|
| 47 |
|
|
#include <string.h>
|
| 48 |
|
|
#include <sys/lock.h>
|
| 49 |
|
|
|
| 50 |
|
|
/*
|
| 51 |
|
|
* The DIRSIZ macro gives the minimum record length which will hold
|
| 52 |
|
|
* the directory entry. This requires the amount of space in struct dirent
|
| 53 |
|
|
* without the d_name field, plus enough space for the name with a terminating
|
| 54 |
|
|
* null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
|
| 55 |
|
|
*/
|
| 56 |
|
|
#undef DIRSIZ
|
| 57 |
|
|
#ifdef _DIRENT_HAVE_D_NAMLEN
|
| 58 |
|
|
#define DIRSIZ(dp) \
|
| 59 |
|
|
((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
|
| 60 |
|
|
#else
|
| 61 |
|
|
#define DIRSIZ(dp) \
|
| 62 |
|
|
((sizeof (struct dirent) - (MAXNAMLEN+1)) + ((strlen((dp)->d_name)+1 + 3) &~ 3))
|
| 63 |
|
|
#endif
|
| 64 |
|
|
|
| 65 |
|
|
#ifndef __P
|
| 66 |
|
|
#define __P(args) ()
|
| 67 |
|
|
#endif
|
| 68 |
|
|
|
| 69 |
|
|
int
|
| 70 |
|
|
_DEFUN(scandir, (dirname, namelist, select, dcomp),
|
| 71 |
|
|
const char *dirname _AND
|
| 72 |
|
|
struct dirent ***namelist _AND
|
| 73 |
|
|
int (*select) __P((const struct dirent *)) _AND
|
| 74 |
|
|
int (*dcomp) __P((const struct dirent **, const struct dirent **)))
|
| 75 |
|
|
{
|
| 76 |
|
|
register struct dirent *d, *p, **names;
|
| 77 |
|
|
register size_t nitems;
|
| 78 |
|
|
struct stat stb;
|
| 79 |
|
|
long arraysz;
|
| 80 |
|
|
DIR *dirp;
|
| 81 |
|
|
int successful = 0;
|
| 82 |
|
|
int rc = 0;
|
| 83 |
|
|
|
| 84 |
|
|
dirp = NULL;
|
| 85 |
|
|
names = NULL;
|
| 86 |
|
|
|
| 87 |
|
|
if ((dirp = opendir(dirname)) == NULL)
|
| 88 |
|
|
return(-1);
|
| 89 |
|
|
#ifdef HAVE_DD_LOCK
|
| 90 |
|
|
__lock_acquire_recursive(dirp->dd_lock);
|
| 91 |
|
|
#endif
|
| 92 |
|
|
if (fstat(dirp->dd_fd, &stb) < 0)
|
| 93 |
|
|
goto cleanup;
|
| 94 |
|
|
|
| 95 |
|
|
/*
|
| 96 |
|
|
* If there were no directory entries, then bail.
|
| 97 |
|
|
*/
|
| 98 |
|
|
if (stb.st_size == 0)
|
| 99 |
|
|
goto cleanup;
|
| 100 |
|
|
|
| 101 |
|
|
/*
|
| 102 |
|
|
* estimate the array size by taking the size of the directory file
|
| 103 |
|
|
* and dividing it by a multiple of the minimum size entry.
|
| 104 |
|
|
*/
|
| 105 |
|
|
arraysz = (stb.st_size / 24);
|
| 106 |
|
|
names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
|
| 107 |
|
|
if (names == NULL)
|
| 108 |
|
|
goto cleanup;
|
| 109 |
|
|
|
| 110 |
|
|
nitems = 0;
|
| 111 |
|
|
while ((d = readdir(dirp)) != NULL) {
|
| 112 |
|
|
if (select != NULL && !(*select)(d))
|
| 113 |
|
|
continue; /* just selected names */
|
| 114 |
|
|
/*
|
| 115 |
|
|
* Make a minimum size copy of the data
|
| 116 |
|
|
*/
|
| 117 |
|
|
p = (struct dirent *)malloc(DIRSIZ(d));
|
| 118 |
|
|
if (p == NULL)
|
| 119 |
|
|
goto cleanup;
|
| 120 |
|
|
p->d_ino = d->d_ino;
|
| 121 |
|
|
p->d_reclen = d->d_reclen;
|
| 122 |
|
|
#ifdef _DIRENT_HAVE_D_NAMLEN
|
| 123 |
|
|
p->d_namlen = d->d_namlen;
|
| 124 |
|
|
bcopy(d->d_name, p->d_name, p->d_namlen + 1);
|
| 125 |
|
|
#else
|
| 126 |
|
|
strcpy(p->d_name, d->d_name);
|
| 127 |
|
|
#endif
|
| 128 |
|
|
/*
|
| 129 |
|
|
* Check to make sure the array has space left and
|
| 130 |
|
|
* realloc the maximum size.
|
| 131 |
|
|
*/
|
| 132 |
|
|
if (++nitems >= arraysz) {
|
| 133 |
|
|
if (fstat(dirp->dd_fd, &stb) < 0)
|
| 134 |
|
|
goto cleanup;
|
| 135 |
|
|
arraysz = stb.st_size / 12;
|
| 136 |
|
|
names = (struct dirent **)reallocf((char *)names,
|
| 137 |
|
|
arraysz * sizeof(struct dirent *));
|
| 138 |
|
|
if (names == NULL)
|
| 139 |
|
|
goto cleanup;
|
| 140 |
|
|
}
|
| 141 |
|
|
names[nitems-1] = p;
|
| 142 |
|
|
}
|
| 143 |
|
|
successful = 1;
|
| 144 |
|
|
cleanup:
|
| 145 |
|
|
closedir(dirp);
|
| 146 |
|
|
if (successful) {
|
| 147 |
|
|
if (nitems && dcomp != NULL)
|
| 148 |
|
|
qsort(names, nitems, sizeof(struct dirent *), (void *)dcomp);
|
| 149 |
|
|
*namelist = names;
|
| 150 |
|
|
rc = nitems;
|
| 151 |
|
|
} else { /* We were unsuccessful, clean up storage and return -1. */
|
| 152 |
|
|
if ( names ) {
|
| 153 |
|
|
int i;
|
| 154 |
|
|
for (i=0; i < nitems; i++ )
|
| 155 |
|
|
free( names[i] );
|
| 156 |
|
|
free( names );
|
| 157 |
|
|
}
|
| 158 |
|
|
rc = -1;
|
| 159 |
|
|
}
|
| 160 |
|
|
|
| 161 |
|
|
#ifdef HAVE_DD_LOCK
|
| 162 |
|
|
__lock_release_recursive(dirp->dd_lock);
|
| 163 |
|
|
#endif
|
| 164 |
|
|
return(rc);
|
| 165 |
|
|
}
|
| 166 |
|
|
|
| 167 |
|
|
/*
|
| 168 |
|
|
* Alphabetic order comparison routine for those who want it.
|
| 169 |
|
|
*/
|
| 170 |
|
|
int
|
| 171 |
|
|
_DEFUN(alphasort, (d1, d2),
|
| 172 |
|
|
const struct dirent **d1 _AND
|
| 173 |
|
|
const struct dirent **d2)
|
| 174 |
|
|
{
|
| 175 |
|
|
return(strcmp((*d1)->d_name, (*d2)->d_name));
|
| 176 |
|
|
}
|
| 177 |
|
|
|
| 178 |
|
|
#endif /* ! HAVE_OPENDIR */
|