summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2020-06-15 11:28:42 -0400
committerAndreas Kling <kling@serenityos.org>2020-06-15 17:34:52 +0200
commit9825f7792b412f8067797fa19fd9e83a18d26682 (patch)
treecc4b1d88d0965bed676c4e5df899d55351a4d553
parent246e0e47ec731fdb7c5b930f1c2c55af3dfa84bc (diff)
downloadserenity-9825f7792b412f8067797fa19fd9e83a18d26682.zip
LibC: Add truncate().
Implemented in user space for now.
-rw-r--r--Libraries/LibC/unistd.cpp13
-rw-r--r--Libraries/LibC/unistd.h1
2 files changed, 14 insertions, 0 deletions
diff --git a/Libraries/LibC/unistd.cpp b/Libraries/LibC/unistd.cpp
index ec01ad1be4..75b0458639 100644
--- a/Libraries/LibC/unistd.cpp
+++ b/Libraries/LibC/unistd.cpp
@@ -553,6 +553,19 @@ int ftruncate(int fd, off_t length)
__RETURN_WITH_ERRNO(rc, rc, -1);
}
+int truncate(const char* path, off_t length)
+{
+ int fd = open(path, O_RDWR | O_CREAT, 0666);
+ if (fd < 0)
+ return fd;
+ int rc = ftruncate(fd, length);
+ int saved_errno = errno;
+ if (int close_rc = close(fd); close_rc < 0)
+ return close_rc;
+ errno = saved_errno;
+ return rc;
+}
+
int gettid()
{
if (!s_cached_tid)
diff --git a/Libraries/LibC/unistd.h b/Libraries/LibC/unistd.h
index 86dd1c529f..1dfb4801ef 100644
--- a/Libraries/LibC/unistd.h
+++ b/Libraries/LibC/unistd.h
@@ -127,6 +127,7 @@ char* getlogin();
int chown(const char* pathname, uid_t, gid_t);
int fchown(int fd, uid_t, gid_t);
int ftruncate(int fd, off_t length);
+int truncate(const char* path, off_t length);
int halt();
int reboot();
int mount(int source_fd, const char* target, const char* fs_type, int flags);