TRUNCATE(2) — SYSTEM CALLS
NAME
truncate, ftruncate − set a file to a specified length
SYNOPSIS
#include <sys/types.h>
int truncate(path, length)
char ∗path;
off_t length;
int ftruncate(fd, length)
int fd;
off_t length;
DESCRIPTION
truncate() causes the file referred to by path (or for ftruncate() the object referred to by fd) to have a size equal to length bytes. If the file was previously longer than length, the extra bytes are removed from the file. If it was shorter, bytes between the old and new lengths are read as zeroes. With ftruncate(), the file must be open for writing.
RETURN VALUE
A value of 0 is returned if the call succeeds. If the call fails a −1 is returned, and the global variable errno specifies the error.
ERRORS
truncate() succeeds unless:
ENOTDIR A component of the path prefix of path is not a directory.
ENAMETOOLONG The length of a component of path exceeds 255 characters, or the length of path exceeds 1023 characters.
ENOENT The file referred to by path does not exist.
EACCES Search permission is denied for a component of the path prefix of path.
EACCES Write permission is denied for the file referred to by path.
ELOOP Too many symbolic links were encountered in translating path.
EISDIR The file referred to by path is a directory.
EROFS The file referred to by path resides on a read-only file system.
EIO An I/O error occurred while reading from or writing to the file system.
EFAULT path points outside the process’s allocated address space.
ftruncate() succeeds unless:
EINVAL fd is not a valid descriptor of a file open for writing.
EINVAL fd refers to a socket, not to a file.
EIO An I/O error occurred while reading from or writing to the file system.
SEE ALSO
BUGS
These calls should be generalized to allow ranges of bytes in a file to be discarded.
Sun Release 4.0 — Last change: 22 March 1989