xref: /onnv-gate/usr/src/ucblib/libucb/port/gen/scandir.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*0Sstevel@tonic-gate  * The Regents of the University of California
33*0Sstevel@tonic-gate  * All Rights Reserved
34*0Sstevel@tonic-gate  *
35*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*0Sstevel@tonic-gate  * contributors.
38*0Sstevel@tonic-gate  */
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate /*LINTLIBRARY*/
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate  * Scan the directory dirname calling select to make a list of selected
46*0Sstevel@tonic-gate  * directory entries then sort using qsort and compare routine dcomp.
47*0Sstevel@tonic-gate  * Returns the number of entries and a pointer to a list of pointers to
48*0Sstevel@tonic-gate  * struct direct (through namelist). Returns -1 if there were any errors.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate #include <sys/types.h>
52*0Sstevel@tonic-gate #include <sys/stat.h>
53*0Sstevel@tonic-gate #include <sys/dir.h>
54*0Sstevel@tonic-gate #include <errno.h>
55*0Sstevel@tonic-gate #include <limits.h>
56*0Sstevel@tonic-gate #include <stdlib.h>
57*0Sstevel@tonic-gate #include <string.h>
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  * The macro DIRSIZ(dp) gives an amount of space required to represent
61*0Sstevel@tonic-gate  * a directory entry.  For any directory entry dp->d_reclen >= DIRSIZ(dp).
62*0Sstevel@tonic-gate  * Specific filesystem types may use this use this macro to construct the value
63*0Sstevel@tonic-gate  * for d_reclen.
64*0Sstevel@tonic-gate  */
65*0Sstevel@tonic-gate #undef DIRSIZ
66*0Sstevel@tonic-gate #define	DIRSIZ(dp)  \
67*0Sstevel@tonic-gate 	((sizeof (struct direct) - sizeof ((dp)->d_name) + \
68*0Sstevel@tonic-gate 	(strlen((dp)->d_name)+1) + 3) & ~3)
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate #if !defined(_LP64)
71*0Sstevel@tonic-gate int
scandir64(char * dirname,struct direct64 * (* namelist[]),int (* select)(struct direct64 *),int (* dcomp)(struct direct64 **,struct direct64 **))72*0Sstevel@tonic-gate scandir64(char *dirname, struct direct64 *(*namelist[]),
73*0Sstevel@tonic-gate     int (*select)(struct direct64 *),
74*0Sstevel@tonic-gate     int (*dcomp)(struct direct64 **, struct direct64 **))
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	struct direct64 *d, *p, **names;
77*0Sstevel@tonic-gate 	int nitems;
78*0Sstevel@tonic-gate 	char *cp1, *cp2;
79*0Sstevel@tonic-gate 	struct stat64 stb;
80*0Sstevel@tonic-gate 	long arraysz;
81*0Sstevel@tonic-gate 	DIR *dirp;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	if ((dirp = opendir(dirname)) == NULL)
84*0Sstevel@tonic-gate 		return (-1);
85*0Sstevel@tonic-gate 	if (fstat64(dirp->dd_fd, &stb) < 0)
86*0Sstevel@tonic-gate 		return (-1);
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	/*
89*0Sstevel@tonic-gate 	 * estimate the array size by taking the size of the directory file
90*0Sstevel@tonic-gate 	 * and dividing it by a multiple of the minimum size entry.
91*0Sstevel@tonic-gate 	 */
92*0Sstevel@tonic-gate 	arraysz = (stb.st_size / 24);
93*0Sstevel@tonic-gate 	names = (struct direct64 **)malloc(arraysz *
94*0Sstevel@tonic-gate 		sizeof (struct direct64 *));
95*0Sstevel@tonic-gate 	if (names == NULL)
96*0Sstevel@tonic-gate 		return (-1);
97*0Sstevel@tonic-gate 
98*0Sstevel@tonic-gate 	nitems = 0;
99*0Sstevel@tonic-gate 	while ((d = readdir64(dirp)) != NULL) {
100*0Sstevel@tonic-gate 		if (select != NULL && !(*select)(d))
101*0Sstevel@tonic-gate 			continue;	/* just selected names */
102*0Sstevel@tonic-gate 		/*
103*0Sstevel@tonic-gate 		 * Make a minimum size copy of the data
104*0Sstevel@tonic-gate 		 */
105*0Sstevel@tonic-gate 		p = (struct direct64 *)malloc(DIRSIZ64(d));
106*0Sstevel@tonic-gate 		if (p == NULL)
107*0Sstevel@tonic-gate 			return (-1);
108*0Sstevel@tonic-gate 		p->d_ino = d->d_ino;
109*0Sstevel@tonic-gate 		p->d_reclen = d->d_reclen;
110*0Sstevel@tonic-gate 		p->d_namlen = d->d_namlen;
111*0Sstevel@tonic-gate 		for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; )
112*0Sstevel@tonic-gate 			;
113*0Sstevel@tonic-gate 		/*
114*0Sstevel@tonic-gate 		 * Check to make sure the array has space left and
115*0Sstevel@tonic-gate 		 * realloc the maximum size.
116*0Sstevel@tonic-gate 		 */
117*0Sstevel@tonic-gate 		if (++nitems >= arraysz) {
118*0Sstevel@tonic-gate 			if (fstat64(dirp->dd_fd, &stb) < 0)
119*0Sstevel@tonic-gate 				return (-1);	/* just might have grown */
120*0Sstevel@tonic-gate 			arraysz = stb.st_size / 12;
121*0Sstevel@tonic-gate 			names = (struct direct64 **)realloc((char *)names,
122*0Sstevel@tonic-gate 				arraysz * sizeof (struct direct64 *));
123*0Sstevel@tonic-gate 			if (names == NULL)
124*0Sstevel@tonic-gate 				return (-1);
125*0Sstevel@tonic-gate 		}
126*0Sstevel@tonic-gate 		names[nitems-1] = p;
127*0Sstevel@tonic-gate 	}
128*0Sstevel@tonic-gate 	(void) closedir(dirp);
129*0Sstevel@tonic-gate 	if (nitems && dcomp != NULL)
130*0Sstevel@tonic-gate 		qsort(names, nitems, sizeof (struct direct64 *),
131*0Sstevel@tonic-gate 			(int(*)(const void *, const void *)) dcomp);
132*0Sstevel@tonic-gate 	*namelist = names;
133*0Sstevel@tonic-gate 	return (nitems);
134*0Sstevel@tonic-gate }
135*0Sstevel@tonic-gate #endif
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate int
scandir(char * dirname,struct direct * (* namelist[]),int (* select)(struct direct *),int (* dcomp)(struct direct **,struct direct **))139*0Sstevel@tonic-gate scandir(char *dirname, struct direct *(*namelist[]),
140*0Sstevel@tonic-gate     int (*select)(struct direct *),
141*0Sstevel@tonic-gate     int (*dcomp)(struct direct **, struct direct **))
142*0Sstevel@tonic-gate {
143*0Sstevel@tonic-gate 	struct direct *d, *p, **names;
144*0Sstevel@tonic-gate 	int nitems;
145*0Sstevel@tonic-gate 	char *cp1, *cp2;
146*0Sstevel@tonic-gate 	struct stat64 stb;
147*0Sstevel@tonic-gate 	long arraysz;
148*0Sstevel@tonic-gate 	DIR *dirp;
149*0Sstevel@tonic-gate 
150*0Sstevel@tonic-gate 	if ((dirp = opendir(dirname)) == NULL)
151*0Sstevel@tonic-gate 		return (-1);
152*0Sstevel@tonic-gate 	if (fstat64(dirp->dd_fd, &stb) < 0)
153*0Sstevel@tonic-gate 		return (-1);
154*0Sstevel@tonic-gate 	/*
155*0Sstevel@tonic-gate 	 * estimate the array size by taking the size of the directory file
156*0Sstevel@tonic-gate 	 * and dividing it by a multiple of the minimum size entry.
157*0Sstevel@tonic-gate 	 */
158*0Sstevel@tonic-gate 	if (stb.st_size > SSIZE_MAX) {
159*0Sstevel@tonic-gate 		errno = EOVERFLOW;
160*0Sstevel@tonic-gate 		return (-1);
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate 	arraysz = (stb.st_size / 24);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	names = (struct direct **)malloc(arraysz * sizeof (struct direct *));
165*0Sstevel@tonic-gate 	if (names == NULL)
166*0Sstevel@tonic-gate 		return (-1);
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	nitems = 0;
169*0Sstevel@tonic-gate 	while ((d = readdir(dirp)) != NULL) {
170*0Sstevel@tonic-gate 		if (select != NULL && !(*select)(d))
171*0Sstevel@tonic-gate 			continue;	/* just selected names */
172*0Sstevel@tonic-gate 		/*
173*0Sstevel@tonic-gate 		 * Make a minimum size copy of the data
174*0Sstevel@tonic-gate 		 */
175*0Sstevel@tonic-gate 		p = (struct direct *)malloc(DIRSIZ(d));
176*0Sstevel@tonic-gate 		if (p == NULL)
177*0Sstevel@tonic-gate 			return (-1);
178*0Sstevel@tonic-gate 		p->d_ino = d->d_ino;
179*0Sstevel@tonic-gate 		p->d_reclen = d->d_reclen;
180*0Sstevel@tonic-gate 		p->d_namlen = d->d_namlen;
181*0Sstevel@tonic-gate 		for (cp1 = p->d_name, cp2 = d->d_name; *cp1++ = *cp2++; )
182*0Sstevel@tonic-gate 			;
183*0Sstevel@tonic-gate 		/*
184*0Sstevel@tonic-gate 		 * Check to make sure the array has space left and
185*0Sstevel@tonic-gate 		 * realloc the maximum size.
186*0Sstevel@tonic-gate 		 */
187*0Sstevel@tonic-gate 		if (++nitems >= arraysz) {
188*0Sstevel@tonic-gate 			if (fstat64(dirp->dd_fd, &stb) < 0)
189*0Sstevel@tonic-gate 				return (-1);	/* just might have grown */
190*0Sstevel@tonic-gate 			arraysz = stb.st_size / 12;
191*0Sstevel@tonic-gate 			names = (struct direct **)realloc((char *)names,
192*0Sstevel@tonic-gate 				arraysz * sizeof (struct direct *));
193*0Sstevel@tonic-gate 			if (names == NULL)
194*0Sstevel@tonic-gate 				return (-1);
195*0Sstevel@tonic-gate 		}
196*0Sstevel@tonic-gate 		names[nitems-1] = p;
197*0Sstevel@tonic-gate 	}
198*0Sstevel@tonic-gate 	(void) closedir(dirp);
199*0Sstevel@tonic-gate 	if (nitems && dcomp != NULL)
200*0Sstevel@tonic-gate 		qsort(names, nitems, sizeof (struct direct *),
201*0Sstevel@tonic-gate 			(int(*)(const void *, const void *)) dcomp);
202*0Sstevel@tonic-gate 	*namelist = names;
203*0Sstevel@tonic-gate 	return (nitems);
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate /*
207*0Sstevel@tonic-gate  * Alphabetic order comparison routine for those who want it.
208*0Sstevel@tonic-gate  */
209*0Sstevel@tonic-gate int
alphasort(struct direct ** d1,struct direct ** d2)210*0Sstevel@tonic-gate alphasort(struct direct **d1, struct direct **d2)
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate 	return (strcmp((*d1)->d_name, (*d2)->d_name));
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate #if !defined(_LP64)
216*0Sstevel@tonic-gate int
alphasort64(struct direct64 ** d1,struct direct64 ** d2)217*0Sstevel@tonic-gate alphasort64(struct direct64 **d1, struct direct64 **d2)
218*0Sstevel@tonic-gate {
219*0Sstevel@tonic-gate 	return (strcmp((*d1)->d_name, (*d2)->d_name));
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate #endif
222