Whamcloud - gitweb
40275e73f386fff6bdd213aaa1204e234d4a97d3
[tools/e2fsprogs.git] / lib / ext2fs / llseek.c
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 Public
8  * License.
9  * %End-Header%
10  */
11
12 #if HAVE_SYS_TYPES_H
13 #include <sys/types.h>
14 #endif
15
16 #include <errno.h>
17 #include <stdlib.h>
18 #if HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif
21 #ifdef __MSDOS__
22 #include <io.h>
23 #endif
24 #include "et/com_err.h"
25 #include "ext2fs/ext2_io.h"
26
27 #ifdef __linux__
28
29 #ifdef HAVE_LLSEEK
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif
33 #include <syscall.h>
34
35 #ifndef HAVE_LLSEEK_PROTOTYPE
36 extern long long llseek (int fd, long long offset, int origin);
37 #endif
38
39 #define my_llseek llseek
40
41 #else   /* HAVE_LLSEEK */
42
43 #ifdef __alpha__
44
45 #define llseek lseek
46
47 #else /* !__alpha__ */
48
49 #include <linux/unistd.h>
50
51 #ifndef __NR__llseek
52 #define __NR__llseek            140
53 #endif
54
55 #ifndef __i386__
56 static int _llseek (unsigned int, unsigned long,
57                    unsigned long, ext2_loff_t *, unsigned int);
58
59 static _syscall5(int,_llseek,unsigned int,fd,unsigned long,offset_high,
60                  unsigned long, offset_low,ext2_loff_t *,result,
61                  unsigned int, origin)
62 #endif
63
64 static ext2_loff_t my_llseek (int fd, ext2_loff_t offset, int origin)
65 {
66         ext2_loff_t result;
67         int retval;
68
69 #ifndef __i386__
70         retval = _llseek(fd, ((unsigned long long) offset) >> 32,
71 #else                     
72         retval = syscall(__NR__llseek, fd, (unsigned long long) (offset >> 32),
73 #endif
74                           ((unsigned long long) offset) & 0xffffffff,
75                         &result, origin);
76         return (retval == -1 ? (ext2_loff_t) retval : result);
77 }
78
79 #endif  /* HAVE_LLSEEK */
80
81 #endif /* __alpha__ */
82
83 ext2_loff_t ext2fs_llseek (int fd, ext2_loff_t offset, int origin)
84 {
85         ext2_loff_t result;
86         static int do_compat = 0;
87
88         if ((sizeof(off_t) >= sizeof(ext2_loff_t)) ||
89             (offset < ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1))))
90                 return lseek(fd, (off_t) offset, origin);
91
92         if (do_compat) {
93                 errno = EINVAL;
94                 return -1;
95         }
96         
97         result = my_llseek (fd, offset, origin);
98         if (result == -1 && errno == ENOSYS) {
99                 /*
100                  * Just in case this code runs on top of an old kernel
101                  * which does not support the llseek system call
102                  */
103                 do_compat++;
104                 errno = EINVAL;
105         }
106         return result;
107 }
108
109 #else /* !linux */
110
111 ext2_loff_t ext2fs_llseek (int fd, ext2_loff_t offset, int origin)
112 {
113         if ((sizeof(off_t) < sizeof(ext2_loff_t)) &&
114             (offset >= ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))) {
115                 errno = EINVAL;
116                 return -1;
117         }
118         return lseek (fd, (off_t) offset, origin);
119 }
120
121 #endif  /* linux */
122
123