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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
#include <errno.h>
2
#include <stdlib.h>
3
#include <string.h>
4
#include <fcntl.h>
5
#include <unistd.h>
6
#include <sys/dir.h>
7
#include <sys/stat.h>
8
#include "dirstream.h"
9
 
10
 
11
/* opendir just makes an open() call - it return NULL if it fails
12
 * (open sets errno), otherwise it returns a DIR * pointer.
13
 */
14
DIR *opendir(const char *name)
15
{
16
        int fd;
17
        struct stat statbuf;
18
        char *buf;
19
        DIR *ptr;
20
 
21
        if (stat(name, &statbuf))
22
                return NULL;
23
        if (!S_ISDIR(statbuf.st_mode)) {
24
                __set_errno(ENOTDIR);
25
                return NULL;
26
        }
27
        if ((fd = open(name, O_RDONLY)) < 0)
28
                return NULL;
29
        /* According to POSIX, directory streams should be closed when
30
         * exec. From "Anna Pluzhnikov" <besp@midway.uchicago.edu>.
31
         */
32
        if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
33
                return NULL;
34
        if (!(ptr = malloc(sizeof(*ptr)))) {
35
                close(fd);
36
                __set_errno(ENOMEM);
37
                return NULL;
38
        }
39
 
40
        ptr->dd_fd = fd;
41
        ptr->dd_nextloc = ptr->dd_size = ptr->dd_nextoff = 0;
42
 
43
        ptr->dd_max = statbuf.st_blksize;
44
        if (ptr->dd_max < 512)
45
                ptr->dd_max = 512;
46
 
47
        if (!(buf = calloc(1, ptr->dd_max))) {
48
                close(fd);
49
                free(ptr);
50
                __set_errno(ENOMEM);
51
                return NULL;
52
        }
53
        ptr->dd_buf = buf;
54
#ifdef __UCLIBC_HAS_THREADS__
55
        __pthread_mutex_init(&(ptr->dd_lock), NULL);
56
#endif
57
        return ptr;
58
}

powered by: WebSVN 2.1.0

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