xref: /openbsd-src/lib/libc/sys/truncate.2 (revision 999da09776ffb248df0a9fa1fe2939d7ee8306b0)
1.\"	$OpenBSD: truncate.2,v 1.21 2022/05/23 15:17:11 millert 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: May 23 2022 $
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 process's file size limit or the maximum file size
77of the underlying filesystem.
78.It Bq Er EIO
79An I/O error occurred updating the inode.
80.El
81.Pp
82In addition,
83.Fn truncate
84may return the following errors:
85.Bl -tag -width Er
86.It Bq Er ENOTDIR
87A component of the path prefix is not a directory.
88.It Bq Er ENAMETOOLONG
89A component of a pathname exceeded
90.Dv NAME_MAX
91characters, or an entire pathname (including the terminating NUL)
92exceeded
93.Dv PATH_MAX
94bytes.
95.It Bq Er ENOENT
96The named file does not exist.
97.It Bq Er EACCES
98Search permission is denied for a component of the path prefix.
99.It Bq Er EACCES
100The named file is not writable by the user.
101.It Bq Er ELOOP
102Too many symbolic links were encountered in translating the pathname.
103.It Bq Er EISDIR
104The named file is a directory.
105.It Bq Er EROFS
106The named file resides on a read-only file system.
107.It Bq Er ETXTBSY
108The file is a pure procedure (shared text) file that is being executed.
109.It Bq Er EFAULT
110.Fa path
111points outside the process's allocated address space.
112.El
113.Pp
114.Fn ftruncate
115may return the following errors:
116.Bl -tag -width Er
117.It Bq Er EBADF
118The
119.Fa fd
120is not a valid descriptor.
121.It Bq Er EINVAL
122The
123.Fa fd
124references a socket, not a file.
125.It Bq Er EINVAL
126The
127.Fa fd
128is not open for writing.
129.El
130.Sh SEE ALSO
131.Xr open 2
132.Sh STANDARDS
133The
134.Fn truncate
135and
136.Fn ftruncate
137functions conform to
138.St -p1003.1-2008 .
139.Sh HISTORY
140The
141.Fn truncate
142and
143.Fn ftruncate
144system calls first appeared in
145.Bx 4.1c .
146.Sh BUGS
147These calls should be generalized to allow ranges of bytes in a file
148to be discarded.
149.Pp
150Use of
151.Fn truncate
152to extend a file is not portable.
153