date and time created 91/04/12 13:40:31 by bostic
[unix-history] / usr / src / lib / libc / sys / truncate.2
.\" Copyright (c) 1983, 1991 The Regents of the University of California.
.\" All rights reserved.
.\"
.\" %sccs.include.redist.man%
.\"
.\" @(#)truncate.2 6.9 (Berkeley) %G%
.\"
.Dd
.Dt TRUNCATE 2
.Os BSD 4.2
.Sh NAME
.Nm truncate ,
.Nm ftruncate
.Nd truncate a file to a specified length
.Sh SYNOPSIS
.Fd #include <unistd.h>
.Ft int
.Fn truncate "const char *path" "off_t length"
.Ft int
.Fn ftruncate "int fd" "off_t length"
.Sh DESCRIPTION
.Fn Truncate
causes the file named by
.Fa path
or referenced by
.Fa fd
to be truncated to at most
.Fa length
bytes in size. If the file previously
was larger than this size, the extra data
is lost.
With
.Fn ftruncate ,
the file must be open for writing.
.Sh RETURN VALUES
A value of 0 is returned if the call succeeds. If the call
fails a -1 is returned, and the global variable
.Va errno
specifies the error.
.Sh ERRORS
.Fn Truncate
succeeds unless:
.Bl -tag -width [ENOTDIR]
.It Bq Er ENOTDIR
A component of the path prefix is not a directory.
.It Bq Er EINVAL
The pathname contains a character with the high-order bit set.
.It Bq Er ENAMETOOLONG
A component of a pathname exceeded 255 characters,
or an entire path name exceeded 1023 characters.
.It Bq Er ENOENT
The named file does not exist.
.It Bq Er EACCES
Search permission is denied for a component of the path prefix.
.It Bq Er EACCES
The named file is not writable by the user.
.It Bq Er ELOOP
Too many symbolic links were encountered in translating the pathname.
.It Bq Er EISDIR
The named file is a directory.
.It Bq Er EROFS
The named file resides on a read-only file system.
.It Bq Er ETXTBSY
The file is a pure procedure (shared text) file that is being executed.
.It Bq Er EIO
An I/O error occurred updating the inode.
.It Bq Er EFAULT
.Fa Path
points outside the process's allocated address space.
.El
.Pp
.Fn Ftruncate
succeeds unless:
.Bl -tag -width [ENOTDIR]
.It Bq Er EBADF
The
.Fa fd
is not a valid descriptor.
.It Bq Er EINVAL
The
.Fa fd
references a socket, not a file.
.It Bq Er EINVAL
The
.Fa fd
is not open for writing.
.El
.Sh SEE ALSO
.Xr open 2
.Sh BUGS
These calls should be generalized to allow ranges
of bytes in a file to be discarded.
.Sh HISTORY
The
.Nm
function call appeared in
.Bx 4.2 .