]> git.proxmox.com Git - wasi-libc.git/commitdiff
Implement truncate(2).
authorDan Gohman <sunfish@mozilla.com>
Tue, 14 May 2019 18:49:22 +0000 (11:49 -0700)
committerDan Gohman <sunfish@mozilla.com>
Wed, 15 May 2019 18:53:11 +0000 (11:53 -0700)
This adds a simple implementation of truncate in terms of open and
ftruncate.

expected/wasm32-wasi/defined-symbols.txt
libc-bottom-half/sources/truncate.c [new file with mode: 0644]

index 73f5d77b55f3d0630de7db9fe0ff6f25309e471a..ef518849e49d9e601dd4d15dcd925f288e318a28 100644 (file)
@@ -992,6 +992,7 @@ towlower_l
 towupper
 towupper_l
 trunc
+truncate
 truncf
 truncl
 tsearch
diff --git a/libc-bottom-half/sources/truncate.c b/libc-bottom-half/sources/truncate.c
new file mode 100644 (file)
index 0000000..b56123d
--- /dev/null
@@ -0,0 +1,23 @@
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+int truncate(const char *path, off_t length)
+{
+    int fd = open(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;
+    }
+
+    if (close(fd) < 0)
+        return -1;
+
+    return 0;
+}