ftruncate(2) truncate or extend a file to a specified length

Other Alias

truncate

LIBRARY

Lb libc

SYNOPSIS

In unistd.h Ft int Fn truncate const char *path off_t length Ft int Fn ftruncate int fd off_t length

DESCRIPTION

The Fn truncate system call causes the file named by Fa path or referenced by Fa fd to be truncated or extended to Fa length bytes in size. If the file was larger than this size, the extra data is lost. If the file was smaller than this size, it will be extended as if by writing bytes with the value zero. With Fn ftruncate , the file must be open for writing.

RETURN VALUES

Rv -std If the file to be modified is not a directory or a regular file, the Fn truncate call has no effect and returns the value 0.

ERRORS

The Fn truncate system call succeeds unless:

Bq Er ENOTDIR
A component of the path prefix is not a directory.
Bq Er ENAMETOOLONG
A component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters.
Bq Er ENOENT
The named file does not exist.
Bq Er EACCES
Search permission is denied for a component of the path prefix.
Bq Er EACCES
The named file is not writable by the user.
Bq Er ELOOP
Too many symbolic links were encountered in translating the pathname.
Bq Er EPERM
The named file has its immutable or append-only flag set, see the chflags(2) manual page for more information.
Bq Er EISDIR
The named file is a directory.
Bq Er EROFS
The named file resides on a read-only file system.
Bq Er ETXTBSY
The file is a pure procedure (shared text) file that is being executed.
Bq Er EFBIG
The Fa length argument was greater than the maximum file size.
Bq Er EINVAL
The Fa length argument was less than 0.
Bq Er EIO
An I/O error occurred updating the inode.
Bq Er EFAULT
The Fa path argument points outside the process's allocated address space.

The Fn ftruncate system call succeeds unless:

Bq Er EBADF
The Fa fd argument is not a valid descriptor.
Bq Er EINVAL
The Fa fd argument references a socket, not a file.
Bq Er EINVAL
The Fa fd descriptor is not open for writing.

HISTORY

The Fn truncate system call appeared in BSD 4.2

BUGS

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

Use of Fn truncate to extend a file is not portable.