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...
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.
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
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
27 #define _LARGEFILE64_SOURCE
30 /* We absolutely do _NOT_ want interfaces silently
31 * * * renamed under us or very bad things will happen... */
32 #ifdef __USE_FILE_OFFSET64
33 # undef __USE_FILE_OFFSET64
38 #include <sys/types.h>
39 #include <sys/syscall.h>
43 #define __NR___syscall_pread __NR_pread
44 static inline _syscall4(ssize_t, __syscall_pread, int, fd,
45 void *, buf, size_t, count, off64_t, offset);
47 ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
49 return(__syscall_pread(fd, buf, count, (off64_t)offset));
51 weak_alias (__libc_pread, pread)
53 #if defined __UCLIBC_HAS_LFS__
54 ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
56 return(__syscall_pread(fd, buf, count, offset));
58 weak_alias (__libc_pread64, pread64)
59 #endif /* __UCLIBC_HAS_LFS__ */
60 #endif /* __NR_pread */
64 #define __NR___syscall_pwrite __NR_pwrite
65 static inline _syscall4(ssize_t, __syscall_pwrite, int, fd,
66 const void *, buf, size_t, count, off64_t, offset);
68 ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
70 return(__syscall_pwrite(fd, buf, count, (off64_t)offset));
72 weak_alias (__libc_pwrite, pwrite)
74 #if defined __UCLIBC_HAS_LFS__
75 ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
77 return(__syscall_pwrite(fd, buf, count, offset));
79 weak_alias (__libc_pwrite64, pwrite64)
80 #endif /* __UCLIBC_HAS_LFS__ */
81 #endif /* __NR_pwrite */
85 #if ! defined __NR_pread || ! defined __NR_pwrite
86 static ssize_t __fake_pread_write(int fd, void *buf,
87 size_t count, off_t offset, int do_pwrite)
93 /* Since we must not change the file pointer preserve the
94 * value so that we can restore it later. */
95 if ((old_offset=lseek(fd, 0, SEEK_CUR)) == (off_t) -1)
98 /* Set to wanted position. */
99 if (lseek (fd, offset, SEEK_SET) == (off_t) -1)
103 /* Write the data. */
104 result = write(fd, buf, count);
107 result = read(fd, buf, count);
110 /* Now we have to restore the position. If this fails we
111 * have to return this as an error. */
113 if (lseek(fd, old_offset, SEEK_SET) == (off_t) -1)
116 __set_errno(save_errno);
119 __set_errno(save_errno);
123 #if defined __UCLIBC_HAS_LFS__
124 static ssize_t __fake_pread_write64(int fd, void *buf,
125 size_t count, off64_t offset, int do_pwrite)
131 /* Since we must not change the file pointer preserve the
132 * value so that we can restore it later. */
133 if ((old_offset=lseek64(fd, 0, SEEK_CUR)) == (off64_t) -1)
136 /* Set to wanted position. */
137 if (lseek64(fd, offset, SEEK_SET) == (off64_t) -1)
141 /* Write the data. */
142 result = write(fd, buf, count);
145 result = read(fd, buf, count);
148 /* Now we have to restore the position. */
150 if (lseek64 (fd, old_offset, SEEK_SET) == (off64_t) -1) {
152 __set_errno (save_errno);
155 __set_errno (save_errno);
158 #endif /* __UCLIBC_HAS_LFS__ */
159 #endif /* ! defined __NR_pread || ! defined __NR_pwrite */
162 ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
164 return(__fake_pread_write(fd, buf, count, offset, 0));
166 weak_alias (__libc_pread, pread)
168 #if defined __UCLIBC_HAS_LFS__
169 ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
171 return(__fake_pread_write64(fd, buf, count, offset, 0));
173 weak_alias (__libc_pread64, pread64)
174 #endif /* __UCLIBC_HAS_LFS__ */
175 #endif /* ! __NR_pread */
179 ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
181 return(__fake_pread_write(fd, buf, count, offset, 1));
183 weak_alias (__libc_pwrite, pwrite)
185 #if defined __UCLIBC_HAS_LFS__
186 ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
188 return(__fake_pread_write64(fd, (void*)buf, count, offset, 1));
190 weak_alias (__libc_pwrite64, pwrite64)
191 #endif /* __UCLIBC_HAS_LFS__ */
192 #endif /* ! __NR_pwrite */