1 /* $Id: pread.c 5049 2001-12-12 09:06:00Z rra $
3 ** Replacement for a missing pread.
5 ** Written by Russ Allbery <rra@stanford.edu>
6 ** This work is hereby placed in the public domain by its author.
8 ** Provides the same functionality as the standard library routine pread
9 ** for those platforms that don't have it. Note that pread requires that
10 ** the file pointer not move and without the library function, we can't
11 ** copy that behavior; instead, we approximate it by moving the file
12 ** pointer and then moving it back. This may break threaded programs.
19 /* If we're running the test suite, rename pread to avoid conflicts with the
20 system version. #undef first because large file support may define a
21 macro pread (pointing to pread64) on some platforms (e.g. Solaris). */
24 # define pread test_pread
25 ssize_t test_pread(int, void *, size_t, off_t);
29 pread(int fd, void *buf, size_t nbyte, off_t offset)
35 current = lseek(fd, 0, SEEK_CUR);
36 if (current == (off_t) -1 || lseek(fd, offset, SEEK_SET) == (off_t) -1)
39 nread = read(fd, buf, nbyte);
41 /* Ignore errors in restoring the file position; this isn't ideal, but
42 reporting a failed read when the read succeeded is worse. Make sure
43 that errno, if set, is set by read and not lseek. */
45 lseek(fd, current, SEEK_SET);