pread_write.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <endian.h>
  18. #include <bits/wordsize.h>
  19. #include <cancel.h>
  20. #ifdef __NR_pread64
  21. # undef __NR_pread
  22. # define __NR_pread __NR_pread64
  23. #endif
  24. #ifdef __NR_pwrite64
  25. # undef __NR_pwrite
  26. # define __NR_pwrite __NR_pwrite64
  27. #endif
  28. #ifndef MY_PREAD
  29. # ifdef __NR_pread
  30. # define __NR___syscall_pread __NR_pread
  31. # if defined(__UCLIBC_SYSCALL_ALIGN_64BIT__)
  32. static _syscall6(ssize_t, __syscall_pread, int, fd, void *, buf,
  33. size_t, count, int, dummy, off_t, offset_hi, off_t, offset_lo)
  34. # define MY_PREAD(fd, buf, count, offset) __syscall_pread(fd, buf, count, 0, OFF_HI_LO(offset))
  35. # define MY_PREAD64(fd, buf, count, offset) __syscall_pread(fd, buf, count, 0, OFF64_HI_LO(offset))
  36. # elif __WORDSIZE == 32
  37. static _syscall5(ssize_t, __syscall_pread, int, fd, void *, buf,
  38. size_t, count, off_t, offset_hi, off_t, offset_lo)
  39. # define MY_PREAD(fd, buf, count, offset) __syscall_pread(fd, buf, count, OFF_HI_LO(offset))
  40. # define MY_PREAD64(fd, buf, count, offset) __syscall_pread(fd, buf, count, OFF64_HI_LO(offset))
  41. # else
  42. static _syscall4(ssize_t, __syscall_pread, int, fd, void *, buf,
  43. size_t, count, off_t, offset)
  44. # define MY_PREAD(fd, buf, count, offset) __syscall_pread(fd, buf, count, offset)
  45. # define MY_PREAD64(fd, buf, count, offset) __syscall_pread(fd, buf, count, offset)
  46. # endif
  47. # endif
  48. #endif
  49. #ifndef MY_PWRITE
  50. # ifdef __NR_pwrite
  51. # define __NR___syscall_pwrite __NR_pwrite
  52. # if defined(__UCLIBC_SYSCALL_ALIGN_64BIT__)
  53. static _syscall6(ssize_t, __syscall_pwrite, int, fd, const void *, buf,
  54. size_t, count, int, dummy, off_t, offset_hi, off_t, offset_lo)
  55. # define MY_PWRITE(fd, buf, count, offset) __syscall_pwrite(fd, buf, count, 0, OFF_HI_LO(offset))
  56. # define MY_PWRITE64(fd, buf, count, offset) __syscall_pwrite(fd, buf, count, 0, OFF64_HI_LO(offset))
  57. # elif __WORDSIZE == 32
  58. static _syscall5(ssize_t, __syscall_pwrite, int, fd, const void *, buf,
  59. size_t, count, off_t, offset_hi, off_t, offset_lo)
  60. # define MY_PWRITE(fd, buf, count, offset) __syscall_pwrite(fd, buf, count, OFF_HI_LO(offset))
  61. # define MY_PWRITE64(fd, buf, count, offset) __syscall_pwrite(fd, buf, count, OFF64_HI_LO(offset))
  62. # else
  63. static _syscall4(ssize_t, __syscall_pwrite, int, fd, const void *, buf,
  64. size_t, count, off_t, offset)
  65. # define MY_PWRITE(fd, buf, count, offset) __syscall_pwrite(fd, buf, count, offset)
  66. # define MY_PWRITE64(fd, buf, count, offset) __syscall_pwrite(fd, buf, count, offset)
  67. # endif
  68. # endif
  69. #endif
  70. static ssize_t __NC(pread)(int fd, void *buf, size_t count, off_t offset)
  71. {
  72. return MY_PREAD(fd, buf, count, offset);
  73. }
  74. CANCELLABLE_SYSCALL(ssize_t, pread, (int fd, void *buf, size_t count, off_t offset),
  75. (fd, buf, count, offset))
  76. static ssize_t __NC(pwrite)(int fd, const void *buf, size_t count, off_t offset)
  77. {
  78. return MY_PWRITE(fd, buf, count, offset);
  79. }
  80. CANCELLABLE_SYSCALL(ssize_t, pwrite, (int fd, const void *buf, size_t count, off_t offset),
  81. (fd, buf, count, offset))
  82. #if __WORDSIZE == 32
  83. static ssize_t __NC(pread64)(int fd, void *buf, size_t count, off64_t offset)
  84. {
  85. return MY_PREAD64(fd, buf, count, offset);
  86. }
  87. CANCELLABLE_SYSCALL(ssize_t, pread64, (int fd, void *buf, size_t count, off64_t offset),
  88. (fd, buf, count, offset))
  89. static ssize_t __NC(pwrite64)(int fd, const void *buf, size_t count, off64_t offset)
  90. {
  91. return MY_PWRITE64(fd, buf, count, offset);
  92. }
  93. CANCELLABLE_SYSCALL(ssize_t, pwrite64, (int fd, const void *buf, size_t count, off64_t offset),
  94. (fd, buf, count, offset))
  95. #else
  96. # ifdef __UCLIBC_HAS_LINUXTHREADS__
  97. weak_alias(pread,pread64)
  98. weak_alias(pwrite,pwrite64)
  99. lt_strong_alias(pread64)
  100. lt_strong_alias(pwrite64)
  101. # else
  102. strong_alias_untyped(pread,pread64)
  103. strong_alias_untyped(pwrite,pwrite64)
  104. # endif
  105. #endif