2 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
4 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
10 #include <sys/types.h>
16 #include "dirstream.h"
18 static DIR *fd_to_DIR(int fd, __blksize_t size)
22 ptr = malloc(sizeof(*ptr));
27 ptr->dd_nextloc = ptr->dd_size = ptr->dd_nextoff = 0;
29 if (ptr->dd_max < 512)
32 ptr->dd_buf = calloc(1, ptr->dd_max);
37 __pthread_mutex_init(&ptr->dd_lock, NULL);
42 DIR *fdopendir(int fd)
49 if (!S_ISDIR(st.st_mode)) {
54 flags = fcntl(fd, F_GETFL);
57 if ((flags & O_ACCMODE) == O_WRONLY) {
62 return fd_to_DIR(fd, st.st_blksize);
65 /* opendir just makes an open() call - it return NULL if it fails
66 * (open sets errno), otherwise it returns a DIR * pointer.
68 DIR *opendir(const char *name)
75 /* O_DIRECTORY is linux specific and has been around since like 2.1.x */
76 if (stat(name, &statbuf))
78 if (!S_ISDIR(statbuf.st_mode)) {
82 # define O_DIRECTORY 0
84 fd = open(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_CLOEXEC);
87 /* Note: we should check to make sure that between the stat() and open()
88 * call, 'name' didnt change on us, but that's only if O_DIRECTORY isnt
89 * defined and since Linux has supported it for like ever, i'm not going
90 * to worry about it right now (if ever). */
92 if (fstat(fd, &statbuf) < 0) {
93 /* this close() never fails
95 *saved_errno = errno; */
97 /*__set_errno(saved_errno);*/
101 /* According to POSIX, directory streams should be closed when
102 * exec. From "Anna Pluzhnikov" <besp@midway.uchicago.edu>.
104 #ifndef __ASSUME_O_CLOEXEC
105 fcntl(fd, F_SETFD, FD_CLOEXEC);
108 ptr = fd_to_DIR(fd, statbuf.st_blksize);
115 libc_hidden_def(opendir)