]> Zhao Yanbai Git Server - minix.git/commitdiff
lib - a pwrite() implementation (Contributed by Buccapatnam Tirumala, Gautam)
authorBen Gras <ben@minix3.org>
Tue, 22 Jun 2010 22:04:43 +0000 (22:04 +0000)
committerBen Gras <ben@minix3.org>
Tue, 22 Jun 2010 22:04:43 +0000 (22:04 +0000)
include/unistd.h
lib/libc/posix/Makefile.inc
lib/libc/posix/pwrite.c [new file with mode: 0644]

index df28fff5789d18c09809dbd0a1fbe5e9d0ed889d..a6405f2eeaaa1100e78b7486f465703248ebfc81 100644 (file)
@@ -147,6 +147,7 @@ _PROTOTYPE( int tcsetpgrp, (int _fd, pid_t _pgrp_id)                        );
 _PROTOTYPE( char *ttyname, (int _fd)                                   );
 _PROTOTYPE( int unlink, (const char *_path)                            );
 _PROTOTYPE( ssize_t write, (int _fd, const void *_buf, size_t _n)      );
+_PROTOTYPE( ssize_t pwrite, (int _fd, const void *_buf, size_t _n, off_t _offset));
 _PROTOTYPE( int truncate, (const char *_path, off_t _length)           );
 _PROTOTYPE( int ftruncate, (int _fd, off_t _length)                    );
 _PROTOTYPE( int nice, (int _incr)                                      );
index 7c6833c25a9b0b44e2820002b3a7c658941c7355..7b40f7877445e6fbff8c4966e226a32f3626f21d 100644 (file)
@@ -107,4 +107,5 @@ SRCS+=       \
        nice.c \
        priority.c \
        pread.c \
+       pwrite.c \
        usleep.c
diff --git a/lib/libc/posix/pwrite.c b/lib/libc/posix/pwrite.c
new file mode 100644 (file)
index 0000000..7cb3f52
--- /dev/null
@@ -0,0 +1,26 @@
+#include <lib.h>
+#include <unistd.h>
+
+ssize_t pwrite(int fd, const void *buffer, size_t nbytes, off_t where)
+{
+       off_t here;
+       ssize_t w;
+
+       if((here = lseek(fd, 0, SEEK_CUR)) < 0)
+               return -1;
+
+       if(lseek(fd, where, SEEK_SET) < 0)
+               return -1;
+
+       if((w=write(fd, buffer, nbytes)) < 0) {
+               int e = errno;
+               lseek(fd, here, SEEK_SET);
+               errno = e;
+               return -1;
+       }
+
+       if(lseek(fd, here, SEEK_SET) < 0)
+               return -1;
+
+       return w;
+}