master
 1#include <unistd.h>
 2#include <fcntl.h>
 3#include <errno.h>
 4#include <wasi/libc.h>
 5
 6int truncate(const char *path, off_t length)
 7{
 8    int fd = __wasilibc_open_nomode(path, O_WRONLY | O_CLOEXEC | O_NOCTTY);
 9    if (fd < 0)
10        return -1;
11
12    int result = ftruncate(fd, length);
13    if (result != 0) {
14        int save_errno = errno;
15        (void)close(fd);
16        errno = save_errno;
17        return -1;
18    }
19
20    return close(fd);
21}