File: pread.c

package info (click to toggle)
inn2 2.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,720 kB
  • ctags: 8,983
  • sloc: ansic: 92,499; sh: 13,509; perl: 12,921; makefile: 2,985; yacc: 842; python: 342; lex: 255
file content (48 lines) | stat: -rw-r--r-- 1,563 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*  $Id: pread.c 5049 2001-12-12 09:06:00Z rra $
**
**  Replacement for a missing pread.
**
**  Written by Russ Allbery <rra@stanford.edu>
**  This work is hereby placed in the public domain by its author.
**
**  Provides the same functionality as the standard library routine pread
**  for those platforms that don't have it.  Note that pread requires that
**  the file pointer not move and without the library function, we can't
**  copy that behavior; instead, we approximate it by moving the file
**  pointer and then moving it back.  This may break threaded programs.
*/

#include "config.h"
#include "clibrary.h"
#include <errno.h>

/* If we're running the test suite, rename pread to avoid conflicts with the
   system version.  #undef first because large file support may define a
   macro pread (pointing to pread64) on some platforms (e.g. Solaris). */
#if TESTING
# undef pread
# define pread test_pread
ssize_t test_pread(int, void *, size_t, off_t);
#endif

ssize_t
pread(int fd, void *buf, size_t nbyte, off_t offset)
{
    off_t current;
    ssize_t nread;
    int oerrno;

    current = lseek(fd, 0, SEEK_CUR);
    if (current == (off_t) -1 || lseek(fd, offset, SEEK_SET) == (off_t) -1)
        return -1;

    nread = read(fd, buf, nbyte);

    /* Ignore errors in restoring the file position; this isn't ideal, but
       reporting a failed read when the read succeeded is worse.  Make sure
       that errno, if set, is set by read and not lseek. */
    oerrno = errno;
    lseek(fd, current, SEEK_SET);
    errno = oerrno;
    return nread;
}