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