TRUNCATE(2)

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 VALUES

truncate() returns:

0   on success.

-1  on failure and sets errno to indicate the error.

ERRORS

truncate() may set errno to:

EACCES
    Search permission is denied for a component of the path prefix of path.

    Write permission is denied for the file referred to by path.

EFAULT
    path points outside the process's allocated address space.

EIO An I/O error occurred while reading from or writing to the file system.

EISDIR
    The file referred to by path is a directory.

ELOOP
    Too many symbolic links were encountered in translating path.

ENAMETOOLONG
    The length of the path argument exceeds {PATH_MAX}.

    A pathname component is longer than {NAME_MAX} (see sysconf(2V)) while {_POSIX_NO_TRUNC} is in effect (see pathconf(2V)).

ENOENT
    The file referred to by path does not exist.

ENOTDIR
    A component of the path prefix of path is not a directory.

EROFS
    The file referred to by path resides on a read-only file system.

ftruncate() may set errno to:

EINVAL
    fd is not a valid descriptor of a file open for writing.

    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
open(2V)

BUGS

These calls should be generalized to allow ranges of bytes in a file to be discarded.