summaryrefslogtreecommitdiff
path: root/libc/misc/dirent/opendir.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2001-03-19 22:49:36 +0000
committerEric Andersen <andersen@codepoet.org>2001-03-19 22:49:36 +0000
commit8bdc62b0890760ebe6ddd70f2841b5136fbf7cb0 (patch)
tree4c1d6888d0bc4b3e1ce914faccd5063c77b41a1d /libc/misc/dirent/opendir.c
parent025490cb327377cf26993dfd839fcd0c81f5ce7e (diff)
Move the dirent stuff. I suppose it is system dependant, but then
it really should be grouped together too. And it needed to be grouped more then it needed to be in sysdeps/linux/common
Diffstat (limited to 'libc/misc/dirent/opendir.c')
-rw-r--r--libc/misc/dirent/opendir.c55
1 files changed, 55 insertions, 0 deletions
diff --git a/libc/misc/dirent/opendir.c b/libc/misc/dirent/opendir.c
new file mode 100644
index 000000000..329515447
--- /dev/null
+++ b/libc/misc/dirent/opendir.c
@@ -0,0 +1,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;
+ struct dirent *buf;
+ DIR *ptr;
+
+ if (stat(name, &statbuf))
+ return NULL;
+ if (!S_ISDIR(statbuf.st_mode)) {
+ 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);
+ 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);
+ 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;
+}