OS/161 Reference Manual


ftruncate - set size of a file


Standard C Library (libc, -lc)


#include <unistd.h>

ftruncate(int fd, off_t filesize);


ftruncate forcibly sets the size of the file referred to by fd to filesize. If this expands the file, the new data appears as if it is zero-filled. (On file systems that support sparse files, the new space does not need to be physically allocated.) If the action shrinks the file, the excess data is discarded.

The file must be open for write.

ftruncate must be atomic. For recoverable file systems, this includes after crashing and running recovery.

Return Values

On success, ftruncate returns 0. On error, -1 is returned, and errno is set according to the error encountered.


The following error codes should be returned under the conditions given. Other error codes may be returned for other cases not mentioned here.
  EBADF fd is not a valid file handle, or it is not open for writing.
EIO A hard I/O error occurred.
EFAULT buf points to an invalid address.