pread_write.c 5.5 KB

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