xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/scandir.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
60Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
70Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
80Sstevel@tonic-gate  * advertising materials, and other materials related to such
90Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
100Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
110Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
120Sstevel@tonic-gate  * from this software without specific prior written permission.
130Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
140Sstevel@tonic-gate  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
150Sstevel@tonic-gate  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate /*
190Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
200Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
210Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
220Sstevel@tonic-gate  */
230Sstevel@tonic-gate 
24*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
25*722Smuffin 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Scan the directory dirname calling select to make a list of selected
280Sstevel@tonic-gate  * directory entries then sort using qsort and compare routine dcomp.
290Sstevel@tonic-gate  * Returns the number of entries and a pointer to a list of pointers to
300Sstevel@tonic-gate  * struct direct (through namelist). Returns -1 if there were any errors.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <sys/dir.h>
360Sstevel@tonic-gate 
37*722Smuffin int
scandir(char * dirname,struct direct * (* namelist[]),int (* select)(),int (* dcomp)())38*722Smuffin scandir(char *dirname, struct direct *(*namelist[]),
39*722Smuffin     int (*select)(), int (*dcomp)())
400Sstevel@tonic-gate {
41*722Smuffin 	struct direct *d, *p, **names;
42*722Smuffin 	int nitems;
43*722Smuffin 	char *cp1, *cp2;
440Sstevel@tonic-gate 	struct stat stb;
450Sstevel@tonic-gate 	long arraysz;
460Sstevel@tonic-gate 	DIR *dirp;
470Sstevel@tonic-gate 
480Sstevel@tonic-gate 	if ((dirp = opendir(dirname)) == NULL)
49*722Smuffin 		return (-1);
500Sstevel@tonic-gate 	if (fstat(dirp->dd_fd, &stb) < 0)
51*722Smuffin 		return (-1);
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	/*
540Sstevel@tonic-gate 	 * estimate the array size by taking the size of the directory file
550Sstevel@tonic-gate 	 * and dividing it by a multiple of the minimum size entry.
560Sstevel@tonic-gate 	 */
570Sstevel@tonic-gate 	arraysz = (stb.st_size / 24);
580Sstevel@tonic-gate 	names = (struct direct **)malloc(arraysz * sizeof(struct direct *));
590Sstevel@tonic-gate 	if (names == NULL)
60*722Smuffin 		return (-1);
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	nitems = 0;
630Sstevel@tonic-gate 	while ((d = readdir(dirp)) != NULL) {
640Sstevel@tonic-gate 		if (select != NULL && !(*select)(d))
650Sstevel@tonic-gate 			continue;	/* just selected names */
660Sstevel@tonic-gate 		/*
670Sstevel@tonic-gate 		 * Make a minimum size copy of the data
680Sstevel@tonic-gate 		 */
690Sstevel@tonic-gate 		p = (struct direct *)malloc(DIRSIZ(d));
700Sstevel@tonic-gate 		if (p == NULL)
71*722Smuffin 			return (-1);
720Sstevel@tonic-gate 		p->d_ino = d->d_ino;
730Sstevel@tonic-gate 		p->d_reclen = d->d_reclen;
740Sstevel@tonic-gate 		p->d_namlen = d->d_namlen;
750Sstevel@tonic-gate 		for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; );
760Sstevel@tonic-gate 		/*
770Sstevel@tonic-gate 		 * Check to make sure the array has space left and
780Sstevel@tonic-gate 		 * realloc the maximum size.
790Sstevel@tonic-gate 		 */
800Sstevel@tonic-gate 		if (++nitems >= arraysz) {
810Sstevel@tonic-gate 			if (fstat(dirp->dd_fd, &stb) < 0)
82*722Smuffin 				return (-1);	/* just might have grown */
830Sstevel@tonic-gate 			arraysz = stb.st_size / 12;
840Sstevel@tonic-gate 			names = (struct direct **)realloc((char *)names,
850Sstevel@tonic-gate 				arraysz * sizeof(struct direct *));
860Sstevel@tonic-gate 			if (names == NULL)
87*722Smuffin 				return (-1);
880Sstevel@tonic-gate 		}
890Sstevel@tonic-gate 		names[nitems-1] = p;
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 	closedir(dirp);
920Sstevel@tonic-gate 	if (nitems && dcomp != NULL)
930Sstevel@tonic-gate 		qsort(names, nitems, sizeof(struct direct *), dcomp);
940Sstevel@tonic-gate 	*namelist = names;
95*722Smuffin 	return (nitems);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * Alphabetic order comparison routine for those who want it.
1000Sstevel@tonic-gate  */
101*722Smuffin int
alphasort(struct direct ** d1,struct direct ** d2)102*722Smuffin alphasort(struct direct **d1, struct direct **d2)
1030Sstevel@tonic-gate {
104*722Smuffin 	return (strcmp((*d1)->d_name, (*d2)->d_name));
1050Sstevel@tonic-gate }
106