1 /* 2 * Copyright (c) 1983, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 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. 16 * 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. 28 */ 29 30 #if defined(LIBC_SCCS) && !defined(lint) 31 static char rcsid[] = "$OpenBSD: scandir.c,v 1.7 2003/06/02 20:18:34 millert Exp $"; 32 #endif /* LIBC_SCCS and not lint */ 33 34 /* 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. 39 */ 40 41 #include <sys/types.h> 42 #include <sys/stat.h> 43 #include <dirent.h> 44 #include <errno.h> 45 #include <limits.h> 46 #include <stdlib.h> 47 #include <string.h> 48 49 /* 50 * The DIRSIZ macro is the minimum record length which will hold the directory 51 * entry. This requires the amount of space in struct dirent without the 52 * d_name field, plus enough space for the name and a terminating nul byte 53 * (dp->d_namlen + 1), rounded up to a 4 byte boundary. 54 */ 55 #undef DIRSIZ 56 #define DIRSIZ(dp) \ 57 ((sizeof(struct dirent) - sizeof(dp)->d_name) + \ 58 (((dp)->d_namlen + 1 + 3) &~ 3)) 59 60 int 61 scandir(dirname, namelist, select, dcomp) 62 const char *dirname; 63 struct dirent ***namelist; 64 int (*select)(struct dirent *); 65 int (*dcomp)(const void *, const void *); 66 { 67 register struct dirent *d, *p, **names; 68 register size_t nitems; 69 struct stat stb; 70 long arraysz; 71 DIR *dirp; 72 73 if ((dirp = opendir(dirname)) == NULL) 74 return(-1); 75 if (fstat(dirp->dd_fd, &stb) < 0) 76 return(-1); 77 78 /* 79 * estimate the array size by taking the size of the directory file 80 * and dividing it by a multiple of the minimum size entry. 81 */ 82 arraysz = (stb.st_size / 24); 83 if (arraysz > SIZE_T_MAX / sizeof(struct dirent *)) { 84 errno = ENOMEM; 85 return(-1); 86 } 87 names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *)); 88 if (names == NULL) 89 return(-1); 90 91 nitems = 0; 92 while ((d = readdir(dirp)) != NULL) { 93 if (select != NULL && !(*select)(d)) 94 continue; /* just selected names */ 95 /* 96 * Make a minimum size copy of the data 97 */ 98 p = (struct dirent *)malloc(DIRSIZ(d)); 99 if (p == NULL) 100 return(-1); 101 p->d_ino = d->d_ino; 102 p->d_type = d->d_type; 103 p->d_reclen = d->d_reclen; 104 p->d_namlen = d->d_namlen; 105 bcopy(d->d_name, p->d_name, p->d_namlen + 1); 106 /* 107 * Check to make sure the array has space left and 108 * realloc the maximum size. 109 */ 110 if (++nitems >= arraysz) { 111 register struct dirent **nnames; 112 113 if (fstat(dirp->dd_fd, &stb) < 0) 114 return(-1); /* just might have grown */ 115 arraysz = stb.st_size / 12; 116 nnames = (struct dirent **)realloc((char *)names, 117 arraysz * sizeof(struct dirent *)); 118 if (nnames == NULL) { 119 if (names) 120 free(names); 121 return(-1); 122 } 123 names = nnames; 124 } 125 names[nitems-1] = p; 126 } 127 closedir(dirp); 128 if (nitems && dcomp != NULL) 129 qsort(names, nitems, sizeof(struct dirent *), dcomp); 130 *namelist = names; 131 return(nitems); 132 } 133 134 /* 135 * Alphabetic order comparison routine for those who want it. 136 */ 137 int 138 alphasort(d1, d2) 139 const void *d1; 140 const void *d2; 141 { 142 return(strcmp((*(struct dirent **)d1)->d_name, 143 (*(struct dirent **)d2)->d_name)); 144 } 145