TRUNCATE(2) SysV TRUNCATE(2)
NAME
truncate, ftruncate - Change file length
SYNOPSIS
#include <sys/types.h>
int truncate(path, length)
const char *path;
off_t length;
int ftruncate(fd, length)
int fildes;
off_t length;
DESCRIPTION
The truncate and ftruncate functions change the length of a file to the
size in bytes specified by the length argument. If the new length is less
than the previous length, the truncate and ftruncate functions remove all
data beyond length bytes from the specified file. All file data between
the new End-of-File and the previous End-of-File is discarded.
A file's length can only be increased by the ftruncate and truncate calls
in an AES environment.
The path argument specifies the name of a file that is opened, truncated,
and then closed. path must point to a pathname which names a regular file
for which the calling process has write permission. If the path argument
refers to a symbolic link, the length of the file pointed to by the
symbolic link will be truncated.
filedes specifies the descriptor of a file that must be open for writing.
These functions do not modify the seek pointer of the file.
length specifies the new length of the file in bytes.
truncate and ftruncate on a FIFO fail with [EBADF]. On a directory,
truncate fails with [EISDIR]. ftruncate on a directory requires that the
directory be open for writing; such an open fails since Domain/OS does
not allow writing to directories.
Upon successful completion, the truncate and ftruncate functions mark the
st_ctime and st_mtime fields of the file for update.
DIAGNOSTICS
Upon successful completion, a value of 0 (zero) is returned. If the
truncate or ftruncate function fails, it returns a value of -1, and errno
is set to indicate the error.
ERRORS
If the truncate or ftruncate function fails, errno is set to one of the
following values:
[EACCES] Write access permission to the file was denied.
[EFBIG] The new file size would exceed the process' file size limit or
the maximum file size.
[EROFS] The file resides on a read-only file system.
[EAGAIN] The file has enforced mode file locking enabled and there are
file locks on the file.
[ENAMETOOLONG]
The size of the pathname exceeds PATH_MAX or a pathname
component is longer than NAME_MAX.
[ENOENT] A component of the specified pathname does not exist, or the
path argument points to an empty string.
[ENOTDIR] A component of the path prefix is not a directory.
[ELOOP] Too many symbolic links were encountered in translating the
pathname.
[ETXTBSY] The file is a pure procedure (shared text) file that is being
executed.
[EIO] An I/O error occurred updating the inode.
[EFAULT] path points outside the process' allocated address space.
In addition, if the ftruncate function fails, errno is set to the
following value:
[EBADF] The filedes argument is not a valid file descriptor open for
writing.
[EINVAL] The fd references a socket, not a file.
[EINVAL] The fd is not open for writing.
SEE ALSO
chmod(2), fcntl(2), open(2)