Update.
[glibc.git] / sysdeps / unix / sysv / linux / getdents.c
blob5f1e7c1b30b4df80997d99e704e6c99c4619876b
1 /* Copyright (C) 1993, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
19 #include <alloca.h>
20 #include <assert.h>
21 #include <errno.h>
22 #include <dirent.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/param.h>
27 #include <sys/types.h>
29 #include <sysdep.h>
30 #include <sys/syscall.h>
32 #include <linux/posix_types.h>
34 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
37 extern int __syscall_getdents (int fd, char *buf, size_t nbytes);
39 /* For Linux we need a special version of this file since the
40 definition of `struct dirent' is not the same for the kernel and
41 the libc. There is one additional field which might be introduced
42 in the kernel structure in the future.
44 Here is the kernel definition of `struct dirent' as of 2.1.20: */
46 struct kernel_dirent
48 long int d_ino;
49 __kernel_off_t d_off;
50 unsigned short int d_reclen;
51 char d_name[256];
54 #ifdef GETDENTS64
55 # define __getdents __getdents64
56 # define dirent dirent64
57 #endif
59 /* The problem here is that we cannot simply read the next NBYTES
60 bytes. We need to take the additional field into account. We use
61 some heuristic. Assuming the directory contains names with 14
62 characters on average we can compute an estimated number of entries
63 which fit in the buffer. Taking this number allows us to specify a
64 reasonable number of bytes to read. If we should be wrong, we can
65 reset the file descriptor. In practice the kernel is limiting the
66 amount of data returned much more then the reduced buffer size. */
67 ssize_t
68 internal_function
69 __getdents (int fd, char *buf, size_t nbytes)
71 off_t last_offset = -1;
72 size_t red_nbytes;
73 struct kernel_dirent *skdp, *kdp;
74 struct dirent *dp;
75 int retval;
76 const size_t size_diff = (offsetof (struct dirent, d_name)
77 - offsetof (struct kernel_dirent, d_name));
79 red_nbytes = MIN (nbytes
80 - ((nbytes / (offsetof (struct dirent, d_name) + 14))
81 * size_diff),
82 nbytes - size_diff);
84 dp = (struct dirent *) buf;
85 skdp = kdp = __alloca (red_nbytes);
87 retval = INLINE_SYSCALL (getdents, 3, fd, (char *) kdp, red_nbytes);
89 if (retval == -1)
90 return -1;
92 while ((char *) kdp < (char *) skdp + retval)
94 const size_t alignment = __alignof__ (struct dirent);
95 /* Since kdp->d_reclen is already aligned for the kernel structure
96 this may compute a value that is bigger than necessary. */
97 size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
98 & ~(alignment - 1));
99 if ((char *) dp + new_reclen > buf + nbytes)
101 /* Our heuristic failed. We read too many entries. Reset
102 the stream. */
103 assert (last_offset != -1);
104 __lseek (fd, last_offset, SEEK_SET);
106 if ((char *) dp == buf)
108 /* The buffer the user passed in is too small to hold even
109 one entry. */
110 __set_errno (EINVAL);
111 return -1;
114 break;
117 last_offset = kdp->d_off;
118 dp->d_ino = kdp->d_ino;
119 dp->d_off = kdp->d_off;
120 dp->d_reclen = new_reclen;
121 dp->d_type = DT_UNKNOWN;
122 memcpy (dp->d_name, kdp->d_name,
123 kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
125 dp = (struct dirent *) ((char *) dp + new_reclen);
126 kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
129 return (char *) dp - buf;