OSDN Git Service

c6273f3c9e582d4e087538e2cdf4c575d8a9a9fc
[pf3gnuchains/sourceware.git] / newlib / libc / sys / linux / pread64.c
1 /*
2 FUNCTION
3 <<pread64>>---read a large file from specified position
4
5 INDEX
6         pread64
7
8 ANSI_SYNOPSIS
9         #include <unistd.h>
10         ssize_t pread64(int <[fd]>, void *<[buf]>, size_t <[n]>, loff_t <[off]>);
11
12 TRAD_SYNOPSIS
13         #include <unistd.h>
14         ssize_t pread64(<[fd]>, <[buf]>, <[n]>, <[off]>)
15         int <[fd]>;
16         void *<[buf]>;
17         size_t <[n]>;
18         loff_t <[off]>;
19
20 DESCRIPTION
21 The <<pread64>> function is similar to <<pread>>.  The only difference is
22 that it operates on large files and so takes a 64-bit offset.  Like <<pread>>>,
23 the file position is unchanged by the function (i.e. the file position
24 is the same before and after a call to <<pread>>).
25
26 RETURNS
27 <<pread64>> returns the number of bytes read or <<-1>> if failure occurred.
28
29 PORTABILITY
30 <<pread64>> is an EL/IX extension.
31
32 Supporting OS subroutine required: <<read>>, <<lseek64>>.
33 */
34
35 #include <_ansi.h>
36 #include <unistd.h>
37 #include <reent.h>
38
39 ssize_t
40 _DEFUN (pread64, (fd, buf, n, off),
41      int fd _AND
42      _PTR buf _AND
43      size_t n _AND
44      loff_t off)
45 {
46   loff_t cur_pos;
47   _READ_WRITE_RETURN_TYPE num_read;
48   
49   if ((cur_pos = lseek64 (fd, 0, SEEK_CUR)) == (loff_t)-1)
50     return -1;
51
52   if (lseek64 (fd, off, SEEK_SET) == (loff_t)-1)
53     return -1;
54
55   num_read = read (fd, buf, n);
56
57   if (lseek64 (fd, cur_pos, SEEK_SET) == (loff_t)-1)
58     return -1;
59
60   return (ssize_t)num_read;
61 }
62