OSDN Git Service

6a38772e8c84b05a7d3b76f5a93e07bda8b7c490
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / common / getdents.c
1 /*
2  * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
3  *
4  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
5  */
6
7 #include <alloca.h>
8 #include <assert.h>
9 #include <errno.h>
10 #include <dirent.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <sys/param.h>
16 #include <sys/types.h>
17 #include <sys/syscall.h>
18 #include <bits/kernel_types.h>
19 #include <bits/kernel-features.h>
20
21 #if !(defined __UCLIBC_HAS_LFS__ && defined __NR_getdents64 && __WORDSIZE == 64)
22 /* If the condition above is met, __getdents is defined as an alias
23  * for __getdents64 (see getdents64.c). Otherwise...
24  */
25
26 /* With newer versions of linux, the getdents syscall returns d_type
27  * information after the name field.
28  *
29  * See __ASSUME_GETDENTS32_D_TYPE in glibc's kernel-features.h for specific
30  * version / arch details.
31  */
32
33 #ifndef offsetof
34 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
35 #endif
36
37 struct kernel_dirent
38 {
39         long int d_ino;
40         __kernel_off_t d_off;
41         unsigned short int d_reclen;
42         char d_name[256];
43 };
44
45 ssize_t __getdents (int fd, char *buf, size_t nbytes) attribute_hidden;
46
47 #define __NR___syscall_getdents __NR_getdents
48 static __always_inline _syscall3(int, __syscall_getdents, int, fd, unsigned char *, kdirp, size_t, count)
49
50 #if defined __ASSUME_GETDENTS32_D_TYPE
51
52 ssize_t __getdents (int fd, char *buf, size_t nbytes)
53 {
54         ssize_t retval;
55
56         retval = __syscall_getdents(fd, (unsigned char *)buf, nbytes);
57
58         /* The kernel added the d_type value after the name.  Change
59         this now.  */
60         if (retval != -1) {
61                 union {
62                         struct kernel_dirent k;
63                         struct dirent u;
64                 } *kbuf = (void *) buf;
65
66                 while ((char *) kbuf < buf + retval) {
67                         char d_type = *((char *) kbuf + kbuf->k.d_reclen - 1);
68                         memmove (kbuf->u.d_name, kbuf->k.d_name,
69                         strlen (kbuf->k.d_name) + 1);
70                         kbuf->u.d_type = d_type;
71
72                         kbuf = (void *) ((char *) kbuf + kbuf->k.d_reclen);
73                 }
74         }
75
76         return retval;
77 }
78
79 #elif ! defined __UCLIBC_HAS_LFS__ || ! defined __NR_getdents64
80
81 ssize_t __getdents (int fd, char *buf, size_t nbytes)
82 {
83     struct dirent *dp;
84     off_t last_offset = -1;
85     ssize_t retval;
86     size_t red_nbytes;
87     struct kernel_dirent *skdp, *kdp;
88     const size_t size_diff = (offsetof (struct dirent, d_name)
89             - offsetof (struct kernel_dirent, d_name));
90
91     red_nbytes = MIN (nbytes - ((nbytes /
92                     (offsetof (struct dirent, d_name) + 14)) * size_diff),
93             nbytes - size_diff);
94
95     dp = (struct dirent *) buf;
96     skdp = kdp = alloca (red_nbytes);
97
98     retval = __syscall_getdents(fd, (unsigned char *)kdp, red_nbytes);
99     if (retval == -1)
100         return -1;
101
102     while ((char *) kdp < (char *) skdp + retval) {
103         const size_t alignment = __alignof__ (struct dirent);
104         /* Since kdp->d_reclen is already aligned for the kernel structure
105            this may compute a value that is bigger than necessary.  */
106         size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
107                 & ~(alignment - 1));
108         if ((char *) dp + new_reclen > buf + nbytes) {
109             /* Our heuristic failed.  We read too many entries.  Reset
110                the stream.  */
111             assert (last_offset != -1);
112             lseek(fd, last_offset, SEEK_SET);
113
114             if ((char *) dp == buf) {
115                 /* The buffer the user passed in is too small to hold even
116                    one entry.  */
117                 __set_errno (EINVAL);
118                 return -1;
119             }
120             break;
121         }
122
123         last_offset = kdp->d_off;
124         dp->d_ino = kdp->d_ino;
125         dp->d_off = kdp->d_off;
126         dp->d_reclen = new_reclen;
127         dp->d_type = DT_UNKNOWN;
128         memcpy (dp->d_name, kdp->d_name,
129                 kdp->d_reclen - offsetof (struct kernel_dirent, d_name));
130         dp = (struct dirent *) ((char *) dp + new_reclen);
131         kdp = (struct kernel_dirent *) (((char *) kdp) + kdp->d_reclen);
132     }
133     return (char *) dp - buf;
134 }
135
136 #if defined __UCLIBC_HAS_LFS__ && ! defined __NR_getdents64
137 attribute_hidden strong_alias(__getdents,__getdents64)
138 #endif
139
140 #elif __WORDSIZE == 32
141
142 extern __typeof(__getdents) __getdents64 attribute_hidden;
143 ssize_t __getdents (int fd, char *buf, size_t nbytes)
144 {
145     struct dirent *dp;
146     struct dirent64 *dp64;
147     ssize_t ret = __getdents64 (fd, buf, nbytes);
148
149     if (ret <= 0)
150         return ret;
151
152     dp64 = (struct dirent64 *) buf;
153     buf += ret;
154     while ((void *) dp64 < (void *) buf) {
155         dp = (struct dirent *) dp64;
156         dp->d_ino = dp64->d_ino;
157         dp->d_off = dp64->d_off;
158         dp->d_reclen = dp64->d_reclen;
159         dp->d_type = dp64->d_type;
160         memmove (dp->d_name, dp64->d_name, dp->d_reclen - offsetof (struct dirent64, d_name));
161         memmove (dp64, dp, dp->d_reclen);
162         dp64 = ((void *) dp64) + dp->d_reclen;
163     }
164
165     return ret;
166 }
167
168 #endif
169
170 #endif