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