Skip to content

Commit 24a5f48

Browse files
committed
lib - a pwrite() implementation (Contributed by Buccapatnam Tirumala, Gautam)
1 parent d78ae27 commit 24a5f48

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

include/unistd.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ _PROTOTYPE( int tcsetpgrp, (int _fd, pid_t _pgrp_id) );
147147
_PROTOTYPE( char *ttyname, (int _fd) );
148148
_PROTOTYPE( int unlink, (const char *_path) );
149149
_PROTOTYPE( ssize_t write, (int _fd, const void *_buf, size_t _n) );
150+
_PROTOTYPE( ssize_t pwrite, (int _fd, const void *_buf, size_t _n, off_t _offset));
150151
_PROTOTYPE( int truncate, (const char *_path, off_t _length) );
151152
_PROTOTYPE( int ftruncate, (int _fd, off_t _length) );
152153
_PROTOTYPE( int nice, (int _incr) );

lib/libc/posix/Makefile.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,5 @@ SRCS+= \
107107
nice.c \
108108
priority.c \
109109
pread.c \
110+
pwrite.c \
110111
usleep.c

lib/libc/posix/pwrite.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <lib.h>
2+
#include <unistd.h>
3+
4+
ssize_t pwrite(int fd, const void *buffer, size_t nbytes, off_t where)
5+
{
6+
off_t here;
7+
ssize_t w;
8+
9+
if((here = lseek(fd, 0, SEEK_CUR)) < 0)
10+
return -1;
11+
12+
if(lseek(fd, where, SEEK_SET) < 0)
13+
return -1;
14+
15+
if((w=write(fd, buffer, nbytes)) < 0) {
16+
int e = errno;
17+
lseek(fd, here, SEEK_SET);
18+
errno = e;
19+
return -1;
20+
}
21+
22+
if(lseek(fd, here, SEEK_SET) < 0)
23+
return -1;
24+
25+
return w;
26+
}

0 commit comments

Comments
 (0)