]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
commit, for the record. May never be used.
authorJim Meyering <jim@meyering.net>
Tue, 15 Aug 2006 11:08:03 +0000 (11:08 +0000)
committerJim Meyering <jim@meyering.net>
Tue, 15 Aug 2006 11:08:03 +0000 (11:08 +0000)
lib/fdopendir-glibc.c [new file with mode: 0644]

diff --git a/lib/fdopendir-glibc.c b/lib/fdopendir-glibc.c
new file mode 100644 (file)
index 0000000..6b66a6f
--- /dev/null
@@ -0,0 +1,121 @@
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <stdbool.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+
+#if _LIBC
+# include <dirstream.h>
+# include <not-cancel.h>
+
+#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);
+}