wasi-libc/libc-bottom-half/sources/truncate.c
Dan Gohman 6cddc9250b Implement truncate(2).
This adds a simple implementation of truncate in terms of open and
ftruncate.
2019-05-15 11:53:11 -07:00

24 lines
432 B
C

#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;
}