Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / unix / pread.c
blob61daac3b473473ba46610d81cea0f109073e23e9
1 #ifndef _NO_PREAD
2 /*
3 FUNCTION
4 <<pread>>---read a file from specified position
6 INDEX
7 pread
8 INDEX
9 _pread_r
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]>);
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>>).
24 The <<_pread_r>> function is the same as <<pread>>, only a reentrant
25 struct pointer <[rptr]> is provided to preserve reentrancy.
27 RETURNS
28 <<pread>> returns the number of bytes read or <<-1>> if failure occurred.
30 PORTABILITY
31 <<pread>> is non-ANSI and is specified by the Single Unix Specification.
33 Supporting OS subroutine required: <<read>>, <<lseek>>.
36 #include <_ansi.h>
37 #include <unistd.h>
38 #include <reent.h>
40 ssize_t
41 _pread_r (struct _reent *rptr,
42 int fd,
43 void *buf,
44 size_t n,
45 off_t off)
47 off_t cur_pos;
48 _READ_WRITE_RETURN_TYPE num_read;
50 if ((cur_pos = _lseek_r (rptr, fd, 0, SEEK_CUR)) == (off_t)-1)
51 return -1;
53 if (_lseek_r (rptr, fd, off, SEEK_SET) == (off_t)-1)
54 return -1;
56 num_read = _read_r (rptr, fd, buf, n);
58 if (_lseek_r (rptr, fd, cur_pos, SEEK_SET) == (off_t)-1)
59 return -1;
61 return (ssize_t)num_read;
64 #ifndef _REENT_ONLY
66 ssize_t
67 pread (int fd,
68 void *buf,
69 size_t n,
70 off_t off)
72 return _pread_r (_REENT, fd, buf, n, off);
75 #endif /* !_REENT_ONLY */
76 #endif /* !_NO_PREAD */