1 /* replacement pread function
2 Copyright (C) 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #define __libc_lseek(f,o,w) lseek (f, o, w)
25 #define __set_errno(Val) errno = (Val)
27 /* pread substitute for systems that the function, such as mingw32 and BeOS. */
28 /* The following is identical to the function from glibc's
29 sysdeps/posix/pread.c */
31 /* Note: This implementation of pread is not multithread-safe. */
34 pread (int fd, void *buf, size_t nbyte, off_t offset)
36 /* Since we must not change the file pointer preserve the value so that
37 we can restore it later. */
40 off_t old_offset = __libc_lseek (fd, 0, SEEK_CUR);
41 if (old_offset == (off_t) -1)
44 /* Set to wanted position. */
45 if (__libc_lseek (fd, offset, SEEK_SET) == (off_t) -1)
48 /* Write out the data. */
49 result = __libc_read (fd, buf, nbyte);
51 /* Now we have to restore the position. If this fails we have to
52 return this as an error. But if the writing also failed we
55 if (__libc_lseek (fd, old_offset, SEEK_SET) == (off_t) -1)
58 __set_errno (save_errno);
61 __set_errno (save_errno);