pread_write.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  20. #include <sysdep-cancel.h>
  21. #else
  22. #define SINGLE_THREAD_P 1
  23. #endif
  24. #ifdef __NR_pread64 /* Newer kernels renamed but it's the same. */
  25. # ifdef __NR_pread
  26. # error "__NR_pread and __NR_pread64 both defined???"
  27. # endif
  28. # define __NR_pread __NR_pread64
  29. #endif
  30. #ifdef __NR_pread
  31. extern __typeof(pread) __libc_pread;
  32. # define __NR___syscall_pread __NR_pread
  33. static __inline__ _syscall6(ssize_t, __syscall_pread, int, fd, void *, buf,
  34. size_t, count, int, dummy, off_t, offset_hi, off_t, offset_lo)
  35. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  36. {
  37. if (SINGLE_THREAD_P)
  38. return(__syscall_pread(fd,buf,count,0,__LONG_LONG_PAIR(offset >> 31,offset)));
  39. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  40. int oldtype = LIBC_CANCEL_ASYNC ();
  41. ssize_t result = __syscall_pread(fd,buf,count,0,__LONG_LONG_PAIR(offset >> 31,offset));
  42. LIBC_CANCEL_RESET (oldtype);
  43. return result;
  44. #endif
  45. }
  46. weak_alias(__libc_pread,pread)
  47. # ifdef __UCLIBC_HAS_LFS__
  48. extern __typeof(pread64) __libc_pread64;
  49. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  50. {
  51. uint32_t low = offset & 0xffffffff;
  52. uint32_t high = offset >> 32;
  53. if (SINGLE_THREAD_P)
  54. return __syscall_pread(fd, buf, count, 0, __LONG_LONG_PAIR (high, low));
  55. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  56. int oldtype = LIBC_CANCEL_ASYNC ();
  57. ssize_t result = __syscall_pread(fd, buf, count, 0, __LONG_LONG_PAIR (high, low));
  58. LIBC_CANCEL_RESET (oldtype);
  59. return result;
  60. #endif
  61. }
  62. weak_alias(__libc_pread64,pread64)
  63. # endif /* __UCLIBC_HAS_LFS__ */
  64. #endif /* __NR_pread */
  65. /**********************************************************************/
  66. #ifdef __NR_pwrite64 /* Newer kernels renamed but it's the same. */
  67. # ifdef __NR_pwrite
  68. # error "__NR_pwrite and __NR_pwrite64 both defined???"
  69. # endif
  70. # define __NR_pwrite __NR_pwrite64
  71. #endif
  72. #ifdef __NR_pwrite
  73. extern __typeof(pwrite) __libc_pwrite;
  74. # define __NR___syscall_pwrite __NR_pwrite
  75. static __inline__ _syscall6(ssize_t, __syscall_pwrite, int, fd, const void *, buf,
  76. size_t, count, int, dummy, off_t, offset_hi, off_t, offset_lo)
  77. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  78. {
  79. if (SINGLE_THREAD_P)
  80. return __syscall_pwrite(fd,buf,count,0,__LONG_LONG_PAIR(offset >> 31,offset));
  81. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  82. int oldtype = LIBC_CANCEL_ASYNC ();
  83. ssize_t result = __syscall_pwrite(fd,buf,count,0,__LONG_LONG_PAIR(offset >> 31,offset));
  84. LIBC_CANCEL_RESET (oldtype);
  85. return result;
  86. #endif
  87. }
  88. weak_alias(__libc_pwrite,pwrite)
  89. # ifdef __UCLIBC_HAS_LFS__
  90. extern __typeof(pwrite64) __libc_pwrite64;
  91. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  92. {
  93. uint32_t low = offset & 0xffffffff;
  94. uint32_t high = offset >> 32;
  95. if (SINGLE_THREAD_P)
  96. return __syscall_pwrite(fd, buf, count, 0, __LONG_LONG_PAIR (high, low));
  97. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  98. int oldtype = LIBC_CANCEL_ASYNC ();
  99. ssize_t result = __syscall_pwrite(fd, buf, count, 0, __LONG_LONG_PAIR (high, low));
  100. LIBC_CANCEL_RESET (oldtype);
  101. return result;
  102. #endif
  103. }
  104. weak_alias(__libc_pwrite64,pwrite64)
  105. # endif /* __UCLIBC_HAS_LFS__ */
  106. #endif /* __NR_pwrite */