yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org> |
| 3 | * |
| 4 | * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. |
| 5 | */ |
| 6 | /* truncate64 syscall. Copes with 64 bit and 32 bit machines |
| 7 | * and on 32 bit machines this sends things into the kernel as |
| 8 | * two 32-bit arguments (high and low 32 bits of length) that |
| 9 | * are ordered based on endianess. It turns out endian.h has |
| 10 | * just the macro we need to order things, __LONG_LONG_PAIR. |
| 11 | */ |
| 12 | |
| 13 | #include <features.h> |
| 14 | #include <unistd.h> |
| 15 | #include <errno.h> |
| 16 | #include <endian.h> |
| 17 | #include <stdint.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/syscall.h> |
| 20 | |
| 21 | #if defined __UCLIBC_HAS_LFS__ |
| 22 | |
| 23 | #if defined __NR_truncate64 |
| 24 | |
| 25 | #if __WORDSIZE == 64 |
| 26 | |
| 27 | /* For a 64 bit machine, life is simple... */ |
| 28 | _syscall2(int, truncate64, const char *, path, __off64_t, length) |
| 29 | |
| 30 | #elif __WORDSIZE == 32 |
| 31 | |
| 32 | /* The exported truncate64 function. */ |
| 33 | int truncate64(const char * path, __off64_t length) |
| 34 | { |
| 35 | uint32_t low = length & 0xffffffff; |
| 36 | uint32_t high = length >> 32; |
| 37 | #if defined(__UCLIBC_TRUNCATE64_HAS_4_ARGS__) |
| 38 | return INLINE_SYSCALL(truncate64, 4, path, 0, |
| 39 | __LONG_LONG_PAIR(high, low)); |
| 40 | #else |
| 41 | return INLINE_SYSCALL(truncate64, 3, path, |
| 42 | __LONG_LONG_PAIR(high, low)); |
| 43 | #endif |
| 44 | } |
| 45 | |
| 46 | #else /* __WORDSIZE */ |
| 47 | #error Your machine is not 64 bit nor 32 bit, I am dazed and confused. |
| 48 | #endif /* __WORDSIZE */ |
| 49 | |
| 50 | #else /* __NR_truncate64 */ |
| 51 | |
| 52 | |
| 53 | int truncate64(const char * path, __off64_t length) |
| 54 | { |
| 55 | __off_t x = (__off_t) length; |
| 56 | |
| 57 | if (x == length) { |
| 58 | return truncate(path, x); |
| 59 | } |
| 60 | |
| 61 | __set_errno((x < 0) ? EINVAL : EFBIG); |
| 62 | |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | #endif /* __NR_truncate64 */ |
| 67 | |
| 68 | #endif /* __UCLIBC_HAS_LFS__ */ |