pread_write.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. # else
  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. # endif
  42. # endif
  43. #endif
  44. #ifndef MY_PWRITE
  45. # ifdef __NR_pwrite
  46. # define __NR___syscall_pwrite __NR_pwrite
  47. # if defined(__UCLIBC_SYSCALL_ALIGN_64BIT__)
  48. static _syscall6(ssize_t, __syscall_pwrite, int, fd, const void *, buf,
  49. size_t, count, int, dummy, off_t, offset_hi, off_t, offset_lo)
  50. # define MY_PWRITE(fd, buf, count, offset) __syscall_pwrite(fd, buf, count, 0, OFF_HI_LO(offset))
  51. # define MY_PWRITE64(fd, buf, count, offset) __syscall_pwrite(fd, buf, count, 0, OFF64_HI_LO(offset))
  52. # else
  53. static _syscall5(ssize_t, __syscall_pwrite, int, fd, const void *, buf,
  54. size_t, count, off_t, offset_hi, off_t, offset_lo)
  55. # define MY_PWRITE(fd, buf, count, offset) __syscall_pwrite(fd, buf, count, OFF_HI_LO(offset))
  56. # define MY_PWRITE64(fd, buf, count, offset) __syscall_pwrite(fd, buf, count, OFF64_HI_LO(offset))
  57. # endif
  58. # endif
  59. #endif
  60. static ssize_t __NC(pread)(int fd, void *buf, size_t count, off_t offset)
  61. {
  62. return MY_PREAD(fd, buf, count, offset);
  63. }
  64. CANCELLABLE_SYSCALL(ssize_t, pread, (int fd, void *buf, size_t count, off_t offset),
  65. (fd, buf, count, offset))
  66. static ssize_t __NC(pwrite)(int fd, const void *buf, size_t count, off_t offset)
  67. {
  68. return MY_PWRITE(fd, buf, count, offset);
  69. }
  70. CANCELLABLE_SYSCALL(ssize_t, pwrite, (int fd, const void *buf, size_t count, off_t offset),
  71. (fd, buf, count, offset))
  72. #ifdef __UCLIBC_HAS_LFS__
  73. # if __WORDSIZE == 32
  74. static ssize_t __NC(pread64)(int fd, void *buf, size_t count, off64_t offset)
  75. {
  76. return MY_PREAD64(fd, buf, count, offset);
  77. }
  78. CANCELLABLE_SYSCALL(ssize_t, pread64, (int fd, void *buf, size_t count, off64_t offset),
  79. (fd, buf, count, offset))
  80. static ssize_t __NC(pwrite64)(int fd, const void *buf, size_t count, off64_t offset)
  81. {
  82. return MY_PWRITE64(fd, buf, count, offset);
  83. }
  84. CANCELLABLE_SYSCALL(ssize_t, pwrite64, (int fd, const void *buf, size_t count, off64_t offset),
  85. (fd, buf, count, offset))
  86. # else
  87. # ifdef __LINUXTHREADS_OLD__
  88. weak_alias(pread,pread64)
  89. weak_alias(pwrite,pwrite64)
  90. lt_strong_alias(pread64)
  91. lt_strong_alias(pwrite64)
  92. # else
  93. strong_alias_untyped(pread,pread64)
  94. strong_alias_untyped(pwrite,pwrite64)
  95. # endif
  96. # endif
  97. #endif