diff options
author | Brian Gianforcaro <b.gianfo@gmail.com> | 2021-04-21 02:11:04 -0700 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-04-21 13:13:23 +0200 |
commit | 14f6425b8fd8f54942a79b121467f9605db8da50 (patch) | |
tree | 7aad9554482f9539aec09906195e757fb56fa8f3 /Userland/Libraries | |
parent | 56ba3e1cbdc971acd5d96b1dccbe22287e4e83a6 (diff) | |
download | serenity-14f6425b8fd8f54942a79b121467f9605db8da50.zip |
LibC: Add pwrite(..) implementation to match the existing pread(..)
Add a basic non-thread safe implementation of pwrite, following the
existing pread(..) design.
This is needed for https://fio.readthedocs.io
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibC/unistd.cpp | 30 | ||||
-rw-r--r-- | Userland/Libraries/LibC/unistd.h | 1 |
2 files changed, 21 insertions, 10 deletions
diff --git a/Userland/Libraries/LibC/unistd.cpp b/Userland/Libraries/LibC/unistd.cpp index 727dcfc7aa..4ba8672cfd 100644 --- a/Userland/Libraries/LibC/unistd.cpp +++ b/Userland/Libraries/LibC/unistd.cpp @@ -289,12 +289,32 @@ ssize_t read(int fd, void* buf, size_t count) __RETURN_WITH_ERRNO(rc, rc, -1); } +ssize_t pread(int fd, void* buf, size_t count, off_t offset) +{ + // FIXME: This is not thread safe and should be implemented in the kernel instead. + off_t old_offset = lseek(fd, 0, SEEK_CUR); + lseek(fd, offset, SEEK_SET); + ssize_t nread = read(fd, buf, count); + lseek(fd, old_offset, SEEK_SET); + return nread; +} + ssize_t write(int fd, const void* buf, size_t count) { int rc = syscall(SC_write, fd, buf, count); __RETURN_WITH_ERRNO(rc, rc, -1); } +ssize_t pwrite(int fd, const void* buf, size_t count, off_t offset) +{ + // FIXME: This is not thread safe and should be implemented in the kernel instead. + off_t old_offset = lseek(fd, 0, SEEK_CUR); + lseek(fd, offset, SEEK_SET); + ssize_t nwritten = write(fd, buf, count); + lseek(fd, old_offset, SEEK_SET); + return nwritten; +} + int ttyname_r(int fd, char* buffer, size_t size) { int rc = syscall(SC_ttyname, fd, buffer, size); @@ -769,16 +789,6 @@ int unveil(const char* path, const char* permissions) __RETURN_WITH_ERRNO(rc, rc, -1); } -ssize_t pread(int fd, void* buf, size_t count, off_t offset) -{ - // FIXME: This is not thread safe and should be implemented in the kernel instead. - off_t old_offset = lseek(fd, 0, SEEK_CUR); - lseek(fd, offset, SEEK_SET); - ssize_t nread = read(fd, buf, count); - lseek(fd, old_offset, SEEK_SET); - return nread; -} - char* getpass(const char* prompt) { dbgln("FIXME: getpass('{}')", prompt); diff --git a/Userland/Libraries/LibC/unistd.h b/Userland/Libraries/LibC/unistd.h index 61cb577c4b..82d9193945 100644 --- a/Userland/Libraries/LibC/unistd.h +++ b/Userland/Libraries/LibC/unistd.h @@ -101,6 +101,7 @@ int tcsetpgrp(int fd, pid_t pgid); ssize_t read(int fd, void* buf, size_t count); ssize_t pread(int fd, void* buf, size_t count, off_t); ssize_t write(int fd, const void* buf, size_t count); +ssize_t pwrite(int fd, const void* buf, size_t count, off_t); int close(int fd); int chdir(const char* path); int fchdir(int fd); |