From 3901fa14bf884e0713d1a8e9e051b23bd75a8a79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Tue, 14 Sep 2004 22:10:43 +0000 Subject: [PATCH] Moved dirent.h functions from unistd/directory.c to new dirent.c file. Added dirfd() call for BeOS compatibility. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@8951 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/libroot/posix/Jamfile | 1 + src/kernel/libroot/posix/dirent.c | 99 +++++++++++++++++++++ src/kernel/libroot/posix/unistd/directory.c | 79 ++-------------- 3 files changed, 105 insertions(+), 74 deletions(-) create mode 100644 src/kernel/libroot/posix/dirent.c diff --git a/src/kernel/libroot/posix/Jamfile b/src/kernel/libroot/posix/Jamfile index 443f9aa2b4..86360746f7 100644 --- a/src/kernel/libroot/posix/Jamfile +++ b/src/kernel/libroot/posix/Jamfile @@ -5,6 +5,7 @@ UsePrivateHeaders [ FDirName syslog_daemon ] ; KernelMergeObject posix_main.o : assert.c dlfcn.c + dirent.c errno.c fnmatch.c glob.c diff --git a/src/kernel/libroot/posix/dirent.c b/src/kernel/libroot/posix/dirent.c new file mode 100644 index 0000000000..457d2c9301 --- /dev/null +++ b/src/kernel/libroot/posix/dirent.c @@ -0,0 +1,99 @@ +/* +** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the Haiku License. +*/ + + +#include + +#include +#include +#include + + +#define BUFFER_SIZE 4096 + + +#define RETURN_AND_SET_ERRNO(err) \ + if (err < 0) { \ + errno = err; \ + return -1; \ + } \ + return err; + + +DIR * +opendir(const char *path) +{ + DIR *dir; + + int fd = _kern_open_dir(-1, path); + if (fd < 0) { + errno = fd; + return NULL; + } + + /* allocate the memory for the DIR structure */ + if ((dir = (DIR *)malloc(sizeof(DIR) + BUFFER_SIZE)) == NULL) { + errno = B_NO_MEMORY; + _kern_close(fd); + return NULL; + } + + dir->fd = fd; + + return dir; +} + + +int +closedir(DIR *dir) +{ + int status = _kern_close(dir->fd); + + free(dir); + + RETURN_AND_SET_ERRNO(status); +} + + +struct dirent * +readdir(DIR *dir) +{ + /* get the next entry and return a pointer to a dirent structure + * containing the data + */ + + ssize_t count = _kern_read_dir(dir->fd, &dir->ent, BUFFER_SIZE, 1); + if (count <= 0) { + if (count < 0) + errno = count; + + return NULL; + } + + return &dir->ent; +} + + +void +rewinddir(DIR *dir) +{ + status_t status = _kern_rewind_dir(dir->fd); + if (status < 0) + errno = status; +} + + +/** This is no POSIX compatible call; it's not exported in the headers + * but here for BeOS compatiblity. + */ + + +int dirfd(DIR *dir); + +int +dirfd(DIR *dir) +{ + return dir->fd; +} diff --git a/src/kernel/libroot/posix/unistd/directory.c b/src/kernel/libroot/posix/unistd/directory.c index eade87b77b..54de82c661 100644 --- a/src/kernel/libroot/posix/unistd/directory.c +++ b/src/kernel/libroot/posix/unistd/directory.c @@ -1,19 +1,13 @@ /* -** Copyright 2002, Axel Dörfler, axeld@pinc-software.de. All rights reserved. -** Distributed under the terms of the OpenBeOS License. +** Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the Haiku License. */ -#include + #include -#include + +#include #include -#include -#include -#include -#include - - -#define BUFFER_SIZE 4096 #define RETURN_AND_SET_ERRNO(err) \ @@ -24,69 +18,6 @@ return err; -DIR * -opendir(const char *path) -{ - DIR *dir; - - int fd = _kern_open_dir(-1, path); - if (fd < 0) { - errno = fd; - return NULL; - } - - /* allocate the memory for the DIR structure */ - if ((dir = (DIR *)malloc(sizeof(DIR) + BUFFER_SIZE)) == NULL) { - errno = B_NO_MEMORY; - _kern_close(fd); - return NULL; - } - - dir->fd = fd; - - return dir; -} - - -int -closedir(DIR *dir) -{ - int status = _kern_close(dir->fd); - - free(dir); - - RETURN_AND_SET_ERRNO(status); -} - - -struct dirent * -readdir(DIR *dir) -{ - /* get the next entry and return a pointer to a dirent structure - * containing the data - */ - - ssize_t count = _kern_read_dir(dir->fd, &dir->ent, BUFFER_SIZE, 1); - if (count <= 0) { - if (count < 0) - errno = count; - - return NULL; - } - - return &dir->ent; -} - - -void -rewinddir(DIR *dirp) -{ - status_t status = _kern_rewind_dir(dirp->fd); - if (status < 0) - errno = status; -} - - int chdir(const char *path) {