1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\" must display the following acknowledgement:
15 .\" This product includes software developed by the University of
16 .\" California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\" may be used to endorse or promote products derived from this software
19 .\" without specific prior written permission.
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 .\" @(#)truncate.2 6.9 (Berkeley) 3/10/91
36 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
37 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
38 .\" Modified 1998-12-21 by Andries Brouwer <aeb@cwi.nl>
39 .\" Modified 2002-01-07 by Michael Kerrisk <mtk.manpages@gmail.com>
40 .\" Modified 2002-04-06 by Andries Brouwer <aeb@cwi.nl>
41 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
43 .TH TRUNCATE 2 2019-03-06 "Linux" "Linux Programmer's Manual"
45 truncate, ftruncate \- truncate a file to a specified length
47 .B #include <unistd.h>
49 .B #include <sys/types.h>
51 .BI "int truncate(const char *" path ", off_t " length );
53 .BI "int ftruncate(int " fd ", off_t " length );
56 Feature Test Macro Requirements for glibc (see
57 .BR feature_test_macros (7)):
63 _XOPEN_SOURCE\ >=\ 500
64 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
66 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
67 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
72 _XOPEN_SOURCE\ >=\ 500
73 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
74 || /* Since glibc 2.3.5: */ _POSIX_C_SOURCE\ >=\ 200112L
75 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
83 functions cause the regular file named by
87 to be truncated to a size of precisely
91 If the file previously was larger than this size, the extra data is lost.
92 If the file previously was shorter, it is extended, and
93 the extended part reads as null bytes (\(aq\e0\(aq).
95 The file offset is not changed.
97 If the size changed, then the st_ctime and st_mtime fields
98 (respectively, time of last status change and
99 time of last modification; see
101 for the file are updated,
102 and the set-user-ID and set-group-ID mode bits may be cleared.
106 the file must be open for writing; with
108 the file must be writable.
110 On success, zero is returned.
111 On error, \-1 is returned, and
113 is set appropriately.
119 Search permission is denied for a component of the path prefix,
120 or the named file is not writable by the user.
122 .BR path_resolution (7).)
127 points outside the process's allocated address space.
132 is larger than the maximum file size. (XSI)
135 While blocked waiting to complete,
136 the call was interrupted by a signal handler; see
144 is negative or larger than the maximum file size.
147 An I/O error occurred updating the inode.
150 The named file is a directory.
153 Too many symbolic links were encountered in translating the pathname.
156 A component of a pathname exceeded 255 characters,
157 or an entire pathname exceeded 1023 characters.
160 The named file does not exist.
163 A component of the path prefix is not a directory.
166 .\" This happens for at least MSDOS and VFAT filesystems
168 The underlying filesystem does not support extending
169 a file beyond its current size.
172 The operation was prevented by a file seal; see
176 The named file resides on a read-only filesystem.
179 The file is an executable file that is being executed.
183 the same errors apply, but instead of things that can be wrong with
185 we now have things that can be wrong with the file descriptor,
190 is not a valid file descriptor.
192 .BR EBADF " or " EINVAL
194 is not open for writing.
198 does not reference a regular file or a POSIX shared memory object.
200 .BR EINVAL " or " EBADF
203 is not open for writing.
204 POSIX permits, and portable applications should handle,
205 either error for this case.
209 POSIX.1-2001, POSIX.1-2008,
210 4.4BSD, SVr4 (these calls first appeared in 4.2BSD).
212 .\" .BR ftruncate ().
213 .\" POSIX.1-2001 also has
215 .\" as an XSI extension.
217 .\" SVr4 documents additional
219 .\" error conditions EMFILE, EMULTIHP, ENFILE, ENOLINK. SVr4 documents for
221 .\" an additional EAGAIN error condition.
224 can also be used to set the size of a POSIX shared memory object; see
227 The details in DESCRIPTION are for XSI-compliant systems.
228 For non-XSI-compliant systems, the POSIX standard allows
233 exceeds the file length
236 is not specified at all in such an environment):
237 either returning an error, or extending the file.
238 Like most UNIX implementations, Linux follows the XSI requirement
239 when dealing with native filesystems.
240 However, some nonnative filesystems do not permit
244 to be used to extend a file beyond its current length:
245 a notable example on Linux is VFAT.
246 .\" At the very least: OSF/1, Solaris 7, and FreeBSD conform, mtk, Jan 2002
252 system calls were not designed to handle large file offsets.
253 Consequently, Linux 2.4 added
257 system calls that handle large files.
258 However, these details can be ignored by applications using glibc, whose
259 wrapper functions transparently employ the more recent system calls
260 where they are available.
262 On some 32-bit architectures,
263 the calling signature for these system calls differ,
264 for the reasons described in
267 A header file bug in glibc 2.12 meant that the minimum value of
268 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=12037
270 required to expose the declaration of
272 was 200809L instead of 200112L.
273 This has been fixed in later glibc versions.
278 .BR path_resolution (7)