Update.
[glibc.git] / sysdeps / unix / sysv / linux / getdents.c
blobb1b532a396145abb8f57094968fe95eb80d00e58
1 /* Copyright (C) 1993, 1995, 1996, 1997 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 <dirent.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/param.h>
25 #include <sys/types.h>
27 #include <linux/posix_types.h>
29 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
32 extern int __getdents __P ((int fd, char *buf, size_t nbytes));
34 /* For Linux we need a special version of this file since the
35 definition of `struct dirent' is not the same for the kernel and
36 the libc. There is one additional field which might be introduced
37 in the kernel structure in the future.
39 He is the kernel definition of `struct dirent' as of 2.1.20: */
41 struct kernel_dirent
43 long int d_ino;
44 __kernel_off_t d_off;
45 unsigned short int d_reclen;
46 char d_name[256];
49 #ifdef GETDENTS64
50 #define __getdirentries __getdirentries64
51 #define dirent dirent64
52 #endif
54 /* The problem here is that we cannot simply read the next NBYTES
55 bytes. We need to take the additional field into account. We use
56 some heuristic. Assuming the directory contains names with 14
57 characters on average we can compute an estimate number of entries
58 which fit in the buffer. Taking this number allows us to specify a
59 correct number of bytes to read. If we should be wrong, we can reset
60 the file descriptor. */
61 ssize_t
62 __getdirentries (fd, buf, nbytes, basep)
63 int fd;
64 char *buf;
65 size_t nbytes;
66 off_t *basep;
68 off_t base = __lseek (fd, (off_t) 0, SEEK_CUR);
69 off_t last_offset = base;
70 size_t red_nbytes;
71 struct kernel_dirent *skdp, *kdp;
72 struct dirent *dp;
73 int retval;
74 const size_t size_diff = (offsetof (struct dirent, d_name)
75 - offsetof (struct kernel_dirent, d_name));
77 red_nbytes = nbytes - ((nbytes / (offsetof (struct dirent, d_name) + 14))
78 * size_diff);
80 dp = (struct dirent *) buf;
81 skdp = kdp = __alloca (red_nbytes);
83 retval = __getdents (fd, (char *) kdp, red_nbytes);
85 while ((char *) kdp < (char *) skdp + retval)
87 const size_t alignment = __alignof__ (struct dirent);
88 /* Since kdp->d_reclen is already aligned for the kernel structure
89 this may compute a value that is bigger than necessary. */
90 size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
91 & ~(alignment - 1));
92 if ((char *) dp + new_reclen > buf + nbytes)
94 /* Our heuristic failed. We read too many entries. Reset
95 the stream. */
96 __lseek (fd, last_offset, SEEK_SET);
97 break;
100 last_offset = kdp->d_off;
101 dp->d_ino = kdp->d_ino;
102 dp->d_off = kdp->d_off;
103 dp->d_reclen = new_reclen;
104 dp->d_type = DT_UNKNOWN;
105 memcpy (dp->d_name, kdp->d_name,
106 kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
108 dp = (struct dirent *) ((char *) dp + new_reclen);
109 kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
112 if (basep)
113 *basep = base;
115 return (char *) dp - buf;
118 #ifndef GETDENTS64
119 weak_alias (__getdirentries, getdirentries)
120 #endif