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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uC-libc/] [sysdeps/] [opendir.c] - Blame information for rev 1775

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

Line No. Rev Author Line
1 199 simons
#include <sys/dirent.h>
2
#include <sys/stat.h>
3
#include <stdlib.h>
4
#include <string.h>
5
#define open __normal_open
6
#define fcntl __normal_fcntl
7
#include <fcntl.h>
8
#include <unistd.h>
9
#undef _POSIX_PTHREADS
10
#include <syscall.h>
11
#include <errno.h>
12
 
13
#include "dirstream.h"
14
 
15
#undef close
16
#undef open
17
#undef fcntl 
18
#undef stat
19
 
20
static inline
21
_syscall1(int,close,int,fd)
22
 
23
static inline
24
_syscall2(int,open,const char *,name,int,flags)
25
 
26
static inline
27
_syscall3(int,fcntl,int,fd,int,cmd,long,args)
28
 
29
/*
30
 * opendir just makes an open() call - it return NULL if it fails
31
 * (open sets errno), otherwise it returns a DIR * pointer.
32
 */
33
DIR *
34
opendir(const char * name)
35
{
36
  int fd;
37
  struct stat statbuf;
38
  struct dirent *buf;
39
  DIR *ptr;
40
 
41
  if (stat(name,&statbuf)) return NULL;
42
  if (!S_ISDIR(statbuf.st_mode)) {
43
    errno = ENOTDIR;
44
    return NULL;
45
  }
46
  if ((fd = open(name,O_RDONLY)) < 0)
47
    return NULL;
48
  /* According to POSIX, directory streams should be closed when
49
   * exec. From "Anna Pluzhnikov" <besp@midway.uchicago.edu>.
50
   */
51
  if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
52
    return NULL;
53
  if (!(ptr=malloc(sizeof(*ptr)))) {
54
    close(fd);
55
    errno = ENOMEM;
56
    return NULL;
57
  }
58
 
59
  ptr->dd_max = statbuf.st_blksize;
60
  if (ptr->dd_max < 512)
61
      ptr->dd_max = 512;
62
 
63
  if (!(buf=malloc(ptr->dd_max))) {
64
    close(fd);
65
    free(ptr);
66
    errno = ENOMEM;
67
    return NULL;
68
  }
69
  ptr->dd_fd = fd;
70
  ptr->dd_nextoff = ptr->dd_nextloc = ptr->dd_size = 0;
71
  ptr->dd_buf = buf;
72
  ptr->dd_getdents = unknown;
73
  return ptr;
74
}

powered by: WebSVN 2.1.0

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