pread_write.c 4.8 KB

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