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