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 3 of the License, or
8 (at your option) any later version.
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
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 # include <dirstream.h>
32 # include <not-cancel.h>
37 # define __builtin_expect(expr, expected_val) expr
42 # define dirent64 dirent
43 # define __fxstat64(V, fd, sb) fstat(fd, sb)
44 # define __fcntl fcntl
45 # define __set_errno(Val) do { errno = (Val); } while (0)
46 # define __libc_lock_init(NAME) ((NAME) = 0, 0)
47 # define close_not_cancel_no_status(fd) close (fd)
49 # define internal_function __attribute ((regparm (3), stdcall))
51 # define internal_function
56 int fd
; /* File descriptor. */
58 char *data
; /* Directory block. */
59 size_t allocation
; /* Space allocated for the block. */
60 size_t size
; /* Total valid data in the block. */
61 size_t offset
; /* Current offset into the block. */
63 off_t filepos
; /* Position of next entry to read. */
68 #undef _STATBUF_ST_BLKSIZE
72 __alloc_dir (int fd
, bool close_fd
)
74 if (__builtin_expect (__fcntl (fd
, F_SETFD
, FD_CLOEXEC
), 0) < 0)
78 #ifdef _STATBUF_ST_BLKSIZE
79 if (__builtin_expect ((size_t) statp
->st_blksize
>= sizeof (struct dirent64
),
81 allocation
= statp
->st_blksize
;
84 allocation
= (BUFSIZ
< sizeof (struct dirent64
)
85 ? sizeof (struct dirent64
) : BUFSIZ
);
87 const int pad
= -sizeof (DIR) % __alignof__ (struct dirent64
);
89 DIR *dirp
= (DIR *) malloc (sizeof (DIR) + allocation
+ pad
);
95 int save_errno
= errno
;
96 close_not_cancel_no_status (fd
);
97 __set_errno (save_errno
);
101 memset (dirp
, '\0', sizeof (DIR));
102 dirp
->data
= (char *) (dirp
+ 1) + pad
;
103 dirp
->allocation
= allocation
;
106 __libc_lock_init (dirp
->lock
);
115 struct stat64 statbuf
;
117 if (__builtin_expect (__fxstat64 (_STAT_VER
, fd
, &statbuf
), 0) < 0)
119 if (__builtin_expect (! S_ISDIR (statbuf
.st_mode
), 0))
121 __set_errno (ENOTDIR
);
124 /* Make sure the descriptor allows for reading. */
125 int flags
= __fcntl (fd
, F_GETFL
);
126 if (__builtin_expect (flags
== -1, 0))
128 if (__builtin_expect ((flags
& O_ACCMODE
) == O_WRONLY
, 0))
130 __set_errno (EINVAL
);
135 return __alloc_dir (fd
, false);