3 * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
5 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
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...
15 #include "../common/syscalls.h"
18 #ifndef __UCLIBC_HAS_LFS__
19 # define off64_t off_t
23 extern __typeof(pread) __libc_pread;
24 # define __NR___syscall_pread __NR_pread
25 static inline _syscall4(ssize_t, __syscall_pread, int, fd,
26 void *, buf, size_t, count, off64_t, offset);
28 ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
30 return(__syscall_pread(fd, buf, count, (off64_t)offset));
32 weak_alias(__libc_pread,pread)
34 # ifdef __UCLIBC_HAS_LFS__
35 extern __typeof(pread64) __libc_pread64;
36 ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
38 return(__syscall_pread(fd, buf, count, offset));
40 weak_alias(__libc_pread64,pread64)
41 # endif /* __UCLIBC_HAS_LFS__ */
42 #endif /* __NR_pread */
46 extern __typeof(pwrite) __libc_pwrite;
47 # define __NR___syscall_pwrite __NR_pwrite
48 static inline _syscall4(ssize_t, __syscall_pwrite, int, fd,
49 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)
53 return(__syscall_pwrite(fd, buf, count, (off64_t)offset));
55 weak_alias(__libc_pwrite,pwrite)
57 # ifdef __UCLIBC_HAS_LFS__
58 extern __typeof(pwrite64) __libc_pwrite64;
59 ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
61 return(__syscall_pwrite(fd, buf, count, offset));
63 weak_alias(__libc_pwrite64,pwrite64)
64 # endif /* __UCLIBC_HAS_LFS__ */
65 #endif /* __NR_pwrite */
69 #if ! defined __NR_pread || ! defined __NR_pwrite
70 libc_hidden_proto(read)
71 libc_hidden_proto(write)
72 libc_hidden_proto(lseek)
73 libc_hidden_proto(lseek64)
75 static ssize_t __fake_pread_write(int fd, void *buf,
76 size_t count, off_t offset, int do_pwrite)
82 /* Since we must not change the file pointer preserve the
83 * value so that we can restore it later. */
84 if ((old_offset=lseek(fd, 0, SEEK_CUR)) == (off_t) -1)
87 /* Set to wanted position. */
88 if (lseek (fd, offset, SEEK_SET) == (off_t) -1)
93 result = write(fd, buf, count);
96 result = read(fd, buf, count);
99 /* Now we have to restore the position. If this fails we
100 * have to return this as an error. */
102 if (lseek(fd, old_offset, SEEK_SET) == (off_t) -1)
105 __set_errno(save_errno);
108 __set_errno(save_errno);
112 # ifdef __UCLIBC_HAS_LFS__
113 static ssize_t __fake_pread_write64(int fd, void *buf,
114 size_t count, off64_t offset, int do_pwrite)
120 /* Since we must not change the file pointer preserve the
121 * value so that we can restore it later. */
122 if ((old_offset=lseek64(fd, 0, SEEK_CUR)) == (off64_t) -1)
125 /* Set to wanted position. */
126 if (lseek64(fd, offset, SEEK_SET) == (off64_t) -1)
130 /* Write the data. */
131 result = write(fd, buf, count);
134 result = read(fd, buf, count);
137 /* Now we have to restore the position. */
139 if (lseek64 (fd, old_offset, SEEK_SET) == (off64_t) -1) {
141 __set_errno (save_errno);
144 __set_errno (save_errno);
147 # endif /* __UCLIBC_HAS_LFS__ */
148 #endif /* ! defined __NR_pread || ! defined __NR_pwrite */
151 ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
153 return(__fake_pread_write(fd, buf, count, offset, 0));
155 weak_alias(__libc_pread,pread)
157 # ifdef __UCLIBC_HAS_LFS__
158 ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
160 return(__fake_pread_write64(fd, buf, count, offset, 0));
162 weak_alias(__libc_pread64,pread64)
163 # endif /* __UCLIBC_HAS_LFS__ */
164 #endif /* ! __NR_pread */
168 ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
170 return(__fake_pread_write(fd, buf, count, offset, 1));
172 weak_alias(__libc_pwrite,pwrite)
174 # ifdef __UCLIBC_HAS_LFS__
175 ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
177 return(__fake_pread_write64(fd, (void*)buf, count, offset, 1));
179 weak_alias(__libc_pwrite64,pwrite64)
180 # endif /* __UCLIBC_HAS_LFS__ */
181 #endif /* ! __NR_pwrite */