6b66a6f2a966da06be1bdc8c60d7da9c1490c0da
[platform/upstream/coreutils.git] / lib / fdopendir-glibc.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9 #include <stdbool.h>
10 #include <dirent.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <sys/stat.h>
14
15 #if _LIBC
16 # include <dirstream.h>
17 # include <not-cancel.h>
18
19 #else
20
21 # if __GNUC__ < 3
22 #  define __builtin_expect(expr, expected_val) expr
23 # endif
24
25 # include "openat.h"
26 # define stat64 stat
27 # define dirent64 dirent
28 # define __fxstat64(V, fd, sb) fstat(fd, sb)
29 # define __fcntl fcntl
30 # define __set_errno(Val) do { errno = (Val); } while (0)
31 # define __libc_lock_init(NAME) ((NAME) = 0, 0)
32 # define close_not_cancel_no_status(fd) close (fd)
33 # ifdef __i386__
34 #  define internal_function __attribute ((regparm (3), stdcall))
35 # else
36 #  define internal_function
37 # endif
38
39 struct __dirstream
40   {
41     int fd;                     /* File descriptor.  */
42
43     char *data;                 /* Directory block.  */
44     size_t allocation;          /* Space allocated for the block.  */
45     size_t size;                /* Total valid data in the block.  */
46     size_t offset;              /* Current offset into the block.  */
47
48     off_t filepos;              /* Position of next entry to read.  */
49     int lock;
50   };
51 #endif
52
53 #undef _STATBUF_ST_BLKSIZE
54
55 static DIR *
56 internal_function
57 __alloc_dir (int fd, bool close_fd)
58 {
59   if (__builtin_expect (__fcntl (fd, F_SETFD, FD_CLOEXEC), 0) < 0)
60     goto lose;
61
62   size_t allocation;
63 #ifdef _STATBUF_ST_BLKSIZE
64   if (__builtin_expect ((size_t) statp->st_blksize >= sizeof (struct dirent64),
65                         1))
66     allocation = statp->st_blksize;
67   else
68 #endif
69     allocation = (BUFSIZ < sizeof (struct dirent64)
70                   ? sizeof (struct dirent64) : BUFSIZ);
71
72   const int pad = -sizeof (DIR) % __alignof__ (struct dirent64);
73
74   DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation + pad);
75   if (dirp == NULL)
76   lose:
77     {
78       if (close_fd)
79         {
80           int save_errno = errno;
81           close_not_cancel_no_status (fd);
82           __set_errno (save_errno);
83         }
84       return NULL;
85     }
86   memset (dirp, '\0', sizeof (DIR));
87   dirp->data = (char *) (dirp + 1) + pad;
88   dirp->allocation = allocation;
89   dirp->fd = fd;
90
91   __libc_lock_init (dirp->lock);
92
93   return dirp;
94 }
95
96 DIR *
97 fdopendir (int fd)
98 {
99 #if 0
100   struct stat64 statbuf;
101
102   if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &statbuf), 0) < 0)
103     return NULL;
104   if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
105     {
106       __set_errno (ENOTDIR);
107       return NULL;
108     }
109   /* Make sure the descriptor allows for reading.  */
110   int flags = __fcntl (fd, F_GETFL);
111   if (__builtin_expect (flags == -1, 0))
112     return NULL;
113   if (__builtin_expect ((flags & O_ACCMODE) == O_WRONLY, 0))
114     {
115       __set_errno (EINVAL);
116       return NULL;
117     }
118 #endif
119
120   return __alloc_dir (fd, false);
121 }