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 |
paul@181 | 8 | * GNU Lesser General Public License. |
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 | |
paul@181 | 34 | #include "blkidP.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 | #elif defined(HAVE_LLSEEK) |
paul@181 | 43 | #include <sys/syscall.h> |
paul@181 | 44 | |
paul@181 | 45 | #ifndef HAVE_LLSEEK_PROTOTYPE |
paul@181 | 46 | extern long long llseek(int fd, long long offset, int origin); |
paul@181 | 47 | #endif |
paul@181 | 48 | |
paul@181 | 49 | #define my_llseek llseek |
paul@181 | 50 | |
paul@181 | 51 | #else /* ! HAVE_LLSEEK */ |
paul@181 | 52 | |
paul@181 | 53 | #if SIZEOF_LONG == SIZEOF_LONG_LONG |
paul@181 | 54 | |
paul@181 | 55 | #define llseek lseek |
paul@181 | 56 | |
paul@181 | 57 | #else /* SIZEOF_LONG != SIZEOF_LONG_LONG */ |
paul@181 | 58 | |
paul@181 | 59 | #include <linux/unistd.h> |
paul@181 | 60 | |
paul@181 | 61 | #ifndef __NR__llseek |
paul@181 | 62 | #define __NR__llseek 140 |
paul@181 | 63 | #endif |
paul@181 | 64 | |
paul@181 | 65 | #ifndef __i386__ |
paul@181 | 66 | static int _llseek(unsigned int, unsigned long, unsigned long, |
paul@181 | 67 | blkid_loff_t *, unsigned int); |
paul@181 | 68 | |
paul@181 | 69 | static _syscall5(int, _llseek, unsigned int, fd, unsigned long, offset_high, |
paul@181 | 70 | unsigned long, offset_low, blkid_loff_t *, result, |
paul@181 | 71 | unsigned int, origin) |
paul@181 | 72 | #endif |
paul@181 | 73 | |
paul@181 | 74 | static blkid_loff_t my_llseek(int fd, blkid_loff_t offset, int origin) |
paul@181 | 75 | { |
paul@181 | 76 | blkid_loff_t result; |
paul@181 | 77 | int retval; |
paul@181 | 78 | |
paul@181 | 79 | #ifndef __i386__ |
paul@181 | 80 | retval = _llseek(fd, ((unsigned long long) offset) >> 32, |
paul@181 | 81 | ((unsigned long long)offset) & 0xffffffff, |
paul@181 | 82 | &result, origin); |
paul@181 | 83 | #else |
paul@181 | 84 | retval = syscall(__NR__llseek, fd, ((unsigned long long) offset) >> 32, |
paul@181 | 85 | ((unsigned long long)offset) & 0xffffffff, |
paul@181 | 86 | &result, origin); |
paul@181 | 87 | #endif |
paul@181 | 88 | return (retval == -1 ? (blkid_loff_t) retval : result); |
paul@181 | 89 | } |
paul@181 | 90 | |
paul@181 | 91 | #endif /* __alpha__ || __ia64__ */ |
paul@181 | 92 | |
paul@181 | 93 | #endif /* HAVE_LLSEEK */ |
paul@181 | 94 | |
paul@181 | 95 | blkid_loff_t blkid_llseek(int fd, blkid_loff_t offset, int whence) |
paul@181 | 96 | { |
paul@181 | 97 | blkid_loff_t result; |
paul@181 | 98 | static int do_compat = 0; |
paul@181 | 99 | |
paul@181 | 100 | if ((sizeof(off_t) >= sizeof(blkid_loff_t)) || |
paul@181 | 101 | (offset < ((blkid_loff_t) 1 << ((sizeof(off_t)*8) -1)))) |
paul@181 | 102 | return lseek(fd, (off_t) offset, whence); |
paul@181 | 103 | |
paul@181 | 104 | if (do_compat) { |
paul@181 | 105 | errno = EOVERFLOW; |
paul@181 | 106 | return -1; |
paul@181 | 107 | } |
paul@181 | 108 | |
paul@181 | 109 | result = my_llseek(fd, offset, whence); |
paul@181 | 110 | if (result == -1 && errno == ENOSYS) { |
paul@181 | 111 | /* |
paul@181 | 112 | * Just in case this code runs on top of an old kernel |
paul@181 | 113 | * which does not support the llseek system call |
paul@181 | 114 | */ |
paul@181 | 115 | do_compat++; |
paul@181 | 116 | errno = EOVERFLOW; |
paul@181 | 117 | } |
paul@181 | 118 | return result; |
paul@181 | 119 | } |
paul@181 | 120 | |
paul@181 | 121 | #else /* !linux */ |
paul@181 | 122 | |
paul@181 | 123 | #ifndef EOVERFLOW |
paul@181 | 124 | #ifdef EXT2_ET_INVALID_ARGUMENT |
paul@181 | 125 | #define EOVERFLOW EXT2_ET_INVALID_ARGUMENT |
paul@181 | 126 | #else |
paul@181 | 127 | #define EOVERFLOW 112 |
paul@181 | 128 | #endif |
paul@181 | 129 | #endif |
paul@181 | 130 | |
paul@181 | 131 | blkid_loff_t blkid_llseek(int fd, blkid_loff_t offset, int origin) |
paul@181 | 132 | { |
paul@181 | 133 | #if defined(HAVE_LSEEK64) && defined(HAVE_LSEEK64_PROTOTYPE) |
paul@181 | 134 | return lseek64 (fd, offset, origin); |
paul@181 | 135 | #else |
paul@181 | 136 | if ((sizeof(off_t) < sizeof(blkid_loff_t)) && |
paul@181 | 137 | (offset >= ((blkid_loff_t) 1 << ((sizeof(off_t)*8) - 1)))) { |
paul@181 | 138 | errno = EOVERFLOW; |
paul@181 | 139 | return -1; |
paul@181 | 140 | } |
paul@181 | 141 | return lseek(fd, (off_t) offset, origin); |
paul@181 | 142 | #endif |
paul@181 | 143 | } |
paul@181 | 144 | |
paul@181 | 145 | #endif /* linux */ |
paul@181 | 146 | |
paul@181 | 147 | |