xref: /csrg-svn/lib/libc/gen/scandir.c (revision 65320)
121357Sdist /*
261111Sbostic  * Copyright (c) 1983, 1993
361111Sbostic  *	The Regents of the University of California.  All rights reserved.
434790Sbostic  *
542626Sbostic  * %sccs.include.redist.c%
621357Sdist  */
721357Sdist 
826582Sdonn #if defined(LIBC_SCCS) && !defined(lint)
9*65320Sbostic static char sccsid[] = "@(#)scandir.c	8.3 (Berkeley) 01/02/94";
1034790Sbostic #endif /* LIBC_SCCS and not lint */
1113582Ssam 
129609Sralph /*
139609Sralph  * Scan the directory dirname calling select to make a list of selected
149609Sralph  * directory entries then sort using qsort and compare routine dcomp.
159609Sralph  * Returns the number of entries and a pointer to a list of pointers to
1636545Smckusick  * struct dirent (through namelist). Returns -1 if there were any errors.
179609Sralph  */
189609Sralph 
199609Sralph #include <sys/types.h>
209609Sralph #include <sys/stat.h>
2136545Smckusick #include <dirent.h>
2246597Sdonn #include <stdlib.h>
2346597Sdonn #include <string.h>
249609Sralph 
2536558Smckusick /*
26*65320Sbostic  * The DIRSIZ macro is the minimum record length which will hold the directory
27*65320Sbostic  * entry.  This requires the amount of space in struct dirent without the
28*65320Sbostic  * d_name field, plus enough space for the name and a terminating nul byte
29*65320Sbostic  * (dp->d_namlen + 1), rounded up to a 4 byte boundary.
3036558Smckusick  */
3136558Smckusick #undef DIRSIZ
32*65320Sbostic #define DIRSIZ(dp)							\
33*65320Sbostic 	((sizeof(struct dirent) - sizeof(dp)->d_name) +			\
34*65320Sbostic 	    (((dp)->d_namlen + 1 + 3) &~ 3))
3536558Smckusick 
3646597Sdonn int
scandir(dirname,namelist,select,dcomp)379609Sralph scandir(dirname, namelist, select, dcomp)
3846597Sdonn 	const char *dirname;
3943534Sbostic 	struct dirent ***namelist;
4046597Sdonn 	int (*select) __P((struct dirent *));
4146597Sdonn 	int (*dcomp) __P((const void *, const void *));
429609Sralph {
4336545Smckusick 	register struct dirent *d, *p, **names;
4446597Sdonn 	register size_t nitems;
459609Sralph 	struct stat stb;
469609Sralph 	long arraysz;
479609Sralph 	DIR *dirp;
489609Sralph 
499609Sralph 	if ((dirp = opendir(dirname)) == NULL)
509609Sralph 		return(-1);
519609Sralph 	if (fstat(dirp->dd_fd, &stb) < 0)
529609Sralph 		return(-1);
539609Sralph 
549609Sralph 	/*
559609Sralph 	 * estimate the array size by taking the size of the directory file
569609Sralph 	 * and dividing it by a multiple of the minimum size entry.
579609Sralph 	 */
589609Sralph 	arraysz = (stb.st_size / 24);
5936545Smckusick 	names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
609609Sralph 	if (names == NULL)
619609Sralph 		return(-1);
629609Sralph 
639609Sralph 	nitems = 0;
649609Sralph 	while ((d = readdir(dirp)) != NULL) {
659609Sralph 		if (select != NULL && !(*select)(d))
669609Sralph 			continue;	/* just selected names */
679609Sralph 		/*
689609Sralph 		 * Make a minimum size copy of the data
699609Sralph 		 */
7036545Smckusick 		p = (struct dirent *)malloc(DIRSIZ(d));
719609Sralph 		if (p == NULL)
729609Sralph 			return(-1);
739609Sralph 		p->d_ino = d->d_ino;
749609Sralph 		p->d_reclen = d->d_reclen;
759609Sralph 		p->d_namlen = d->d_namlen;
7636681Sbostic 		bcopy(d->d_name, p->d_name, p->d_namlen + 1);
779609Sralph 		/*
789609Sralph 		 * Check to make sure the array has space left and
799609Sralph 		 * realloc the maximum size.
809609Sralph 		 */
819609Sralph 		if (++nitems >= arraysz) {
8215661Sralph 			if (fstat(dirp->dd_fd, &stb) < 0)
8315661Sralph 				return(-1);	/* just might have grown */
8415661Sralph 			arraysz = stb.st_size / 12;
8536545Smckusick 			names = (struct dirent **)realloc((char *)names,
8636545Smckusick 				arraysz * sizeof(struct dirent *));
879609Sralph 			if (names == NULL)
889609Sralph 				return(-1);
899609Sralph 		}
909609Sralph 		names[nitems-1] = p;
919609Sralph 	}
929609Sralph 	closedir(dirp);
939609Sralph 	if (nitems && dcomp != NULL)
9436545Smckusick 		qsort(names, nitems, sizeof(struct dirent *), dcomp);
959609Sralph 	*namelist = names;
969609Sralph 	return(nitems);
979609Sralph }
989609Sralph 
999609Sralph /*
1009609Sralph  * Alphabetic order comparison routine for those who want it.
1019609Sralph  */
10246597Sdonn int
alphasort(d1,d2)1039609Sralph alphasort(d1, d2)
10446597Sdonn 	const void *d1;
10546597Sdonn 	const void *d2;
1069609Sralph {
10743609Sbostic 	return(strcmp((*(struct dirent **)d1)->d_name,
10843609Sbostic 	    (*(struct dirent **)d2)->d_name));
1099609Sralph }
110