pread_write.c 5.1 KB

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