ftruncate64.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * ftruncate64 syscall. Copes with 64 bit and 32 bit machines
  3. * and on 32 bit machines this sends things into the kernel as
  4. * two 32-bit arguments (high and low 32 bits of length) that
  5. * are ordered based on endianess. It turns out endian.h has
  6. * just the macro we need to order things, __LONG_LONG_PAIR.
  7. *
  8. * Copyright (C) 2002 Erik Andersen <andersen@codepoet.org>
  9. *
  10. * This file is subject to the terms and conditions of the GNU
  11. * Lesser General Public License. See the file COPYING.LIB in
  12. * the main directory of this archive for more details.
  13. */
  14. #include <features.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <endian.h>
  18. #include <stdint.h>
  19. #include <sys/types.h>
  20. #include <sys/syscall.h>
  21. #if defined __UCLIBC_HAS_LFS__
  22. #if defined __NR_ftruncate64
  23. #if __WORDSIZE == 64
  24. /* For a 64 bit machine, life is simple... */
  25. _syscall2(int, ftruncate64, int, fd, __off64_t, length);
  26. #elif __WORDSIZE == 32
  27. #ifndef INLINE_SYSCALL
  28. #define INLINE_SYSCALL(name, nr, args...) __syscall_ftruncate64 (args)
  29. #define __NR___syscall_ftruncate64 __NR_ftruncate64
  30. #if defined(__powerpc__) || defined(__mips__)
  31. static inline _syscall4(int, __syscall_ftruncate64, int, fd, uint32_t, pad,
  32. unsigned long, high_length, unsigned long, low_length);
  33. #else
  34. static inline _syscall3(int, __syscall_ftruncate64, int, fd,
  35. unsigned long, high_length, unsigned long, low_length);
  36. #endif
  37. #endif
  38. /* The exported ftruncate64 function. */
  39. int ftruncate64 (int fd, __off64_t length)
  40. {
  41. uint32_t low = length & 0xffffffff;
  42. uint32_t high = length >> 32;
  43. #if defined(__powerpc__) || defined(__mips__)
  44. return INLINE_SYSCALL(ftruncate64,
  45. 4, fd, 0, __LONG_LONG_PAIR (high, low));
  46. #else
  47. return INLINE_SYSCALL(ftruncate64, 3, fd,
  48. __LONG_LONG_PAIR (high, low));
  49. #endif
  50. }
  51. #else /* __WORDSIZE */
  52. #error Your machine is not 64 bit or 32 bit, I am dazed and confused.
  53. #endif /* __WORDSIZE */
  54. #else /* __NR_ftruncate64 */
  55. int ftruncate64 (int fd, __off64_t length)
  56. {
  57. __off_t x = (__off_t) length;
  58. if (x == length) {
  59. return ftruncate(fd, x);
  60. }
  61. __set_errno((x < 0) ? EINVAL : EFBIG);
  62. return -1;
  63. }
  64. #endif /* __NR_ftruncate64 */
  65. #endif /* __UCLIBC_HAS_LFS__ */