lseek.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Linux lseek implementation, 32 bits off_t.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library. If not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <unistd.h>
  16. #include <stdint.h>
  17. #include <sys/types.h>
  18. #include <sysdep.h>
  19. #include <errno.h>
  20. #ifndef __OFF_T_MATCHES_OFF64_T
  21. /* Test for overflows of structures where we ask the kernel to fill them
  22. in with standard 64-bit syscalls but return them through APIs that
  23. only expose the low 32 bits of some fields. */
  24. static inline off_t lseek_overflow (loff_t res)
  25. {
  26. off_t retval = (off_t) res;
  27. if (retval == res)
  28. return retval;
  29. __set_errno (EOVERFLOW);
  30. return (off_t) -1;
  31. }
  32. off_t
  33. __lseek (int fd, off_t offset, int whence)
  34. {
  35. # ifdef __NR__llseek
  36. loff_t res;
  37. int rc = INLINE_SYSCALL_CALL (_llseek, fd,
  38. (long) (((uint64_t) (offset)) >> 32),
  39. (long) offset, &res, whence);
  40. return rc ?: lseek_overflow (res);
  41. # else
  42. return INLINE_SYSCALL_CALL (lseek, fd, offset, whence);
  43. # endif
  44. }
  45. libc_hidden_def (__lseek)
  46. weak_alias (__lseek, lseek)
  47. strong_alias (__lseek, __libc_lseek)
  48. #endif /* __OFF_T_MATCHES_OFF64_T */