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
37 #include <sys/types.h>
38 #include <sys/syscall.h>
41 #if ! defined __UCLIBC_HAS_LFS__
46 #define __NR___syscall_pread __NR_pread
47 static inline _syscall4(ssize_t, __syscall_pread, int, fd,
48 void *, buf, size_t, count, off64_t, offset);
50 ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
52 return(__syscall_pread(fd, buf, count, (off64_t)offset));
54 strong_alias(__libc_pread,pread)
56 #if defined __UCLIBC_HAS_LFS__
57 ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
59 return(__syscall_pread(fd, buf, count, offset));
61 strong_alias(__libc_pread64,pread64)
62 #endif /* __UCLIBC_HAS_LFS__ */
63 #endif /* __NR_pread */
67 #define __NR___syscall_pwrite __NR_pwrite
68 static inline _syscall4(ssize_t, __syscall_pwrite, int, fd,
69 const void *, buf, size_t, count, off64_t, offset);
71 ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
73 return(__syscall_pwrite(fd, buf, count, (off64_t)offset));
75 strong_alias(__libc_pwrite,pwrite)
77 #if defined __UCLIBC_HAS_LFS__
78 ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
80 return(__syscall_pwrite(fd, buf, count, offset));
82 strong_alias(__libc_pwrite64,pwrite64)
83 #endif /* __UCLIBC_HAS_LFS__ */
84 #endif /* __NR_pwrite */
88 #if ! defined __NR_pread || ! defined __NR_pwrite
89 libc_hidden_proto(lseek)
90 libc_hidden_proto(lseek64)
91 libc_hidden_proto(read)
92 libc_hidden_proto(write)
94 static ssize_t __fake_pread_write(int fd, void *buf,
95 size_t count, off_t offset, int do_pwrite)
101 /* Since we must not change the file pointer preserve the
102 * value so that we can restore it later. */
103 if ((old_offset=lseek(fd, 0, SEEK_CUR)) == (off_t) -1)
106 /* Set to wanted position. */
107 if (lseek (fd, offset, SEEK_SET) == (off_t) -1)
111 /* Write the data. */
112 result = write(fd, buf, count);
115 result = read(fd, buf, count);
118 /* Now we have to restore the position. If this fails we
119 * have to return this as an error. */
121 if (lseek(fd, old_offset, SEEK_SET) == (off_t) -1)
124 __set_errno(save_errno);
127 __set_errno(save_errno);
131 #if defined __UCLIBC_HAS_LFS__
132 static ssize_t __fake_pread_write64(int fd, void *buf,
133 size_t count, off64_t offset, int do_pwrite)
139 /* Since we must not change the file pointer preserve the
140 * value so that we can restore it later. */
141 if ((old_offset=lseek64(fd, 0, SEEK_CUR)) == (off64_t) -1)
144 /* Set to wanted position. */
145 if (lseek64(fd, offset, SEEK_SET) == (off64_t) -1)
149 /* Write the data. */
150 result = write(fd, buf, count);
153 result = read(fd, buf, count);
156 /* Now we have to restore the position. */
158 if (lseek64 (fd, old_offset, SEEK_SET) == (off64_t) -1) {
160 __set_errno (save_errno);
163 __set_errno (save_errno);
166 #endif /* __UCLIBC_HAS_LFS__ */
167 #endif /* ! defined __NR_pread || ! defined __NR_pwrite */
170 ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
172 return(__fake_pread_write(fd, buf, count, offset, 0));
174 strong_alias(__libc_pread,pread)
176 #if defined __UCLIBC_HAS_LFS__
177 ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
179 return(__fake_pread_write64(fd, buf, count, offset, 0));
181 strong_alias(__libc_pread64,pread64)
182 #endif /* __UCLIBC_HAS_LFS__ */
183 #endif /* ! __NR_pread */
187 ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
189 return(__fake_pread_write(fd, buf, count, offset, 1));
191 strong_alias(__libc_pwrite,pwrite)
193 #if defined __UCLIBC_HAS_LFS__
194 ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
196 return(__fake_pread_write64(fd, (void*)buf, count, offset, 1));
198 strong_alias(__libc_pwrite64,pwrite64)
199 #endif /* __UCLIBC_HAS_LFS__ */
200 #endif /* ! __NR_pwrite */