OSDN Git Service

Add some prototypes to arch specific pread_write.c, mips has __mips64 questionable...
[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 #include "../common/syscalls.h"
16 #include <unistd.h>
17
18 #ifndef __UCLIBC_HAS_LFS__ 
19 # define off64_t off_t
20 #endif
21
22 #ifdef __NR_pread
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);
27
28 ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
29
30         return(__syscall_pread(fd, buf, count, (off64_t)offset));
31 }
32 weak_alias(__libc_pread,pread)
33
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)
37
38         return(__syscall_pread(fd, buf, count, offset));
39 }
40 weak_alias(__libc_pread64,pread64)
41 # endif /* __UCLIBC_HAS_LFS__  */
42 #endif /* __NR_pread */
43
44
45 #ifdef __NR_pwrite
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);
50
51 ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
52
53         return(__syscall_pwrite(fd, buf, count, (off64_t)offset));
54 }
55 weak_alias(__libc_pwrite,pwrite)
56
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)
60
61         return(__syscall_pwrite(fd, buf, count, offset));
62 }
63 weak_alias(__libc_pwrite64,pwrite64)
64 # endif /* __UCLIBC_HAS_LFS__  */
65 #endif /* __NR_pwrite */
66
67
68
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)
74
75 static ssize_t __fake_pread_write(int fd, void *buf, 
76                 size_t count, off_t offset, int do_pwrite)
77 {
78         int save_errno;
79         ssize_t result;
80         off_t old_offset;
81
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)
85                 return -1;
86
87         /* Set to wanted position.  */
88         if (lseek (fd, offset, SEEK_SET) == (off_t) -1)
89                 return -1;
90
91         if (do_pwrite==1) {
92                 /* Write the data.  */
93                 result = write(fd, buf, count);
94         } else {
95                 /* Read the data.  */
96                 result = read(fd, buf, count);
97         }
98
99         /* Now we have to restore the position.  If this fails we 
100          * have to return this as an error.  */
101         save_errno = errno;
102         if (lseek(fd, old_offset, SEEK_SET) == (off_t) -1)
103         {
104                 if (result == -1)
105                         __set_errno(save_errno);
106                 return -1;
107         }
108         __set_errno(save_errno);
109         return(result);
110 }
111
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)
115 {
116         int save_errno;
117         ssize_t result;
118         off64_t old_offset;
119
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)
123                 return -1;
124
125         /* Set to wanted position.  */
126         if (lseek64(fd, offset, SEEK_SET) == (off64_t) -1) 
127                 return -1;                               
128
129         if (do_pwrite==1) {
130                 /* Write the data.  */
131                 result = write(fd, buf, count);
132         } else {
133                 /* Read the data.  */
134                 result = read(fd, buf, count);
135         }
136
137         /* Now we have to restore the position. */
138         save_errno = errno;
139         if (lseek64 (fd, old_offset, SEEK_SET) == (off64_t) -1) {
140                 if (result == -1)
141                         __set_errno (save_errno);
142                 return -1;
143         }
144         __set_errno (save_errno);
145         return result;
146 }
147 # endif /* __UCLIBC_HAS_LFS__  */
148 #endif /*  ! defined __NR_pread || ! defined __NR_pwrite */
149
150 #ifndef __NR_pread
151 ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset)
152 {
153         return(__fake_pread_write(fd, buf, count, offset, 0));
154 }
155 weak_alias(__libc_pread,pread)
156
157 # ifdef __UCLIBC_HAS_LFS__ 
158 ssize_t __libc_pread64(int fd, void *buf, size_t count, off64_t offset)
159
160         return(__fake_pread_write64(fd, buf, count, offset, 0));
161 }
162 weak_alias(__libc_pread64,pread64)
163 # endif /* __UCLIBC_HAS_LFS__  */
164 #endif /* ! __NR_pread */
165
166
167 #ifndef __NR_pwrite
168 ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset)
169 {
170         return(__fake_pread_write(fd, buf, count, offset, 1));
171 }
172 weak_alias(__libc_pwrite,pwrite)
173
174 # ifdef __UCLIBC_HAS_LFS__ 
175 ssize_t __libc_pwrite64(int fd, const void *buf, size_t count, off64_t offset)
176
177         return(__fake_pread_write64(fd, (void*)buf, count, offset, 1));
178 }
179 weak_alias(__libc_pwrite64,pwrite64)
180 # endif /* __UCLIBC_HAS_LFS__  */
181 #endif /* ! __NR_pwrite */