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>
15 #include <not-cancel.h>
17 #include "dirstream.h"
19 static DIR *fd_to_DIR(int fd, __blksize_t size)
23 ptr = malloc(sizeof(*ptr));
28 ptr->dd_nextloc = ptr->dd_size = ptr->dd_nextoff = 0;
30 if (ptr->dd_max < 512)
33 ptr->dd_buf = calloc(1, ptr->dd_max);
38 __UCLIBC_MUTEX_INIT_VAR(ptr->dd_lock);
43 DIR *fdopendir(int fd)
50 if (!S_ISDIR(st.st_mode)) {
55 flags = fcntl(fd, F_GETFL);
58 if ((flags & O_ACCMODE) == O_WRONLY) {
63 return fd_to_DIR(fd, st.st_blksize);
66 /* opendir just makes an open() call - it return NULL if it fails
67 * (open sets errno), otherwise it returns a DIR * pointer.
69 DIR *opendir(const char *name)
76 /* O_DIRECTORY is linux specific and has been around since like 2.1.x */
77 if (stat(name, &statbuf))
79 if (!S_ISDIR(statbuf.st_mode)) {
83 # define O_DIRECTORY 0
85 fd = open_not_cancel_2(name, O_RDONLY|O_NDELAY|O_DIRECTORY|O_CLOEXEC);
88 /* Note: we should check to make sure that between the stat() and open()
89 * call, 'name' didnt change on us, but that's only if O_DIRECTORY isnt
90 * defined and since Linux has supported it for like ever, i'm not going
91 * to worry about it right now (if ever). */
93 if (fstat(fd, &statbuf) < 0) {
94 /* this close() never fails
96 *saved_errno = errno; */
97 close_not_cancel_no_status(fd);
98 /*__set_errno(saved_errno);*/
102 /* According to POSIX, directory streams should be closed when
103 * exec. From "Anna Pluzhnikov" <besp@midway.uchicago.edu>.
105 #ifndef __ASSUME_O_CLOEXEC
106 fcntl_not_cancel(fd, F_SETFD, FD_CLOEXEC);
109 ptr = fd_to_DIR(fd, statbuf.st_blksize);
112 close_not_cancel_no_status(fd);
117 libc_hidden_def(opendir)