TRUNCATE(2)

NAME
truncate, ftruncate - truncate a file to a specified length

SYNOPSIS
#include <sys/types.h>
truncate(path, length)
char *path;
off_t length;

ftruncate(fd, length)
int fd;
off_t length;

DESCRIPTION
Truncate causes the file named by path or referenced by fd to have a size equal to length bytes. If the file previously was larger than this size, the extra data is lost. If it was shorter, bytes between the old and new lengths are read as zeroes. With truncate, the file must be writable by user and with ftruncate, the file must be open for writing.

RETURN VALUES
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:

[EPERM]
    The pathname contains a character with the high-order bit set.

[ENOENT]
    The pathname was too long.

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

[ENOENT]
    The named file does not exist.

[EACCES]
    A component of the path prefix denies search permission.

[EISDIR]
    The named file is a directory.

[EROFS]
    The named file resides on a read-only file system.

[ETXTBSY]
    The file is a pure procedure (shared text) file that is being executed.

[EFAULT]
    Path points outside the process's allocated address space.

[EINVAL]
    The length is negative.

Ftruncate succeeds unless:

[EBADF]
    The fd is not a valid descriptor.

[EINVAL]
    The fd references a socket, not a file.

[EINVAL]
    The length is negative.

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

SEE ALSO
open(2)

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