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