source: trunk/libs/newlib/src/newlib/libc/unix/pread.c @ 450

Last change on this file since 450 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 1.7 KB
Line 
1#ifndef _NO_PREAD
2/*
3FUNCTION
4<<pread>>---read a file from specified position
5
6INDEX
7        pread
8INDEX
9        _pread_r
10
11SYNOPSIS
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
17DESCRIPTION
18The <<pread>> function is similar to <<read>>.  One difference is that
19<<pread>> has an additional parameter <[off]> which is the offset to
20position in the file before reading.  The function also differs in that
21the file position is unchanged by the function (i.e. the file position
22is the same before and after a call to <<pread>>).
23
24The <<_pread_r>> function is the same as <<pread>>, only a reentrant
25struct pointer <[rptr]> is provided to preserve reentrancy.
26
27RETURNS
28<<pread>> returns the number of bytes read or <<-1>> if failure occurred.
29
30PORTABILITY
31<<pread>> is non-ANSI and is specified by the Single Unix Specification.
32
33Supporting OS subroutine required: <<read>>, <<lseek>>.
34*/
35
36#include <_ansi.h>
37#include <unistd.h>
38#include <reent.h>
39
40ssize_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
66ssize_t
67pread (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  */
Note: See TracBrowser for help on using the repository browser.