int
truncate(const char *path, off_t length)
int
ftruncate(int fd, off_t length)
)
causes the file named by
path
or referenced by
fd
to have a size of
length
bytes.
If the file previously was larger than this size, the extra data is discarded.
If it was previously shorter than
length,
its size is increased to the specified value and
the extended area appears as if it were zero-filled.
With
ftruncate(),
the file must be open for writing; for
truncate(),
the process must have write permissions for the file.
)
and
ftruncate()
are:
EISDIR]EROFS]ETXTBSY]EIO]ENOSPC]
Error codes specific to
truncate()
are:
ENOTDIR]ENAMETOOLONG]{NAME_MAX}
characters, or an entire path name exceeded
{PATH_MAX}
characters.
ENOENT]EACCES]ELOOP]EFAULT]path
points outside the process's allocated address space.
Error codes specific to
ftruncate()
are:
EBADF]fd
is not a valid descriptor.
EINVAL]fd
references a socket, not a file, or
the
fd
is not open for writing.
)
to extend a file is an
IEEE Std 1003.1-2004 (``POSIX.1'')
extension, and is thus not portable.
Files can be extended in a portable way seeking (using
lseek(2))
to the required size and writing a single character with
write(2).
)
and
ftruncate()
function calls appeared in
4.2BSD.