summaryrefslogtreecommitdiffstats
path: root/libc-bottom-half/sources/truncate.c
diff options
context:
space:
mode:
Diffstat (limited to 'libc-bottom-half/sources/truncate.c')
-rw-r--r--libc-bottom-half/sources/truncate.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/libc-bottom-half/sources/truncate.c b/libc-bottom-half/sources/truncate.c
new file mode 100644
index 0000000..24ae28f
--- /dev/null
+++ b/libc-bottom-half/sources/truncate.c
@@ -0,0 +1,21 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <wasi/libc.h>
+
+int truncate(const char *path, off_t length)
+{
+ int fd = __wasilibc_open_nomode(path, O_WRONLY | O_CLOEXEC | O_NOCTTY);
+ if (fd < 0)
+ return -1;
+
+ int result = ftruncate(fd, length);
+ if (result != 0) {
+ int save_errno = errno;
+ (void)close(fd);
+ errno = save_errno;
+ return -1;
+ }
+
+ return close(fd);
+}