xref: /xnu-8792.61.2/bsd/man/man2/truncate.2 (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1.\"	$NetBSD: truncate.2,v 1.7 1995/02/27 12:39:00 cgd 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. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\"     @(#)truncate.2	8.1 (Berkeley) 6/4/93
35.\"
36.Dd June 3, 2021
37.Dt TRUNCATE 2
38.Os BSD 4.2
39.Sh NAME
40.Nm ftruncate ,
41.Nm truncate
42.Nd truncate or extend a file to a specified length
43.Sh SYNOPSIS
44.Fd #include <unistd.h>
45.Ft int
46.Fo ftruncate
47.Fa "int fildes"
48.Fa "off_t length"
49.Fc
50.Ft int
51.Fo truncate
52.Fa "const char *path"
53.Fa "off_t length"
54.Fc
55.Sh DESCRIPTION
56.Fn ftruncate
57and
58.Fn truncate
59cause the file named by
60.Fa path ,
61or referenced by
62.Fa fildes ,
63to be truncated (or extended) to
64.Fa length
65bytes in size. If the file size exceeds
66.Fa length ,
67any extra data is discarded. If the file size is smaller than
68.Fa length ,
69the file is extended and filled with zeros to the indicated length.
70The
71.Fn ftruncate
72form requires the file to be open for writing.
73.Pp
74Note:
75.Fn ftruncate
76and
77.Fn truncate
78do not modify the current file offset for any open file descriptions associated with the file.
79.Sh RETURN VALUES
80A value of 0 is returned if the call succeeds.  If the call
81fails a -1 is returned, and the global variable
82.Va errno
83specifies the error.
84.Sh ERRORS
85.Pp
86The
87.Fn ftruncate
88system call will fail if:
89.Bl -tag -width Er
90.\" ===========
91.It Bq Er EBADF
92.Fa fildes
93is not a valid descriptor open for writing.
94.\" ===========
95.It Bq Er EFBIG
96The file is a regular file and
97.Fa length
98is greater than the offset maximum established
99in the open file description associated with
100.Fa fildes .
101.\" ===========
102.It Bq Er EINVAL
103.Fa fildes
104references a socket, not a file.
105.\" ===========
106.It Bq Er EINVAL
107.Fa fildes
108is not open for writing.
109.\" ===========
110.It Bq Er EROFS
111The named file resides on a read-only file system.
112.El
113.Pp
114The
115.Fn truncate
116system call will fail if:
117.Bl -tag -width Er
118.\" ===========
119.It Bq Er EACCES
120Search permission is denied for a component of the path prefix.
121.\" ===========
122.It Bq Er EACCES
123The named file is not writable by the user.
124.\" ===========
125.It Bq Er EFAULT
126.Fa Path
127points outside the process's allocated address space.
128.\" ===========
129.It Bq Er EISDIR
130The named file is a directory.
131.\" ===========
132.It Bq Er ELOOP
133Too many symbolic links are encountered in translating the pathname.
134This is taken to be indicative of a looping symbolic link.
135.\" ===========
136.It Bq Er ENAMETOOLONG
137A component of a pathname exceeds
138.Dv {NAME_MAX}
139characters, or an entire path name exceeds
140.Dv {PATH_MAX}
141characters.
142.\" ===========
143.It Bq Er ENOENT
144The named file does not exist.
145.\" ===========
146.It Bq Er ENOTDIR
147A component of the path prefix is not a directory.
148.\" ===========
149.It Bq Er EDEADLK
150A component of the pathname refers to a
151.Dq dataless
152directory that requires materialization and the I/O policy of the current
153thread or process disallows dataless directory materialization
154.Po see
155.Xr getiopolicy_np 3
156.Pc .
157.\" ===========
158.It Bq Er EROFS
159The named file resides on a read-only file system.
160.\" ===========
161.It Bq Er ETXTBSY
162The file is a pure procedure (shared text) file that is being executed.
163.El
164.Pp
165The
166.Fn ftruncate
167and
168.Fn truncate
169system calls will fail if:
170.Bl -tag -width Er
171.\" ===========
172.It Bq Er EFBIG
173The length argument was greater than the maximum file size.
174.\" ===========
175.It Bq Er EINTR
176A signal is caught during execution.
177.\" ===========
178.It Bq Er EINVAL
179The
180.Fa length
181argument is less than 0.
182.\" ===========
183.It Bq Er EIO
184An I/O error occurred while reading from or writing to a file system.
185.\" ===========
186.It Bq Er EDEADLK
187The file is a
188.Dq dataless
189file that requires materialization and the I/O policy of the current thread
190or process disallows dataless file materialization
191.Po see
192.Xr getiopolicy_np 3
193.Pc .
194.El
195.Sh SEE ALSO
196.Xr open 2
197.Sh BUGS
198These calls should be generalized to allow ranges
199of bytes in a file to be discarded.
200.Pp
201Use of
202.Fn truncate
203to extend a file is not portable.
204.Sh HISTORY
205The
206.Fn truncate
207and
208.Fn ftruncate
209function calls appeared in
210.Bx 4.2 .
211