Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / unix / pwrite.c
blob60166c9f95876ac47f1a30b9b4b8be2a0f08a54e
1 #ifndef _NO_PWRITE
2 /*
3 FUNCTION
4 <<pwrite>>---write a file from specified position
6 INDEX
7 pwrite
8 INDEX
9 _pwrite_r
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]>);
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>>).
25 The <<_pwrite_r>> function is the same as <<pwrite>>, only a reentrant
26 struct pointer <[rptr]> is provided to preserve reentrancy.
28 RETURNS
29 <<pwrite>> returns the number of bytes written or <<-1>> if failure occurred.
31 PORTABILITY
32 <<pwrite>> is non-ANSI and is specified by the Single Unix Specification.
34 Supporting OS subroutine required: <<write>>, <<lseek>>.
37 #include <_ansi.h>
38 #include <unistd.h>
39 #include <reent.h>
41 ssize_t
42 _pwrite_r (struct _reent *rptr,
43 int fd,
44 const void *buf,
45 size_t n,
46 off_t off)
48 off_t cur_pos;
49 _READ_WRITE_RETURN_TYPE num_written;
51 if ((cur_pos = _lseek_r (rptr, fd, 0, SEEK_CUR)) == (off_t)-1)
52 return -1;
54 if (_lseek_r (rptr, fd, off, SEEK_SET) == (off_t)-1)
55 return -1;
57 num_written = _write_r (rptr, fd, buf, n);
59 if (_lseek_r (rptr, fd, cur_pos, SEEK_SET) == (off_t)-1)
60 return -1;
62 return (ssize_t)num_written;
65 #ifndef _REENT_ONLY
67 ssize_t
68 pwrite (int fd,
69 const void *buf,
70 size_t n,
71 off_t off)
73 return _pwrite_r (_REENT, fd, buf, n, off);
76 #endif /* !_REENT_ONLY */
77 #endif /* !_NO_PWRITE */