1.\" $OpenBSD: truncate.2,v 1.10 2001/03/11 05:28:02 aaron Exp $ 2.\" $NetBSD: truncate.2,v 1.7 1995/02/27 12:39:00 cgd Exp $ 3.\" 4.\" Copyright (c) 1983, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. All advertising materials mentioning features or use of this software 16.\" must display the following acknowledgement: 17.\" This product includes software developed by the University of 18.\" California, Berkeley and its contributors. 19.\" 4. Neither the name of the University nor the names of its contributors 20.\" may be used to endorse or promote products derived from this software 21.\" without specific prior written permission. 22.\" 23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33.\" SUCH DAMAGE. 34.\" 35.\" @(#)truncate.2 8.1 (Berkeley) 6/4/93 36.\" 37.Dd June 4, 1993 38.Dt TRUNCATE 2 39.Os 40.Sh NAME 41.Nm truncate , 42.Nm ftruncate 43.Nd truncate or extend a file to a specified length 44.Sh SYNOPSIS 45.Fd #include <unistd.h> 46.Ft int 47.Fn truncate "const char *path" "off_t length" 48.Ft int 49.Fn ftruncate "int fd" "off_t length" 50.Sh DESCRIPTION 51.Fn truncate 52causes the file named by 53.Fa path 54or referenced by 55.Fa fd 56to be truncated or extended to 57.Fa length 58bytes in size. 59If the file was larger than this size, the extra data is lost. 60If the file was smaller than this size, it will be extended as if by 61writing bytes with the value zero. 62With 63.Fn ftruncate , 64the file must be open for writing. 65.Sh RETURN VALUES 66A value of 0 is returned if the call succeeds. 67If the call fails a \-1 is returned, and the global variable 68.Va errno 69specifies the error. 70.Sh ERRORS 71.Fn truncate 72succeeds unless: 73.Bl -tag -width Er 74.It Bq Er ENOTDIR 75A component of the path prefix is not a directory. 76.It Bq Er ENAMETOOLONG 77A component of a pathname exceeded 78.Dv {NAME_MAX} 79characters, or an entire path name exceeded 80.Dv {PATH_MAX} 81characters. 82.It Bq Er ENOENT 83The named file does not exist. 84.It Bq Er EACCES 85Search permission is denied for a component of the path prefix. 86.It Bq Er EACCES 87The named file is not writable by the user. 88.It Bq Er ELOOP 89Too many symbolic links were encountered in translating the pathname. 90.It Bq Er EISDIR 91The named file is a directory. 92.It Bq Er EROFS 93The named file resides on a read-only file system. 94.It Bq Er ETXTBSY 95The file is a pure procedure (shared text) file that is being executed. 96.It Bq Er EIO 97An I/O error occurred updating the inode. 98.It Bq Er EFAULT 99.Fa path 100points outside the process's allocated address space. 101.It Bq Er EPERM 102The effective user ID does not match the owner of the file and 103the effective user ID is not the superuser. 104.El 105.Pp 106.Fn ftruncate 107succeeds unless: 108.Bl -tag -width Er 109.It Bq Er EBADF 110The 111.Fa fd 112is not a valid descriptor. 113.It Bq Er EINVAL 114The 115.Fa fd 116references a socket, not a file. 117.It Bq Er EINVAL 118The 119.Fa fd 120is not open for writing. 121.El 122.Sh SEE ALSO 123.Xr open 2 124.Sh HISTORY 125The 126.Fn truncate 127and 128.Fn ftruncate 129function calls appeared in 130.Bx 4.2 . 131.Sh BUGS 132These calls should be generalized to allow ranges of bytes in a file 133to be discarded. 134.Pp 135Use of 136.Fn truncate 137to extend a file is not portable. 138