seq no longer mishandles cases like "seq 0 0.000001 0.000003",
[coreutils/ericb.git] / lib / fdopendir-glibc.c
blob2bafac94e540352061933ae6d1a3dc0ccb92216c
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)
8 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 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. */
19 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <stdbool.h>
26 #include <dirent.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
31 #if _LIBC
32 # include <dirstream.h>
33 # include <not-cancel.h>
35 #else
37 # if __GNUC__ < 3
38 # define __builtin_expect(expr, expected_val) expr
39 # endif
41 # include "openat.h"
42 # define stat64 stat
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)
49 # ifdef __i386__
50 # define internal_function __attribute ((regparm (3), stdcall))
51 # else
52 # define internal_function
53 # endif
55 struct __dirstream
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. */
65 int lock;
67 #endif
69 #undef _STATBUF_ST_BLKSIZE
71 static DIR *
72 internal_function
73 __alloc_dir (int fd, bool close_fd)
75 if (__builtin_expect (__fcntl (fd, F_SETFD, FD_CLOEXEC), 0) < 0)
76 goto lose;
78 size_t allocation;
79 #ifdef _STATBUF_ST_BLKSIZE
80 if (__builtin_expect ((size_t) statp->st_blksize >= sizeof (struct dirent64),
81 1))
82 allocation = statp->st_blksize;
83 else
84 #endif
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);
91 if (dirp == NULL)
92 lose:
94 if (close_fd)
96 int save_errno = errno;
97 close_not_cancel_no_status (fd);
98 __set_errno (save_errno);
100 return NULL;
102 memset (dirp, '\0', sizeof (DIR));
103 dirp->data = (char *) (dirp + 1) + pad;
104 dirp->allocation = allocation;
105 dirp->fd = fd;
107 __libc_lock_init (dirp->lock);
109 return dirp;
112 DIR *
113 fdopendir (int fd)
115 #if 0
116 struct stat64 statbuf;
118 if (__builtin_expect (__fxstat64 (_STAT_VER, fd, &statbuf), 0) < 0)
119 return NULL;
120 if (__builtin_expect (! S_ISDIR (statbuf.st_mode), 0))
122 __set_errno (ENOTDIR);
123 return NULL;
125 /* Make sure the descriptor allows for reading. */
126 int flags = __fcntl (fd, F_GETFL);
127 if (__builtin_expect (flags == -1, 0))
128 return NULL;
129 if (__builtin_expect ((flags & O_ACCMODE) == O_WRONLY, 0))
131 __set_errno (EINVAL);
132 return NULL;
134 #endif
136 return __alloc_dir (fd, false);