pread_write.c 4.5 KB

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