pread_write.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* vi: set sw=4 ts=4:
  2. *
  3. * Copyright (C) 2002 by Erik Andersen <andersen@uclibc.org>
  4. * Based in part on the files
  5. * ./sysdeps/unix/sysv/linux/pwrite.c,
  6. * ./sysdeps/unix/sysv/linux/pread.c,
  7. * sysdeps/posix/pread.c
  8. * sysdeps/posix/pwrite.c
  9. * from GNU libc 2.2.5, but reworked considerably...
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU Library General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at your
  14. * option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but WITHOUT
  17. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  19. * for more details.
  20. *
  21. * You should have received a copy of the GNU Library General Public License
  22. * along with this program; if not, write to the Free Software Foundation,
  23. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #define _GNU_SOURCE
  26. #define _LARGEFILE64_SOURCE
  27. #include <features.h>
  28. #undef __OPTIMIZE__
  29. /* We absolutely do _NOT_ want interfaces silently
  30. * * * renamed under us or very bad things will happen... */
  31. #ifdef __USE_FILE_OFFSET64
  32. # undef __USE_FILE_OFFSET64
  33. #endif
  34. #include <errno.h>
  35. #include <sys/types.h>
  36. #include <sys/syscall.h>
  37. #include <unistd.h>
  38. #ifdef __NR_pread
  39. #define __NR___syscall_pread __NR_pread
  40. static inline _syscall4(ssize_t, __syscall_pread, int, fd,
  41. void *, buf, size_t, count, off64_t, offset);
  42. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  43. {
  44. return(__syscall_pread(fd, buf, count, (off64_t)offset));
  45. }
  46. weak_alias (__libc_pread, pread)
  47. #if defined __UCLIBC_HAS_LFS__
  48. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  49. {
  50. return(__syscall_pread(fd, buf, count, offset));
  51. }
  52. weak_alias (__libc_pread64, pread64)
  53. #endif /* __UCLIBC_HAS_LFS__ */
  54. #endif /* __NR_pread */
  55. #ifdef __NR_pwrite
  56. #define __NR___syscall_pwrite __NR_pwrite
  57. static inline _syscall4(ssize_t, __syscall_pwrite, int, fd,
  58. const void *, buf, size_t, count, off64_t, offset);
  59. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  60. {
  61. return(__syscall_pwrite(fd, buf, count, (off64_t)offset));
  62. }
  63. weak_alias (__libc_pwrite, pwrite)
  64. #if defined __UCLIBC_HAS_LFS__
  65. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  66. {
  67. return(__syscall_pwrite(fd, buf, count, offset));
  68. }
  69. weak_alias (__libc_pwrite64, pwrite64)
  70. #endif /* __UCLIBC_HAS_LFS__ */
  71. #endif /* __NR_pwrite */
  72. #if ! defined __NR_pread || ! defined __NR_pwrite
  73. static ssize_t __fake_pread_write(int fd, void *buf,
  74. size_t count, off_t offset, int do_pwrite)
  75. {
  76. int save_errno;
  77. ssize_t result;
  78. off_t old_offset;
  79. /* Since we must not change the file pointer preserve the
  80. * value so that we can restore it later. */
  81. if ((old_offset=lseek(fd, 0, SEEK_CUR)) == (off_t) -1)
  82. return -1;
  83. /* Set to wanted position. */
  84. if (lseek (fd, offset, SEEK_SET) == (off_t) -1)
  85. return -1;
  86. if (do_pwrite==1) {
  87. /* Write the data. */
  88. result = write(fd, buf, count);
  89. } else {
  90. /* Read the data. */
  91. result = read(fd, buf, count);
  92. }
  93. /* Now we have to restore the position. If this fails we
  94. * have to return this as an error. */
  95. save_errno = errno;
  96. if (lseek(fd, old_offset, SEEK_SET) == (off_t) -1)
  97. {
  98. if (result == -1)
  99. __set_errno(save_errno);
  100. return -1;
  101. }
  102. __set_errno(save_errno);
  103. return(result);
  104. }
  105. #if defined __UCLIBC_HAS_LFS__
  106. static ssize_t __fake_pread_write64(int fd, void *buf,
  107. size_t count, off64_t offset, int do_pwrite)
  108. {
  109. int save_errno;
  110. ssize_t result;
  111. off64_t old_offset;
  112. /* Since we must not change the file pointer preserve the
  113. * value so that we can restore it later. */
  114. if ((old_offset=lseek64(fd, 0, SEEK_CUR)) == (off64_t) -1)
  115. return -1;
  116. /* Set to wanted position. */
  117. if (lseek64(fd, offset, SEEK_SET) == (off64_t) -1)
  118. return -1;
  119. if (do_pwrite==1) {
  120. /* Write the data. */
  121. result = write(fd, buf, count);
  122. } else {
  123. /* Read the data. */
  124. result = read(fd, buf, count);
  125. }
  126. /* Now we have to restore the position. */
  127. save_errno = errno;
  128. if (lseek64 (fd, old_offset, SEEK_SET) == (off64_t) -1) {
  129. if (result == -1)
  130. __set_errno (save_errno);
  131. return -1;
  132. }
  133. __set_errno (save_errno);
  134. return result;
  135. }
  136. #endif /* __UCLIBC_HAS_LFS__ */
  137. #endif /* ! defined __NR_pread || ! defined __NR_pwrite */
  138. #ifndef __NR_pread
  139. ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
  140. {
  141. return(__fake_pread_write(fd, buf, count, offset, 0));
  142. }
  143. weak_alias (__libc_pread, pread)
  144. #if defined __UCLIBC_HAS_LFS__
  145. ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
  146. {
  147. return(__fake_pread_write64(fd, buf, count, offset, 0));
  148. }
  149. weak_alias (__libc_pread64, pread64)
  150. #endif /* __UCLIBC_HAS_LFS__ */
  151. #endif /* ! __NR_pread */
  152. #ifndef __NR_pwrite
  153. ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
  154. {
  155. return(__fake_pread_write(fd, buf, count, offset, 1));
  156. }
  157. weak_alias (__libc_pwrite, pwrite)
  158. #if defined __UCLIBC_HAS_LFS__
  159. ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
  160. {
  161. return(__fake_pread_write64(fd, (void*)buf, count, offset, 1));
  162. }
  163. weak_alias (__libc_pwrite64, pwrite64)
  164. #endif /* __UCLIBC_HAS_LFS__ */
  165. #endif /* ! __NR_pwrite */