From a0fcf6276707a1a1d4bff445ffc1f3651099d301 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Tue, 15 Aug 2006 11:08:03 +0000 Subject: commit, for the record. May never be used. --- lib/fdopendir-glibc.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 lib/fdopendir-glibc.c (limited to 'lib') diff --git a/lib/fdopendir-glibc.c b/lib/fdopendir-glibc.c new file mode 100644 index 000000000..6b66a6f2a --- /dev/null +++ b/lib/fdopendir-glibc.c @@ -0,0 +1,121 @@ +#ifdef HAVE_CONFIG_H +# include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#if _LIBC +# include +# include + +#else + +# if __GNUC__ < 3 +# define __builtin_expect(expr, expected_val) expr +# endif + +# include "openat.h" +# define stat64 stat +# define dirent64 dirent +# define __fxstat64(V, fd, sb) fstat(fd, sb) +# define __fcntl fcntl +# define __set_errno(Val) do { errno = (Val); } while (0) +# define __libc_lock_init(NAME) ((NAME) = 0, 0) +# define close_not_cancel_no_status(fd) close (fd) +# ifdef __i386__ +# define internal_function __attribute ((regparm (3), stdcall)) +# else +# define internal_function +# endif + +struct __dirstream + { + int fd; /* File descriptor. */ + + char *data; /* Directory block. */ + size_t allocation; /* Space allocated for the block. */ + size_t size; /* Total valid data in the block. */ + size_t offset; /* Current offset into the block. */ + + off_t filepos; /* Position of next entry to read. */ + int lock; + }; +#endif + +#undef _STATBUF_ST_BLKSIZE + +static DIR * +internal_function +__alloc_dir (int fd, bool close_fd) +{ + if (__builtin_expect (__fcntl (fd, F_SETFD, FD_CLOEXEC), 0) < 0) + goto lose; + + size_t allocation; +#ifdef _STATBUF_ST_BLKSIZE + if (__builtin_expect ((size_t) statp->st_blksize >= sizeof (struct dirent64), + 1)) + allocation = statp->st_blksize; + else +#endif + allocation = (BUFSIZ < sizeof (struct dirent64) + ? sizeof (struct dirent64) : BUFSIZ); + + const int pad = -sizeof (DIR) % __alignof__ (struct dirent64); + + DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation + pad); + if (dirp == NULL) + lose: + { + if (close_fd) + { + int save_errno = errno; + close_not_cancel_no_status (fd); + __set_errno (save_errno); + } + return NULL; + } + memset (dirp, '\0', sizeof (DIR)); + dirp->data = (char *) (dirp + 1) + pad; + dirp->allocation = allocation; + dirp->fd = fd; + + __libc_lock_init (dirp->lock); + + return dirp; +} + +DIR * +fdopendir (int fd) +{ +#if 0 + struct stat64 statbuf; + + if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &statbuf), 0) < 0) + return NULL; + if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0)) + { + __set_errno (ENOTDIR); + return NULL; + } + /* Make sure the descriptor allows for reading. */ + int flags = __fcntl (fd, F_GETFL); + if (__builtin_expect (flags == -1, 0)) + return NULL; + if (__builtin_expect ((flags & O_ACCMODE) == O_WRONLY, 0)) + { + __set_errno (EINVAL); + return NULL; + } +#endif + + return __alloc_dir (fd, false); +} -- cgit v1.2.3-54-g00ecf