|
| 1 | +#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 <stddef.h> |
| 46 | +#include <dirent.h> |
| 47 | +#include <stdlib.h> |
| 48 | +#include <string.h> |
| 49 | +#include <sys/lock.h> |
| 50 | + |
| 51 | +/* |
| 52 | + * The DIRSIZ macro gives the minimum record length which will hold |
| 53 | + * the directory entry. This requires the amount of space in struct dirent |
| 54 | + * without the d_name field, plus enough space for the name with a terminating |
| 55 | + * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary. |
| 56 | + */ |
| 57 | +#undef DIRSIZ |
| 58 | +#ifdef _DIRENT_HAVE_D_NAMLEN |
| 59 | +#define DIRSIZ(dp) \ |
| 60 | + (offsetof (struct dirent, d_name) + (((dp)->d_namlen+1 + 3) &~ 3)) |
| 61 | +#else |
| 62 | +#define DIRSIZ(dp) \ |
| 63 | + (offsetof (struct dirent, d_name) + ((strlen((dp)->d_name)+1 + 3) &~ 3)) |
| 64 | +#endif |
| 65 | + |
| 66 | +#ifndef __P |
| 67 | +#define __P(args) () |
| 68 | +#endif |
| 69 | + |
| 70 | +int |
| 71 | +_DEFUN(scandir, (dirname, namelist, select, dcomp), |
| 72 | + const char *dirname _AND |
| 73 | + struct dirent ***namelist _AND |
| 74 | + int (*select) __P((const struct dirent *)) _AND |
| 75 | + int (*dcomp) __P((const struct dirent **, const struct dirent **))) |
| 76 | +{ |
| 77 | + register struct dirent *d, *p, **names; |
| 78 | + register size_t nitems; |
| 79 | + struct stat stb; |
| 80 | + long arraysz; |
| 81 | + DIR *dirp; |
| 82 | + int successful = 0; |
| 83 | + int rc = 0; |
| 84 | + |
| 85 | + dirp = NULL; |
| 86 | + names = NULL; |
| 87 | + |
| 88 | + if ((dirp = opendir(dirname)) == NULL) |
| 89 | + return(-1); |
| 90 | + /* |
| 91 | +#ifdef HAVE_DD_LOCK |
| 92 | + __lock_acquire_recursive(dirp->dd_lock); |
| 93 | +#endif |
| 94 | + if (fstat(dirp->dd_fd, &stb) < 0) |
| 95 | + goto cleanup; |
| 96 | +*/ |
| 97 | + /* |
| 98 | + * If there were no directory entries, then bail. |
| 99 | + */ |
| 100 | + if (stb.st_size == 0) |
| 101 | + goto cleanup; |
| 102 | + |
| 103 | + /* |
| 104 | + * estimate the array size by taking the size of the directory file |
| 105 | + * and dividing it by a multiple of the minimum size entry. |
| 106 | + */ |
| 107 | + arraysz = (stb.st_size / 24); |
| 108 | + names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *)); |
| 109 | + if (names == NULL) |
| 110 | + goto cleanup; |
| 111 | + |
| 112 | + nitems = 0; |
| 113 | + while ((d = readdir(dirp)) != NULL) { |
| 114 | + if (select != NULL && !(*select)(d)) |
| 115 | + continue; /* just selected names */ |
| 116 | + /* |
| 117 | + * Make a minimum size copy of the data |
| 118 | + */ |
| 119 | + p = (struct dirent *)malloc(DIRSIZ(d)); |
| 120 | + if (p == NULL) |
| 121 | + goto cleanup; |
| 122 | + p->d_ino = d->d_ino; |
| 123 | + //p->d_reclen = d->d_reclen; |
| 124 | +#ifdef _DIRENT_HAVE_D_NAMLEN |
| 125 | + p->d_namlen = d->d_namlen; |
| 126 | + bcopy(d->d_name, p->d_name, p->d_namlen + 1); |
| 127 | +#else |
| 128 | + strcpy(p->d_name, d->d_name); |
| 129 | +#endif |
| 130 | + /* |
| 131 | + * Check to make sure the array has space left and |
| 132 | + * realloc the maximum size. |
| 133 | + */ |
| 134 | + if (++nitems >= arraysz) { |
| 135 | + //if (fstat(dirp->dd_fd, &stb) < 0) |
| 136 | + // goto cleanup; |
| 137 | + arraysz = stb.st_size / 12; |
| 138 | + names = (struct dirent **)reallocf((char *)names, |
| 139 | + arraysz * sizeof(struct dirent *)); |
| 140 | + if (names == NULL) |
| 141 | + goto cleanup; |
| 142 | + } |
| 143 | + names[nitems-1] = p; |
| 144 | + } |
| 145 | + successful = 1; |
| 146 | +cleanup: |
| 147 | + closedir(dirp); |
| 148 | + if (successful) { |
| 149 | + if (nitems && dcomp != NULL) |
| 150 | + qsort(names, nitems, sizeof(struct dirent *), (void *)dcomp); |
| 151 | + *namelist = names; |
| 152 | + rc = nitems; |
| 153 | + } else { /* We were unsuccessful, clean up storage and return -1. */ |
| 154 | + if ( names ) { |
| 155 | + int i; |
| 156 | + for (i=0; i < nitems; i++ ) |
| 157 | + free( names[i] ); |
| 158 | + free( names ); |
| 159 | + } |
| 160 | + rc = -1; |
| 161 | + } |
| 162 | + |
| 163 | +#ifdef HAVE_DD_LOCK |
| 164 | + __lock_release_recursive(dirp->dd_lock); |
| 165 | +#endif |
| 166 | + return(rc); |
| 167 | +} |
| 168 | + |
| 169 | +/* |
| 170 | + * Alphabetic order comparison routine for those who want it. |
| 171 | + */ |
| 172 | +int |
| 173 | +_DEFUN(alphasort, (d1, d2), |
| 174 | + const struct dirent **d1 _AND |
| 175 | + const struct dirent **d2) |
| 176 | +{ |
| 177 | + return(strcmp((*d1)->d_name, (*d2)->d_name)); |
| 178 | +} |
| 179 | + |
| 180 | +#endif /* ! HAVE_OPENDIR */ |
0 commit comments