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 / getdents64.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 <features.h>
20 #include <alloca.h>
21 #include <assert.h>
22 #include <errno.h>
23 #include <dirent.h>
24 #include <stddef.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <sys/param.h>
29 #include <sys/types.h>
30 #include <sys/syscall.h>
31
32 libc_hidden_proto(memcpy)
33 libc_hidden_proto(lseek64)
34
35 #if defined __UCLIBC_HAS_LFS__ && defined __NR_getdents64 
36
37 #ifndef offsetof
38 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
39 #endif
40
41 struct kernel_dirent64 
42 {
43     uint64_t            d_ino;
44     int64_t             d_off;
45     unsigned short      d_reclen;
46     unsigned char       d_type;
47     char                d_name[256];
48 };
49
50
51 #define __NR___syscall_getdents64 __NR_getdents64
52 static inline _syscall3(int, __syscall_getdents64, int, fd, unsigned char *, dirp, size_t, count);
53
54
55 ssize_t attribute_hidden __getdents64 (int fd, char *buf, size_t nbytes)
56 {
57     struct dirent64 *dp;
58     off64_t last_offset = -1;
59     ssize_t retval;
60     size_t red_nbytes;
61     struct kernel_dirent64 *skdp, *kdp;
62     const size_t size_diff = (offsetof (struct dirent64, d_name)
63             - offsetof (struct kernel_dirent64, d_name));
64
65     red_nbytes = MIN (nbytes - ((nbytes / 
66                     (offsetof (struct dirent64, d_name) + 14)) * size_diff), 
67             nbytes - size_diff);
68
69     dp = (struct dirent64 *) buf;
70     skdp = kdp = alloca (red_nbytes);
71
72     retval = __syscall_getdents64(fd, (char *)kdp, red_nbytes);
73     if (retval == -1)
74         return -1;
75
76     while ((char *) kdp < (char *) skdp + retval) {
77         const size_t alignment = __alignof__ (struct dirent64);
78         /* Since kdp->d_reclen is already aligned for the kernel structure
79            this may compute a value that is bigger than necessary.  */
80         size_t new_reclen = ((kdp->d_reclen + size_diff + alignment - 1)
81                 & ~(alignment - 1));
82         if ((char *) dp + new_reclen > buf + nbytes) {
83             /* Our heuristic failed.  We read too many entries.  Reset
84                the stream.  */
85             assert (last_offset != -1);
86             lseek64(fd, last_offset, SEEK_SET);
87
88             if ((char *) dp == buf) {
89                 /* The buffer the user passed in is too small to hold even
90                    one entry.  */
91                 __set_errno (EINVAL);
92                 return -1;
93             }
94             break;
95         }
96
97         last_offset = kdp->d_off;
98         dp->d_ino = kdp->d_ino;
99         dp->d_off = kdp->d_off;
100         dp->d_reclen = new_reclen;
101         dp->d_type = DT_UNKNOWN;
102         memcpy (dp->d_name, kdp->d_name,
103                 kdp->d_reclen - offsetof (struct kernel_dirent64, d_name));
104         dp = (struct dirent64 *) ((char *) dp + new_reclen);
105         kdp = (struct kernel_dirent64 *) (((char *) kdp) + kdp->d_reclen);
106     }
107     return (char *) dp - buf;
108 }
109 #else
110 extern ssize_t __getdents (int fd, char *buf, size_t nbytes) attribute_hidden;
111 ssize_t attribute_hidden __getdents64 (int fd, char *buf, size_t nbytes)
112 {
113     return(__getdents(fd, buf, nbytes));
114 }
115 #endif /* __UCLIBC_HAS_LFS__ */