pread_write.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /* Based in part on the files
  8. * ./sysdeps/unix/sysv/linux/pwrite.c,
  9. * ./sysdeps/unix/sysv/linux/pread.c,
  10. * sysdeps/posix/pread.c
  11. * sysdeps/posix/pwrite.c
  12. * from GNU libc 2.2.5, but reworked considerably...
  13. */
  14. #include <sys/syscall.h>
  15. #include <unistd.h>
  16. #include <endian.h>
  17. #ifndef __UCLIBC_HAS_LFS__
  18. # define off64_t off_t
  19. #endif
  20. #ifdef __NR_pread
  21. extern __typeof(pread) __libc_pread;
  22. # define __NR___syscall_pread __NR_pread
  23. static __inline__ _syscall6(ssize_t, __syscall_pread, int, fd,
  24. void *, buf, size_t, count, int, dummy, off_t, offset_hi, off_t, offset_lo)
  25. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  26. {
  27. return(__syscall_pread(fd, buf, count, 0, __LONG_LONG_PAIR(offset >> 31, offset)));
  28. }
  29. weak_alias(__libc_pread,pread)
  30. # ifdef __UCLIBC_HAS_LFS__
  31. extern __typeof(pread64) __libc_pread64;
  32. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  33. {
  34. return(__syscall_pread(fd, buf, count, 0, __LONG_LONG_PAIR(offset >> 32, offset)));
  35. }
  36. weak_alias(__libc_pread64,pread64)
  37. # endif /* __UCLIBC_HAS_LFS__ */
  38. #endif /* __NR_pread */
  39. #ifdef __NR_pwrite
  40. extern __typeof(pwrite) __libc_pwrite;
  41. # define __NR___syscall_pwrite __NR_pwrite
  42. static __inline__ _syscall6(ssize_t, __syscall_pwrite, int, fd,
  43. const void *, buf, size_t, count, int, dummy, off_t, offset_hi, off_t, offset_lo)
  44. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  45. {
  46. return(__syscall_pwrite(fd, buf, count, 0, __LONG_LONG_PAIR(offset >> 31, offset)));
  47. }
  48. weak_alias(__libc_pwrite,pwrite)
  49. # ifdef __UCLIBC_HAS_LFS__
  50. extern __typeof(pwrite64) __libc_pwrite64;
  51. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  52. {
  53. return(__syscall_pwrite(fd, buf, count, 0, __LONG_LONG_PAIR(offset >> 32, offset)));
  54. }
  55. weak_alias(__libc_pwrite64,pwrite64)
  56. # endif /* __UCLIBC_HAS_LFS__ */
  57. #endif /* __NR_pwrite */
  58. #if ! defined __NR_pread || ! defined __NR_pwrite
  59. static ssize_t __fake_pread_write(int fd, void *buf,
  60. size_t count, off_t offset, int do_pwrite)
  61. {
  62. int save_errno;
  63. ssize_t result;
  64. off_t old_offset;
  65. /* Since we must not change the file pointer preserve the
  66. * value so that we can restore it later. */
  67. if ((old_offset=lseek(fd, 0, SEEK_CUR)) == (off_t) -1)
  68. return -1;
  69. /* Set to wanted position. */
  70. if (lseek (fd, offset, SEEK_SET) == (off_t) -1)
  71. return -1;
  72. if (do_pwrite == 1) {
  73. /* Write the data. */
  74. result = write(fd, buf, count);
  75. } else {
  76. /* Read the data. */
  77. result = read(fd, buf, count);
  78. }
  79. /* Now we have to restore the position. If this fails we
  80. * have to return this as an error. */
  81. save_errno = errno;
  82. if (lseek(fd, old_offset, SEEK_SET) == (off_t) -1)
  83. {
  84. if (result == -1)
  85. __set_errno(save_errno);
  86. return -1;
  87. }
  88. __set_errno(save_errno);
  89. return(result);
  90. }
  91. # ifdef __UCLIBC_HAS_LFS__
  92. static ssize_t __fake_pread_write64(int fd, void *buf,
  93. size_t count, off64_t offset, int do_pwrite)
  94. {
  95. int save_errno;
  96. ssize_t result;
  97. off64_t old_offset;
  98. /* Since we must not change the file pointer preserve the
  99. * value so that we can restore it later. */
  100. if ((old_offset=lseek64(fd, 0, SEEK_CUR)) == (off64_t) -1)
  101. return -1;
  102. /* Set to wanted position. */
  103. if (lseek64(fd, offset, SEEK_SET) == (off64_t) -1)
  104. return -1;
  105. if (do_pwrite == 1) {
  106. /* Write the data. */
  107. result = write(fd, buf, count);
  108. } else {
  109. /* Read the data. */
  110. result = read(fd, buf, count);
  111. }
  112. /* Now we have to restore the position. */
  113. save_errno = errno;
  114. if (lseek64 (fd, old_offset, SEEK_SET) == (off64_t) -1) {
  115. if (result == -1)
  116. __set_errno (save_errno);
  117. return -1;
  118. }
  119. __set_errno (save_errno);
  120. return result;
  121. }
  122. # endif /* __UCLIBC_HAS_LFS__ */
  123. #endif /* ! defined __NR_pread || ! defined __NR_pwrite */
  124. #ifndef __NR_pread
  125. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  126. {
  127. return(__fake_pread_write(fd, buf, count, offset, 0));
  128. }
  129. weak_alias(__libc_pread,pread)
  130. # ifdef __UCLIBC_HAS_LFS__
  131. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  132. {
  133. return(__fake_pread_write64(fd, buf, count, offset, 0));
  134. }
  135. weak_alias(__libc_pread64,pread64)
  136. # endif /* __UCLIBC_HAS_LFS__ */
  137. #endif /* ! __NR_pread */
  138. #ifndef __NR_pwrite
  139. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  140. {
  141. return(__fake_pread_write(fd, (void*)buf, count, offset, 1));
  142. }
  143. weak_alias(__libc_pwrite,pwrite)
  144. # ifdef __UCLIBC_HAS_LFS__
  145. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  146. {
  147. return(__fake_pread_write64(fd, (void*)buf, count, offset, 1));
  148. }
  149. weak_alias(__libc_pwrite64,pwrite64)
  150. # endif /* __UCLIBC_HAS_LFS__ */
  151. #endif /* ! __NR_pwrite */