truncate(S)
truncate, ftruncate --
set a file to a specified length
Syntax
cc . . . -lc
#include <unistd.h>
int truncate (const char *path, off_t length);
int ftruncate (int fildes, off_t length);
Description
truncate-
set a file to a specified length
ftruncate-
set a file to a specified length using a file descriptor
The file whose name is given by path or referenced by the
file descriptor fildes has its size set to
length bytes.
If the file was previously longer than length, bytes past
length will no longer be accessible. If it was shorter,
bytes from the EOF before the call to the EOF
after the call will be read in as zeros. The effective user
ID of the process must have write permission for the file,
and for ftruncate the file must be open for writing.
Return values
Upon successful completion, a value of 0 is returned. Otherwise, a
value of -1 is returned and errno is set to indicate the
error.
Diagnostics
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's allocated address space.
[EFBIG]-
An attempt is made to write a file that exceeds the process file
size limit or the maximum file size (see
getrlimit(S)
and
ulimit(S)).
[EINTR]-
A signal was caught during execution of the truncate
routine.
[EINVAL]-
path is not an ordinary file.
[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.
[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
{NAME_MAX} characters, or the length of path
exceeds {PATH_MAX} 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.
[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.
[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(S)).
[EBADF]-
fildes is not a file descriptor open for writing.
[EINTR-
A signal was caught during execution of the ftruncate
routine.
[EIO]-
An I/O error occurred while reading from or writing to the
file system.
[ENOLINK]-
fildes points to a remote machine and the link to that
machine is no longer active.
[EINVAL]-
fildes does not correspond to an ordinary file.
Files
/lib/libc.a-
linking library
See also
fcntl(S),
open(S)
Standards conformance
truncate and ftruncate are conformant with:
AT&T SVID Issue 3.
© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003