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. */
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: */
45 unsigned short int d_reclen
;
50 #define __getdirentries __getdirentries64
51 #define dirent dirent64
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. */
62 __getdirentries (fd
, buf
, nbytes
, basep
)
68 off_t base
= __lseek (fd
, (off_t
) 0, SEEK_CUR
);
69 off_t last_offset
= base
;
71 struct kernel_dirent
*skdp
, *kdp
;
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))
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)
92 if ((char *) dp
+ new_reclen
> buf
+ nbytes
)
94 /* Our heuristic failed. We read too many entries. Reset
96 __lseek (fd
, last_offset
, SEEK_SET
);
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
);
115 return (char *) dp
- buf
;
119 weak_alias (__getdirentries
, getdirentries
)