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