]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/truncate.2
ip.7: tfix
[thirdparty/man-pages.git] / man2 / truncate.2
1 .\" Copyright (c) 1983, 1991 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
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
7 .\" are met:
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.
20 .\"
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
31 .\" SUCH DAMAGE.
32 .\" %%%LICENSE_END
33 .\"
34 .\" @(#)truncate.2 6.9 (Berkeley) 3/10/91
35 .\"
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>
42 .\"
43 .TH TRUNCATE 2 2016-03-15 "Linux" "Linux Programmer's Manual"
44 .SH NAME
45 truncate, ftruncate \- truncate a file to a specified length
46 .SH SYNOPSIS
47 .B #include <unistd.h>
48 .br
49 .B #include <sys/types.h>
50 .sp
51 .BI "int truncate(const char *" path ", off_t " length );
52 .br
53 .BI "int ftruncate(int " fd ", off_t " length );
54 .sp
55 .in -4n
56 Feature Test Macro Requirements for glibc (see
57 .BR feature_test_macros (7)):
58 .in
59 .ad l
60 .PD 0
61 .sp
62 .BR truncate ():
63 .RS 4
64 _XOPEN_SOURCE\ >=\ 500
65 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
66 .br
67 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
68 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
69 .RE
70 .sp
71 .BR ftruncate ():
72 .RS 4
73 _XOPEN_SOURCE\ >=\ 500
74 .\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
75 || /* Since glibc 2.3.5: */ _POSIX_C_SOURCE\ >=\ 200112L
76 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
77 .RE
78 .PD
79 .ad b
80 .SH DESCRIPTION
81 The
82 .BR truncate ()
83 and
84 .BR ftruncate ()
85 functions cause the regular file named by
86 .I path
87 or referenced by
88 .I fd
89 to be truncated to a size of precisely
90 .I length
91 bytes.
92 .LP
93 If the file previously was larger than this size, the extra data is lost.
94 If the file previously was shorter, it is extended, and
95 the extended part reads as null bytes (\(aq\\0\(aq).
96 .LP
97 The file offset is not changed.
98 .LP
99 If the size changed, then the st_ctime and st_mtime fields
100 (respectively, time of last status change and
101 time of last modification; see
102 .BR stat (2))
103 for the file are updated,
104 and the set-user-ID and set-group-ID mode bits may be cleared.
105 .LP
106 With
107 .BR ftruncate (),
108 the file must be open for writing; with
109 .BR truncate (),
110 the file must be writable.
111 .SH RETURN VALUE
112 On success, zero is returned.
113 On error, \-1 is returned, and
114 .I errno
115 is set appropriately.
116 .SH ERRORS
117 For
118 .BR truncate ():
119 .TP
120 .B EACCES
121 Search permission is denied for a component of the path prefix,
122 or the named file is not writable by the user.
123 (See also
124 .BR path_resolution (7).)
125 .TP
126 .B EFAULT
127 The argument
128 .I path
129 points outside the process's allocated address space.
130 .TP
131 .B EFBIG
132 The argument
133 .I length
134 is larger than the maximum file size. (XSI)
135 .TP
136 .B EINTR
137 While blocked waiting to complete,
138 the call was interrupted by a signal handler; see
139 .BR fcntl (2)
140 and
141 .BR signal (7).
142 .TP
143 .B EINVAL
144 The argument
145 .I length
146 is negative or larger than the maximum file size.
147 .TP
148 .B EIO
149 An I/O error occurred updating the inode.
150 .TP
151 .B EISDIR
152 The named file is a directory.
153 .TP
154 .B ELOOP
155 Too many symbolic links were encountered in translating the pathname.
156 .TP
157 .B ENAMETOOLONG
158 A component of a pathname exceeded 255 characters,
159 or an entire pathname exceeded 1023 characters.
160 .TP
161 .B ENOENT
162 The named file does not exist.
163 .TP
164 .B ENOTDIR
165 A component of the path prefix is not a directory.
166 .TP
167 .B EPERM
168 .\" This happens for at least MSDOS and VFAT filesystems
169 .\" on kernel 2.6.13
170 The underlying filesystem does not support extending
171 a file beyond its current size.
172 .TP
173 .B EPERM
174 The operation was prevented by a file seal; see
175 .BR fcntl (2).
176 .TP
177 .B EROFS
178 The named file resides on a read-only filesystem.
179 .TP
180 .B ETXTBSY
181 The file is an executable file that is being executed.
182 .PP
183 For
184 .BR ftruncate ()
185 the same errors apply, but instead of things that can be wrong with
186 .IR path ,
187 we now have things that can be wrong with the file descriptor,
188 .IR fd :
189 .TP
190 .B EBADF
191 .I fd
192 is not a valid file descriptor.
193 .TP
194 .BR EBADF " or " EINVAL
195 .I fd
196 is not open for writing.
197 .TP
198 .B EINVAL
199 .I fd
200 does not reference a regular file.
201 .TP
202 .BR EINVAL " or " EBADF
203 The file descriptor
204 .I fd
205 is not open for writing.
206 POSIX permits, and portable applications should handle,
207 either error for this case.
208 (Linux produces
209 .BR EINVAL .)
210 .SH CONFORMING TO
211 POSIX.1-2001, POSIX.1-2008,
212 4.4BSD, SVr4 (these calls first appeared in 4.2BSD).
213 .\" POSIX.1-1996 has
214 .\" .BR ftruncate ().
215 .\" POSIX.1-2001 also has
216 .\" .BR truncate (),
217 .\" as an XSI extension.
218 .\" .LP
219 .\" SVr4 documents additional
220 .\" .BR truncate ()
221 .\" error conditions EMFILE, EMULTIHP, ENFILE, ENOLINK. SVr4 documents for
222 .\" .BR ftruncate ()
223 .\" an additional EAGAIN error condition.
224 .SH NOTES
225 The details in DESCRIPTION are for XSI-compliant systems.
226 For non-XSI-compliant systems, the POSIX standard allows
227 two behaviors for
228 .BR ftruncate ()
229 when
230 .I length
231 exceeds the file length
232 (note that
233 .BR truncate ()
234 is not specified at all in such an environment):
235 either returning an error, or extending the file.
236 Like most UNIX implementations, Linux follows the XSI requirement
237 when dealing with native filesystems.
238 However, some nonnative filesystems do not permit
239 .BR truncate ()
240 and
241 .BR ftruncate ()
242 to be used to extend a file beyond its current length:
243 a notable example on Linux is VFAT.
244 .\" At the very least: OSF/1, Solaris 7, and FreeBSD conform, mtk, Jan 2002
245
246 The original Linux
247 .BR truncate ()
248 and
249 .BR ftruncate ()
250 system calls were not designed to handle large file offsets.
251 Consequently, Linux 2.4 added
252 .BR truncate64 ()
253 and
254 .BR ftruncate64 ()
255 system calls that handle large files.
256 However, these details can be ignored by applications using glibc, whose
257 wrapper functions transparently employ the more recent system calls
258 where they are available.
259
260 On some 32-bit architectures,
261 the calling signature for these system calls differ,
262 for the reasons described in
263 .BR syscall (2).
264 .SH BUGS
265 A header file bug in glibc 2.12 meant that the minimum value of
266 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=12037
267 .BR _POSIX_C_SOURCE
268 required to expose the declaration of
269 .BR ftruncate ()
270 was 200809L instead of 200112L.
271 This has been fixed in later glibc versions.
272 .SH SEE ALSO
273 .BR truncate (1),
274 .BR open (2),
275 .BR stat (2),
276 .BR path_resolution (7)