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