diff --git a/include/zephyr/posix/unistd.h b/include/zephyr/posix/unistd.h index d5782200275..990211730ea 100644 --- a/include/zephyr/posix/unistd.h +++ b/include/zephyr/posix/unistd.h @@ -238,6 +238,7 @@ ssize_t write(int file, const void *buffer, size_t count); ssize_t read(int file, void *buffer, size_t count); off_t lseek(int file, off_t offset, int whence); int fsync(int fd); +int ftruncate(int fd, off_t length); /* File System related operations */ int rename(const char *old, const char *newp); diff --git a/lib/posix/options/fs.c b/lib/posix/options/fs.c index d7203d150e9..8285ef1648b 100644 --- a/lib/posix/options/fs.c +++ b/lib/posix/options/fs.c @@ -416,3 +416,18 @@ int mkdir(const char *path, mode_t mode) return 0; } + +/** + * @brief Truncate file to specified length. + * + */ +int ftruncate(int fd, off_t length) +{ + struct posix_fs_desc *ptr = NULL; + + ptr = z_get_fd_obj(fd, NULL, EBADF); + if (!ptr) + return -1; + + return fs_truncate(&ptr->file, length); +}