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