[PATCH] fdopendir(3)

From: Xin LI <delphij_at_delphij.net>
Date: Sun, 13 Apr 2008 14:47:02 -0700
Hi,

Any objection for the attached patch which implements fdopendir(3) that 
is found in various other OSes?  Basically it splits __opendir2 into two 
parts, and expose the second part which deals with fd to provide 
fdopendir(3) functionalities.

Cheers,
-- 
Xin LI <delphij_at_delphij.net>	http://www.delphij.net/
FreeBSD - The Power to Serve!

Index: include/dirent.h
===================================================================
RCS file: /home/ncvs/src/include/dirent.h,v
retrieving revision 1.15
diff -u -p -r1.15 dirent.h
--- include/dirent.h	20 Nov 2007 01:49:00 -0000	1.15
+++ include/dirent.h	13 Apr 2008 07:25:58 -0000
_at__at_ -100,6 +100,7 _at__at_ int	 getdents(int, char *, int);
 int	 getdirentries(int, char *, int, long *);
 #endif
 DIR	*opendir(const char *);
+DIR	*fdopendir(int);
 struct dirent *
 	 readdir(DIR *);
 #if __POSIX_VISIBLE >= 199506 || __XSI_VISIBLE >= 500
Index: lib/libc/gen/Makefile.inc
===================================================================
RCS file: /home/ncvs/src/lib/libc/gen/Makefile.inc,v
retrieving revision 1.133
diff -u -p -r1.133 Makefile.inc
--- lib/libc/gen/Makefile.inc	29 Mar 2008 16:19:35 -0000	1.133
+++ lib/libc/gen/Makefile.inc	13 Apr 2008 07:48:19 -0000
_at__at_ -72,6 +72,7 _at__at_ MLINKS+=arc4random.3 arc4random_addrando
 MLINKS+=ctermid.3 ctermid_r.3
 MLINKS+=devname.3 devname_r.3
 MLINKS+=directory.3 closedir.3 directory.3 dirfd.3 directory.3 opendir.3 \
+	directory.3 fdopendir.3 \
 	directory.3 readdir.3 directory.3 readdir_r.3 directory.3 rewinddir.3 \
 	directory.3 seekdir.3 directory.3 telldir.3
 MLINKS+=dlopen.3 dlclose.3 dlopen.3 dlerror.3 dlopen.3 dlfunc.3 \
Index: lib/libc/gen/Symbol.map
===================================================================
RCS file: /home/ncvs/src/lib/libc/gen/Symbol.map,v
retrieving revision 1.8
diff -u -p -r1.8 Symbol.map
--- lib/libc/gen/Symbol.map	26 Jan 2008 17:09:40 -0000	1.8
+++ lib/libc/gen/Symbol.map	13 Apr 2008 07:45:37 -0000
_at__at_ -451,3 +451,8 _at__at_ FBSDprivate_1.0 {
 	__waitpid;
 	_waitpid;
 };
+
+
+FBSD_1.1 {
+	fdopendir;
+};
Index: lib/libc/gen/directory.3
===================================================================
RCS file: /home/ncvs/src/lib/libc/gen/directory.3,v
retrieving revision 1.16
diff -u -p -r1.16 directory.3
--- lib/libc/gen/directory.3	9 Jan 2007 00:27:53 -0000	1.16
+++ lib/libc/gen/directory.3	13 Apr 2008 07:43:44 -0000
_at__at_ -28,11 +28,12 _at__at_
 .\"     _at_(#)directory.3	8.1 (Berkeley) 6/4/93
 .\" $FreeBSD: src/lib/libc/gen/directory.3,v 1.16 2007/01/09 00:27:53 imp Exp $
 .\"
-.Dd June 4, 1993
+.Dd April 13, 2008
 .Dt DIRECTORY 3
 .Os
 .Sh NAME
 .Nm opendir ,
+.Nm fdopendir ,
 .Nm readdir ,
 .Nm readdir_r ,
 .Nm telldir ,
_at__at_ -48,6 +49,8 _at__at_
 .In dirent.h
 .Ft DIR *
 .Fn opendir "const char *filename"
+.Ft DIR *
+.Fn fdopendir "int filedes"
 .Ft struct dirent *
 .Fn readdir "DIR *dirp"
 .Ft int
_at__at_ -84,6 +87,15 _at__at_ cannot be accessed, or if it cannot
 enough memory to hold the whole thing.
 .Pp
 The
+.Fn fdopendir
+function
+associates a stream with the existing file descriptor,
+.Fa fildes .
+When the stream is closed via
+.Xr closedir 3 ,
+fildes is closed also.
+.Pp
+The
 .Fn readdir
 function
 returns a pointer to the next directory entry.
Index: lib/libc/gen/opendir.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/gen/opendir.c,v
retrieving revision 1.23
diff -u -p -r1.23 opendir.c
--- lib/libc/gen/opendir.c	9 Jan 2007 00:27:54 -0000	1.23
+++ lib/libc/gen/opendir.c	13 Apr 2008 07:59:29 -0000
_at__at_ -1,4 +1,4 _at__at_
-/*
+/*-
  * Copyright (c) 1983, 1993
  *	The Regents of the University of California.  All rights reserved.
  *
_at__at_ -47,32 +47,38 _at__at_ __FBSDID("$FreeBSD: src/lib/libc/gen/ope
 #include "un-namespace.h"
 
 #include "telldir.h"
+
+static inline DIR * __opendir_common(int, const char *, int);
+
 /*
  * Open a directory.
  */
 DIR *
-opendir(name)
-	const char *name;
+opendir(const char *name)
 {
 
 	return (__opendir2(name, DTF_HIDEW|DTF_NODUP));
 }
 
+/*
+ * Open a directory with existing file descriptor.
+ */
 DIR *
-__opendir2(name, flags)
-	const char *name;
-	int flags;
+fdopendir(int fd)
+{
+
+	return (__opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP));
+}
+
+DIR *
+__opendir2(const char *name, int flags)
 {
-	DIR *dirp;
 	int fd;
-	int incr;
-	int saved_errno;
-	int unionstack;
 	struct stat statb;
 
 	/*
 	 * stat() before _open() because opening of special files may be
-	 * harmful.  _fstat() after open because the file may have changed.
+	 * harmful.
 	 */
 	if (stat(name, &statb) != 0)
 		return (NULL);
_at__at_ -82,7 +88,21 _at__at_ __opendir2(name, flags)
 	}
 	if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1)
 		return (NULL);
+
+	return __opendir_common(fd, name, flags);
+}
+
+static inline DIR *
+__opendir_common(int fd, const char *name, int flags)
+{
+	DIR *dirp;
+	int incr;
+	int saved_errno;
+	int unionstack;
+	struct stat statb;
+
 	dirp = NULL;
+	/* _fstat() the open handler because the file may have changed.  */
 	if (_fstat(fd, &statb) != 0)
 		goto fail;
 	if (!S_ISDIR(statb.st_mode)) {
Received on Sun Apr 13 2008 - 19:47:14 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:29 UTC