xref: /onnv-gate/usr/src/lib/libc/port/gen/scandir.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate  * The Regents of the University of California
330Sstevel@tonic-gate  * All Rights Reserved
340Sstevel@tonic-gate  *
350Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate  * contributors.
380Sstevel@tonic-gate  */
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * Based on usr/src/ucblib/libucb/port/gen/scandir.c
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate 
460Sstevel@tonic-gate /*
470Sstevel@tonic-gate  * Scan the directory dirname calling select to make a list of selected
480Sstevel@tonic-gate  * directory entries then sort using qsort and compare routine dcomp.
490Sstevel@tonic-gate  * Returns the number of entries and a pointer to a list of pointers to
500Sstevel@tonic-gate  * struct direct (through namelist). Returns -1 if there were any errors.
510Sstevel@tonic-gate  */
520Sstevel@tonic-gate 
530Sstevel@tonic-gate #include <sys/feature_tests.h>
540Sstevel@tonic-gate 
55*6812Sraf #pragma weak _scandir = scandir
56*6812Sraf #pragma weak _alphasort = alphasort
570Sstevel@tonic-gate #if !defined(_LP64)
58*6812Sraf #pragma weak _scandir64 = scandir64
59*6812Sraf #pragma weak _alphasort64 = alphasort64
600Sstevel@tonic-gate #endif
610Sstevel@tonic-gate 
62*6812Sraf #include "lint.h"
630Sstevel@tonic-gate #include <dirent.h>
640Sstevel@tonic-gate #include <errno.h>
650Sstevel@tonic-gate #include <sys/types.h>
660Sstevel@tonic-gate #include <sys/stat.h>
670Sstevel@tonic-gate #include <stdlib.h>
680Sstevel@tonic-gate #include <string.h>
690Sstevel@tonic-gate #include <limits.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #if !defined(_LP64)
730Sstevel@tonic-gate int
scandir64(const char * dirname,struct dirent64 * (* namelist[]),int (* select)(const struct dirent64 *),int (* dcomp)(const struct dirent64 **,const struct dirent64 **))740Sstevel@tonic-gate scandir64(const char *dirname, struct dirent64 *(*namelist[]),
750Sstevel@tonic-gate     int (*select)(const struct dirent64 *),
760Sstevel@tonic-gate     int (*dcomp)(const struct dirent64 **, const struct dirent64 **))
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	struct dirent64	*d, *p, **names = NULL;
790Sstevel@tonic-gate 	size_t	nitems = 0;
800Sstevel@tonic-gate 	size_t	arraysz, entlen;
810Sstevel@tonic-gate 	struct stat64	stb;
820Sstevel@tonic-gate 	DIR	*dirp;
830Sstevel@tonic-gate 	u_longlong_t	tmp_arraysz;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if ((dirp = opendir(dirname)) == NULL)
860Sstevel@tonic-gate 		return (-1);
870Sstevel@tonic-gate 	if (fstat64(dirp->dd_fd, &stb) < 0)
880Sstevel@tonic-gate 		goto fail;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	/*
910Sstevel@tonic-gate 	 * estimate the array size by taking the size of the directory file
920Sstevel@tonic-gate 	 * and dividing it by a multiple of the minimum size entry.
930Sstevel@tonic-gate 	 */
940Sstevel@tonic-gate 	tmp_arraysz = stb.st_size / 24;	/* 24 bytes on a 64-bit system */
950Sstevel@tonic-gate 	if (tmp_arraysz > INT_MAX)
960Sstevel@tonic-gate 		arraysz = INT_MAX;
970Sstevel@tonic-gate 	else
980Sstevel@tonic-gate 		arraysz = (size_t)tmp_arraysz;
990Sstevel@tonic-gate 	names = malloc(arraysz * sizeof (struct dirent64 *));
1000Sstevel@tonic-gate 	if (names == NULL)
1010Sstevel@tonic-gate 		goto fail;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	while ((d = readdir64(dirp)) != NULL) {
1040Sstevel@tonic-gate 		if (select != NULL && !(*select)(d))
1050Sstevel@tonic-gate 			continue;	/* just selected names */
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 		entlen = d->d_reclen;
1080Sstevel@tonic-gate 		/*
1090Sstevel@tonic-gate 		 * Make a minimum size copy of the data
1100Sstevel@tonic-gate 		 */
1110Sstevel@tonic-gate 		p = malloc(entlen);
1120Sstevel@tonic-gate 		if (p == NULL)
1130Sstevel@tonic-gate 			goto fail;
1140Sstevel@tonic-gate 		(void) memcpy(p, d, entlen);
1150Sstevel@tonic-gate 		/*
1160Sstevel@tonic-gate 		 * Check to make sure the array has space left and
1170Sstevel@tonic-gate 		 * realloc the maximum size.
1180Sstevel@tonic-gate 		 */
1190Sstevel@tonic-gate 		if (nitems >= arraysz) {
1200Sstevel@tonic-gate 			struct dirent64	**tmp;
1210Sstevel@tonic-gate 			if (nitems == INT_MAX) {
1220Sstevel@tonic-gate 				/* overflow */
1230Sstevel@tonic-gate 				free(p);
1240Sstevel@tonic-gate 				errno = EOVERFLOW;
1250Sstevel@tonic-gate 				goto fail;
1260Sstevel@tonic-gate 			}
1270Sstevel@tonic-gate 			arraysz += 512;		/* no science here */
1280Sstevel@tonic-gate 			tmp = realloc(names,
1290Sstevel@tonic-gate 			    arraysz * sizeof (struct dirent64 *));
1300Sstevel@tonic-gate 			if (tmp == NULL) {
1310Sstevel@tonic-gate 				free(p);
1320Sstevel@tonic-gate 				goto fail;
1330Sstevel@tonic-gate 			}
1340Sstevel@tonic-gate 			names = tmp;
1350Sstevel@tonic-gate 		}
1360Sstevel@tonic-gate 		names[nitems++] = p;
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	(void) closedir(dirp);
1390Sstevel@tonic-gate 	if (nitems && dcomp != NULL)
1400Sstevel@tonic-gate 		qsort(names, nitems, sizeof (struct dirent64 *),
141*6812Sraf 		    (int(*)(const void *, const void *))dcomp);
1420Sstevel@tonic-gate 	*namelist = names;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	return ((int)nitems);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate fail:
1470Sstevel@tonic-gate 	while (nitems != 0) {
1480Sstevel@tonic-gate 		free(names[--nitems]);
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 	if (names)
1510Sstevel@tonic-gate 		free(names);
1520Sstevel@tonic-gate 	(void) closedir(dirp);
1530Sstevel@tonic-gate 	return (-1);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate #endif
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate int
scandir(const char * dirname,struct dirent * (* namelist[]),int (* select)(const struct dirent *),int (* dcomp)(const struct dirent **,const struct dirent **))1590Sstevel@tonic-gate scandir(const char *dirname, struct dirent *(*namelist[]),
1600Sstevel@tonic-gate     int (*select)(const struct dirent *),
1610Sstevel@tonic-gate     int (*dcomp)(const struct dirent **, const struct dirent **))
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate 	struct dirent	*d, *p, **names = NULL;
1640Sstevel@tonic-gate 	size_t	nitems = 0;
1650Sstevel@tonic-gate 	size_t	arraysz, entlen;
1660Sstevel@tonic-gate 	struct stat64	stb;
1670Sstevel@tonic-gate 	DIR	*dirp;
1680Sstevel@tonic-gate 	u_longlong_t	tmp_arraysz;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	if ((dirp = opendir(dirname)) == NULL)
1710Sstevel@tonic-gate 		return (-1);
1720Sstevel@tonic-gate 	if (fstat64(dirp->dd_fd, &stb) < 0)
1730Sstevel@tonic-gate 		goto fail;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	/*
1760Sstevel@tonic-gate 	 * estimate the array size by taking the size of the directory file
1770Sstevel@tonic-gate 	 * and dividing it by a multiple of the minimum size entry.
1780Sstevel@tonic-gate 	 */
1790Sstevel@tonic-gate 	tmp_arraysz = stb.st_size / 24;	/* 24 bytes on a 64-bit system */
1800Sstevel@tonic-gate 	if (tmp_arraysz > INT_MAX)
1810Sstevel@tonic-gate 		arraysz = INT_MAX;
1820Sstevel@tonic-gate 	else
1830Sstevel@tonic-gate 		arraysz = (size_t)tmp_arraysz;
1840Sstevel@tonic-gate 	names = malloc(arraysz * sizeof (struct dirent *));
1850Sstevel@tonic-gate 	if (names == NULL)
1860Sstevel@tonic-gate 		goto fail;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	while ((d = readdir(dirp)) != NULL) {
1890Sstevel@tonic-gate 		if (select != NULL && !(*select)(d))
1900Sstevel@tonic-gate 			continue;	/* just selected names */
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		entlen = d->d_reclen;
1930Sstevel@tonic-gate 		/*
1940Sstevel@tonic-gate 		 * Make a minimum size copy of the data
1950Sstevel@tonic-gate 		 */
1960Sstevel@tonic-gate 		p = malloc(entlen);
1970Sstevel@tonic-gate 		if (p == NULL)
1980Sstevel@tonic-gate 			goto fail;
1990Sstevel@tonic-gate 		(void) memcpy(p, d, entlen);
2000Sstevel@tonic-gate 		/*
2010Sstevel@tonic-gate 		 * Check to make sure the array has space left and
2020Sstevel@tonic-gate 		 * realloc the maximum size.
2030Sstevel@tonic-gate 		 */
2040Sstevel@tonic-gate 		if (nitems >= arraysz) {
2050Sstevel@tonic-gate 			struct dirent **tmp;
2060Sstevel@tonic-gate 			if (nitems == INT_MAX) {
2070Sstevel@tonic-gate 				/* overflow */
2080Sstevel@tonic-gate 				free(p);
2090Sstevel@tonic-gate 				errno = EOVERFLOW;
2100Sstevel@tonic-gate 				goto fail;
2110Sstevel@tonic-gate 			}
2120Sstevel@tonic-gate 			arraysz += 512;		/* no science here */
2130Sstevel@tonic-gate 			tmp = realloc(names,
214*6812Sraf 			    arraysz * sizeof (struct dirent *));
2150Sstevel@tonic-gate 			if (tmp == NULL) {
2160Sstevel@tonic-gate 				free(p);
2170Sstevel@tonic-gate 				goto fail;
2180Sstevel@tonic-gate 			}
2190Sstevel@tonic-gate 			names = tmp;
2200Sstevel@tonic-gate 		}
2210Sstevel@tonic-gate 		names[nitems++] = p;
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 	(void) closedir(dirp);
2240Sstevel@tonic-gate 	if (nitems && dcomp != NULL)
2250Sstevel@tonic-gate 		qsort(names, nitems, sizeof (struct dirent *),
226*6812Sraf 		    (int(*)(const void *, const void *))dcomp);
2270Sstevel@tonic-gate 	*namelist = names;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	return ((int)nitems);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate fail:
2320Sstevel@tonic-gate 	while (nitems != 0) {
2330Sstevel@tonic-gate 		free(names[--nitems]);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 	if (names)
2360Sstevel@tonic-gate 		free(names);
2370Sstevel@tonic-gate 	(void) closedir(dirp);
2380Sstevel@tonic-gate 	return (-1);
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate  * Alphabetic order comparison routine for those who want it.
2430Sstevel@tonic-gate  */
2440Sstevel@tonic-gate int
alphasort(const struct dirent ** d1,const struct dirent ** d2)2450Sstevel@tonic-gate alphasort(const struct dirent **d1, const struct dirent **d2)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate 	return (strcoll((*d1)->d_name,
2480Sstevel@tonic-gate 	    (*d2)->d_name));
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate #if !defined(_LP64)
2520Sstevel@tonic-gate int
alphasort64(const struct dirent64 ** d1,const struct dirent64 ** d2)2530Sstevel@tonic-gate alphasort64(const struct dirent64 **d1, const struct dirent64 **d2)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate 	return (strcoll((*d1)->d_name,
2560Sstevel@tonic-gate 	    (*d2)->d_name));
2570Sstevel@tonic-gate }
2580Sstevel@tonic-gate #endif
259