Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * llseek.c -- stub calling the llseek system call |
| 3 | * |
| 4 | * Copyright (C) 1994 Remy Card. This file may be redistributed |
| 5 | * under the terms of the GNU Public License. |
| 6 | */ |
| 7 | |
| 8 | #include <sys/types.h> |
| 9 | |
| 10 | #include <errno.h> |
| 11 | #include <unistd.h> |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 12 | #include "et/com_err.h" |
| 13 | #include "ext2fs/io.h" |
| 14 | |
| 15 | #ifdef __linux__ |
| 16 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame^] | 17 | #ifdef HAVE_LLSEEK |
| 18 | #include <unistd.h> |
| 19 | #include <syscall.h> |
| 20 | |
| 21 | #else /* HAVE_LLSEEK */ |
| 22 | |
| 23 | #ifdef __alpha__ |
| 24 | |
| 25 | #define llseek lseek |
| 26 | |
| 27 | #else /* !__alpha__ */ |
| 28 | |
| 29 | #include <linux/unistd.h> |
| 30 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 31 | #ifndef __NR__llseek |
| 32 | #define __NR__llseek 140 |
| 33 | #endif |
| 34 | |
| 35 | static int _llseek (unsigned int, unsigned long, |
| 36 | unsigned long, ext2_loff_t *, unsigned int); |
| 37 | |
| 38 | static _syscall5(int,_llseek,unsigned int,fd,unsigned long,offset_high, |
| 39 | unsigned long, offset_low,ext2_loff_t *,result, |
| 40 | unsigned int, origin) |
| 41 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame^] | 42 | static ext2_loff_t llseek (unsigned int fd, ext2_loff_t offset, |
| 43 | unsigned int origin) |
| 44 | { |
| 45 | ext2_loff_t result; |
| 46 | int retval; |
| 47 | |
| 48 | retval = _llseek (fd, ((unsigned long long) offset) >> 32, |
| 49 | ((unsigned long long) offset) & 0xffffffff, |
| 50 | &result, origin); |
| 51 | return (retval == -1 ? (ext2_loff_t) retval : result); |
| 52 | } |
| 53 | |
| 54 | #endif /* HAVE_LLSEEK */ |
| 55 | |
| 56 | #endif /* __alpha__ */ |
| 57 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 58 | ext2_loff_t ext2_llseek (unsigned int fd, ext2_loff_t offset, |
| 59 | unsigned int origin) |
| 60 | { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 61 | ext2_loff_t result; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 62 | static int do_compat = 0; |
| 63 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame^] | 64 | if ((sizeof(off_t) >= sizeof(ext2_loff_t)) || |
| 65 | (offset < ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))) |
| 66 | return lseek(fd, (off_t) offset, origin); |
| 67 | |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 68 | if (do_compat) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame^] | 69 | errno = EINVAL; |
| 70 | return -1; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame^] | 73 | result = llseek (fd, offset, origin); |
| 74 | if (result == -1 && errno == ENOSYS) { |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 75 | /* |
| 76 | * Just in case this code runs on top of an old kernel |
| 77 | * which does not support the llseek system call |
| 78 | */ |
| 79 | do_compat++; |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame^] | 80 | errno = EINVAL; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 81 | } |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 82 | return result; |
| 83 | } |
| 84 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame^] | 85 | #else /* !linux */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 86 | |
| 87 | ext2_loff_t ext2_llseek (unsigned int fd, ext2_loff_t offset, |
| 88 | unsigned int origin) |
| 89 | { |
| 90 | if ((sizeof(off_t) < sizeof(ext2_loff_t)) && |
| 91 | (offset >= ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))) { |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame^] | 92 | errno = EINVAL; |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 93 | return -1; |
| 94 | } |
| 95 | return lseek (fd, (off_t) offset, origin); |
| 96 | } |
| 97 | |
Theodore Ts'o | 50e1e10 | 1997-04-26 13:58:21 +0000 | [diff] [blame^] | 98 | #endif /* linux */ |
Theodore Ts'o | f3db356 | 1997-04-26 13:34:30 +0000 | [diff] [blame] | 99 | |
| 100 | |