sl@0: /* sl@0: * Copyright (c) 1983, 1993 sl@0: * The Regents of the University of California. All rights reserved. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the above copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 4. Neither the name of the University nor the names of its contributors sl@0: * may be used to endorse or promote products derived from this software sl@0: * without specific prior written permission. sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * © * Portions Copyright (c) 2006-2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. sl@0: */ sl@0: sl@0: #if defined(LIBC_SCCS) && !defined(lint) sl@0: static char sccsid[] = "@(#)scandir.c 8.3 (Berkeley) 1/2/94"; sl@0: #endif /* LIBC_SCCS and not lint */ sl@0: #include sl@0: __FBSDID("$FreeBSD: src/lib/libc/gen/scandir.c,v 1.7 2002/02/01 01:32:19 obrien Exp $"); sl@0: sl@0: /* sl@0: * Scan the directory dirname calling select to make a list of selected sl@0: * directory entries then sort using qsort and compare routine dcomp. sl@0: * Returns the number of entries and a pointer to a list of pointers to sl@0: * struct dirent (through namelist). Returns -1 if there were any errors. sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /* sl@0: * The DIRSIZ macro is the minimum record length which will hold the directory sl@0: * entry. This requires the amount of space in struct dirent without the sl@0: * d_name field, plus enough space for the name and a terminating nul byte sl@0: * (dp->d_namlen + 1), rounded up to a 4 byte boundary. sl@0: */ sl@0: #undef DIRSIZ sl@0: #define DIRSIZ(dp) \ sl@0: ((sizeof(struct dirent) - sizeof(dp)->d_name) + \ sl@0: (((dp)->d_namlen + 1 + 3) &~ 3)) sl@0: sl@0: EXPORT_C sl@0: int sl@0: scandir(dirname, namelist, select, dcomp) sl@0: const char *dirname; sl@0: struct dirent ***namelist; sl@0: int (*select)(struct dirent *); sl@0: int (*dcomp)(const void *, const void *); sl@0: { sl@0: struct dirent *d, *p, **names = NULL; sl@0: size_t nitems = 0; sl@0: long arraysz; sl@0: DIR *dirp; sl@0: sl@0: if ((dirp = opendir(dirname)) == NULL) sl@0: return(-1); sl@0: /* sl@0: * consider the number of entries in the dir is 10, initially. sl@0: */ sl@0: arraysz = 10; sl@0: names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *)); sl@0: if (names == NULL) sl@0: goto fail; sl@0: sl@0: while ((d = readdir(dirp)) != NULL) { sl@0: if (select != NULL && !(*select)(d)) sl@0: continue; /* just selected names */ sl@0: /* sl@0: * Make a minimum size copy of the data sl@0: */ sl@0: p = (struct dirent *)malloc(DIRSIZ(d)); sl@0: if (p == NULL) sl@0: goto fail; sl@0: p->d_fileno = d->d_fileno; sl@0: p->d_type = d->d_type; sl@0: p->d_reclen = d->d_reclen; sl@0: p->d_namlen = d->d_namlen; sl@0: bcopy(d->d_name, p->d_name, p->d_namlen + 1); sl@0: sl@0: /* sl@0: * Check to make sure the array has space left and sl@0: * realloc the maximum size. sl@0: */ sl@0: if (nitems >= arraysz) { sl@0: const int inc = 10; /* increase by this much */ sl@0: struct dirent **names2; sl@0: sl@0: names2 = (struct dirent **)realloc((char *)names, sl@0: (arraysz + inc) * sizeof(struct dirent *)); sl@0: if (names2 == NULL) { sl@0: free(p); sl@0: goto fail; sl@0: } sl@0: names = names2; sl@0: arraysz += inc; sl@0: } sl@0: names[nitems++] = p; sl@0: } sl@0: closedir(dirp); sl@0: if (nitems && dcomp != NULL) sl@0: qsort(names, nitems, sizeof(struct dirent *), dcomp); sl@0: *namelist = names; sl@0: return(nitems); sl@0: sl@0: fail: sl@0: while (nitems > 0) sl@0: free(names[--nitems]); sl@0: free(names); sl@0: closedir(dirp); sl@0: return -1; sl@0: } sl@0: sl@0: /* sl@0: * Alphabetic order comparison routine for those who want it. sl@0: */ sl@0: EXPORT_C sl@0: int sl@0: alphasort(d1, d2) sl@0: const void *d1; sl@0: const void *d2; sl@0: { sl@0: return(strcmp((*(struct dirent **)d1)->d_name, sl@0: (*(struct dirent **)d2)->d_name)); sl@0: }