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