]> git.ipfire.org Git - thirdparty/util-linux.git/blob - fdisk/llseek.c
Imported from util-linux-2.8 tarball.
[thirdparty/util-linux.git] / fdisk / 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 #include <sys/types.h>
9
10 #include <errno.h>
11 #include <unistd.h>
12
13 #if defined(__GNUC__) || defined(HAS_LONG_LONG)
14 typedef long long ext2_loff_t;
15 #else
16 typedef long ext2_loff_t;
17 #endif
18
19 extern ext2_loff_t ext2_llseek (unsigned int, ext2_loff_t, unsigned int);
20
21 #ifdef __linux__
22
23 #ifdef HAVE_LLSEEK
24 #include <syscall.h>
25
26 #else /* HAVE_LLSEEK */
27
28 #ifdef __alpha__
29
30 #define my_llseek lseek
31
32 #else
33 #include <linux/unistd.h> /* for __NR__llseek */
34
35 static int _llseek (unsigned int, unsigned long,
36 unsigned long, ext2_loff_t *, unsigned int);
37
38 static _syscall5(int,_llseek,unsigned int,fd,unsigned long,offset_high,
39 unsigned long, offset_low,ext2_loff_t *,result,
40 unsigned int, origin)
41
42 static ext2_loff_t my_llseek (unsigned int fd, ext2_loff_t offset,
43 unsigned int origin)
44 {
45 ext2_loff_t result;
46 int retval;
47
48 retval = _llseek (fd, ((unsigned long long) offset) >> 32,
49 ((unsigned long long) offset) & 0xffffffff,
50 &result, origin);
51 return (retval == -1 ? (ext2_loff_t) retval : result);
52 }
53
54 #endif /* __alpha__ */
55
56 #endif /* HAVE_LLSEEK */
57
58 ext2_loff_t ext2_llseek (unsigned int fd, ext2_loff_t offset,
59 unsigned int origin)
60 {
61 ext2_loff_t result;
62 static int do_compat = 0;
63
64 if (!do_compat) {
65 result = my_llseek (fd, offset, origin);
66 if (!(result == -1 && errno == ENOSYS))
67 return result;
68
69 /*
70 * Just in case this code runs on top of an old kernel
71 * which does not support the llseek system call
72 */
73 do_compat = 1;
74 /*
75 * Now try ordinary lseek.
76 */
77 }
78
79 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) ||
80 (offset < ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1))))
81 return lseek(fd, (off_t) offset, origin);
82
83 errno = EINVAL;
84 return -1;
85 }
86
87 #else /* !linux */
88
89 ext2_loff_t ext2_llseek (unsigned int fd, ext2_loff_t offset,
90 unsigned int origin)
91 {
92 if ((sizeof(off_t) < sizeof(ext2_loff_t)) &&
93 (offset >= ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))) {
94 errno = EINVAL;
95 return -1;
96 }
97 return lseek (fd, (off_t) offset, origin);
98 }
99
100 #endif /* linux */
101
102