xref: /freebsd/lib/libsys/truncate.2 (revision abd87254)
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.Dd March 30, 2020
29.Dt TRUNCATE 2
30.Os
31.Sh NAME
32.Nm truncate ,
33.Nm ftruncate
34.Nd truncate or extend a file to a specified length
35.Sh LIBRARY
36.Lb libc
37.Sh SYNOPSIS
38.In unistd.h
39.Ft int
40.Fn truncate "const char *path" "off_t length"
41.Ft int
42.Fn ftruncate "int fd" "off_t length"
43.Sh DESCRIPTION
44The
45.Fn truncate
46system call
47causes the file named by
48.Fa path
49or referenced by
50.Fa fd
51to be truncated or extended to
52.Fa length
53bytes in size.
54If the file
55was larger than this size, the extra data
56is lost.
57If the file was smaller than this size,
58it will be extended as if by writing bytes
59with the value zero.
60.Pp
61The
62.Fn ftruncate
63system call causes the file or shared memory object backing the file descriptor
64.Fa fd
65to be truncated or extended to
66.Fa length
67bytes in size.
68The file descriptor must be a valid file descriptor open for writing.
69The file position pointer associated with the file descriptor
70.Fa fd
71will not be modified.
72.Sh RETURN VALUES
73.Rv -std
74If the file to be modified is not a directory or
75a regular file, the
76.Fn truncate
77call has no effect and returns the value 0.
78.Sh ERRORS
79The
80.Fn truncate
81system call
82succeeds unless:
83.Bl -tag -width Er
84.It Bq Er ENOTDIR
85A component of the path prefix is not a directory.
86.It Bq Er ENAMETOOLONG
87A component of a pathname exceeded 255 characters,
88or an entire path name exceeded 1023 characters.
89.It Bq Er ENOENT
90The named file does not exist.
91.It Bq Er EACCES
92Search permission is denied for a component of the path prefix.
93.It Bq Er EACCES
94The named file is not writable by the user.
95.It Bq Er ELOOP
96Too many symbolic links were encountered in translating the pathname.
97.It Bq Er EPERM
98The named file has its immutable or append-only flag set, see the
99.Xr chflags 2
100manual page for more information.
101.It Bq Er EISDIR
102The named file is a directory.
103.It Bq Er EROFS
104The named file resides on a read-only file system.
105.It Bq Er ETXTBSY
106The file is a pure procedure (shared text) file that is being executed.
107.It Bq Er EFBIG
108The
109.Fa length
110argument was greater than the maximum file size.
111.It Bq Er EINVAL
112The
113.Fa length
114argument was less than 0.
115.It Bq Er EIO
116An I/O error occurred updating the inode.
117.It Bq Er EINTEGRITY
118Corrupted data was detected while reading from the file system.
119.It Bq Er EFAULT
120The
121.Fa path
122argument
123points outside the process's allocated address space.
124.El
125.Pp
126The
127.Fn ftruncate
128system call
129succeeds unless:
130.Bl -tag -width Er
131.It Bq Er EBADF
132The
133.Fa fd
134argument
135is not a valid descriptor.
136.It Bq Er EINVAL
137The
138.Fa fd
139argument
140references a file descriptor that is not a regular file or shared memory object.
141.It Bq Er EINVAL
142The
143.Fa fd
144descriptor
145is not open for writing.
146.El
147.Sh SEE ALSO
148.Xr chflags 2 ,
149.Xr open 2 ,
150.Xr shm_open 2
151.Sh HISTORY
152The
153.Fn truncate
154and
155.Fn ftruncate
156system calls appeared in
157.Bx 4.2 .
158.Sh BUGS
159These calls should be generalized to allow ranges
160of bytes in a file to be discarded.
161.Pp
162Historically, the use of
163.Fn truncate
164or
165.Fn ftruncate
166to extend a file was not portable, but this behavior became required in
167.St -p1003.1-2008 .
168