pread_write.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. /*
  8. * Based in part on the files
  9. * ./sysdeps/unix/sysv/linux/pwrite.c,
  10. * ./sysdeps/unix/sysv/linux/pread.c,
  11. * sysdeps/posix/pread.c
  12. * sysdeps/posix/pwrite.c
  13. * from GNU libc 2.2.5, but reworked considerably...
  14. */
  15. #include <sys/syscall.h>
  16. #include <unistd.h>
  17. #include <stdint.h>
  18. #include <endian.h>
  19. extern __typeof(pread) __libc_pread;
  20. extern __typeof(pwrite) __libc_pwrite;
  21. #ifdef __UCLIBC_HAS_LFS__
  22. extern __typeof(pread64) __libc_pread64;
  23. extern __typeof(pwrite64) __libc_pwrite64;
  24. #endif
  25. #ifdef __NR_pread64 /* Newer kernels renamed but it's the same. */
  26. # undef __NR_pread
  27. # define __NR_pread __NR_pread64
  28. #endif
  29. #include <bits/kernel_types.h>
  30. #ifdef __NR_pread
  31. # define __NR___syscall_pread __NR_pread
  32. static __inline__ _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
  33. size_t, count, off_t, offset_hi, off_t, offset_lo)
  34. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  35. {
  36. return __syscall_pread(fd, buf, count, __LONG_LONG_PAIR(offset >> 31, offset));
  37. }
  38. weak_alias(__libc_pread,pread)
  39. # ifdef __UCLIBC_HAS_LFS__
  40. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  41. {
  42. uint32_t low = offset & 0xffffffff;
  43. uint32_t high = offset >> 32;
  44. return __syscall_pread(fd, buf, count, __LONG_LONG_PAIR(high, low));
  45. }
  46. weak_alias(__libc_pread64,pread64)
  47. # endif /* __UCLIBC_HAS_LFS__ */
  48. #endif /* __NR_pread */
  49. #ifdef __NR_pwrite64 /* Newer kernels renamed but it's the same. */
  50. # undef __NR_pwrite
  51. # define __NR_pwrite __NR_pwrite64
  52. #endif
  53. #ifdef __NR_pwrite
  54. # define __NR___syscall_pwrite __NR_pwrite
  55. static __inline__ _syscall5(ssize_t, __syscall_pwrite, int, fd, const void *, buf,
  56. size_t, count, off_t, offset_hi, off_t, offset_lo)
  57. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  58. {
  59. return __syscall_pwrite(fd, buf, count, __LONG_LONG_PAIR(offset >> 31, offset));
  60. }
  61. weak_alias(__libc_pwrite,pwrite)
  62. # ifdef __UCLIBC_HAS_LFS__
  63. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  64. {
  65. uint32_t low = offset & 0xffffffff;
  66. uint32_t high = offset >> 32;
  67. return __syscall_pwrite(fd, buf, count, __LONG_LONG_PAIR(high, low));
  68. }
  69. weak_alias(__libc_pwrite64,pwrite64)
  70. # endif /* __UCLIBC_HAS_LFS__ */
  71. #endif /* __NR_pwrite */