xref: /freebsd-14.2/lib/libc/sys/truncate.2 (revision b2c76c41)
1.\" Copyright (c) 1983, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.\"     @(#)truncate.2	8.1 (Berkeley) 6/4/93
29.\"
30.Dd March 30, 2020
31.Dt TRUNCATE 2
32.Os
33.Sh NAME
34.Nm truncate ,
35.Nm ftruncate
36.Nd truncate or extend a file to a specified length
37.Sh LIBRARY
38.Lb libc
39.Sh SYNOPSIS
40.In unistd.h
41.Ft int
42.Fn truncate "const char *path" "off_t length"
43.Ft int
44.Fn ftruncate "int fd" "off_t length"
45.Sh DESCRIPTION
46The
47.Fn truncate
48system call
49causes the file named by
50.Fa path
51or referenced by
52.Fa fd
53to be truncated or extended to
54.Fa length
55bytes in size.
56If the file
57was larger than this size, the extra data
58is lost.
59If the file was smaller than this size,
60it will be extended as if by writing bytes
61with the value zero.
62.Pp
63The
64.Fn ftruncate
65system call causes the file or shared memory object backing the file descriptor
66.Fa fd
67to be truncated or extended to
68.Fa length
69bytes in size.
70The file descriptor must be a valid file descriptor open for writing.
71The file position pointer associated with the file descriptor
72.Fa fd
73will not be modified.
74.Sh RETURN VALUES
75.Rv -std
76If the file to be modified is not a directory or
77a regular file, the
78.Fn truncate
79call has no effect and returns the value 0.
80.Sh ERRORS
81The
82.Fn truncate
83system call
84succeeds unless:
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 255 characters,
90or an entire path name exceeded 1023 characters.
91.It Bq Er ENOENT
92The named file does not exist.
93.It Bq Er EACCES
94Search permission is denied for a component of the path prefix.
95.It Bq Er EACCES
96The named file is not writable by the user.
97.It Bq Er ELOOP
98Too many symbolic links were encountered in translating the pathname.
99.It Bq Er EPERM
100The named file has its immutable or append-only flag set, see the
101.Xr chflags 2
102manual page for more information.
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 EFBIG
110The
111.Fa length
112argument was greater than the maximum file size.
113.It Bq Er EINVAL
114The
115.Fa length
116argument was less than 0.
117.It Bq Er EIO
118An I/O error occurred updating the inode.
119.It Bq Er EINTEGRITY
120Corrupted data was detected while reading from the file system.
121.It Bq Er EFAULT
122The
123.Fa path
124argument
125points outside the process's allocated address space.
126.El
127.Pp
128The
129.Fn ftruncate
130system call
131succeeds unless:
132.Bl -tag -width Er
133.It Bq Er EBADF
134The
135.Fa fd
136argument
137is not a valid descriptor.
138.It Bq Er EINVAL
139The
140.Fa fd
141argument
142references a file descriptor that is not a regular file or shared memory object.
143.It Bq Er EINVAL
144The
145.Fa fd
146descriptor
147is not open for writing.
148.El
149.Sh SEE ALSO
150.Xr chflags 2 ,
151.Xr open 2 ,
152.Xr shm_open 2
153.Sh HISTORY
154The
155.Fn truncate
156and
157.Fn ftruncate
158system calls appeared in
159.Bx 4.2 .
160.Sh BUGS
161These calls should be generalized to allow ranges
162of bytes in a file to be discarded.
163.Pp
164Historically, the use of
165.Fn truncate
166or
167.Fn ftruncate
168to extend a file was not portable, but this behavior became required in
169.St -p1003.1-2008 .
170