truncate64.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 <_lfs_64.h>
  13. #include <sys/syscall.h>
  14. #include <unistd.h>
  15. #ifdef __NR_truncate64
  16. # include <bits/wordsize.h>
  17. # if __WORDSIZE == 64
  18. _syscall2(int, truncate64, const char *, path, __off64_t, length)
  19. # elif __WORDSIZE == 32
  20. # include <endian.h>
  21. # include <stdint.h>
  22. int truncate64(const char * path, __off64_t length)
  23. {
  24. uint32_t low = length & 0xffffffff;
  25. uint32_t high = length >> 32;
  26. # if defined(__UCLIBC_TRUNCATE64_HAS_4_ARGS__)
  27. return INLINE_SYSCALL(truncate64, 4, path, 0,
  28. __LONG_LONG_PAIR(high, low));
  29. # else
  30. return INLINE_SYSCALL(truncate64, 3, path,
  31. __LONG_LONG_PAIR(high, low));
  32. # endif
  33. }
  34. # else
  35. # error Your machine is not 64 bit nor 32 bit, I am dazed and confused.
  36. # endif
  37. #else
  38. # include <errno.h>
  39. int truncate64(const char * path, __off64_t length)
  40. {
  41. __off_t x = (__off_t) length;
  42. if (x == length) {
  43. return truncate(path, x);
  44. }
  45. __set_errno((x < 0) ? EINVAL : EFBIG);
  46. return -1;
  47. }
  48. #endif