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