Changes the length of regular files.
Standard C Library (libc.a)
#include <unistd.h>
int truncate ( Path, Length)
const char *Path;
off_t Length;
int ftruncate ( FileDescriptor, Length)
int FileDescriptor;
off_t Length;
The truncate and ftruncate subroutines change the length of regular files.
The Path parameter must point to a regular file for which the calling process has write permission. The Length parameter specifies the desired length of the new file in bytes.
The Length parameter measures the specified file in bytes from the beginning of the file. If the new length is less than the previous length, all data between the new length and the previous end of file is removed. If the new length in the specified file is greater than the previous length, data between the old and new lengths is read as 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.
If the file designated in the Path parameter names a symbolic link, the link will be traversed and path-name resolution will continue.
These subroutines do not modify the seek pointer of the file.
These subroutines cannot be applied to a file that a process has open with the O_DEFER flag.
Successful completion of the truncate or ftruncate subroutine updates the st_ctime and st_mtime fields of the file. Successful completion also clears the SetUserID bit (S_ISUID) of the file if any of the following are true:
These subroutines also clear the SetGroupID bit (S_ISGID) if:
truncate and ftruncate can be used to specify any size up to OFF_MAX. truncate64 and ftruncate64 can be used to specify any length up to the maximum file size for the file.
In the large file enabled programming environment, truncate is redefined to be truncate64 and ftruncate is redefined to be ftruncate64.
Upon successful completion, a value of 0 is returned. If the truncate or ftruncate subroutine is unsuccessful, a value of -1 is returned and the errno global variable is set to indicate the nature of the error.
The truncate and ftruncate subroutines fail if the following is true:
EROFS | An attempt was made to truncate a file that resides on a read-only file system. |
The truncate and ftruncate subroutines fail if one of the following is true:
The ftruncate subroutine fails if the following is true:
EBADF | The FileDescriptor parameter is not a valid file descriptor open for writing. |
EINVAL | The FileDescriptor argument references a file that was opened without write permission. |
The truncate function will fail if:
EACCES | A component of the path prefix denies search permission, or write permission is denied on the file. | |
EISDIR | The named file is a directory. | |
ELOOP | Too many symbolic links were encountered in resolving path. | |
ENAMETOOLONG | The length of the specified pathname exceeds PATH_MAX bytes, or the length of a component of the pathname exceeds NAME_MAX bytes. | |
ENOENT | A component of path does not name an existing file or path is an empty string. | |
ENTDIR | A component of the path prefix of path is not a directory. | |
EROFS | The named file resides on a read-only file system. |
The truncate function may fail if:
ENAMETOOLONG | Pathname resolution of a symbolic link produced an intermediate result whose length exceeds PATH_MAX. |
The fclear subroutine, openx, open, or creat subroutine.
Appendix A. Base Operating System Error Codes for Services That Require Path-Name Resolution.
Files, Directories, and File Systems for Programmers in AIX 5L Version 5.2 General Programming Concepts: Writing and Debugging Programs.