truncate(2) — System Calls
OSF
NAME
truncate, ftruncate − Changes file length
SYNOPSIS
#include <sys/types.h> int truncate (
const char ∗path,
off_t length ); int ftruncate (
int filedes,
off_t length );
PARAMETERS
pathSpecifies the name of a file that is opened, truncated, and then closed. The path parameter must point to a pathname which names a regular file for which the calling process has write permission. If the path parameter refers to a symbolic link, the length of the file pointed to by the symbolic link is truncated.
filedesSpecifies the descriptor of a file that must be open for writing.
lengthSpecifies the new length of the file in bytes.
DESCRIPTION
The truncate() and ftruncate() functions change the length of a file to the size in bytes specified by the length parameter. 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. If the new length is greater than the previous length, new file data between the previous End-of-File and the new End-of-File is added, consisting of all zeros.
Full blocks are returned to the file system so that they can be used again, and the file size is changed to the value of the length parameter.
The truncate() and ftruncate() functions have no effect on FIFO special files or directories. These functions do not modify the seek pointer of the file.
Upon successful completion, the truncate() and ftruncate() functions mark the st_ctime and st_mtime fields of the file for update. If the file is a regular file, the ftruncate() and truncate() functions clear the S_ISUID and S_ISGID attributes of the file.
If the file has enforced file locking enabled and there are file locks on the file, the truncate() or ftruncate() function fails.
NOTES
AES Support Level:
Trial use
RETURN VALUES
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 may be set to one of the following values:
[EINVAL]The file is not a regular file.
[EAGAIN]The write operation failed due to an enforced write lock on the file.
[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.
In addition, the truncate() function fails if errors occur that apply to any service requiring pathname resolution, or if one of the following are true:
[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 parameter points to an empty string.
[ENOTDIR]A component of the path prefix is not a directory.
In addition, if the ftruncate() function fails, errno may be set to the following value:
[EBADF]The filedes parameter is not a valid file descriptor open for writing.