blob: d75f42dbab3dad536edd1785887efc90aeeee98d [file] [log] [blame]
Theodore Ts'of3db3561997-04-26 13:34:30 +00001/*
2 * llseek.c -- stub calling the llseek system call
3 *
Theodore Ts'o1c27cac1997-08-14 17:20:42 +00004 * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00005 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
Theodore Ts'of3db3561997-04-26 13:34:30 +000010 */
11
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000012#if HAVE_SYS_TYPES_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000013#include <sys/types.h>
Theodore Ts'o1d2ff461997-10-19 23:00:21 +000014#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000015
Theodore Ts'oc555aeb1997-10-25 04:16:53 +000016#if HAVE_ERRNO_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000017#include <errno.h>
Theodore Ts'oc555aeb1997-10-25 04:16:53 +000018#endif
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000019#if HAVE_UNISTD_H
Theodore Ts'of3db3561997-04-26 13:34:30 +000020#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000021#endif
Theodore Ts'o3cb6c501997-08-11 20:29:22 +000022#ifdef __MSDOS__
23#include <io.h>
24#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000025#include "et/com_err.h"
Theodore Ts'od40259f1997-10-20 00:44:26 +000026#include "ext2fs/ext2_io.h"
Theodore Ts'of3db3561997-04-26 13:34:30 +000027
28#ifdef __linux__
29
Theodore Ts'o50e1e101997-04-26 13:58:21 +000030#ifdef HAVE_LLSEEK
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000031#if HAVE_UNISTD_H
Theodore Ts'o50e1e101997-04-26 13:58:21 +000032#include <unistd.h>
Theodore Ts'o4cbe8af1997-08-10 23:07:40 +000033#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000034#include <syscall.h>
35
Theodore Ts'ob0b9c4d1997-10-20 23:36:30 +000036#ifndef HAVE_LLSEEK_PROTOTYPE
37extern long long llseek (int fd, long long offset, int origin);
Theodore Ts'o1c27cac1997-08-14 17:20:42 +000038#endif
39
40#define my_llseek llseek
41
Theodore Ts'o50e1e101997-04-26 13:58:21 +000042#else /* HAVE_LLSEEK */
43
44#ifdef __alpha__
45
46#define llseek lseek
47
48#else /* !__alpha__ */
49
50#include <linux/unistd.h>
51
Theodore Ts'of3db3561997-04-26 13:34:30 +000052#ifndef __NR__llseek
53#define __NR__llseek 140
54#endif
55
Theodore Ts'o1c27cac1997-08-14 17:20:42 +000056#ifndef __i386__
Theodore Ts'of3db3561997-04-26 13:34:30 +000057static int _llseek (unsigned int, unsigned long,
58 unsigned long, ext2_loff_t *, unsigned int);
59
60static _syscall5(int,_llseek,unsigned int,fd,unsigned long,offset_high,
61 unsigned long, offset_low,ext2_loff_t *,result,
62 unsigned int, origin)
Theodore Ts'o1c27cac1997-08-14 17:20:42 +000063#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000064
Theodore Ts'o1c27cac1997-08-14 17:20:42 +000065static ext2_loff_t my_llseek (int fd, ext2_loff_t offset, int origin)
Theodore Ts'o50e1e101997-04-26 13:58:21 +000066{
67 ext2_loff_t result;
68 int retval;
69
Theodore Ts'o1c27cac1997-08-14 17:20:42 +000070#ifndef __i386__
71 retval = _llseek(fd, ((unsigned long long) offset) >> 32,
72#else
73 retval = syscall(__NR__llseek, fd, (unsigned long long) (offset >> 32),
74#endif
75 ((unsigned long long) offset) & 0xffffffff,
Theodore Ts'o50e1e101997-04-26 13:58:21 +000076 &result, origin);
77 return (retval == -1 ? (ext2_loff_t) retval : result);
78}
79
80#endif /* HAVE_LLSEEK */
81
82#endif /* __alpha__ */
83
Theodore Ts'o1c27cac1997-08-14 17:20:42 +000084ext2_loff_t ext2fs_llseek (int fd, ext2_loff_t offset, int origin)
Theodore Ts'of3db3561997-04-26 13:34:30 +000085{
Theodore Ts'of3db3561997-04-26 13:34:30 +000086 ext2_loff_t result;
Theodore Ts'of3db3561997-04-26 13:34:30 +000087 static int do_compat = 0;
88
Theodore Ts'o50e1e101997-04-26 13:58:21 +000089 if ((sizeof(off_t) >= sizeof(ext2_loff_t)) ||
90 (offset < ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1))))
91 return lseek(fd, (off_t) offset, origin);
92
Theodore Ts'of3db3561997-04-26 13:34:30 +000093 if (do_compat) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +000094 errno = EINVAL;
95 return -1;
Theodore Ts'of3db3561997-04-26 13:34:30 +000096 }
97
Theodore Ts'o1c27cac1997-08-14 17:20:42 +000098 result = my_llseek (fd, offset, origin);
Theodore Ts'o50e1e101997-04-26 13:58:21 +000099 if (result == -1 && errno == ENOSYS) {
Theodore Ts'of3db3561997-04-26 13:34:30 +0000100 /*
101 * Just in case this code runs on top of an old kernel
102 * which does not support the llseek system call
103 */
104 do_compat++;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000105 errno = EINVAL;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000106 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000107 return result;
108}
109
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000110#else /* !linux */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000111
Theodore Ts'o73f17cf1999-01-04 07:35:45 +0000112#ifndef EINVAL
113#define EINVAL EXT2_ET_INVALID_ARGUMENT
114#endif
115
Theodore Ts'od163b091997-10-03 17:42:28 +0000116ext2_loff_t ext2fs_llseek (int fd, ext2_loff_t offset, int origin)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000117{
118 if ((sizeof(off_t) < sizeof(ext2_loff_t)) &&
119 (offset >= ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))) {
Theodore Ts'o73f17cf1999-01-04 07:35:45 +0000120 errno = EINVAL;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000121 return -1;
122 }
123 return lseek (fd, (off_t) offset, origin);
124}
125
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000126#endif /* linux */
Theodore Ts'of3db3561997-04-26 13:34:30 +0000127
128