From 9825f7792b412f8067797fa19fd9e83a18d26682 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 15 Jun 2020 11:28:42 -0400 Subject: [PATCH] LibC: Add truncate(). Implemented in user space for now. --- Libraries/LibC/unistd.cpp | 13 +++++++++++++ Libraries/LibC/unistd.h | 1 + 2 files changed, 14 insertions(+) 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);