]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/llseek.c
Imported from util-linux-2.7.1 tarball.
[thirdparty/util-linux.git] / disk-utils / llseek.c
1 /*
2 * llseek.c -- stub calling the llseek system call
3 *
4 * Copyright (C) 1994 Remy Card. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
8 #define FOR_UTIL_LINUX
9
10 #include <sys/types.h>
11
12 #include <errno.h>
13 #include <unistd.h>
14
15 #ifndef FOR_UTIL_LINUX
16
17 #include "et/com_err.h"
18 #include "ext2fs/io.h"
19
20 #else /* FOR_UTIL_LINUX */
21
22 #if defined(__GNUC__) || defined(HAS_LONG_LONG)
23 typedef long long ext2_loff_t;
24 #else
25 typedef long ext2_loff_t;
26 #endif
27
28 ext2_loff_t ext2_llseek (unsigned int, ext2_loff_t, unsigned int);
29
30 #endif /* FOR_UTIL_LINUX */
31
32 #ifdef __linux__
33
34 #ifdef HAVE_LLSEEK
35 #include <syscall.h>
36
37 #else /* HAVE_LLSEEK */
38
39 #ifdef __alpha__
40
41 #define my_llseek lseek
42
43 #elif __i386__
44
45 #include <linux/unistd.h>
46
47 #ifndef __NR__llseek
48 #define __NR__llseek 140
49 #endif
50
51 static int _llseek (unsigned int, unsigned long,
52 unsigned long, ext2_loff_t *, unsigned int);
53
54 static _syscall5(int,_llseek,unsigned int,fd,unsigned long,offset_high,
55 unsigned long, offset_low,ext2_loff_t *,result,
56 unsigned int, origin)
57
58 static ext2_loff_t my_llseek (unsigned int fd, ext2_loff_t offset,
59 unsigned int origin)
60 {
61 ext2_loff_t result;
62 int retval;
63
64 retval = _llseek (fd, ((unsigned long long) offset) >> 32,
65 ((unsigned long long) offset) & 0xffffffff,
66 &result, origin);
67 return (retval == -1 ? (ext2_loff_t) retval : result);
68 }
69
70 #else
71
72 #error "llseek() is not available"
73
74 #endif /* __alpha__ */
75
76 #endif /* HAVE_LLSEEK */
77
78 ext2_loff_t ext2_llseek (unsigned int fd, ext2_loff_t offset,
79 unsigned int origin)
80 {
81 ext2_loff_t result;
82 static int do_compat = 0;
83
84 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) ||
85 (offset < ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1))))
86 return lseek(fd, (off_t) offset, origin);
87
88 if (do_compat) {
89 errno = EINVAL;
90 return -1;
91 }
92
93 result = my_llseek (fd, offset, origin);
94 if (result == -1 && errno == ENOSYS) {
95 /*
96 * Just in case this code runs on top of an old kernel
97 * which does not support the llseek system call
98 */
99 do_compat++;
100 errno = EINVAL;
101 }
102 return result;
103 }
104
105 #else /* !linux */
106
107 ext2_loff_t ext2_llseek (unsigned int fd, ext2_loff_t offset,
108 unsigned int origin)
109 {
110 if ((sizeof(off_t) < sizeof(ext2_loff_t)) &&
111 (offset >= ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))) {
112 errno = EINVAL;
113 return -1;
114 }
115 return lseek (fd, (off_t) offset, origin);
116 }
117
118 #endif /* linux */
119
120