xref: /illumos-gate/usr/src/lib/libc/port/gen/scandir.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7257d1b4Sraf  * Common Development and Distribution License (the "License").
6*7257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*7257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * Based on usr/src/ucblib/libucb/port/gen/scandir.c
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  * Scan the directory dirname calling select to make a list of selected
467c478bd9Sstevel@tonic-gate  * directory entries then sort using qsort and compare routine dcomp.
477c478bd9Sstevel@tonic-gate  * Returns the number of entries and a pointer to a list of pointers to
487c478bd9Sstevel@tonic-gate  * struct direct (through namelist). Returns -1 if there were any errors.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <sys/feature_tests.h>
527c478bd9Sstevel@tonic-gate 
53*7257d1b4Sraf #pragma weak _scandir = scandir
54*7257d1b4Sraf #pragma weak _alphasort = alphasort
557c478bd9Sstevel@tonic-gate #if !defined(_LP64)
56*7257d1b4Sraf #pragma weak _scandir64 = scandir64
57*7257d1b4Sraf #pragma weak _alphasort64 = alphasort64
587c478bd9Sstevel@tonic-gate #endif
597c478bd9Sstevel@tonic-gate 
60*7257d1b4Sraf #include "lint.h"
617c478bd9Sstevel@tonic-gate #include <dirent.h>
627c478bd9Sstevel@tonic-gate #include <errno.h>
637c478bd9Sstevel@tonic-gate #include <sys/types.h>
647c478bd9Sstevel@tonic-gate #include <sys/stat.h>
657c478bd9Sstevel@tonic-gate #include <stdlib.h>
667c478bd9Sstevel@tonic-gate #include <string.h>
677c478bd9Sstevel@tonic-gate #include <limits.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #if !defined(_LP64)
717c478bd9Sstevel@tonic-gate int
scandir64(const char * dirname,struct dirent64 * (* namelist[]),int (* select)(const struct dirent64 *),int (* dcomp)(const struct dirent64 **,const struct dirent64 **))727c478bd9Sstevel@tonic-gate scandir64(const char *dirname, struct dirent64 *(*namelist[]),
737c478bd9Sstevel@tonic-gate     int (*select)(const struct dirent64 *),
747c478bd9Sstevel@tonic-gate     int (*dcomp)(const struct dirent64 **, const struct dirent64 **))
757c478bd9Sstevel@tonic-gate {
767c478bd9Sstevel@tonic-gate 	struct dirent64	*d, *p, **names = NULL;
777c478bd9Sstevel@tonic-gate 	size_t	nitems = 0;
787c478bd9Sstevel@tonic-gate 	size_t	arraysz, entlen;
797c478bd9Sstevel@tonic-gate 	struct stat64	stb;
807c478bd9Sstevel@tonic-gate 	DIR	*dirp;
817c478bd9Sstevel@tonic-gate 	u_longlong_t	tmp_arraysz;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(dirname)) == NULL)
847c478bd9Sstevel@tonic-gate 		return (-1);
857c478bd9Sstevel@tonic-gate 	if (fstat64(dirp->dd_fd, &stb) < 0)
867c478bd9Sstevel@tonic-gate 		goto fail;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * estimate the array size by taking the size of the directory file
907c478bd9Sstevel@tonic-gate 	 * and dividing it by a multiple of the minimum size entry.
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	tmp_arraysz = stb.st_size / 24;	/* 24 bytes on a 64-bit system */
937c478bd9Sstevel@tonic-gate 	if (tmp_arraysz > INT_MAX)
947c478bd9Sstevel@tonic-gate 		arraysz = INT_MAX;
957c478bd9Sstevel@tonic-gate 	else
967c478bd9Sstevel@tonic-gate 		arraysz = (size_t)tmp_arraysz;
977c478bd9Sstevel@tonic-gate 	names = malloc(arraysz * sizeof (struct dirent64 *));
987c478bd9Sstevel@tonic-gate 	if (names == NULL)
997c478bd9Sstevel@tonic-gate 		goto fail;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	while ((d = readdir64(dirp)) != NULL) {
1027c478bd9Sstevel@tonic-gate 		if (select != NULL && !(*select)(d))
1037c478bd9Sstevel@tonic-gate 			continue;	/* just selected names */
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		entlen = d->d_reclen;
1067c478bd9Sstevel@tonic-gate 		/*
1077c478bd9Sstevel@tonic-gate 		 * Make a minimum size copy of the data
1087c478bd9Sstevel@tonic-gate 		 */
1097c478bd9Sstevel@tonic-gate 		p = malloc(entlen);
1107c478bd9Sstevel@tonic-gate 		if (p == NULL)
1117c478bd9Sstevel@tonic-gate 			goto fail;
1127c478bd9Sstevel@tonic-gate 		(void) memcpy(p, d, entlen);
1137c478bd9Sstevel@tonic-gate 		/*
1147c478bd9Sstevel@tonic-gate 		 * Check to make sure the array has space left and
1157c478bd9Sstevel@tonic-gate 		 * realloc the maximum size.
1167c478bd9Sstevel@tonic-gate 		 */
1177c478bd9Sstevel@tonic-gate 		if (nitems >= arraysz) {
1187c478bd9Sstevel@tonic-gate 			struct dirent64	**tmp;
1197c478bd9Sstevel@tonic-gate 			if (nitems == INT_MAX) {
1207c478bd9Sstevel@tonic-gate 				/* overflow */
1217c478bd9Sstevel@tonic-gate 				free(p);
1227c478bd9Sstevel@tonic-gate 				errno = EOVERFLOW;
1237c478bd9Sstevel@tonic-gate 				goto fail;
1247c478bd9Sstevel@tonic-gate 			}
1257c478bd9Sstevel@tonic-gate 			arraysz += 512;		/* no science here */
1267c478bd9Sstevel@tonic-gate 			tmp = realloc(names,
1277c478bd9Sstevel@tonic-gate 			    arraysz * sizeof (struct dirent64 *));
1287c478bd9Sstevel@tonic-gate 			if (tmp == NULL) {
1297c478bd9Sstevel@tonic-gate 				free(p);
1307c478bd9Sstevel@tonic-gate 				goto fail;
1317c478bd9Sstevel@tonic-gate 			}
1327c478bd9Sstevel@tonic-gate 			names = tmp;
1337c478bd9Sstevel@tonic-gate 		}
1347c478bd9Sstevel@tonic-gate 		names[nitems++] = p;
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
1377c478bd9Sstevel@tonic-gate 	if (nitems && dcomp != NULL)
1387c478bd9Sstevel@tonic-gate 		qsort(names, nitems, sizeof (struct dirent64 *),
1397c478bd9Sstevel@tonic-gate 		    (int(*)(const void *, const void *))dcomp);
1407c478bd9Sstevel@tonic-gate 	*namelist = names;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	return ((int)nitems);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate fail:
1457c478bd9Sstevel@tonic-gate 	while (nitems != 0) {
1467c478bd9Sstevel@tonic-gate 		free(names[--nitems]);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 	if (names)
1497c478bd9Sstevel@tonic-gate 		free(names);
1507c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
1517c478bd9Sstevel@tonic-gate 	return (-1);
1527c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate #endif
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate int
scandir(const char * dirname,struct dirent * (* namelist[]),int (* select)(const struct dirent *),int (* dcomp)(const struct dirent **,const struct dirent **))1577c478bd9Sstevel@tonic-gate scandir(const char *dirname, struct dirent *(*namelist[]),
1587c478bd9Sstevel@tonic-gate     int (*select)(const struct dirent *),
1597c478bd9Sstevel@tonic-gate     int (*dcomp)(const struct dirent **, const struct dirent **))
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	struct dirent	*d, *p, **names = NULL;
1627c478bd9Sstevel@tonic-gate 	size_t	nitems = 0;
1637c478bd9Sstevel@tonic-gate 	size_t	arraysz, entlen;
1647c478bd9Sstevel@tonic-gate 	struct stat64	stb;
1657c478bd9Sstevel@tonic-gate 	DIR	*dirp;
1667c478bd9Sstevel@tonic-gate 	u_longlong_t	tmp_arraysz;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(dirname)) == NULL)
1697c478bd9Sstevel@tonic-gate 		return (-1);
1707c478bd9Sstevel@tonic-gate 	if (fstat64(dirp->dd_fd, &stb) < 0)
1717c478bd9Sstevel@tonic-gate 		goto fail;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	/*
1747c478bd9Sstevel@tonic-gate 	 * estimate the array size by taking the size of the directory file
1757c478bd9Sstevel@tonic-gate 	 * and dividing it by a multiple of the minimum size entry.
1767c478bd9Sstevel@tonic-gate 	 */
1777c478bd9Sstevel@tonic-gate 	tmp_arraysz = stb.st_size / 24;	/* 24 bytes on a 64-bit system */
1787c478bd9Sstevel@tonic-gate 	if (tmp_arraysz > INT_MAX)
1797c478bd9Sstevel@tonic-gate 		arraysz = INT_MAX;
1807c478bd9Sstevel@tonic-gate 	else
1817c478bd9Sstevel@tonic-gate 		arraysz = (size_t)tmp_arraysz;
1827c478bd9Sstevel@tonic-gate 	names = malloc(arraysz * sizeof (struct dirent *));
1837c478bd9Sstevel@tonic-gate 	if (names == NULL)
1847c478bd9Sstevel@tonic-gate 		goto fail;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	while ((d = readdir(dirp)) != NULL) {
1877c478bd9Sstevel@tonic-gate 		if (select != NULL && !(*select)(d))
1887c478bd9Sstevel@tonic-gate 			continue;	/* just selected names */
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		entlen = d->d_reclen;
1917c478bd9Sstevel@tonic-gate 		/*
1927c478bd9Sstevel@tonic-gate 		 * Make a minimum size copy of the data
1937c478bd9Sstevel@tonic-gate 		 */
1947c478bd9Sstevel@tonic-gate 		p = malloc(entlen);
1957c478bd9Sstevel@tonic-gate 		if (p == NULL)
1967c478bd9Sstevel@tonic-gate 			goto fail;
1977c478bd9Sstevel@tonic-gate 		(void) memcpy(p, d, entlen);
1987c478bd9Sstevel@tonic-gate 		/*
1997c478bd9Sstevel@tonic-gate 		 * Check to make sure the array has space left and
2007c478bd9Sstevel@tonic-gate 		 * realloc the maximum size.
2017c478bd9Sstevel@tonic-gate 		 */
2027c478bd9Sstevel@tonic-gate 		if (nitems >= arraysz) {
2037c478bd9Sstevel@tonic-gate 			struct dirent **tmp;
2047c478bd9Sstevel@tonic-gate 			if (nitems == INT_MAX) {
2057c478bd9Sstevel@tonic-gate 				/* overflow */
2067c478bd9Sstevel@tonic-gate 				free(p);
2077c478bd9Sstevel@tonic-gate 				errno = EOVERFLOW;
2087c478bd9Sstevel@tonic-gate 				goto fail;
2097c478bd9Sstevel@tonic-gate 			}
2107c478bd9Sstevel@tonic-gate 			arraysz += 512;		/* no science here */
2117c478bd9Sstevel@tonic-gate 			tmp = realloc(names,
2127c478bd9Sstevel@tonic-gate 			    arraysz * sizeof (struct dirent *));
2137c478bd9Sstevel@tonic-gate 			if (tmp == NULL) {
2147c478bd9Sstevel@tonic-gate 				free(p);
2157c478bd9Sstevel@tonic-gate 				goto fail;
2167c478bd9Sstevel@tonic-gate 			}
2177c478bd9Sstevel@tonic-gate 			names = tmp;
2187c478bd9Sstevel@tonic-gate 		}
2197c478bd9Sstevel@tonic-gate 		names[nitems++] = p;
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
2227c478bd9Sstevel@tonic-gate 	if (nitems && dcomp != NULL)
2237c478bd9Sstevel@tonic-gate 		qsort(names, nitems, sizeof (struct dirent *),
2247c478bd9Sstevel@tonic-gate 		    (int(*)(const void *, const void *))dcomp);
2257c478bd9Sstevel@tonic-gate 	*namelist = names;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	return ((int)nitems);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate fail:
2307c478bd9Sstevel@tonic-gate 	while (nitems != 0) {
2317c478bd9Sstevel@tonic-gate 		free(names[--nitems]);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 	if (names)
2347c478bd9Sstevel@tonic-gate 		free(names);
2357c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
2367c478bd9Sstevel@tonic-gate 	return (-1);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate /*
2407c478bd9Sstevel@tonic-gate  * Alphabetic order comparison routine for those who want it.
2417c478bd9Sstevel@tonic-gate  */
2427c478bd9Sstevel@tonic-gate int
alphasort(const struct dirent ** d1,const struct dirent ** d2)2437c478bd9Sstevel@tonic-gate alphasort(const struct dirent **d1, const struct dirent **d2)
2447c478bd9Sstevel@tonic-gate {
2457c478bd9Sstevel@tonic-gate 	return (strcoll((*d1)->d_name,
2467c478bd9Sstevel@tonic-gate 	    (*d2)->d_name));
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate #if !defined(_LP64)
2507c478bd9Sstevel@tonic-gate int
alphasort64(const struct dirent64 ** d1,const struct dirent64 ** d2)2517c478bd9Sstevel@tonic-gate alphasort64(const struct dirent64 **d1, const struct dirent64 **d2)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	return (strcoll((*d1)->d_name,
2547c478bd9Sstevel@tonic-gate 	    (*d2)->d_name));
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate #endif
257