OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / common / getdents.c
1 /* Copyright (C) 1993, 1995-2002 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
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    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <alloca.h>
20 #include <assert.h>
21 #include <errno.h>
22 #include <dirent.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/param.h>
28 #include <sys/types.h>
29 #include <sys/syscall.h>
30
31 libc_hidden_proto(memcpy)
32 libc_hidden_proto(lseek)
33
34 #ifndef offsetof
35 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
36 #endif
37
38 struct kernel_dirent
39 {
40     long                d_ino;
41     __kernel_off_t      d_off;
42     unsigned short      d_reclen;
43     char                d_name[256];
44 };
45
46 #define __NR___syscall_getdents __NR_getdents
47 static inline _syscall3(int, __syscall_getdents, int, fd, unsigned char *, kdirp, size_t, count);
48
49
50 ssize_t attribute_hidden __getdents (int fd, char *buf, size_t nbytes)
51 {
52     struct dirent *dp;
53     off_t last_offset = -1;
54     ssize_t retval;
55     size_t red_nbytes;
56     struct kernel_dirent *skdp, *kdp;
57     const size_t size_diff = (offsetof (struct dirent, d_name)
58             - offsetof (struct kernel_dirent, d_name));
59
60     red_nbytes = MIN (nbytes - ((nbytes / 
61                     (offsetof (struct dirent, d_name) + 14)) * size_diff), 
62             nbytes - size_diff);
63
64     dp = (struct dirent *) buf;
65     skdp = kdp = alloca (red_nbytes);
66
67     retval = __syscall_getdents(fd, (char *)kdp, red_nbytes);
68     if (retval == -1)
69         return -1;
70
71     while ((char *) kdp < (char *) skdp + retval) {
72         const size_t alignment = __alignof__ (struct dirent);
73         /* Since kdp->d_reclen is already aligned for the kernel structure
74            this may compute a value that is bigger than necessary.  */
75         size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
76                 & ~(alignment - 1));
77         if ((char *) dp + new_reclen > buf + nbytes) {
78             /* Our heuristic failed.  We read too many entries.  Reset
79                the stream.  */
80             assert (last_offset != -1);
81             lseek(fd, last_offset, SEEK_SET);
82
83             if ((char *) dp == buf) {
84                 /* The buffer the user passed in is too small to hold even
85                    one entry.  */
86                 __set_errno (EINVAL);
87                 return -1;
88             }
89             break;
90         }
91
92         last_offset = kdp->d_off;
93         dp->d_ino = kdp->d_ino;
94         dp->d_off = kdp->d_off;
95         dp->d_reclen = new_reclen;
96         dp->d_type = DT_UNKNOWN;
97         memcpy (dp->d_name, kdp->d_name,
98                 kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
99         dp = (struct dirent *) ((char *) dp + new_reclen);
100         kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
101     }
102     return (char *) dp - buf;
103 }