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 2004 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 /* 43*0Sstevel@tonic-gate * Based on usr/src/ucblib/libucb/port/gen/scandir.c 44*0Sstevel@tonic-gate */ 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate /* 47*0Sstevel@tonic-gate * Scan the directory dirname calling select to make a list of selected 48*0Sstevel@tonic-gate * directory entries then sort using qsort and compare routine dcomp. 49*0Sstevel@tonic-gate * Returns the number of entries and a pointer to a list of pointers to 50*0Sstevel@tonic-gate * struct direct (through namelist). Returns -1 if there were any errors. 51*0Sstevel@tonic-gate */ 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate #include <sys/feature_tests.h> 54*0Sstevel@tonic-gate 55*0Sstevel@tonic-gate #pragma weak scandir = _scandir 56*0Sstevel@tonic-gate #pragma weak alphasort = _alphasort 57*0Sstevel@tonic-gate #if !defined(_LP64) 58*0Sstevel@tonic-gate #pragma weak scandir64 = _scandir64 59*0Sstevel@tonic-gate #pragma weak alphasort64 = _alphasort64 60*0Sstevel@tonic-gate #endif 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate #include "synonyms.h" 63*0Sstevel@tonic-gate #include <dirent.h> 64*0Sstevel@tonic-gate #include <errno.h> 65*0Sstevel@tonic-gate #include <sys/types.h> 66*0Sstevel@tonic-gate #include <sys/stat.h> 67*0Sstevel@tonic-gate #include <stdlib.h> 68*0Sstevel@tonic-gate #include <string.h> 69*0Sstevel@tonic-gate #include <limits.h> 70*0Sstevel@tonic-gate 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate #if !defined(_LP64) 73*0Sstevel@tonic-gate int 74*0Sstevel@tonic-gate scandir64(const char *dirname, struct dirent64 *(*namelist[]), 75*0Sstevel@tonic-gate int (*select)(const struct dirent64 *), 76*0Sstevel@tonic-gate int (*dcomp)(const struct dirent64 **, const struct dirent64 **)) 77*0Sstevel@tonic-gate { 78*0Sstevel@tonic-gate struct dirent64 *d, *p, **names = NULL; 79*0Sstevel@tonic-gate size_t nitems = 0; 80*0Sstevel@tonic-gate size_t arraysz, entlen; 81*0Sstevel@tonic-gate struct stat64 stb; 82*0Sstevel@tonic-gate DIR *dirp; 83*0Sstevel@tonic-gate u_longlong_t tmp_arraysz; 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gate if ((dirp = opendir(dirname)) == NULL) 86*0Sstevel@tonic-gate return (-1); 87*0Sstevel@tonic-gate if (fstat64(dirp->dd_fd, &stb) < 0) 88*0Sstevel@tonic-gate goto fail; 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate /* 91*0Sstevel@tonic-gate * estimate the array size by taking the size of the directory file 92*0Sstevel@tonic-gate * and dividing it by a multiple of the minimum size entry. 93*0Sstevel@tonic-gate */ 94*0Sstevel@tonic-gate tmp_arraysz = stb.st_size / 24; /* 24 bytes on a 64-bit system */ 95*0Sstevel@tonic-gate if (tmp_arraysz > INT_MAX) 96*0Sstevel@tonic-gate arraysz = INT_MAX; 97*0Sstevel@tonic-gate else 98*0Sstevel@tonic-gate arraysz = (size_t)tmp_arraysz; 99*0Sstevel@tonic-gate names = malloc(arraysz * sizeof (struct dirent64 *)); 100*0Sstevel@tonic-gate if (names == NULL) 101*0Sstevel@tonic-gate goto fail; 102*0Sstevel@tonic-gate 103*0Sstevel@tonic-gate while ((d = readdir64(dirp)) != NULL) { 104*0Sstevel@tonic-gate if (select != NULL && !(*select)(d)) 105*0Sstevel@tonic-gate continue; /* just selected names */ 106*0Sstevel@tonic-gate 107*0Sstevel@tonic-gate entlen = d->d_reclen; 108*0Sstevel@tonic-gate /* 109*0Sstevel@tonic-gate * Make a minimum size copy of the data 110*0Sstevel@tonic-gate */ 111*0Sstevel@tonic-gate p = malloc(entlen); 112*0Sstevel@tonic-gate if (p == NULL) 113*0Sstevel@tonic-gate goto fail; 114*0Sstevel@tonic-gate (void) memcpy(p, d, entlen); 115*0Sstevel@tonic-gate /* 116*0Sstevel@tonic-gate * Check to make sure the array has space left and 117*0Sstevel@tonic-gate * realloc the maximum size. 118*0Sstevel@tonic-gate */ 119*0Sstevel@tonic-gate if (nitems >= arraysz) { 120*0Sstevel@tonic-gate struct dirent64 **tmp; 121*0Sstevel@tonic-gate if (nitems == INT_MAX) { 122*0Sstevel@tonic-gate /* overflow */ 123*0Sstevel@tonic-gate free(p); 124*0Sstevel@tonic-gate errno = EOVERFLOW; 125*0Sstevel@tonic-gate goto fail; 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate arraysz += 512; /* no science here */ 128*0Sstevel@tonic-gate tmp = realloc(names, 129*0Sstevel@tonic-gate arraysz * sizeof (struct dirent64 *)); 130*0Sstevel@tonic-gate if (tmp == NULL) { 131*0Sstevel@tonic-gate free(p); 132*0Sstevel@tonic-gate goto fail; 133*0Sstevel@tonic-gate } 134*0Sstevel@tonic-gate names = tmp; 135*0Sstevel@tonic-gate } 136*0Sstevel@tonic-gate names[nitems++] = p; 137*0Sstevel@tonic-gate } 138*0Sstevel@tonic-gate (void) closedir(dirp); 139*0Sstevel@tonic-gate if (nitems && dcomp != NULL) 140*0Sstevel@tonic-gate qsort(names, nitems, sizeof (struct dirent64 *), 141*0Sstevel@tonic-gate (int(*)(const void *, const void *))dcomp); 142*0Sstevel@tonic-gate *namelist = names; 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate return ((int)nitems); 145*0Sstevel@tonic-gate 146*0Sstevel@tonic-gate fail: 147*0Sstevel@tonic-gate while (nitems != 0) { 148*0Sstevel@tonic-gate free(names[--nitems]); 149*0Sstevel@tonic-gate } 150*0Sstevel@tonic-gate if (names) 151*0Sstevel@tonic-gate free(names); 152*0Sstevel@tonic-gate (void) closedir(dirp); 153*0Sstevel@tonic-gate return (-1); 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate #endif 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate int 159*0Sstevel@tonic-gate scandir(const char *dirname, struct dirent *(*namelist[]), 160*0Sstevel@tonic-gate int (*select)(const struct dirent *), 161*0Sstevel@tonic-gate int (*dcomp)(const struct dirent **, const struct dirent **)) 162*0Sstevel@tonic-gate { 163*0Sstevel@tonic-gate struct dirent *d, *p, **names = NULL; 164*0Sstevel@tonic-gate size_t nitems = 0; 165*0Sstevel@tonic-gate size_t arraysz, entlen; 166*0Sstevel@tonic-gate struct stat64 stb; 167*0Sstevel@tonic-gate DIR *dirp; 168*0Sstevel@tonic-gate u_longlong_t tmp_arraysz; 169*0Sstevel@tonic-gate 170*0Sstevel@tonic-gate if ((dirp = opendir(dirname)) == NULL) 171*0Sstevel@tonic-gate return (-1); 172*0Sstevel@tonic-gate if (fstat64(dirp->dd_fd, &stb) < 0) 173*0Sstevel@tonic-gate goto fail; 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate /* 176*0Sstevel@tonic-gate * estimate the array size by taking the size of the directory file 177*0Sstevel@tonic-gate * and dividing it by a multiple of the minimum size entry. 178*0Sstevel@tonic-gate */ 179*0Sstevel@tonic-gate tmp_arraysz = stb.st_size / 24; /* 24 bytes on a 64-bit system */ 180*0Sstevel@tonic-gate if (tmp_arraysz > INT_MAX) 181*0Sstevel@tonic-gate arraysz = INT_MAX; 182*0Sstevel@tonic-gate else 183*0Sstevel@tonic-gate arraysz = (size_t)tmp_arraysz; 184*0Sstevel@tonic-gate names = malloc(arraysz * sizeof (struct dirent *)); 185*0Sstevel@tonic-gate if (names == NULL) 186*0Sstevel@tonic-gate goto fail; 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate while ((d = readdir(dirp)) != NULL) { 189*0Sstevel@tonic-gate if (select != NULL && !(*select)(d)) 190*0Sstevel@tonic-gate continue; /* just selected names */ 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate entlen = d->d_reclen; 193*0Sstevel@tonic-gate /* 194*0Sstevel@tonic-gate * Make a minimum size copy of the data 195*0Sstevel@tonic-gate */ 196*0Sstevel@tonic-gate p = malloc(entlen); 197*0Sstevel@tonic-gate if (p == NULL) 198*0Sstevel@tonic-gate goto fail; 199*0Sstevel@tonic-gate (void) memcpy(p, d, entlen); 200*0Sstevel@tonic-gate /* 201*0Sstevel@tonic-gate * Check to make sure the array has space left and 202*0Sstevel@tonic-gate * realloc the maximum size. 203*0Sstevel@tonic-gate */ 204*0Sstevel@tonic-gate if (nitems >= arraysz) { 205*0Sstevel@tonic-gate struct dirent **tmp; 206*0Sstevel@tonic-gate if (nitems == INT_MAX) { 207*0Sstevel@tonic-gate /* overflow */ 208*0Sstevel@tonic-gate free(p); 209*0Sstevel@tonic-gate errno = EOVERFLOW; 210*0Sstevel@tonic-gate goto fail; 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate arraysz += 512; /* no science here */ 213*0Sstevel@tonic-gate tmp = realloc(names, 214*0Sstevel@tonic-gate arraysz * sizeof (struct dirent *)); 215*0Sstevel@tonic-gate if (tmp == NULL) { 216*0Sstevel@tonic-gate free(p); 217*0Sstevel@tonic-gate goto fail; 218*0Sstevel@tonic-gate } 219*0Sstevel@tonic-gate names = tmp; 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate names[nitems++] = p; 222*0Sstevel@tonic-gate } 223*0Sstevel@tonic-gate (void) closedir(dirp); 224*0Sstevel@tonic-gate if (nitems && dcomp != NULL) 225*0Sstevel@tonic-gate qsort(names, nitems, sizeof (struct dirent *), 226*0Sstevel@tonic-gate (int(*)(const void *, const void *))dcomp); 227*0Sstevel@tonic-gate *namelist = names; 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate return ((int)nitems); 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate fail: 232*0Sstevel@tonic-gate while (nitems != 0) { 233*0Sstevel@tonic-gate free(names[--nitems]); 234*0Sstevel@tonic-gate } 235*0Sstevel@tonic-gate if (names) 236*0Sstevel@tonic-gate free(names); 237*0Sstevel@tonic-gate (void) closedir(dirp); 238*0Sstevel@tonic-gate return (-1); 239*0Sstevel@tonic-gate } 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate /* 242*0Sstevel@tonic-gate * Alphabetic order comparison routine for those who want it. 243*0Sstevel@tonic-gate */ 244*0Sstevel@tonic-gate int 245*0Sstevel@tonic-gate alphasort(const struct dirent **d1, const struct dirent **d2) 246*0Sstevel@tonic-gate { 247*0Sstevel@tonic-gate return (strcoll((*d1)->d_name, 248*0Sstevel@tonic-gate (*d2)->d_name)); 249*0Sstevel@tonic-gate } 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate #if !defined(_LP64) 252*0Sstevel@tonic-gate int 253*0Sstevel@tonic-gate alphasort64(const struct dirent64 **d1, const struct dirent64 **d2) 254*0Sstevel@tonic-gate { 255*0Sstevel@tonic-gate return (strcoll((*d1)->d_name, 256*0Sstevel@tonic-gate (*d2)->d_name)); 257*0Sstevel@tonic-gate } 258*0Sstevel@tonic-gate #endif 259