truncate64.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * truncate64 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_truncate64
  23. #if __WORDSIZE == 64
  24. /* For a 64 bit machine, life is simple... */
  25. _syscall2(int, truncate64, const char *, path, __off64_t, length);
  26. #elif __WORDSIZE == 32
  27. #ifndef INLINE_SYSCALL
  28. #define INLINE_SYSCALL(name, nr, args...) __syscall_truncate64 (args)
  29. #define __NR___syscall_truncate64 __NR_truncate64
  30. #if defined(__powerpc__) || defined(__mips__)
  31. static inline _syscall4(int, __syscall_truncate64, const char *, path,
  32. uint32_t, pad, unsigned long, high_length, unsigned long, low_length);
  33. #else
  34. static inline _syscall3(int, __syscall_truncate64, const char *, path,
  35. unsigned long, high_length, unsigned long, low_length);
  36. #endif
  37. #endif
  38. /* The exported truncate64 function. */
  39. int truncate64 (const char * path, __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(truncate64, 4, path, 0,
  45. __LONG_LONG_PAIR (high, low));
  46. #else
  47. return INLINE_SYSCALL(truncate64, 3, path,
  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_truncate64 */
  55. int truncate64 (const char * path, __off64_t length)
  56. {
  57. __off_t x = (__off_t) length;
  58. if (x == length) {
  59. return truncate(path, x);
  60. }
  61. __set_errno((x < 0) ? EINVAL : EFBIG);
  62. return -1;
  63. }
  64. #endif /* __NR_truncate64 */
  65. #endif /* __UCLIBC_HAS_LFS__ */