/src/e2fsprogs/lib/ext2fs/llseek.c
Line | Count | Source |
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 | | #ifndef _LARGEFILE_SOURCE |
13 | | #define _LARGEFILE_SOURCE |
14 | | #endif |
15 | | #ifndef _LARGEFILE64_SOURCE |
16 | | #define _LARGEFILE64_SOURCE |
17 | | #endif |
18 | | |
19 | | #include "config.h" |
20 | | #if HAVE_SYS_TYPES_H |
21 | | #include <sys/types.h> |
22 | | #endif |
23 | | |
24 | | #if HAVE_ERRNO_H |
25 | | #include <errno.h> |
26 | | #endif |
27 | | #if HAVE_UNISTD_H |
28 | | #include <unistd.h> |
29 | | #endif |
30 | | #ifdef __MSDOS__ |
31 | | #include <io.h> |
32 | | #endif |
33 | | #include "et/com_err.h" |
34 | | #include "ext2fs/ext2_io.h" |
35 | | |
36 | | #ifdef __linux__ |
37 | | |
38 | | #include <linux/unistd.h> |
39 | | |
40 | | static ext2_loff_t my_llseek (int fd, ext2_loff_t offset, int origin) |
41 | 180 | { |
42 | 180 | #if SIZEOF_OFF_T >= 8 |
43 | 180 | return lseek(fd, offset, origin); |
44 | | #elif HAVE_LSEEK64_PROTOTYPE |
45 | | return lseek64(fd, offset, origin); |
46 | | #elif HAVE_LLSEEK_PROTOTYPE |
47 | | return llseek(fd, offset, origin); |
48 | | #elif defined(__NR__llseek) |
49 | | loff_t result; |
50 | | int retval; |
51 | | retval = syscall(__NR__llseek, fd, |
52 | | (unsigned long)(offset >> 32), |
53 | | (unsigned long)(offset & 0xffffffff), |
54 | | &result, origin); |
55 | | return (retval == -1 ? retval : result); |
56 | | #else |
57 | | errno = ENOSYS; |
58 | | return -1; |
59 | | #endif |
60 | 180 | } |
61 | | |
62 | | ext2_loff_t ext2fs_llseek (int fd, ext2_loff_t offset, int origin) |
63 | 180 | { |
64 | 180 | ext2_loff_t result; |
65 | 180 | static int do_compat = 0; |
66 | | |
67 | 180 | if (do_compat) |
68 | 0 | goto fallback; |
69 | | |
70 | 180 | result = my_llseek (fd, offset, origin); |
71 | 180 | if (result == -1 && errno == ENOSYS) { |
72 | | /* |
73 | | * Just in case this code runs on top of an old kernel |
74 | | * which does not support the llseek system call |
75 | | */ |
76 | 0 | do_compat++; |
77 | 0 | fallback: |
78 | 0 | if (offset < ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1))) |
79 | 0 | return lseek(fd, (off_t) offset, origin); |
80 | 0 | errno = EINVAL; |
81 | 0 | return -1; |
82 | 0 | } |
83 | 180 | return result; |
84 | 180 | } |
85 | | |
86 | | #else /* !linux */ |
87 | | |
88 | | #ifndef EINVAL |
89 | | #define EINVAL EXT2_ET_INVALID_ARGUMENT |
90 | | #endif |
91 | | |
92 | | ext2_loff_t ext2fs_llseek (int fd, ext2_loff_t offset, int origin) |
93 | | { |
94 | | #if defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE) |
95 | | return lseek64 (fd, offset, origin); |
96 | | #else |
97 | | if ((sizeof(off_t) < sizeof(ext2_loff_t)) && |
98 | | (offset >= ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))) { |
99 | | errno = EINVAL; |
100 | | return -1; |
101 | | } |
102 | | return lseek (fd, (off_t) offset, origin); |
103 | | #endif |
104 | | } |
105 | | |
106 | | #endif /* linux */ |
107 | | |
108 | | |