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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [libc/] [misc/] [dirent/] [scandir64.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* Copyright (C) 1992-1998, 2000 Free Software Foundation, Inc.
2
   This file is part of the GNU C Library.
3
 
4
   The GNU C Library is free software; you can redistribute it and/or
5
   modify it under the terms of the GNU Lesser General Public
6
   License as published by the Free Software Foundation; either
7
   version 2.1 of the License, or (at your option) any later version.
8
 
9
   The GNU C Library is distributed in the hope that it will be useful,
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
   Lesser General Public License for more details.
13
 
14
   You should have received a copy of the GNU Lesser General Public
15
   License along with the GNU C Library; if not, write to the Free
16
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17
   02111-1307 USA.
18
   */
19
 
20
/* Modified for uClibc by Erik Andersen
21
   */
22
 
23
#include <features.h>
24
#ifdef __UCLIBC_HAS_LFS__
25
#if defined _FILE_OFFSET_BITS && _FILE_OFFSET_BITS != 64 
26
#undef _FILE_OFFSET_BITS
27
#define _FILE_OFFSET_BITS   64
28
#endif
29
#ifndef __USE_LARGEFILE64
30
# define __USE_LARGEFILE64      1
31
#endif
32
/* We absolutely do _NOT_ want interfaces silently
33
 * renamed under us or very bad things will happen... */
34
#ifdef __USE_FILE_OFFSET64
35
# undef __USE_FILE_OFFSET64
36
#endif
37
 
38
#include <dirent.h>
39
#include <stdio.h>
40
#include <string.h>
41
#include <stdlib.h>
42
#include <errno.h>
43
#include <sys/types.h>
44
#include "dirstream.h"
45
 
46
int scandir64(const char *dir, struct dirent64 ***namelist,
47
        int (*selector) (const struct dirent64 *),
48
        int (*compar) (const void *, const void *))
49
{
50
    DIR *dp = opendir (dir);
51
    struct dirent64 *current;
52
    struct dirent64 **names = NULL;
53
    size_t names_size = 0, pos;
54
    int save;
55
 
56
    if (dp == NULL)
57
        return -1;
58
 
59
    save = errno;
60
    __set_errno (0);
61
 
62
    pos = 0;
63
    while ((current = readdir64 (dp)) != NULL)
64
        if (selector == NULL || (*selector) (current))
65
        {
66
            struct dirent64 *vnew;
67
            size_t dsize;
68
 
69
            /* Ignore errors from selector or readdir64 */
70
            __set_errno (0);
71
 
72
            if (unlikely(pos == names_size))
73
            {
74
                struct dirent64 **new;
75
                if (names_size == 0)
76
                    names_size = 10;
77
                else
78
                    names_size *= 2;
79
                new = (struct dirent64 **) realloc (names, names_size * sizeof (struct dirent64 *));
80
                if (new == NULL)
81
                    break;
82
                names = new;
83
            }
84
 
85
            dsize = &current->d_name[_D_ALLOC_NAMLEN (current)] - (char *) current;
86
            vnew = (struct dirent64 *) malloc (dsize);
87
            if (vnew == NULL)
88
                break;
89
 
90
            names[pos++] = (struct dirent64 *) memcpy (vnew, current, dsize);
91
        }
92
 
93
    if (unlikely(errno != 0))
94
    {
95
        save = errno;
96
        closedir (dp);
97
        while (pos > 0)
98
            free (names[--pos]);
99
        free (names);
100
        __set_errno (save);
101
        return -1;
102
    }
103
 
104
    closedir (dp);
105
    __set_errno (save);
106
 
107
    /* Sort the list if we have a comparison function to sort with.  */
108
    if (compar != NULL)
109
        qsort (names, pos, sizeof (struct dirent64 *), compar);
110
    *namelist = names;
111
    return pos;
112
}
113
#endif /* __UCLIBC_HAS_LFS__ */
114
 

powered by: WebSVN 2.1.0

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