truncate64.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. /* The exported truncate64 function. */
  26. int truncate64(const char * path, __off64_t length)
  27. {
  28. uint32_t low = length & 0xffffffff;
  29. uint32_t high = length >> 32;
  30. #if defined(__UCLIBC_TRUNCATE64_HAS_4_ARGS__)
  31. return INLINE_SYSCALL(truncate64, 4, path, 0,
  32. __LONG_LONG_PAIR(high, low));
  33. #else
  34. return INLINE_SYSCALL(truncate64, 3, path,
  35. __LONG_LONG_PAIR(high, low));
  36. #endif
  37. }
  38. #else /* __WORDSIZE */
  39. #error Your machine is not 64 bit nor 32 bit, I am dazed and confused.
  40. #endif /* __WORDSIZE */
  41. #else /* __NR_truncate64 */
  42. int truncate64(const char * path, __off64_t length)
  43. {
  44. __off_t x = (__off_t) length;
  45. if (x == length) {
  46. return truncate(path, x);
  47. }
  48. __set_errno((x < 0) ? EINVAL : EFBIG);
  49. return -1;
  50. }
  51. #endif /* __NR_truncate64 */
  52. #endif /* __UCLIBC_HAS_LFS__ */