pread_write.c 4.7 KB

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