DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 

truncate(SLIB)


truncate, ftruncate -- set a file to a specified length

Syntax

cc ... -lsocket

#include <sys/types.h>

int truncate(path, length) char *path; u_long length;

int ftruncate(fd, length) int fd; u_long length;

Description

truncate causes the file referred to by path (or for ftruncate the object referred to by fd) to have a size equal to length bytes. If the file was previously longer than length, the extra bytes are removed from the file. If it was shorter, bytes between the old and new lengths are read as zeroes. With ftruncate the file must be open for writing.

Return values

truncate and ftruncate return:

0
on success

-1
on failure and set errno to indicate the error

truncate may set errno to:


[EACCES]
Search permission is denied for a component of the path prefix of path.

Write permission is denied for the file referred to by path.


[EFAULT]
path points outside the process's allocated address space.

[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.

[ENOENT]
The file referred to by path does not exist.

[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 may set errno to:


[EINVAL]
fd is not a valid descriptor of a file open for writing.

fd refers to a socket, not to a file.


[EIO]
An I/O error occurred while reading from or writing to the file system.

Limitations

The truncation emulation uses the F_FREESPC fcntl. As such, files can only be made shorter.

See also

fcntl(S), open(S)
© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003