paul@181 | 1 | /* |
paul@181 | 2 | * llseek.c -- stub calling the llseek system call |
paul@181 | 3 | * |
paul@181 | 4 | * Copyright (C) 1994, 1995, 1996, 1997 Theodore Ts'o. |
paul@181 | 5 | * |
paul@181 | 6 | * %Begin-Header% |
paul@181 | 7 | * This file may be redistributed under the terms of the GNU Library |
paul@181 | 8 | * General Public License, version 2. |
paul@181 | 9 | * %End-Header% |
paul@181 | 10 | */ |
paul@181 | 11 | |
paul@181 | 12 | #ifndef _LARGEFILE_SOURCE |
paul@181 | 13 | #define _LARGEFILE_SOURCE |
paul@181 | 14 | #endif |
paul@181 | 15 | #ifndef _LARGEFILE64_SOURCE |
paul@181 | 16 | #define _LARGEFILE64_SOURCE |
paul@181 | 17 | #endif |
paul@181 | 18 | |
paul@181 | 19 | #include "config.h" |
paul@181 | 20 | #if HAVE_SYS_TYPES_H |
paul@181 | 21 | #include <sys/types.h> |
paul@181 | 22 | #endif |
paul@181 | 23 | |
paul@181 | 24 | #if HAVE_ERRNO_H |
paul@181 | 25 | #include <errno.h> |
paul@181 | 26 | #endif |
paul@181 | 27 | #if HAVE_UNISTD_H |
paul@181 | 28 | #include <unistd.h> |
paul@181 | 29 | #endif |
paul@181 | 30 | #ifdef __MSDOS__ |
paul@181 | 31 | #include <io.h> |
paul@181 | 32 | #endif |
paul@181 | 33 | #include "et/com_err.h" |
paul@181 | 34 | #include "ext2fs/ext2_io.h" |
paul@181 | 35 | |
paul@181 | 36 | #ifdef __linux__ |
paul@181 | 37 | |
paul@181 | 38 | #if defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE) |
paul@181 | 39 | |
paul@181 | 40 | #define my_llseek lseek64 |
paul@181 | 41 | |
paul@181 | 42 | #else |
paul@181 | 43 | #if defined(HAVE_LLSEEK) |
paul@181 | 44 | #include <sys/syscall.h> |
paul@181 | 45 | |
paul@181 | 46 | #ifndef HAVE_LLSEEK_PROTOTYPE |
paul@181 | 47 | extern long long llseek (int fd, long long offset, int origin); |
paul@181 | 48 | #endif |
paul@181 | 49 | |
paul@181 | 50 | #define my_llseek llseek |
paul@181 | 51 | |
paul@181 | 52 | #else /* ! HAVE_LLSEEK */ |
paul@181 | 53 | |
paul@181 | 54 | #if SIZEOF_LONG == SIZEOF_LONG_LONG |
paul@181 | 55 | |
paul@181 | 56 | #define my_llseek lseek |
paul@181 | 57 | |
paul@181 | 58 | #else /* SIZEOF_LONG != SIZEOF_LONG_LONG */ |
paul@181 | 59 | |
paul@181 | 60 | #include <linux/unistd.h> |
paul@181 | 61 | |
paul@181 | 62 | #ifndef __NR__llseek |
paul@181 | 63 | #define __NR__llseek 140 |
paul@181 | 64 | #endif |
paul@181 | 65 | |
paul@181 | 66 | #ifndef __i386__ |
paul@181 | 67 | static int _llseek (unsigned int, unsigned long, |
paul@181 | 68 | unsigned long, ext2_loff_t *, unsigned int); |
paul@181 | 69 | |
paul@181 | 70 | static _syscall5(int,_llseek,unsigned int,fd,unsigned long,offset_high, |
paul@181 | 71 | unsigned long, offset_low,ext2_loff_t *,result, |
paul@181 | 72 | unsigned int, origin) |
paul@181 | 73 | #endif |
paul@181 | 74 | |
paul@181 | 75 | static ext2_loff_t my_llseek (int fd, ext2_loff_t offset, int origin) |
paul@181 | 76 | { |
paul@181 | 77 | ext2_loff_t result; |
paul@181 | 78 | int retval; |
paul@181 | 79 | |
paul@181 | 80 | #ifndef __i386__ |
paul@181 | 81 | retval = _llseek(fd, ((unsigned long long) offset) >> 32, |
paul@181 | 82 | #else |
paul@181 | 83 | retval = syscall(__NR__llseek, fd, (unsigned long long) (offset >> 32), |
paul@181 | 84 | #endif |
paul@181 | 85 | ((unsigned long long) offset) & 0xffffffff, |
paul@181 | 86 | &result, origin); |
paul@181 | 87 | return (retval == -1 ? (ext2_loff_t) retval : result); |
paul@181 | 88 | } |
paul@181 | 89 | |
paul@181 | 90 | #endif /* SIZE_LONG == SIZEOF_LONG_LONG */ |
paul@181 | 91 | |
paul@181 | 92 | #endif /* HAVE_LLSEEK */ |
paul@181 | 93 | #endif /* defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE) */ |
paul@181 | 94 | |
paul@181 | 95 | ext2_loff_t ext2fs_llseek (int fd, ext2_loff_t offset, int origin) |
paul@181 | 96 | { |
paul@181 | 97 | #if SIZEOF_OFF_T >= SIZEOF_LONG_LONG |
paul@181 | 98 | return my_llseek (fd, offset, origin); |
paul@181 | 99 | #else |
paul@181 | 100 | ext2_loff_t result; |
paul@181 | 101 | static int do_compat = 0; |
paul@181 | 102 | |
paul@181 | 103 | if (do_compat) |
paul@181 | 104 | goto fallback; |
paul@181 | 105 | |
paul@181 | 106 | result = my_llseek (fd, offset, origin); |
paul@181 | 107 | if (result == -1 && errno == ENOSYS) { |
paul@181 | 108 | /* |
paul@181 | 109 | * Just in case this code runs on top of an old kernel |
paul@181 | 110 | * which does not support the llseek system call |
paul@181 | 111 | */ |
paul@181 | 112 | do_compat++; |
paul@181 | 113 | fallback: |
paul@181 | 114 | if (offset < ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1))) |
paul@181 | 115 | return lseek(fd, (off_t) offset, origin); |
paul@181 | 116 | errno = EINVAL; |
paul@181 | 117 | return -1; |
paul@181 | 118 | } |
paul@181 | 119 | return result; |
paul@181 | 120 | #endif |
paul@181 | 121 | } |
paul@181 | 122 | |
paul@181 | 123 | #else /* !linux */ |
paul@181 | 124 | |
paul@181 | 125 | #ifndef EINVAL |
paul@181 | 126 | #define EINVAL EXT2_ET_INVALID_ARGUMENT |
paul@181 | 127 | #endif |
paul@181 | 128 | |
paul@181 | 129 | ext2_loff_t ext2fs_llseek (int fd, ext2_loff_t offset, int origin) |
paul@181 | 130 | { |
paul@181 | 131 | #if defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE) |
paul@181 | 132 | return lseek64 (fd, offset, origin); |
paul@181 | 133 | #else |
paul@181 | 134 | if ((sizeof(off_t) < sizeof(ext2_loff_t)) && |
paul@181 | 135 | (offset >= ((ext2_loff_t) 1 << ((sizeof(off_t)*8) -1)))) { |
paul@181 | 136 | errno = EINVAL; |
paul@181 | 137 | return -1; |
paul@181 | 138 | } |
paul@181 | 139 | return lseek (fd, (off_t) offset, origin); |
paul@181 | 140 | #endif |
paul@181 | 141 | } |
paul@181 | 142 | |
paul@181 | 143 | #endif /* linux */ |
paul@181 | 144 | |
paul@181 | 145 | |