update from main archive 960105
[glibc.git] / sysdeps / unix / sysv / linux / getdents.c
blob0504393e6763c06995d81224ea270c2247f3b32b
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 <dirent.h>
20 #include <stddef.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <sys/types.h>
25 #include <linux/posix_types.h>
27 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
30 extern int __getdents __P ((int fd, char *buf, size_t nbytes));
32 /* For Linux we need a special version of this file since the
33 definition of `struct dirent' is not the same for the kernel and
34 the libc. There is one additional field which might be introduced
35 in the kernel structure in the future.
37 He is the kernel definition of `struct dirent' as of 2.1.20: */
39 struct kernel_dirent
41 long int d_ino;
42 __kernel_off_t d_off;
43 unsigned short int d_reclen;
44 char d_name[256];
48 /* The problem here is that we cannot simply read the next NBYTES
49 bytes. We need to take the additional field into account. We use
50 some heuristic. Assume the directory contains names with at least
51 3 characters we can compute a maximum number of entries which fit
52 in the buffer. Taking this number allows us to specify a correct
53 number of bytes to read. If we should be wrong, we can reset the
54 file descriptor. */
55 int __getdirentries __P ((int fd, char *buf, size_t nbytes, off_t *basep));
56 int
57 __getdirentries (fd, buf, nbytes, basep)
58 int fd;
59 char *buf;
60 size_t nbytes;
61 off_t *basep;
63 off_t base = __lseek (fd, (off_t) 0, SEEK_CUR);
64 size_t red_nbytes;
65 struct kernel_dirent *kdp;
66 struct dirent *dp;
67 int retval;
69 red_nbytes = nbytes - (nbytes / (offsetof (struct dirent, d_name) + 3));
71 dp = (struct dirent *) buf;
72 kdp = (struct kernel_dirent *) (buf + (nbytes - red_nbytes));
74 retval = __getdents (fd, (char *) kdp, red_nbytes);
76 while ((char *) kdp < buf + (nbytes - red_nbytes) + retval)
78 dp->d_ino = kdp->d_ino;
79 dp->d_off = kdp->d_off;
80 dp->d_reclen = (kdp->d_reclen
81 + (offsetof (struct dirent, d_name)
82 - offsetof (struct kernel_dirent, d_name)));
83 dp->d_type = DT_UNKNOWN;
84 memmove (dp->d_name, kdp->d_name,
85 kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
87 dp = (struct dirent *) (((char *) dp) + dp->d_reclen);
88 kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
90 if ((char *) dp >= (char *) kdp)
92 /* Our heuristic failed. We read too many entries. Reset
93 the stream. */
94 off_t used = ((char *) kdp - (char *) buf) - (nbytes - red_nbytes);
95 base = __lseek (fd, retval - used, SEEK_CUR);
99 if (basep)
100 *basep = base;
102 return (char *) dp - (char *) buf;
105 weak_alias (__getdirentries, getdirentries)