xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getdents/getdents.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2013 Cyril Hrubis <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  *
6*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software;  you can redistribute it and/or modify
7*49cdfc7eSAndroid Build Coastguard Worker  * it under the terms of the GNU General Public License as published by
8*49cdfc7eSAndroid Build Coastguard Worker  * the Free Software Foundation; either version 2 of the License, or
9*49cdfc7eSAndroid Build Coastguard Worker  * (at your option) any later version.
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it will be useful,
12*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
13*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14*49cdfc7eSAndroid Build Coastguard Worker  * the GNU General Public License for more details.
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
17*49cdfc7eSAndroid Build Coastguard Worker  * along with this program;  if not, write to the Free Software
18*49cdfc7eSAndroid Build Coastguard Worker  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #ifndef GETDENTS_H
22*49cdfc7eSAndroid Build Coastguard Worker #define GETDENTS_H
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #include <stdint.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
26*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #if HAVE_GETDENTS || HAVE_GETDENTS64
29*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
30*49cdfc7eSAndroid Build Coastguard Worker #endif
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker /*
33*49cdfc7eSAndroid Build Coastguard Worker  * See fs/compat.c struct compat_linux_dirent
34*49cdfc7eSAndroid Build Coastguard Worker  */
35*49cdfc7eSAndroid Build Coastguard Worker struct linux_dirent {
36*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long   d_ino;
37*49cdfc7eSAndroid Build Coastguard Worker 	unsigned long   d_off;
38*49cdfc7eSAndroid Build Coastguard Worker 	unsigned short  d_reclen;
39*49cdfc7eSAndroid Build Coastguard Worker 	char            d_name[];
40*49cdfc7eSAndroid Build Coastguard Worker };
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker static inline int
linux_getdents(unsigned int fd,struct linux_dirent * dirp,unsigned int size)43*49cdfc7eSAndroid Build Coastguard Worker linux_getdents(unsigned int fd, struct linux_dirent *dirp, unsigned int size)
44*49cdfc7eSAndroid Build Coastguard Worker {
45*49cdfc7eSAndroid Build Coastguard Worker 	return tst_syscall(__NR_getdents, fd, dirp, size);
46*49cdfc7eSAndroid Build Coastguard Worker }
47*49cdfc7eSAndroid Build Coastguard Worker 
48*49cdfc7eSAndroid Build Coastguard Worker struct linux_dirent64 {
49*49cdfc7eSAndroid Build Coastguard Worker 	uint64_t	d_ino;
50*49cdfc7eSAndroid Build Coastguard Worker 	int64_t		d_off;
51*49cdfc7eSAndroid Build Coastguard Worker 	unsigned short	d_reclen;
52*49cdfc7eSAndroid Build Coastguard Worker 	unsigned char	d_type;
53*49cdfc7eSAndroid Build Coastguard Worker 	char		d_name[];
54*49cdfc7eSAndroid Build Coastguard Worker };
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker static inline int
linux_getdents64(unsigned int fd,struct linux_dirent64 * dirp64,unsigned int size)57*49cdfc7eSAndroid Build Coastguard Worker linux_getdents64(unsigned int fd, struct linux_dirent64 *dirp64, unsigned int size)
58*49cdfc7eSAndroid Build Coastguard Worker {
59*49cdfc7eSAndroid Build Coastguard Worker 	return tst_syscall(__NR_getdents64, fd, dirp64, size);
60*49cdfc7eSAndroid Build Coastguard Worker }
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker static inline size_t
tst_dirp_size(void)63*49cdfc7eSAndroid Build Coastguard Worker tst_dirp_size(void)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker 	switch (tst_variant) {
66*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
67*49cdfc7eSAndroid Build Coastguard Worker 		return sizeof(struct linux_dirent);
68*49cdfc7eSAndroid Build Coastguard Worker 	case 1:
69*49cdfc7eSAndroid Build Coastguard Worker 		return sizeof(struct linux_dirent64);
70*49cdfc7eSAndroid Build Coastguard Worker #if HAVE_GETDENTS
71*49cdfc7eSAndroid Build Coastguard Worker 	case 2:
72*49cdfc7eSAndroid Build Coastguard Worker 		return sizeof(struct dirent);
73*49cdfc7eSAndroid Build Coastguard Worker #endif
74*49cdfc7eSAndroid Build Coastguard Worker #if HAVE_GETDENTS64
75*49cdfc7eSAndroid Build Coastguard Worker 	case 3:
76*49cdfc7eSAndroid Build Coastguard Worker 		return sizeof(struct dirent64);
77*49cdfc7eSAndroid Build Coastguard Worker #endif
78*49cdfc7eSAndroid Build Coastguard Worker 	}
79*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker static inline const char *
tst_dirp_name(void * dirp)83*49cdfc7eSAndroid Build Coastguard Worker tst_dirp_name(void *dirp)
84*49cdfc7eSAndroid Build Coastguard Worker {
85*49cdfc7eSAndroid Build Coastguard Worker 	switch (tst_variant) {
86*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
87*49cdfc7eSAndroid Build Coastguard Worker 		return ((struct linux_dirent *)dirp)->d_name;
88*49cdfc7eSAndroid Build Coastguard Worker 	case 1:
89*49cdfc7eSAndroid Build Coastguard Worker 		return ((struct linux_dirent64 *)dirp)->d_name;
90*49cdfc7eSAndroid Build Coastguard Worker #if HAVE_GETDENTS
91*49cdfc7eSAndroid Build Coastguard Worker 	case 2:
92*49cdfc7eSAndroid Build Coastguard Worker 		return ((struct dirent *)dirp)->d_name;
93*49cdfc7eSAndroid Build Coastguard Worker #endif
94*49cdfc7eSAndroid Build Coastguard Worker #if HAVE_GETDENTS64
95*49cdfc7eSAndroid Build Coastguard Worker 	case 3:
96*49cdfc7eSAndroid Build Coastguard Worker 		return ((struct dirent64 *)dirp)->d_name;
97*49cdfc7eSAndroid Build Coastguard Worker #endif
98*49cdfc7eSAndroid Build Coastguard Worker 	}
99*49cdfc7eSAndroid Build Coastguard Worker 	return NULL;
100*49cdfc7eSAndroid Build Coastguard Worker }
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker static inline size_t
tst_dirp_reclen(void * dirp)103*49cdfc7eSAndroid Build Coastguard Worker tst_dirp_reclen(void *dirp)
104*49cdfc7eSAndroid Build Coastguard Worker {
105*49cdfc7eSAndroid Build Coastguard Worker 	switch (tst_variant) {
106*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
107*49cdfc7eSAndroid Build Coastguard Worker 		return ((struct linux_dirent *)dirp)->d_reclen;
108*49cdfc7eSAndroid Build Coastguard Worker 	case 1:
109*49cdfc7eSAndroid Build Coastguard Worker 		return ((struct linux_dirent64 *)dirp)->d_reclen;
110*49cdfc7eSAndroid Build Coastguard Worker #if HAVE_GETDENTS
111*49cdfc7eSAndroid Build Coastguard Worker 	case 2:
112*49cdfc7eSAndroid Build Coastguard Worker 		return ((struct dirent *)dirp)->d_reclen;
113*49cdfc7eSAndroid Build Coastguard Worker #endif
114*49cdfc7eSAndroid Build Coastguard Worker #if HAVE_GETDENTS64
115*49cdfc7eSAndroid Build Coastguard Worker 	case 3:
116*49cdfc7eSAndroid Build Coastguard Worker 		return ((struct dirent64 *)dirp)->d_reclen;
117*49cdfc7eSAndroid Build Coastguard Worker #endif
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	}
120*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
121*49cdfc7eSAndroid Build Coastguard Worker }
122*49cdfc7eSAndroid Build Coastguard Worker 
123*49cdfc7eSAndroid Build Coastguard Worker static inline int
tst_getdents(int fd,void * dirp,unsigned int size)124*49cdfc7eSAndroid Build Coastguard Worker tst_getdents(int fd, void *dirp, unsigned int size)
125*49cdfc7eSAndroid Build Coastguard Worker {
126*49cdfc7eSAndroid Build Coastguard Worker 	switch (tst_variant) {
127*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
128*49cdfc7eSAndroid Build Coastguard Worker 		return linux_getdents(fd, dirp, size);
129*49cdfc7eSAndroid Build Coastguard Worker 	case 1:
130*49cdfc7eSAndroid Build Coastguard Worker 		return linux_getdents64(fd, dirp, size);
131*49cdfc7eSAndroid Build Coastguard Worker #if HAVE_GETDENTS
132*49cdfc7eSAndroid Build Coastguard Worker 	case 2:
133*49cdfc7eSAndroid Build Coastguard Worker 		return getdents(fd, dirp, size);
134*49cdfc7eSAndroid Build Coastguard Worker #endif
135*49cdfc7eSAndroid Build Coastguard Worker #if HAVE_GETDENTS64
136*49cdfc7eSAndroid Build Coastguard Worker 	case 3:
137*49cdfc7eSAndroid Build Coastguard Worker 		return getdents64(fd, dirp, size);
138*49cdfc7eSAndroid Build Coastguard Worker #endif
139*49cdfc7eSAndroid Build Coastguard Worker 	}
140*49cdfc7eSAndroid Build Coastguard Worker 	return -1;
141*49cdfc7eSAndroid Build Coastguard Worker }
142*49cdfc7eSAndroid Build Coastguard Worker 
143*49cdfc7eSAndroid Build Coastguard Worker static inline void
getdents_info(void)144*49cdfc7eSAndroid Build Coastguard Worker getdents_info(void)
145*49cdfc7eSAndroid Build Coastguard Worker {
146*49cdfc7eSAndroid Build Coastguard Worker 	switch (tst_variant) {
147*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
148*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Testing the SYS_getdents syscall");
149*49cdfc7eSAndroid Build Coastguard Worker 		break;
150*49cdfc7eSAndroid Build Coastguard Worker 	case 1:
151*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Testing the SYS_getdents64 syscall");
152*49cdfc7eSAndroid Build Coastguard Worker 		break;
153*49cdfc7eSAndroid Build Coastguard Worker 	case 2:
154*49cdfc7eSAndroid Build Coastguard Worker #if HAVE_GETDENTS
155*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Testing libc getdents()");
156*49cdfc7eSAndroid Build Coastguard Worker #else
157*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "libc getdents() is not implemented");
158*49cdfc7eSAndroid Build Coastguard Worker #endif
159*49cdfc7eSAndroid Build Coastguard Worker 		break;
160*49cdfc7eSAndroid Build Coastguard Worker 	case 3:
161*49cdfc7eSAndroid Build Coastguard Worker #if HAVE_GETDENTS64
162*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TINFO, "Testing libc getdents64()");
163*49cdfc7eSAndroid Build Coastguard Worker #else
164*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TCONF, "libc getdents64() is not implemented");
165*49cdfc7eSAndroid Build Coastguard Worker #endif
166*49cdfc7eSAndroid Build Coastguard Worker 		break;
167*49cdfc7eSAndroid Build Coastguard Worker 	}
168*49cdfc7eSAndroid Build Coastguard Worker }
169*49cdfc7eSAndroid Build Coastguard Worker 
170*49cdfc7eSAndroid Build Coastguard Worker #define TEST_VARIANTS 4
171*49cdfc7eSAndroid Build Coastguard Worker 
172*49cdfc7eSAndroid Build Coastguard Worker #endif /* GETDENTS_H */
173