summaryrefslogtreecommitdiffstats
path: root/libc-bottom-half/sources/truncate.c
blob: 24ae28f15eb87077ac3d20b3f6e807f54a32d039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);
}