1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #pragma ident "%Z%%M% %I% %E% SMI" 19 /* from UCB 5.1 85/05/30 */ 20 21 /* 22 * Copyright (c) 1983 Regents of the University of California. 23 * All rights reserved. The Berkeley software License Agreement 24 * specifies the terms and conditions for redistribution. 25 */ 26 27 /* 28 * Scan the directory dirname calling select to make a list of selected 29 * directory entries then sort using qsort and compare routine dcomp. 30 * Returns the number of entries and a pointer to a list of pointers to 31 * struct direct (through namelist). Returns -1 if there were any errors. 32 */ 33 34 #include <sys/types.h> 35 #include <sys/stat.h> 36 #include <sys/dir.h> 37 38 scandir(dirname, namelist, select, dcomp) 39 char *dirname; 40 struct direct *(*namelist[]); 41 int (*select)(), (*dcomp)(); 42 { 43 register struct direct *d, *p, **names; 44 register int nitems; 45 register char *cp1, *cp2; 46 struct stat stb; 47 long arraysz; 48 DIR *dirp; 49 50 if ((dirp = opendir(dirname)) == NULL) 51 return(-1); 52 if (fstat(dirp->dd_fd, &stb) < 0) 53 return(-1); 54 55 /* 56 * estimate the array size by taking the size of the directory file 57 * and dividing it by a multiple of the minimum size entry. 58 */ 59 arraysz = (stb.st_size / 24); 60 names = (struct direct **)malloc(arraysz * sizeof(struct direct *)); 61 if (names == NULL) 62 return(-1); 63 64 nitems = 0; 65 while ((d = readdir(dirp)) != NULL) { 66 if (select != NULL && !(*select)(d)) 67 continue; /* just selected names */ 68 /* 69 * Make a minimum size copy of the data 70 */ 71 p = (struct direct *)malloc(DIRSIZ(d)); 72 if (p == NULL) 73 return(-1); 74 p->d_ino = d->d_ino; 75 p->d_reclen = d->d_reclen; 76 p->d_namlen = d->d_namlen; 77 for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; ); 78 /* 79 * Check to make sure the array has space left and 80 * realloc the maximum size. 81 */ 82 if (++nitems >= arraysz) { 83 if (fstat(dirp->dd_fd, &stb) < 0) 84 return(-1); /* just might have grown */ 85 arraysz = stb.st_size / 12; 86 names = (struct direct **)realloc((char *)names, 87 arraysz * sizeof(struct direct *)); 88 if (names == NULL) 89 return(-1); 90 } 91 names[nitems-1] = p; 92 } 93 closedir(dirp); 94 if (nitems && dcomp != NULL) 95 qsort(names, nitems, sizeof(struct direct *), dcomp); 96 *namelist = names; 97 return(nitems); 98 } 99 100 /* 101 * Alphabetic order comparison routine for those who want it. 102 */ 103 alphasort(d1, d2) 104 struct direct **d1, **d2; 105 { 106 return(strcmp((*d1)->d_name, (*d2)->d_name)); 107 } 108