1.\" $OpenBSD: truncate.2,v 1.20 2020/02/11 13:19:17 schwarze 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. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)truncate.2 8.1 (Berkeley) 6/4/93 32.\" 33.Dd $Mdocdate: February 11 2020 $ 34.Dt TRUNCATE 2 35.Os 36.Sh NAME 37.Nm truncate , 38.Nm ftruncate 39.Nd truncate or extend a file to a specified length 40.Sh SYNOPSIS 41.In unistd.h 42.Ft int 43.Fn truncate "const char *path" "off_t length" 44.Ft int 45.Fn ftruncate "int fd" "off_t length" 46.Sh DESCRIPTION 47.Fn truncate 48causes the file named by 49.Fa path 50or referenced by 51.Fa fd 52to be truncated or extended to 53.Fa length 54bytes in size. 55If the file was larger than this size, the extra data is lost. 56If the file was smaller than this size, it will be extended as if by 57writing bytes with the value zero. 58With 59.Fn ftruncate , 60the file must be open for writing. 61.Sh RETURN VALUES 62.Rv -std 63.Sh ERRORS 64.Fn truncate 65and 66.Fn ftruncate 67will fail if: 68.Bl -tag -width Er 69.It Bq Er EINVAL 70The 71.Fa length 72is a negative value. 73.It Bq Er EFBIG 74The 75.Fa length 76exceeds the maximum file size of the underlying filesystem. 77.It Bq Er EIO 78An I/O error occurred updating the inode. 79.El 80.Pp 81In addition, 82.Fn truncate 83may return the following errors: 84.Bl -tag -width Er 85.It Bq Er ENOTDIR 86A component of the path prefix is not a directory. 87.It Bq Er ENAMETOOLONG 88A component of a pathname exceeded 89.Dv NAME_MAX 90characters, or an entire pathname (including the terminating NUL) 91exceeded 92.Dv PATH_MAX 93bytes. 94.It Bq Er ENOENT 95The named file does not exist. 96.It Bq Er EACCES 97Search permission is denied for a component of the path prefix. 98.It Bq Er EACCES 99The named file is not writable by the user. 100.It Bq Er ELOOP 101Too many symbolic links were encountered in translating the pathname. 102.It Bq Er EISDIR 103The named file is a directory. 104.It Bq Er EROFS 105The named file resides on a read-only file system. 106.It Bq Er ETXTBSY 107The file is a pure procedure (shared text) file that is being executed. 108.It Bq Er EFAULT 109.Fa path 110points outside the process's allocated address space. 111.El 112.Pp 113.Fn ftruncate 114may return the following errors: 115.Bl -tag -width Er 116.It Bq Er EBADF 117The 118.Fa fd 119is not a valid descriptor. 120.It Bq Er EINVAL 121The 122.Fa fd 123references a socket, not a file. 124.It Bq Er EINVAL 125The 126.Fa fd 127is not open for writing. 128.El 129.Sh SEE ALSO 130.Xr open 2 131.Sh STANDARDS 132The 133.Fn truncate 134and 135.Fn ftruncate 136functions conform to 137.St -p1003.1-2008 . 138.Sh HISTORY 139The 140.Fn truncate 141and 142.Fn ftruncate 143system calls first appeared in 144.Bx 4.1c . 145.Sh BUGS 146These calls should be generalized to allow ranges of bytes in a file 147to be discarded. 148.Pp 149Use of 150.Fn truncate 151to extend a file is not portable. 152