ftruncate64.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. * ftruncate64 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. #include <features.h>
  13. #ifdef __UCLIBC_HAS_LFS__
  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. libc_hidden_proto(ftruncate64)
  21. # ifdef __NR_ftruncate64
  22. # if __WORDSIZE == 64
  23. /* For a 64 bit machine, life is simple... */
  24. _syscall2(int, ftruncate64, int, fd, __off64_t, length);
  25. # elif __WORDSIZE == 32
  26. # ifndef INLINE_SYSCALL
  27. # define INLINE_SYSCALL(name, nr, args...) __syscall_ftruncate64 (args)
  28. # define __NR___syscall_ftruncate64 __NR_ftruncate64
  29. # if defined(__powerpc__) || defined(__mips__)
  30. static inline _syscall4(int, __syscall_ftruncate64, int, fd, uint32_t, pad,
  31. unsigned long, high_length, unsigned long, low_length);
  32. # else
  33. static inline _syscall3(int, __syscall_ftruncate64, int, fd,
  34. unsigned long, high_length, unsigned long, low_length);
  35. # endif
  36. # endif
  37. /* The exported ftruncate64 function. */
  38. int ftruncate64 (int fd, __off64_t length)
  39. {
  40. uint32_t low = length & 0xffffffff;
  41. uint32_t high = length >> 32;
  42. # if defined(__powerpc__) || defined(__mips__)
  43. return INLINE_SYSCALL(ftruncate64,
  44. 4, fd, 0, __LONG_LONG_PAIR (high, low));
  45. # else
  46. return INLINE_SYSCALL(ftruncate64, 3, fd,
  47. __LONG_LONG_PAIR (high, low));
  48. # endif
  49. }
  50. # else /* __WORDSIZE */
  51. # error Your machine is not 64 bit or 32 bit, I am dazed and confused.
  52. # endif /* __WORDSIZE */
  53. # else /* __NR_ftruncate64 */
  54. libc_hidden_proto(ftruncate)
  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. libc_hidden_def(ftruncate64)
  66. #endif /* __UCLIBC_HAS_LFS__ */