truncate(3C) truncate(3C)
NAME
truncate, ftruncate, truncate64, ftruncate64 - set a file to a speci-
fied length
SYNOPSIS
#include <unistd.h>
int truncate(const char *path, offt length);
int ftruncate(int fildes, offt length);
int truncate64(const char *path, off64t length);
int ftruncate64(int fildes, off64t length);
DESCRIPTION
The ftruncate() function causes the regular file referenced by fildes
to have a size of length bytes.
The truncate() function causes the regular file named by path to have
a size of length bytes.
The effect of ftruncate() and truncate() on other types of files is
unspecified. If the file previously was larger than length, the extra
data is lost. If it was previously shorter than length, bytes between
the old and new lengths are read as zeroes. With ftruncate(), the file
must be open for writing; for truncate(), the process must have write
permission for the file.
If the request would cause the file size to exceed the soft file size
limit for the process, the request will fail and the implementation
will generate the SIGXFSZ signal for the process.
These functions do not modify the file offset for any open file
descriptions associated with the file. On successful completion, if
the file size is changed, these functions will mark for update the
stctime and stmtime fields of the file, and if the file is a regular
file, the SISUID and SISGID bits of the file mode may be cleared.
These functions are used only on pre-existing files and so do not have
the potential programming hazard as does creat().
When ftruncate() is used to increase the size of a file, the semantics
are similar to a write() of zeroes to the file. For consistency with
write(), the ftruncate() function will fail when the request is beyond
the offset maximum (even if the effect of the request would be to
shorten the file).
There is no functional difference between truncate()/ftruncate() and
truncate64()/ftruncate64(), except for the interpretation of off64t
[see lfs(5)].
Page 1 Reliant UNIX 5.44 Printed 11/98
truncate(3C) truncate(3C)
ERRORS
The following error code descriptions are function-specific. You will
find a general description in introprm2(2) or in errno(5).
ftruncate() and truncate() fail if one or more of the following are
true:
EINTR A signal was caught during the execution of truncate().
EINVAL The length argument was less than 0.
EIO An I/O error occurred while reading from or writing to
the file system.
EFBIG or EINVAL
The length argument was greater than the maximum file
size.
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' allocated address
space.
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 NAMEMAX charac-
ters, or the length of path exceeds PATHMAX 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.
Page 2 Reliant UNIX 5.44 Printed 11/98
truncate(3C) truncate(3C)
ENOTDIR A component of the path prefix of path is not a direc-
tory.
EROFS The file referred to by path resides on a read-only file
system.
ETXTBSY The file referred to by path is a pure procedure (shared
text) file that is being executed.
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 or EINVAL
fildes is not a file descriptor open for writing.
EFBIG The file is a regular file and length is greater than
the offset maximum established in the open file descrip-
tion associated with fildes.
ENOLINK fildes points to a remote machine and the link to that
machine is no longer active.
EINVAL The fildes argument references a file that was opened
without write permission.
The truncate() function may fail if:
ENAMETOOLONG Pathname resolution of a symbolic link produced an
intermediate result whose length exceeds PATHMAX.
RESULT
Upon successful completion, a value of 0 is returned. Otherwise, a
value of -1 is returned and errno is set to indicate the error.
SEE ALSO
fcntl(2), open(2), unistd(4), lfs(5).
Page 3 Reliant UNIX 5.44 Printed 11/98