OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [posix/] [scandir.c] - Blame information for rev 1773

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1010 ivang
#ifndef HAVE_OPENDIR
2
 
3
/*
4
 * Copyright (c) 1983 Regents of the University of California.
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 * 3. All advertising materials mentioning features or use of this software
16
 *    must display the following acknowledgement:
17
 *      This product includes software developed by the University of
18
 *      California, Berkeley and its contributors.
19
 * 4. Neither the name of the University nor the names of its contributors
20
 *    may be used to endorse or promote products derived from this software
21
 *    without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33
 * SUCH DAMAGE.
34
 */
35
 
36
#if defined(LIBC_SCCS) && !defined(lint)
37
static char sccsid[] = "@(#)scandir.c   5.10 (Berkeley) 2/23/91";
38
#endif /* LIBC_SCCS and not lint */
39
 
40
/*
41
 * Scan the directory dirname calling select to make a list of selected
42
 * directory entries then sort using qsort and compare routine dcomp.
43
 * Returns the number of entries and a pointer to a list of pointers to
44
 * struct dirent (through namelist). Returns -1 if there were any errors.
45
 */
46
 
47
#include <sys/types.h>
48
#include <sys/stat.h>
49
#include <dirent.h>
50
#include <stdlib.h>
51
#include <string.h>
52
 
53
/*
54
 * The DIRSIZ macro gives the minimum record length which will hold
55
 * the directory entry.  This requires the amount of space in struct dirent
56
 * without the d_name field, plus enough space for the name with a terminating
57
 * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
58
 */
59
#undef DIRSIZ
60
#ifdef _DIRENT_HAVE_D_NAMLEN
61
#define DIRSIZ(dp) \
62
    ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
63
#else
64
#define DIRSIZ(dp) \
65
    ((sizeof (struct dirent) - (MAXNAMLEN+1)) + ((strlen((dp)->d_name)+1 + 3) &~ 3))
66
#endif
67
 
68
#ifndef __P
69
#define __P(args) ()
70
#endif
71
 
72
int
73
scandir(dirname, namelist, select, dcomp)
74
        const char *dirname;
75
        struct dirent ***namelist;
76
        int (*select) __P((struct dirent *));
77
        int (*dcomp) __P((const void *, const void *));
78
{
79
        register struct dirent *d, *p, **names;
80
        register size_t nitems;
81
        struct stat stb;
82
        long arraysz;
83
        DIR *dirp;
84
 
85
        if ((dirp = opendir(dirname)) == NULL)
86
                return(-1);
87
        if (fstat(dirp->dd_fd, &stb) < 0)
88
                return(-1);
89
 
90
        /*
91
         * estimate the array size by taking the size of the directory file
92
         * and dividing it by a multiple of the minimum size entry.
93
         */
94
        arraysz = (stb.st_size / 24);
95
        names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
96
        if (names == NULL)
97
                return(-1);
98
 
99
        nitems = 0;
100
        while ((d = readdir(dirp)) != NULL) {
101
                if (select != NULL && !(*select)(d))
102
                        continue;       /* just selected names */
103
                /*
104
                 * Make a minimum size copy of the data
105
                 */
106
                p = (struct dirent *)malloc(DIRSIZ(d));
107
                if (p == NULL)
108
                        return(-1);
109
                p->d_ino = d->d_ino;
110
                p->d_reclen = d->d_reclen;
111
#ifdef _DIRENT_HAVE_D_NAMLEN
112
                p->d_namlen = d->d_namlen;
113
                bcopy(d->d_name, p->d_name, p->d_namlen + 1);
114
#else
115
               strcpy(p->d_name, d->d_name);
116
#endif
117
                /*
118
                 * Check to make sure the array has space left and
119
                 * realloc the maximum size.
120
                 */
121
                if (++nitems >= arraysz) {
122
                        if (fstat(dirp->dd_fd, &stb) < 0)
123
                                return(-1);     /* just might have grown */
124
                        arraysz = stb.st_size / 12;
125
                        names = (struct dirent **)realloc((char *)names,
126
                                arraysz * sizeof(struct dirent *));
127
                        if (names == NULL)
128
                                return(-1);
129
                }
130
                names[nitems-1] = p;
131
        }
132
        closedir(dirp);
133
        if (nitems && dcomp != NULL)
134
                qsort(names, nitems, sizeof(struct dirent *), dcomp);
135
        *namelist = names;
136
        return(nitems);
137
}
138
 
139
/*
140
 * Alphabetic order comparison routine for those who want it.
141
 */
142
int
143
alphasort(d1, d2)
144
       const struct dirent **d1;
145
       const struct dirent **d2;
146
{
147
       return(strcmp((*d1)->d_name, (*d2)->d_name));
148
}
149
 
150
#endif /* ! HAVE_OPENDIR */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.