OSDN Git Service

make DODEBUG=y happy, update sysdeps/common/* copyright
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / powerpc / pread_write.c
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 /* 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...
13  */
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
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/syscall.h>
28 #include <unistd.h>
29
30 #if ! defined __UCLIBC_HAS_LFS__ 
31 #define off64_t off_t
32 #endif
33
34 #ifdef __NR_pread
35 #define __NR___syscall_pread __NR_pread 
36 static inline _syscall4(ssize_t, __syscall_pread, int, fd, 
37                 void *, buf, size_t, count, off64_t, offset);
38
39 ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
40
41         return(__syscall_pread(fd, buf, count, (off64_t)offset));
42 }
43 strong_alias(__libc_pread,pread)
44
45 #if defined __UCLIBC_HAS_LFS__ 
46 ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
47
48         return(__syscall_pread(fd, buf, count, offset));
49 }
50 strong_alias(__libc_pread64,pread64)
51 #endif /* __UCLIBC_HAS_LFS__  */
52 #endif /* __NR_pread */
53
54
55 #ifdef __NR_pwrite
56 #define __NR___syscall_pwrite __NR_pwrite 
57 static inline _syscall4(ssize_t, __syscall_pwrite, int, fd, 
58                 const void *, buf, size_t, count, off64_t, offset);
59
60 ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
61
62         return(__syscall_pwrite(fd, buf, count, (off64_t)offset));
63 }
64 strong_alias(__libc_pwrite,pwrite)
65
66 #if defined __UCLIBC_HAS_LFS__ 
67 ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
68
69         return(__syscall_pwrite(fd, buf, count, offset));
70 }
71 strong_alias(__libc_pwrite64,pwrite64)
72 #endif /* __UCLIBC_HAS_LFS__  */
73 #endif /* __NR_pwrite */
74
75
76
77 #if ! defined __NR_pread || ! defined __NR_pwrite
78 libc_hidden_proto(read)
79 libc_hidden_proto(write)
80 libc_hidden_proto(lseek)
81 libc_hidden_proto(lseek64)
82
83 static ssize_t __fake_pread_write(int fd, void *buf, 
84                 size_t count, off_t offset, int do_pwrite)
85 {
86         int save_errno;
87         ssize_t result;
88         off_t old_offset;
89
90         /* Since we must not change the file pointer preserve the 
91          * value so that we can restore it later.  */
92         if ((old_offset=lseek(fd, 0, SEEK_CUR)) == (off_t) -1)
93                 return -1;
94
95         /* Set to wanted position.  */
96         if (lseek (fd, offset, SEEK_SET) == (off_t) -1)
97                 return -1;
98
99         if (do_pwrite==1) {
100                 /* Write the data.  */
101                 result = write(fd, buf, count);
102         } else {
103                 /* Read the data.  */
104                 result = read(fd, buf, count);
105         }
106
107         /* Now we have to restore the position.  If this fails we 
108          * have to return this as an error.  */
109         save_errno = errno;
110         if (lseek(fd, old_offset, SEEK_SET) == (off_t) -1)
111         {
112                 if (result == -1)
113                         __set_errno(save_errno);
114                 return -1;
115         }
116         __set_errno(save_errno);
117         return(result);
118 }
119
120 #if defined __UCLIBC_HAS_LFS__ 
121 static ssize_t __fake_pread_write64(int fd, void *buf, 
122                 size_t count, off64_t offset, int do_pwrite)
123 {
124         int save_errno;
125         ssize_t result;
126         off64_t old_offset;
127
128         /* Since we must not change the file pointer preserve the 
129          * value so that we can restore it later.  */
130         if ((old_offset=lseek64(fd, 0, SEEK_CUR)) == (off64_t) -1)
131                 return -1;
132
133         /* Set to wanted position.  */
134         if (lseek64(fd, offset, SEEK_SET) == (off64_t) -1) 
135                 return -1;                               
136
137         if (do_pwrite==1) {
138                 /* Write the data.  */
139                 result = write(fd, buf, count);
140         } else {
141                 /* Read the data.  */
142                 result = read(fd, buf, count);
143         }
144
145         /* Now we have to restore the position. */
146         save_errno = errno;
147         if (lseek64 (fd, old_offset, SEEK_SET) == (off64_t) -1) {
148                 if (result == -1)
149                         __set_errno (save_errno);
150                 return -1;
151         }
152         __set_errno (save_errno);
153         return result;
154 }
155 #endif /* __UCLIBC_HAS_LFS__  */
156 #endif /*  ! defined __NR_pread || ! defined __NR_pwrite */
157
158 #ifndef __NR_pread
159 ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
160 {
161         return(__fake_pread_write(fd, buf, count, offset, 0));
162 }
163 strong_alias(__libc_pread,pread)
164
165 #if defined __UCLIBC_HAS_LFS__ 
166 ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
167
168         return(__fake_pread_write64(fd, buf, count, offset, 0));
169 }
170 strong_alias(__libc_pread64,pread64)
171 #endif /* __UCLIBC_HAS_LFS__  */
172 #endif /* ! __NR_pread */
173
174
175 #ifndef __NR_pwrite
176 ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
177 {
178         return(__fake_pread_write(fd, buf, count, offset, 1));
179 }
180 strong_alias(__libc_pwrite,pwrite)
181
182 #if defined __UCLIBC_HAS_LFS__ 
183 ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
184
185         return(__fake_pread_write64(fd, (void*)buf, count, offset, 1));
186 }
187 strong_alias(__libc_pwrite64,pwrite64)
188 #endif /* __UCLIBC_HAS_LFS__  */
189 #endif /* ! __NR_pwrite */