[6/6] Add generic implementation of fdopendir()

Message ID 20181008133900.3554-6-sebastian.huber@embedded-brains.de
State New
Headers show
Series
  • [1/6] O_CLOEXEC O_NOFOLLOW O_DIRECTORY O_EXEC O_DIRECT
Related show

Commit Message

Sebastian Huber Oct. 8, 2018, 1:39 p.m.
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>

---
 newlib/libc/posix/opendir.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

-- 
2.16.4

Comments

Corinna Vinschen Oct. 10, 2018, 10:08 a.m. | #1
On Oct  8 15:39, Sebastian Huber wrote:
> Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>

> ---

>  newlib/libc/posix/opendir.c | 29 +++++++++++++++++++++--------

>  1 file changed, 21 insertions(+), 8 deletions(-)


Depends on the O_CLOEXEC thingy but otherwise ACK.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat

Patch

diff --git a/newlib/libc/posix/opendir.c b/newlib/libc/posix/opendir.c
index 650cbfe8d..2cf1ba541 100644
--- a/newlib/libc/posix/opendir.c
+++ b/newlib/libc/posix/opendir.c
@@ -43,17 +43,11 @@  static char sccsid[] = "@(#)opendir.c	5.11 (Berkeley) 2/23/91";
 #include <unistd.h>
 #include <sys/lock.h>
 
-/*
- * open a directory.
- */
-DIR *
-opendir (const char *name)
+static DIR *
+_opendir_common(int fd)
 {
 	DIR *dirp;
-	int fd;
 
-	if ((fd = open(name, O_RDONLY | O_DIRECTORY | O_CLOEXEC)) == -1)
-		return NULL;
 	if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
 		close (fd);
 		return NULL;
@@ -87,4 +81,23 @@  opendir (const char *name)
 	return dirp;
 }
 
+DIR *
+opendir(const char *name)
+{
+	int fd;
+
+	if ((fd = open(name, O_RDONLY | O_DIRECTORY | O_CLOEXEC)) == -1)
+		return (NULL);
+	return (_opendir_common(fd));
+}
+
+DIR *
+fdopendir(int fd)
+{
+
+	if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
+		return (NULL);
+	return (_opendir_common(fd));
+}
+
 #endif /* ! HAVE_OPENDIR */