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