tests: ls -v: exercise the bug fixed by gnulib's new filevercmp
[coreutils.git] / lib / fdopendir-glibc.c
blob71c32e2461fa5d1e722a770cd577136017bfbb0e
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/>. */
18 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <stdbool.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
30 #if _LIBC
31 # include <dirstream.h>
32 # include <not-cancel.h>
34 #else
36 # if __GNUC__ < 3
37 # define __builtin_expect(expr, expected_val) expr
38 # endif
40 # include "openat.h"
41 # define stat64 stat
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)
48 # ifdef __i386__
49 # define internal_function __attribute ((regparm (3), stdcall))
50 # else
51 # define internal_function
52 # endif
54 struct __dirstream
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. */
64 int lock;
66 #endif
68 #undef _STATBUF_ST_BLKSIZE
70 static DIR *
71 internal_function
72 __alloc_dir (int fd, bool close_fd)
74 if (__builtin_expect (__fcntl (fd, F_SETFD, FD_CLOEXEC), 0) < 0)
75 goto lose;
77 size_t allocation;
78 #ifdef _STATBUF_ST_BLKSIZE
79 if (__builtin_expect ((size_t) statp->st_blksize >= sizeof (struct dirent64),
80 1))
81 allocation = statp->st_blksize;
82 else
83 #endif
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);
90 if (dirp == NULL)
91 lose:
93 if (close_fd)
95 int save_errno = errno;
96 close_not_cancel_no_status (fd);
97 __set_errno (save_errno);
99 return NULL;
101 memset (dirp, '\0', sizeof (DIR));
102 dirp->data = (char *) (dirp + 1) + pad;
103 dirp->allocation = allocation;
104 dirp->fd = fd;
106 __libc_lock_init (dirp->lock);
108 return dirp;
111 DIR *
112 fdopendir (int fd)
114 #if 0
115 struct stat64 statbuf;
117 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &statbuf), 0) < 0)
118 return NULL;
119 if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
121 __set_errno (ENOTDIR);
122 return NULL;
124 /* Make sure the descriptor allows for reading. */
125 int flags = __fcntl (fd, F_GETFL);
126 if (__builtin_expect (flags == -1, 0))
127 return NULL;
128 if (__builtin_expect ((flags & O_ACCMODE) == O_WRONLY, 0))
130 __set_errno (EINVAL);
131 return NULL;
133 #endif
135 return __alloc_dir (fd, false);