xref: /aosp_15_r20/external/e2fsprogs/lib/ext2fs/llseek.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * llseek.c -- stub calling the llseek system call
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
5*6a54128fSAndroid Build Coastguard Worker  *
6*6a54128fSAndroid Build Coastguard Worker  * %Begin-Header%
7*6a54128fSAndroid Build Coastguard Worker  * This file may be redistributed under the terms of the GNU Library
8*6a54128fSAndroid Build Coastguard Worker  * General Public License, version 2.
9*6a54128fSAndroid Build Coastguard Worker  * %End-Header%
10*6a54128fSAndroid Build Coastguard Worker  */
11*6a54128fSAndroid Build Coastguard Worker 
12*6a54128fSAndroid Build Coastguard Worker #ifndef _LARGEFILE_SOURCE
13*6a54128fSAndroid Build Coastguard Worker #define _LARGEFILE_SOURCE
14*6a54128fSAndroid Build Coastguard Worker #endif
15*6a54128fSAndroid Build Coastguard Worker #ifndef _LARGEFILE64_SOURCE
16*6a54128fSAndroid Build Coastguard Worker #define _LARGEFILE64_SOURCE
17*6a54128fSAndroid Build Coastguard Worker #endif
18*6a54128fSAndroid Build Coastguard Worker 
19*6a54128fSAndroid Build Coastguard Worker #include "config.h"
20*6a54128fSAndroid Build Coastguard Worker #if HAVE_SYS_TYPES_H
21*6a54128fSAndroid Build Coastguard Worker #include <sys/types.h>
22*6a54128fSAndroid Build Coastguard Worker #endif
23*6a54128fSAndroid Build Coastguard Worker 
24*6a54128fSAndroid Build Coastguard Worker #if HAVE_ERRNO_H
25*6a54128fSAndroid Build Coastguard Worker #include <errno.h>
26*6a54128fSAndroid Build Coastguard Worker #endif
27*6a54128fSAndroid Build Coastguard Worker #if HAVE_UNISTD_H
28*6a54128fSAndroid Build Coastguard Worker #include <unistd.h>
29*6a54128fSAndroid Build Coastguard Worker #endif
30*6a54128fSAndroid Build Coastguard Worker #ifdef __MSDOS__
31*6a54128fSAndroid Build Coastguard Worker #include <io.h>
32*6a54128fSAndroid Build Coastguard Worker #endif
33*6a54128fSAndroid Build Coastguard Worker #include "et/com_err.h"
34*6a54128fSAndroid Build Coastguard Worker #include "ext2fs/ext2_io.h"
35*6a54128fSAndroid Build Coastguard Worker 
36*6a54128fSAndroid Build Coastguard Worker #ifdef __linux__
37*6a54128fSAndroid Build Coastguard Worker 
38*6a54128fSAndroid Build Coastguard Worker #if defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE)
39*6a54128fSAndroid Build Coastguard Worker 
40*6a54128fSAndroid Build Coastguard Worker #define my_llseek lseek64
41*6a54128fSAndroid Build Coastguard Worker 
42*6a54128fSAndroid Build Coastguard Worker #else
43*6a54128fSAndroid Build Coastguard Worker #if defined(HAVE_LLSEEK)
44*6a54128fSAndroid Build Coastguard Worker #include <sys/syscall.h>
45*6a54128fSAndroid Build Coastguard Worker 
46*6a54128fSAndroid Build Coastguard Worker #ifndef HAVE_LLSEEK_PROTOTYPE
47*6a54128fSAndroid Build Coastguard Worker extern long long llseek (int fd, long long offset, int origin);
48*6a54128fSAndroid Build Coastguard Worker #endif
49*6a54128fSAndroid Build Coastguard Worker 
50*6a54128fSAndroid Build Coastguard Worker #define my_llseek llseek
51*6a54128fSAndroid Build Coastguard Worker 
52*6a54128fSAndroid Build Coastguard Worker #else	/* ! HAVE_LLSEEK */
53*6a54128fSAndroid Build Coastguard Worker 
54*6a54128fSAndroid Build Coastguard Worker #if SIZEOF_LONG == SIZEOF_LONG_LONG || _FILE_OFFSET_BITS+0 == 64
55*6a54128fSAndroid Build Coastguard Worker 
56*6a54128fSAndroid Build Coastguard Worker #define my_llseek lseek
57*6a54128fSAndroid Build Coastguard Worker 
58*6a54128fSAndroid Build Coastguard Worker #else /* SIZEOF_LONG != SIZEOF_LONG_LONG */
59*6a54128fSAndroid Build Coastguard Worker 
60*6a54128fSAndroid Build Coastguard Worker #include <linux/unistd.h>
61*6a54128fSAndroid Build Coastguard Worker 
62*6a54128fSAndroid Build Coastguard Worker #ifndef __NR__llseek
63*6a54128fSAndroid Build Coastguard Worker #define __NR__llseek            140
64*6a54128fSAndroid Build Coastguard Worker #endif
65*6a54128fSAndroid Build Coastguard Worker 
66*6a54128fSAndroid Build Coastguard Worker #ifndef __i386__
67*6a54128fSAndroid Build Coastguard Worker static int _llseek (unsigned int, unsigned long,
68*6a54128fSAndroid Build Coastguard Worker 		   unsigned long, ext2_loff_t *, unsigned int);
69*6a54128fSAndroid Build Coastguard Worker 
70*6a54128fSAndroid Build Coastguard Worker static _syscall5(int,_llseek,unsigned int,fd,unsigned long,offset_high,
71*6a54128fSAndroid Build Coastguard Worker 		 unsigned long, offset_low,ext2_loff_t *,result,
72*6a54128fSAndroid Build Coastguard Worker 		 unsigned int, origin);
73*6a54128fSAndroid Build Coastguard Worker #endif
74*6a54128fSAndroid Build Coastguard Worker 
my_llseek(int fd,ext2_loff_t offset,int origin)75*6a54128fSAndroid Build Coastguard Worker static ext2_loff_t my_llseek (int fd, ext2_loff_t offset, int origin)
76*6a54128fSAndroid Build Coastguard Worker {
77*6a54128fSAndroid Build Coastguard Worker 	ext2_loff_t result;
78*6a54128fSAndroid Build Coastguard Worker 	int retval;
79*6a54128fSAndroid Build Coastguard Worker 
80*6a54128fSAndroid Build Coastguard Worker #ifndef __i386__
81*6a54128fSAndroid Build Coastguard Worker 	retval = _llseek(fd, ((unsigned long long) offset) >> 32,
82*6a54128fSAndroid Build Coastguard Worker #else
83*6a54128fSAndroid Build Coastguard Worker 	retval = syscall(__NR__llseek, fd, (unsigned long long) (offset >> 32),
84*6a54128fSAndroid Build Coastguard Worker #endif
85*6a54128fSAndroid Build Coastguard Worker 			  ((unsigned long long) offset) & 0xffffffff,
86*6a54128fSAndroid Build Coastguard Worker 			&result, origin);
87*6a54128fSAndroid Build Coastguard Worker 	return (retval == -1 ? (ext2_loff_t) retval : result);
88*6a54128fSAndroid Build Coastguard Worker }
89*6a54128fSAndroid Build Coastguard Worker 
90*6a54128fSAndroid Build Coastguard Worker #endif	/* SIZE_LONG == SIZEOF_LONG_LONG */
91*6a54128fSAndroid Build Coastguard Worker 
92*6a54128fSAndroid Build Coastguard Worker #endif /* HAVE_LLSEEK */
93*6a54128fSAndroid Build Coastguard Worker #endif /* defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE) */
94*6a54128fSAndroid Build Coastguard Worker 
ext2fs_llseek(int fd,ext2_loff_t offset,int origin)95*6a54128fSAndroid Build Coastguard Worker ext2_loff_t ext2fs_llseek (int fd, ext2_loff_t offset, int origin)
96*6a54128fSAndroid Build Coastguard Worker {
97*6a54128fSAndroid Build Coastguard Worker #if SIZEOF_OFF_T >= SIZEOF_LONG_LONG
98*6a54128fSAndroid Build Coastguard Worker 	return my_llseek (fd, offset, origin);
99*6a54128fSAndroid Build Coastguard Worker #else
100*6a54128fSAndroid Build Coastguard Worker 	ext2_loff_t result;
101*6a54128fSAndroid Build Coastguard Worker 	static int do_compat = 0;
102*6a54128fSAndroid Build Coastguard Worker 
103*6a54128fSAndroid Build Coastguard Worker 	if (do_compat)
104*6a54128fSAndroid Build Coastguard Worker 		goto fallback;
105*6a54128fSAndroid Build Coastguard Worker 
106*6a54128fSAndroid Build Coastguard Worker 	result = my_llseek (fd, offset, origin);
107*6a54128fSAndroid Build Coastguard Worker 	if (result == -1 && errno == ENOSYS) {
108*6a54128fSAndroid Build Coastguard Worker 		/*
109*6a54128fSAndroid Build Coastguard Worker 		 * Just in case this code runs on top of an old kernel
110*6a54128fSAndroid Build Coastguard Worker 		 * which does not support the llseek system call
111*6a54128fSAndroid Build Coastguard Worker 		 */
112*6a54128fSAndroid Build Coastguard Worker 		do_compat++;
113*6a54128fSAndroid Build Coastguard Worker 	fallback:
114*6a54128fSAndroid Build Coastguard Worker 		if (offset < ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))
115*6a54128fSAndroid Build Coastguard Worker 			return lseek(fd, (off_t) offset, origin);
116*6a54128fSAndroid Build Coastguard Worker 		errno = EINVAL;
117*6a54128fSAndroid Build Coastguard Worker 		return -1;
118*6a54128fSAndroid Build Coastguard Worker 	}
119*6a54128fSAndroid Build Coastguard Worker 	return result;
120*6a54128fSAndroid Build Coastguard Worker #endif
121*6a54128fSAndroid Build Coastguard Worker }
122*6a54128fSAndroid Build Coastguard Worker 
123*6a54128fSAndroid Build Coastguard Worker #else /* !linux */
124*6a54128fSAndroid Build Coastguard Worker 
125*6a54128fSAndroid Build Coastguard Worker #ifndef EINVAL
126*6a54128fSAndroid Build Coastguard Worker #define EINVAL EXT2_ET_INVALID_ARGUMENT
127*6a54128fSAndroid Build Coastguard Worker #endif
128*6a54128fSAndroid Build Coastguard Worker 
ext2fs_llseek(int fd,ext2_loff_t offset,int origin)129*6a54128fSAndroid Build Coastguard Worker ext2_loff_t ext2fs_llseek (int fd, ext2_loff_t offset, int origin)
130*6a54128fSAndroid Build Coastguard Worker {
131*6a54128fSAndroid Build Coastguard Worker #if defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE)
132*6a54128fSAndroid Build Coastguard Worker 	return lseek64 (fd, offset, origin);
133*6a54128fSAndroid Build Coastguard Worker #else
134*6a54128fSAndroid Build Coastguard Worker 	if ((sizeof(off_t) < sizeof(ext2_loff_t)) &&
135*6a54128fSAndroid Build Coastguard Worker 	    (offset >= ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))) {
136*6a54128fSAndroid Build Coastguard Worker 		errno = EINVAL;
137*6a54128fSAndroid Build Coastguard Worker 		return -1;
138*6a54128fSAndroid Build Coastguard Worker 	}
139*6a54128fSAndroid Build Coastguard Worker 	return lseek (fd, (off_t) offset, origin);
140*6a54128fSAndroid Build Coastguard Worker #endif
141*6a54128fSAndroid Build Coastguard Worker }
142*6a54128fSAndroid Build Coastguard Worker 
143*6a54128fSAndroid Build Coastguard Worker #endif 	/* linux */
144*6a54128fSAndroid Build Coastguard Worker 
145*6a54128fSAndroid Build Coastguard Worker 
146