From 6cddc9250b22501e788f7f49c07cddb2698d12dc Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 14 May 2019 11:49:22 -0700 Subject: [PATCH] Implement truncate(2). This adds a simple implementation of truncate in terms of open and ftruncate. --- expected/wasm32-wasi/defined-symbols.txt | 1 + libc-bottom-half/sources/truncate.c | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 libc-bottom-half/sources/truncate.c diff --git a/expected/wasm32-wasi/defined-symbols.txt b/expected/wasm32-wasi/defined-symbols.txt index 73f5d77..ef51884 100644 --- a/expected/wasm32-wasi/defined-symbols.txt +++ b/expected/wasm32-wasi/defined-symbols.txt @@ -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 index 0000000..b56123d --- /dev/null +++ b/libc-bottom-half/sources/truncate.c @@ -0,0 +1,23 @@ +#include +#include +#include + +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; +} -- 2.39.2