1 /* fdopendir implementation derived from glibc.
3 Copyright (C) 2006 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
32 # include <dirstream.h>
33 # include <not-cancel.h>
38 # define __builtin_expect(expr, expected_val) expr
43 # define dirent64 dirent
44 # define __fxstat64(V, fd, sb) fstat(fd, sb)
45 # define __fcntl fcntl
46 # define __set_errno(Val) do { errno = (Val); } while (0)
47 # define __libc_lock_init(NAME) ((NAME) = 0, 0)
48 # define close_not_cancel_no_status(fd) close (fd)
50 # define internal_function __attribute ((regparm (3), stdcall))
52 # define internal_function
57 int fd; /* File descriptor. */
59 char *data; /* Directory block. */
60 size_t allocation; /* Space allocated for the block. */
61 size_t size; /* Total valid data in the block. */
62 size_t offset; /* Current offset into the block. */
64 off_t filepos; /* Position of next entry to read. */
69 #undef _STATBUF_ST_BLKSIZE
73 __alloc_dir (int fd, bool close_fd)
75 if (__builtin_expect (__fcntl (fd, F_SETFD, FD_CLOEXEC), 0) < 0)
79 #ifdef _STATBUF_ST_BLKSIZE
80 if (__builtin_expect ((size_t) statp->st_blksize >= sizeof (struct dirent64),
82 allocation = statp->st_blksize;
85 allocation = (BUFSIZ < sizeof (struct dirent64)
86 ? sizeof (struct dirent64) : BUFSIZ);
88 const int pad = -sizeof (DIR) % __alignof__ (struct dirent64);
90 DIR *dirp = (DIR *) malloc (sizeof (DIR) + allocation + pad);
96 int save_errno = errno;
97 close_not_cancel_no_status (fd);
98 __set_errno (save_errno);
102 memset (dirp, '\0', sizeof (DIR));
103 dirp->data = (char *) (dirp + 1) + pad;
104 dirp->allocation = allocation;
107 __libc_lock_init (dirp->lock);
116 struct stat64 statbuf;
118 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &statbuf), 0) < 0)
120 if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
122 __set_errno (ENOTDIR);
125 /* Make sure the descriptor allows for reading. */
126 int flags = __fcntl (fd, F_GETFL);
127 if (__builtin_expect (flags == -1, 0))
129 if (__builtin_expect ((flags & O_ACCMODE) == O_WRONLY, 0))
131 __set_errno (EINVAL);
136 return __alloc_dir (fd, false);