1 | #ifndef _NO_PREAD |
---|
2 | /* |
---|
3 | FUNCTION |
---|
4 | <<pread>>---read a file from specified position |
---|
5 | |
---|
6 | INDEX |
---|
7 | pread |
---|
8 | INDEX |
---|
9 | _pread_r |
---|
10 | |
---|
11 | SYNOPSIS |
---|
12 | #include <unistd.h> |
---|
13 | ssize_t pread(int <[fd]>, void *<[buf]>, size_t <[n]>, off_t <[off]>); |
---|
14 | ssize_t _pread_r(struct _reent *<[rptr]>, int <[fd]>, |
---|
15 | void *<[buf]>, size_t <[n]>, off_t <[off]>); |
---|
16 | |
---|
17 | DESCRIPTION |
---|
18 | The <<pread>> function is similar to <<read>>. One difference is that |
---|
19 | <<pread>> has an additional parameter <[off]> which is the offset to |
---|
20 | position in the file before reading. The function also differs in that |
---|
21 | the file position is unchanged by the function (i.e. the file position |
---|
22 | is the same before and after a call to <<pread>>). |
---|
23 | |
---|
24 | The <<_pread_r>> function is the same as <<pread>>, only a reentrant |
---|
25 | struct pointer <[rptr]> is provided to preserve reentrancy. |
---|
26 | |
---|
27 | RETURNS |
---|
28 | <<pread>> returns the number of bytes read or <<-1>> if failure occurred. |
---|
29 | |
---|
30 | PORTABILITY |
---|
31 | <<pread>> is non-ANSI and is specified by the Single Unix Specification. |
---|
32 | |
---|
33 | Supporting OS subroutine required: <<read>>, <<lseek>>. |
---|
34 | */ |
---|
35 | |
---|
36 | #include <_ansi.h> |
---|
37 | #include <unistd.h> |
---|
38 | #include <reent.h> |
---|
39 | |
---|
40 | ssize_t |
---|
41 | _pread_r (struct _reent *rptr, |
---|
42 | int fd, |
---|
43 | void *buf, |
---|
44 | size_t n, |
---|
45 | off_t off) |
---|
46 | { |
---|
47 | off_t cur_pos; |
---|
48 | _READ_WRITE_RETURN_TYPE num_read; |
---|
49 | |
---|
50 | if ((cur_pos = _lseek_r (rptr, fd, 0, SEEK_CUR)) == (off_t)-1) |
---|
51 | return -1; |
---|
52 | |
---|
53 | if (_lseek_r (rptr, fd, off, SEEK_SET) == (off_t)-1) |
---|
54 | return -1; |
---|
55 | |
---|
56 | num_read = _read_r (rptr, fd, buf, n); |
---|
57 | |
---|
58 | if (_lseek_r (rptr, fd, cur_pos, SEEK_SET) == (off_t)-1) |
---|
59 | return -1; |
---|
60 | |
---|
61 | return (ssize_t)num_read; |
---|
62 | } |
---|
63 | |
---|
64 | #ifndef _REENT_ONLY |
---|
65 | |
---|
66 | ssize_t |
---|
67 | pread (int fd, |
---|
68 | void *buf, |
---|
69 | size_t n, |
---|
70 | off_t off) |
---|
71 | { |
---|
72 | return _pread_r (_REENT, fd, buf, n, off); |
---|
73 | } |
---|
74 | |
---|
75 | #endif /* !_REENT_ONLY */ |
---|
76 | #endif /* !_NO_PREAD */ |
---|