svn commit: r193019 - in stable/7/lib/libc: . gen string

Xin LI delphij at FreeBSD.org
Fri May 29 06:55:16 UTC 2009


Author: delphij
Date: Fri May 29 06:55:15 2009
New Revision: 193019
URL: http://svn.freebsd.org/changeset/base/193019

Log:
  MFC r178253 and r178256:
  
  Implement fdopendir(3) by splitting __opendir2() into two parts, the upper
  part deals with the usual __opendir2() calls, and the rest part with an
  interface translator to expose fdopendir(3) functionality.  Manual page was
  obtained from kib@'s work for *at(2) system calls.

Modified:
  stable/7/lib/libc/   (props changed)
  stable/7/lib/libc/gen/Makefile.inc
  stable/7/lib/libc/gen/Symbol.map
  stable/7/lib/libc/gen/directory.3
  stable/7/lib/libc/gen/opendir.c
  stable/7/lib/libc/string/ffsll.c   (props changed)
  stable/7/lib/libc/string/flsll.c   (props changed)

Modified: stable/7/lib/libc/gen/Makefile.inc
==============================================================================
--- stable/7/lib/libc/gen/Makefile.inc	Fri May 29 06:41:23 2009	(r193018)
+++ stable/7/lib/libc/gen/Makefile.inc	Fri May 29 06:55:15 2009	(r193019)
@@ -71,6 +71,7 @@ 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 \

Modified: stable/7/lib/libc/gen/Symbol.map
==============================================================================
--- stable/7/lib/libc/gen/Symbol.map	Fri May 29 06:41:23 2009	(r193018)
+++ stable/7/lib/libc/gen/Symbol.map	Fri May 29 06:55:15 2009	(r193019)
@@ -336,6 +336,10 @@ FBSD_1.0 {
 	wordfree;
 };
 
+FBSD_1.1 {
+	fdopendir;
+};
+
 FBSDprivate_1.0 {
 	/* needed by thread libraries */
 	__thr_jtable;

Modified: stable/7/lib/libc/gen/directory.3
==============================================================================
--- stable/7/lib/libc/gen/directory.3	Fri May 29 06:41:23 2009	(r193018)
+++ stable/7/lib/libc/gen/directory.3	Fri May 29 06:55:15 2009	(r193019)
@@ -28,11 +28,12 @@
 .\"     @(#)directory.3	8.1 (Berkeley) 6/4/93
 .\" $FreeBSD$
 .\"
-.Dd June 4, 1993
+.Dd April 16, 2008
 .Dt DIRECTORY 3
 .Os
 .Sh NAME
 .Nm opendir ,
+.Nm fdopendir ,
 .Nm readdir ,
 .Nm readdir_r ,
 .Nm telldir ,
@@ -48,6 +49,8 @@
 .In dirent.h
 .Ft DIR *
 .Fn opendir "const char *filename"
+.Ft DIR *
+.Fn fdopendir "int fd"
 .Ft struct dirent *
 .Fn readdir "DIR *dirp"
 .Ft int
@@ -84,6 +87,36 @@ cannot be accessed, or if it cannot
 enough memory to hold the whole thing.
 .Pp
 The
+.Fn fdopendir
+function is equivalent to the
+.Fn opendir
+function except that the directory is specified by a file descriptor
+.Fa fd
+rather than by a name.
+The file offset associated with the file descriptor at the time of the call
+determines which entries are returned.
+.Pp
+Upon successful return from
+.Fn fdopendir ,
+the file descriptor is under the control of the system,
+and if any attempt is made to close the file descriptor,
+or to modify the state of the associated description other than by means
+of
+.Fn closedir ,
+.Fn readdir ,
+.Fn readdir_r ,
+or
+.Fn rewinddir ,
+the behavior is undefined.
+Upon calling
+.Fn closedir
+the file descriptor is closed.
+The
+.Dv FD_CLOEXEC
+flag is set on the file descriptor by a successful call to
+.Fn fdopendir .
+.Pp
+The
 .Fn readdir
 function
 returns a pointer to the next directory entry.
@@ -202,3 +235,7 @@ and
 .Fn dirfd
 functions appeared in
 .Bx 4.2 .
+The
+.Fn fdopendir
+function appeared in
+.Fx 8.0 .

Modified: stable/7/lib/libc/gen/opendir.c
==============================================================================
--- stable/7/lib/libc/gen/opendir.c	Fri May 29 06:41:23 2009	(r193018)
+++ stable/7/lib/libc/gen/opendir.c	Fri May 29 06:55:15 2009	(r193019)
@@ -1,4 +1,4 @@
-/*
+/*-
  * Copyright (c) 1983, 1993
  *	The Regents of the University of California.  All rights reserved.
  *
@@ -47,32 +47,38 @@ __FBSDID("$FreeBSD$");
 #include "un-namespace.h"
 
 #include "telldir.h"
+
+static 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);
@@ -82,7 +88,24 @@ __opendir2(name, flags)
 	}
 	if ((fd = _open(name, O_RDONLY | O_NONBLOCK)) == -1)
 		return (NULL);
+
+	return __opendir_common(fd, name, flags);
+}
+
+/*
+ * Common routine for opendir(3), __opendir2(3) and fdopendir(3).
+ */
+static 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)) {


More information about the svn-src-stable-7 mailing list