1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by the University of
15 .\" California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" @(#)truncate.2 6.9 (Berkeley) 3/10/91
34 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
35 .\" Modified 1996-10-22 by Eric S. Raymond <esr@thyrsus.com>
36 .\" Modified 1998-12-21 by Andries Brouwer <aeb@cwi.nl>
37 .\" Modified 2002-01-07 by Michael Kerrisk <mtk-manpages@gmx.net>
38 .\" Modified 2002-04-06 by Andries Brouwer <aeb@cwi.nl>
39 .\" Modified 2004-06-23 by Michael Kerrisk <mtk-manpages@gmx.net>
41 .TH TRUNCATE 2 2004-06-23 "Linux 2.6.7" "Linux Programmer's Manual"
43 truncate, ftruncate \- truncate a file to a specified length
45 .B #include <unistd.h>
47 .B #include <sys/types.h>
49 .BI "int truncate(const char *" path ", off_t " length );
51 .BI "int ftruncate(int " fd ", off_t " length );
57 functions cause the regular file named by
61 to be truncated to a size of precisely
65 If the file previously was larger than this size, the extra data is lost.
66 If the file previously was shorter, it is extended, and
67 the extended part reads as null bytes ('\\0').
69 The file offset is not changed.
71 If the size changed, then the st_ctime and st_mtime fields
72 (respectively, time of last status change and
73 time of last modification; see
75 for the file are updated,
76 and the set-user-ID and set-group-ID permission bits may be cleared.
80 the file must be open for writing; with
82 the file must be writable.
84 On success, zero is returned. On error, \-1 is returned, and
92 Search permission is denied for a component of the path prefix,
93 or the named file is not writable by the user.
95 .BR path_resolution (2).)
99 points outside the process's allocated address space.
104 is larger than the maximum file size. (XSI)
107 A signal was caught during execution.
112 is negative or larger than the maximum file size.
115 An I/O error occurred updating the inode.
118 The named file is a directory.
121 Too many symbolic links were encountered in translating the pathname.
124 A component of a pathname exceeded 255 characters,
125 or an entire pathname exceeded 1023 characters.
128 The named file does not exist.
131 A component of the path prefix is not a directory.
134 .\" This happens for at leas MSDOS and VFAT fil systems
136 The underlying file system does not support extending
137 a file beyond its current size.
140 The named file resides on a read-only file system.
143 The file is a pure procedure (shared text) file that is being executed.
147 the same errors apply, but instead of things that can be wrong with
149 we now have things that can be wrong with
155 is not a valid descriptor.
157 .BR EBADF " or " EINVAL
160 is not open for writing.
165 does not reference a regular file.
167 4.4BSD, SVr4 (these function calls first appeared in 4.2BSD).
168 POSIX 1003.1-1996 has
170 POSIX 1003.1-2001 also has
174 SVr4 documents additional
176 error conditions EMFILE, EMULTIHP, ENFILE, ENOLINK. SVr4 documents for
178 an additional EAGAIN error condition.
180 The above description is for XSI-compliant systems.
181 For non-XSI-compliant systems, the POSIX standard allows
186 exceeds the file length
189 is not specified at all in such an environment):
190 either returning an error, or extending the file.
191 Like most Unix implementations, Linux follows the XSI requirement
192 when dealing with native file systems.
193 However, some non-native file systems do not permit
197 to be used to extend a file beyond its current length:
198 a notable example on Linux is VFAT.
199 .\" At the very least: OSF/1, Solaris 7, and FreeBSD conform, mtk, Jan 2002
202 .BR path_resolution (2),