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 #pragma ident "%Z%%M% %I% %E% SMI" 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate /* 437c478bd9Sstevel@tonic-gate * Based on usr/src/ucblib/libucb/port/gen/scandir.c 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* 477c478bd9Sstevel@tonic-gate * Scan the directory dirname calling select to make a list of selected 487c478bd9Sstevel@tonic-gate * directory entries then sort using qsort and compare routine dcomp. 497c478bd9Sstevel@tonic-gate * Returns the number of entries and a pointer to a list of pointers to 507c478bd9Sstevel@tonic-gate * struct direct (through namelist). Returns -1 if there were any errors. 517c478bd9Sstevel@tonic-gate */ 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #include <sys/feature_tests.h> 547c478bd9Sstevel@tonic-gate 55*7257d1b4Sraf #pragma weak _scandir = scandir 56*7257d1b4Sraf #pragma weak _alphasort = alphasort 577c478bd9Sstevel@tonic-gate #if !defined(_LP64) 58*7257d1b4Sraf #pragma weak _scandir64 = scandir64 59*7257d1b4Sraf #pragma weak _alphasort64 = alphasort64 607c478bd9Sstevel@tonic-gate #endif 617c478bd9Sstevel@tonic-gate 62*7257d1b4Sraf #include "lint.h" 637c478bd9Sstevel@tonic-gate #include <dirent.h> 647c478bd9Sstevel@tonic-gate #include <errno.h> 657c478bd9Sstevel@tonic-gate #include <sys/types.h> 667c478bd9Sstevel@tonic-gate #include <sys/stat.h> 677c478bd9Sstevel@tonic-gate #include <stdlib.h> 687c478bd9Sstevel@tonic-gate #include <string.h> 697c478bd9Sstevel@tonic-gate #include <limits.h> 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #if !defined(_LP64) 737c478bd9Sstevel@tonic-gate int 747c478bd9Sstevel@tonic-gate scandir64(const char *dirname, struct dirent64 *(*namelist[]), 757c478bd9Sstevel@tonic-gate int (*select)(const struct dirent64 *), 767c478bd9Sstevel@tonic-gate int (*dcomp)(const struct dirent64 **, const struct dirent64 **)) 777c478bd9Sstevel@tonic-gate { 787c478bd9Sstevel@tonic-gate struct dirent64 *d, *p, **names = NULL; 797c478bd9Sstevel@tonic-gate size_t nitems = 0; 807c478bd9Sstevel@tonic-gate size_t arraysz, entlen; 817c478bd9Sstevel@tonic-gate struct stat64 stb; 827c478bd9Sstevel@tonic-gate DIR *dirp; 837c478bd9Sstevel@tonic-gate u_longlong_t tmp_arraysz; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate if ((dirp = opendir(dirname)) == NULL) 867c478bd9Sstevel@tonic-gate return (-1); 877c478bd9Sstevel@tonic-gate if (fstat64(dirp->dd_fd, &stb) < 0) 887c478bd9Sstevel@tonic-gate goto fail; 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate /* 917c478bd9Sstevel@tonic-gate * estimate the array size by taking the size of the directory file 927c478bd9Sstevel@tonic-gate * and dividing it by a multiple of the minimum size entry. 937c478bd9Sstevel@tonic-gate */ 947c478bd9Sstevel@tonic-gate tmp_arraysz = stb.st_size / 24; /* 24 bytes on a 64-bit system */ 957c478bd9Sstevel@tonic-gate if (tmp_arraysz > INT_MAX) 967c478bd9Sstevel@tonic-gate arraysz = INT_MAX; 977c478bd9Sstevel@tonic-gate else 987c478bd9Sstevel@tonic-gate arraysz = (size_t)tmp_arraysz; 997c478bd9Sstevel@tonic-gate names = malloc(arraysz * sizeof (struct dirent64 *)); 1007c478bd9Sstevel@tonic-gate if (names == NULL) 1017c478bd9Sstevel@tonic-gate goto fail; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate while ((d = readdir64(dirp)) != NULL) { 1047c478bd9Sstevel@tonic-gate if (select != NULL && !(*select)(d)) 1057c478bd9Sstevel@tonic-gate continue; /* just selected names */ 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate entlen = d->d_reclen; 1087c478bd9Sstevel@tonic-gate /* 1097c478bd9Sstevel@tonic-gate * Make a minimum size copy of the data 1107c478bd9Sstevel@tonic-gate */ 1117c478bd9Sstevel@tonic-gate p = malloc(entlen); 1127c478bd9Sstevel@tonic-gate if (p == NULL) 1137c478bd9Sstevel@tonic-gate goto fail; 1147c478bd9Sstevel@tonic-gate (void) memcpy(p, d, entlen); 1157c478bd9Sstevel@tonic-gate /* 1167c478bd9Sstevel@tonic-gate * Check to make sure the array has space left and 1177c478bd9Sstevel@tonic-gate * realloc the maximum size. 1187c478bd9Sstevel@tonic-gate */ 1197c478bd9Sstevel@tonic-gate if (nitems >= arraysz) { 1207c478bd9Sstevel@tonic-gate struct dirent64 **tmp; 1217c478bd9Sstevel@tonic-gate if (nitems == INT_MAX) { 1227c478bd9Sstevel@tonic-gate /* overflow */ 1237c478bd9Sstevel@tonic-gate free(p); 1247c478bd9Sstevel@tonic-gate errno = EOVERFLOW; 1257c478bd9Sstevel@tonic-gate goto fail; 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate arraysz += 512; /* no science here */ 1287c478bd9Sstevel@tonic-gate tmp = realloc(names, 1297c478bd9Sstevel@tonic-gate arraysz * sizeof (struct dirent64 *)); 1307c478bd9Sstevel@tonic-gate if (tmp == NULL) { 1317c478bd9Sstevel@tonic-gate free(p); 1327c478bd9Sstevel@tonic-gate goto fail; 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate names = tmp; 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate names[nitems++] = p; 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate (void) closedir(dirp); 1397c478bd9Sstevel@tonic-gate if (nitems && dcomp != NULL) 1407c478bd9Sstevel@tonic-gate qsort(names, nitems, sizeof (struct dirent64 *), 1417c478bd9Sstevel@tonic-gate (int(*)(const void *, const void *))dcomp); 1427c478bd9Sstevel@tonic-gate *namelist = names; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate return ((int)nitems); 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate fail: 1477c478bd9Sstevel@tonic-gate while (nitems != 0) { 1487c478bd9Sstevel@tonic-gate free(names[--nitems]); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate if (names) 1517c478bd9Sstevel@tonic-gate free(names); 1527c478bd9Sstevel@tonic-gate (void) closedir(dirp); 1537c478bd9Sstevel@tonic-gate return (-1); 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate #endif 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate int 1597c478bd9Sstevel@tonic-gate scandir(const char *dirname, struct dirent *(*namelist[]), 1607c478bd9Sstevel@tonic-gate int (*select)(const struct dirent *), 1617c478bd9Sstevel@tonic-gate int (*dcomp)(const struct dirent **, const struct dirent **)) 1627c478bd9Sstevel@tonic-gate { 1637c478bd9Sstevel@tonic-gate struct dirent *d, *p, **names = NULL; 1647c478bd9Sstevel@tonic-gate size_t nitems = 0; 1657c478bd9Sstevel@tonic-gate size_t arraysz, entlen; 1667c478bd9Sstevel@tonic-gate struct stat64 stb; 1677c478bd9Sstevel@tonic-gate DIR *dirp; 1687c478bd9Sstevel@tonic-gate u_longlong_t tmp_arraysz; 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate if ((dirp = opendir(dirname)) == NULL) 1717c478bd9Sstevel@tonic-gate return (-1); 1727c478bd9Sstevel@tonic-gate if (fstat64(dirp->dd_fd, &stb) < 0) 1737c478bd9Sstevel@tonic-gate goto fail; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* 1767c478bd9Sstevel@tonic-gate * estimate the array size by taking the size of the directory file 1777c478bd9Sstevel@tonic-gate * and dividing it by a multiple of the minimum size entry. 1787c478bd9Sstevel@tonic-gate */ 1797c478bd9Sstevel@tonic-gate tmp_arraysz = stb.st_size / 24; /* 24 bytes on a 64-bit system */ 1807c478bd9Sstevel@tonic-gate if (tmp_arraysz > INT_MAX) 1817c478bd9Sstevel@tonic-gate arraysz = INT_MAX; 1827c478bd9Sstevel@tonic-gate else 1837c478bd9Sstevel@tonic-gate arraysz = (size_t)tmp_arraysz; 1847c478bd9Sstevel@tonic-gate names = malloc(arraysz * sizeof (struct dirent *)); 1857c478bd9Sstevel@tonic-gate if (names == NULL) 1867c478bd9Sstevel@tonic-gate goto fail; 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate while ((d = readdir(dirp)) != NULL) { 1897c478bd9Sstevel@tonic-gate if (select != NULL && !(*select)(d)) 1907c478bd9Sstevel@tonic-gate continue; /* just selected names */ 1917c478bd9Sstevel@tonic-gate 1927c478bd9Sstevel@tonic-gate entlen = d->d_reclen; 1937c478bd9Sstevel@tonic-gate /* 1947c478bd9Sstevel@tonic-gate * Make a minimum size copy of the data 1957c478bd9Sstevel@tonic-gate */ 1967c478bd9Sstevel@tonic-gate p = malloc(entlen); 1977c478bd9Sstevel@tonic-gate if (p == NULL) 1987c478bd9Sstevel@tonic-gate goto fail; 1997c478bd9Sstevel@tonic-gate (void) memcpy(p, d, entlen); 2007c478bd9Sstevel@tonic-gate /* 2017c478bd9Sstevel@tonic-gate * Check to make sure the array has space left and 2027c478bd9Sstevel@tonic-gate * realloc the maximum size. 2037c478bd9Sstevel@tonic-gate */ 2047c478bd9Sstevel@tonic-gate if (nitems >= arraysz) { 2057c478bd9Sstevel@tonic-gate struct dirent **tmp; 2067c478bd9Sstevel@tonic-gate if (nitems == INT_MAX) { 2077c478bd9Sstevel@tonic-gate /* overflow */ 2087c478bd9Sstevel@tonic-gate free(p); 2097c478bd9Sstevel@tonic-gate errno = EOVERFLOW; 2107c478bd9Sstevel@tonic-gate goto fail; 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate arraysz += 512; /* no science here */ 2137c478bd9Sstevel@tonic-gate tmp = realloc(names, 2147c478bd9Sstevel@tonic-gate arraysz * sizeof (struct dirent *)); 2157c478bd9Sstevel@tonic-gate if (tmp == NULL) { 2167c478bd9Sstevel@tonic-gate free(p); 2177c478bd9Sstevel@tonic-gate goto fail; 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate names = tmp; 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate names[nitems++] = p; 2227c478bd9Sstevel@tonic-gate } 2237c478bd9Sstevel@tonic-gate (void) closedir(dirp); 2247c478bd9Sstevel@tonic-gate if (nitems && dcomp != NULL) 2257c478bd9Sstevel@tonic-gate qsort(names, nitems, sizeof (struct dirent *), 2267c478bd9Sstevel@tonic-gate (int(*)(const void *, const void *))dcomp); 2277c478bd9Sstevel@tonic-gate *namelist = names; 2287c478bd9Sstevel@tonic-gate 2297c478bd9Sstevel@tonic-gate return ((int)nitems); 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate fail: 2327c478bd9Sstevel@tonic-gate while (nitems != 0) { 2337c478bd9Sstevel@tonic-gate free(names[--nitems]); 2347c478bd9Sstevel@tonic-gate } 2357c478bd9Sstevel@tonic-gate if (names) 2367c478bd9Sstevel@tonic-gate free(names); 2377c478bd9Sstevel@tonic-gate (void) closedir(dirp); 2387c478bd9Sstevel@tonic-gate return (-1); 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate /* 2427c478bd9Sstevel@tonic-gate * Alphabetic order comparison routine for those who want it. 2437c478bd9Sstevel@tonic-gate */ 2447c478bd9Sstevel@tonic-gate int 2457c478bd9Sstevel@tonic-gate alphasort(const struct dirent **d1, const struct dirent **d2) 2467c478bd9Sstevel@tonic-gate { 2477c478bd9Sstevel@tonic-gate return (strcoll((*d1)->d_name, 2487c478bd9Sstevel@tonic-gate (*d2)->d_name)); 2497c478bd9Sstevel@tonic-gate } 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate #if !defined(_LP64) 2527c478bd9Sstevel@tonic-gate int 2537c478bd9Sstevel@tonic-gate alphasort64(const struct dirent64 **d1, const struct dirent64 **d2) 2547c478bd9Sstevel@tonic-gate { 2557c478bd9Sstevel@tonic-gate return (strcoll((*d1)->d_name, 2567c478bd9Sstevel@tonic-gate (*d2)->d_name)); 2577c478bd9Sstevel@tonic-gate } 2587c478bd9Sstevel@tonic-gate #endif 259