os/ossrv/genericopenlibs/openenvcore/libc/src/scandir.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericopenlibs/openenvcore/libc/src/scandir.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,144 @@
     1.4 +/*
     1.5 + * Copyright (c) 1983, 1993
     1.6 + *	The Regents of the University of California.  All rights reserved.
     1.7 + *
     1.8 + * Redistribution and use in source and binary forms, with or without
     1.9 + * modification, are permitted provided that the following conditions
    1.10 + * are met:
    1.11 + * 1. Redistributions of source code must retain the above copyright
    1.12 + *    notice, this list of conditions and the following disclaimer.
    1.13 + * 2. Redistributions in binary form must reproduce the above copyright
    1.14 + *    notice, this list of conditions and the following disclaimer in the
    1.15 + *    documentation and/or other materials provided with the distribution.
    1.16 + * 4. Neither the name of the University nor the names of its contributors
    1.17 + *    may be used to endorse or promote products derived from this software
    1.18 + *    without specific prior written permission.
    1.19 + *
    1.20 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    1.21 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.22 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.23 + * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    1.24 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    1.25 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    1.26 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.27 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    1.28 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    1.29 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    1.30 + * SUCH DAMAGE.
    1.31 + * © * Portions Copyright (c) 2006-2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. 
    1.32 + */
    1.33 +
    1.34 +#if defined(LIBC_SCCS) && !defined(lint)
    1.35 +static char sccsid[] = "@(#)scandir.c	8.3 (Berkeley) 1/2/94";
    1.36 +#endif /* LIBC_SCCS and not lint */
    1.37 +#include <sys/cdefs.h>
    1.38 +__FBSDID("$FreeBSD: src/lib/libc/gen/scandir.c,v 1.7 2002/02/01 01:32:19 obrien Exp $");
    1.39 +
    1.40 +/*
    1.41 + * Scan the directory dirname calling select to make a list of selected
    1.42 + * directory entries then sort using qsort and compare routine dcomp.
    1.43 + * Returns the number of entries and a pointer to a list of pointers to
    1.44 + * struct dirent (through namelist). Returns -1 if there were any errors.
    1.45 + */
    1.46 +
    1.47 +#include <sys/types.h>
    1.48 +#include <sys/stat.h>
    1.49 +#include <dirent.h>
    1.50 +#include <stdlib.h>
    1.51 +#include <string.h>
    1.52 +#include <fcntl.h>
    1.53 +
    1.54 +/*
    1.55 + * The DIRSIZ macro is the minimum record length which will hold the directory
    1.56 + * entry.  This requires the amount of space in struct dirent without the
    1.57 + * d_name field, plus enough space for the name and a terminating nul byte
    1.58 + * (dp->d_namlen + 1), rounded up to a 4 byte boundary.
    1.59 + */
    1.60 +#undef DIRSIZ
    1.61 +#define DIRSIZ(dp)							\
    1.62 +	((sizeof(struct dirent) - sizeof(dp)->d_name) +			\
    1.63 +	    (((dp)->d_namlen + 1 + 3) &~ 3))
    1.64 +
    1.65 +EXPORT_C
    1.66 +int
    1.67 +scandir(dirname, namelist, select, dcomp)
    1.68 +	const char *dirname;
    1.69 +	struct dirent ***namelist;
    1.70 +	int (*select)(struct dirent *);
    1.71 +	int (*dcomp)(const void *, const void *);
    1.72 +{
    1.73 +	struct dirent *d, *p, **names = NULL;
    1.74 +	size_t nitems = 0;
    1.75 +	long arraysz;
    1.76 +	DIR *dirp;
    1.77 +
    1.78 +	if ((dirp = opendir(dirname)) == NULL)
    1.79 +		return(-1);
    1.80 +	/*
    1.81 +	 * consider the number of entries in the dir is 10, initially.
    1.82 +	 */
    1.83 +	arraysz = 10;
    1.84 +	names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
    1.85 +	if (names == NULL)
    1.86 +		goto fail;
    1.87 +	
    1.88 +	while ((d = readdir(dirp)) != NULL) {
    1.89 +		if (select != NULL && !(*select)(d))
    1.90 +			continue;	/* just selected names */
    1.91 +		/*
    1.92 +		 * Make a minimum size copy of the data
    1.93 +		 */
    1.94 +		p = (struct dirent *)malloc(DIRSIZ(d));
    1.95 +		if (p == NULL)
    1.96 +			goto fail;
    1.97 +		p->d_fileno = d->d_fileno;
    1.98 +		p->d_type = d->d_type;
    1.99 +		p->d_reclen = d->d_reclen;
   1.100 +		p->d_namlen = d->d_namlen;
   1.101 +		bcopy(d->d_name, p->d_name, p->d_namlen + 1);
   1.102 +		
   1.103 +		/*
   1.104 +		 * Check to make sure the array has space left and
   1.105 +		 * realloc the maximum size.
   1.106 +		 */
   1.107 +		if (nitems >= arraysz) {
   1.108 +			const int inc = 10;	/* increase by this much */
   1.109 +			struct dirent **names2;
   1.110 +
   1.111 +			names2 = (struct dirent **)realloc((char *)names,
   1.112 +				(arraysz + inc) * sizeof(struct dirent *));
   1.113 +			if (names2 == NULL) {
   1.114 +				free(p);
   1.115 +				goto fail;
   1.116 +			}
   1.117 +			names = names2;
   1.118 +			arraysz += inc;
   1.119 +		}
   1.120 +		names[nitems++] = p;
   1.121 +	}
   1.122 +	closedir(dirp);
   1.123 +	if (nitems && dcomp != NULL)
   1.124 +		qsort(names, nitems, sizeof(struct dirent *), dcomp);
   1.125 +	*namelist = names;
   1.126 +	return(nitems);
   1.127 +
   1.128 +fail:
   1.129 +	while (nitems > 0)
   1.130 +		free(names[--nitems]);
   1.131 +	free(names);
   1.132 +	closedir(dirp);
   1.133 +	return -1;
   1.134 +}
   1.135 +
   1.136 +/*
   1.137 + * Alphabetic order comparison routine for those who want it.
   1.138 + */
   1.139 +EXPORT_C
   1.140 +int
   1.141 +alphasort(d1, d2)
   1.142 +	const void *d1;
   1.143 +	const void *d2;
   1.144 +{
   1.145 +	return(strcmp((*(struct dirent **)d1)->d_name,
   1.146 +	    (*(struct dirent **)d2)->d_name));
   1.147 +}