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