truncate(3C)
NAME
truncate, ftruncate − set a file to a specified length
SYNOPSIS
#include <unistd.h>
int truncate(const char ∗path, off_t length);
int ftruncate(int fildes, off_t length);
MT-LEVEL
MT-Safe
DESCRIPTION
The file whose name is given by path or referenced by the descriptor fildes has its size set to length bytes.
If the file was previously longer than length, bytes past length will no longer be accessible. If it was shorter, bytes from the EOF before the call to the EOF after the call will be read in as zeros. The effective user ID of the process must have write permission for the file, and for ftruncate() the file must be open for writing.
RETURN VALUES
Upon successful completion, a value of 0 is returned. Otherwise, a value of −1 is returned and errno is set to indicate the error.
ERRORS
truncate() fails if one or more of the following are true:
EACCES Search permission is denied on a component of the path prefix.
EACCES Write permission is denied for the file referred to by path.
EFAULT path points outside the process’s allocated address space.
EINTR A signal was caught during execution of the truncate routine.
EINVAL path is not an ordinary file.
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.
EMFILE The maximum number of file descriptors available to the process has been reached.
EMULTIHOP Components of path require hopping to multiple remote machines and file system type does not allow it.
ENAMETOOLONG The length of a path component exceeds {NAME_MAX} characters, or the length of path exceeds {PATH_MAX} characters.
ENFILE Could not allocate any more space for the system file table.
ENOENT Either a component of the path prefix or the file referred to by path does not exist.
ENOLINK path points to a remote machine and the link to that machine is no longer active.
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() fails if one or more of the following are true:
EAGAIN The file exists, mandatory file/record locking is set, and there are outstanding record locks on the file (see chmod(2)).
EBADF fildes is not a file descriptor open for writing.
EINTR A signal was caught during execution of the ftruncate routine.
EIO An I/O error occurred while reading from or writing to the file system.
ENOLINK fildes points to a remote machine and the link to that machine is no longer active.
EINVAL fildes does not correspond to an ordinary file.
SEE ALSO
SunOS 5.5.1 — Last change: 22 Jan 1993