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

Subversion Repositories or1k

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

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

Line No. Rev Author Line
1 39 lampret
#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
#define DIRSIZ(dp) \
61
    ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
62
 
63
#ifndef __P
64
#define __P(args) ()
65
#endif
66
 
67
int
68
scandir(dirname, namelist, select, dcomp)
69
        const char *dirname;
70
        struct dirent ***namelist;
71
        int (*select) __P((struct dirent *));
72
        int (*dcomp) __P((const void *, const void *));
73
{
74
        register struct dirent *d, *p, **names;
75
        register size_t nitems;
76
        struct stat stb;
77
        long arraysz;
78
        DIR *dirp;
79
 
80
        if ((dirp = opendir(dirname)) == NULL)
81
                return(-1);
82
        if (fstat(dirp->dd_fd, &stb) < 0)
83
                return(-1);
84
 
85
        /*
86
         * estimate the array size by taking the size of the directory file
87
         * and dividing it by a multiple of the minimum size entry.
88
         */
89
        arraysz = (stb.st_size / 24);
90
        names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
91
        if (names == NULL)
92
                return(-1);
93
 
94
        nitems = 0;
95
        while ((d = readdir(dirp)) != NULL) {
96
                if (select != NULL && !(*select)(d))
97
                        continue;       /* just selected names */
98
                /*
99
                 * Make a minimum size copy of the data
100
                 */
101
                p = (struct dirent *)malloc(DIRSIZ(d));
102
                if (p == NULL)
103
                        return(-1);
104
                p->d_ino = d->d_ino;
105
                p->d_reclen = d->d_reclen;
106
                p->d_namlen = d->d_namlen;
107
                bcopy(d->d_name, p->d_name, p->d_namlen + 1);
108
                /*
109
                 * Check to make sure the array has space left and
110
                 * realloc the maximum size.
111
                 */
112
                if (++nitems >= arraysz) {
113
                        if (fstat(dirp->dd_fd, &stb) < 0)
114
                                return(-1);     /* just might have grown */
115
                        arraysz = stb.st_size / 12;
116
                        names = (struct dirent **)realloc((char *)names,
117
                                arraysz * sizeof(struct dirent *));
118
                        if (names == NULL)
119
                                return(-1);
120
                }
121
                names[nitems-1] = p;
122
        }
123
        closedir(dirp);
124
        if (nitems && dcomp != NULL)
125
                qsort(names, nitems, sizeof(struct dirent *), dcomp);
126
        *namelist = names;
127
        return(nitems);
128
}
129
 
130
/*
131
 * Alphabetic order comparison routine for those who want it.
132
 */
133
int
134
alphasort(d1, d2)
135
        const void *d1;
136
        const void *d2;
137
{
138
        return(strcmp((*(struct dirent **)d1)->d_name,
139
            (*(struct dirent **)d2)->d_name));
140
}
141
 
142
#endif /* ! HAVE_OPENDIR */

powered by: WebSVN 2.1.0

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