truncate64.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #include <features.h>
  13. #include <unistd.h>
  14. #include <errno.h>
  15. #include <endian.h>
  16. #include <stdint.h>
  17. #include <sys/types.h>
  18. #include <sys/syscall.h>
  19. #if defined __UCLIBC_HAS_LFS__
  20. #if defined __NR_truncate64
  21. #if __WORDSIZE == 64
  22. /* For a 64 bit machine, life is simple... */
  23. _syscall2(int, truncate64, const char *, path, __off64_t, length)
  24. #elif __WORDSIZE == 32
  25. #ifndef INLINE_SYSCALL
  26. #define INLINE_SYSCALL(name, nr, args...) __syscall_truncate64(args)
  27. #define __NR___syscall_truncate64 __NR_truncate64
  28. #if defined(__UCLIBC_TRUNCATE64_HAS_4_ARGS__)
  29. static __inline__ _syscall4(int, __syscall_truncate64, const char *, path,
  30. uint32_t, pad, unsigned long, high_length, unsigned long, low_length)
  31. #else
  32. static __inline__ _syscall3(int, __syscall_truncate64, const char *, path,
  33. unsigned long, high_length, unsigned long, low_length)
  34. #endif
  35. #endif
  36. /* The exported truncate64 function. */
  37. int truncate64(const char * path, __off64_t length)
  38. {
  39. uint32_t low = length & 0xffffffff;
  40. uint32_t high = length >> 32;
  41. #if defined(__UCLIBC_TRUNCATE64_HAS_4_ARGS__)
  42. return INLINE_SYSCALL(truncate64, 4, path, 0,
  43. __LONG_LONG_PAIR(high, low));
  44. #else
  45. return INLINE_SYSCALL(truncate64, 3, path,
  46. __LONG_LONG_PAIR(high, low));
  47. #endif
  48. }
  49. #else /* __WORDSIZE */
  50. #error Your machine is not 64 bit nor 32 bit, I am dazed and confused.
  51. #endif /* __WORDSIZE */
  52. #else /* __NR_truncate64 */
  53. /* libc_hidden_proto(truncate) */
  54. int truncate64(const char * path, __off64_t length)
  55. {
  56. __off_t x = (__off_t) length;
  57. if (x == length) {
  58. return truncate(path, x);
  59. }
  60. __set_errno((x < 0) ? EINVAL : EFBIG);
  61. return -1;
  62. }
  63. #endif /* __NR_truncate64 */
  64. #endif /* __UCLIBC_HAS_LFS__ */