source: trunk/libs/newlib/src/newlib/libc/unix/pwrite.c @ 471

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

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

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