]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/truncate.2
chdir.2, chmod.2, chown.2, gethostname.2, getsid.2, pread.2, setpgid.2, sigaltstack...
[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 .\" 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. 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.
19 .\"
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
30 .\" SUCH DAMAGE.
31 .\"
32 .\" @(#)truncate.2 6.9 (Berkeley) 3/10/91
33 .\"
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@gmail.com>
38 .\" Modified 2002-04-06 by Andries Brouwer <aeb@cwi.nl>
39 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
40 .\"
41 .TH TRUNCATE 2 2010-09-26 "Linux" "Linux Programmer's Manual"
42 .SH NAME
43 truncate, ftruncate \- truncate a file to a specified length
44 .SH SYNOPSIS
45 .B #include <unistd.h>
46 .br
47 .B #include <sys/types.h>
48 .sp
49 .BI "int truncate(const char *" path ", off_t " length );
50 .br
51 .BI "int ftruncate(int " fd ", off_t " length );
52 .sp
53 .in -4n
54 Feature Test Macro Requirements for glibc (see
55 .BR feature_test_macros (7)):
56 .in
57 .ad l
58 .PD 0
59 .sp
60 .BR truncate ():
61 .RS 4
62 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
63 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
64 .br
65 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
66 .RE
67 .sp
68 .BR ftruncate ():
69 .RS 4
70 .TP 4
71 .TP
72 Since glibc 2.12:
73 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
74 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED ||
75 .\" FIXME The next piece seems incorrect; see
76 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=12037
77 _POSIX_C_SOURCE\ >=\ 200809L
78 .TP
79 Between glibc 2.3.5 and 2.11
80 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
81 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED ||
82 _POSIX_C_SOURCE\ >=\ 200112L
83 .TP
84 Before glibc 2.3.5:
85 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
86 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
87 .RE
88 .PD
89 .ad b
90 .SH DESCRIPTION
91 The
92 .BR truncate ()
93 and
94 .BR ftruncate ()
95 functions cause the regular file named by
96 .I path
97 or referenced by
98 .I fd
99 to be truncated to a size of precisely
100 .I length
101 bytes.
102 .LP
103 If the file previously was larger than this size, the extra data is lost.
104 If the file previously was shorter, it is extended, and
105 the extended part reads as null bytes (\(aq\\0\(aq).
106 .LP
107 The file offset is not changed.
108 .LP
109 If the size changed, then the st_ctime and st_mtime fields
110 (respectively, time of last status change and
111 time of last modification; see
112 .BR stat (2))
113 for the file are updated,
114 and the set-user-ID and set-group-ID permission bits may be cleared.
115 .LP
116 With
117 .BR ftruncate (),
118 the file must be open for writing; with
119 .BR truncate (),
120 the file must be writable.
121 .SH "RETURN VALUE"
122 On success, zero is returned.
123 On error, \-1 is returned, and
124 .I errno
125 is set appropriately.
126 .SH ERRORS
127 For
128 .BR truncate ():
129 .TP
130 .B EACCES
131 Search permission is denied for a component of the path prefix,
132 or the named file is not writable by the user.
133 (See also
134 .BR path_resolution (7).)
135 .TP
136 .B EFAULT
137 .I Path
138 points outside the process's allocated address space.
139 .TP
140 .B EFBIG
141 The argument
142 .I length
143 is larger than the maximum file size. (XSI)
144 .TP
145 .B EINTR
146 A signal was caught during execution.
147 .TP
148 .B EINVAL
149 The argument
150 .I length
151 is negative or larger than the maximum file size.
152 .TP
153 .B EIO
154 An I/O error occurred updating the inode.
155 .TP
156 .B EINTR
157 While blocked waiting to complete,
158 the call was interrupted by a signal handler; see
159 .BR fcntl (2)
160 and
161 .BR signal (7).
162 .TP
163 .B EISDIR
164 The named file is a directory.
165 .TP
166 .B ELOOP
167 Too many symbolic links were encountered in translating the pathname.
168 .TP
169 .B ENAMETOOLONG
170 A component of a pathname exceeded 255 characters,
171 or an entire pathname exceeded 1023 characters.
172 .TP
173 .B ENOENT
174 The named file does not exist.
175 .TP
176 .B ENOTDIR
177 A component of the path prefix is not a directory.
178 .TP
179 .B EPERM
180 .\" This happens for at least MSDOS and VFAT file systems
181 .\" on kernel 2.6.13
182 The underlying file system does not support extending
183 a file beyond its current size.
184 .TP
185 .B EROFS
186 The named file resides on a read-only file system.
187 .TP
188 .B ETXTBSY
189 The file is a pure procedure (shared text) file that is being executed.
190 .PP
191 For
192 .BR ftruncate ()
193 the same errors apply, but instead of things that can be wrong with
194 .IR path ,
195 we now have things that can be wrong with the file descriptor,
196 .IR fd :
197 .TP
198 .B EBADF
199 .I fd
200 is not a valid descriptor.
201 .TP
202 .BR EBADF " or " EINVAL
203 .I fd
204 is not open for writing.
205 .TP
206 .B EINVAL
207 .I fd
208 does not reference a regular file.
209 .SH "CONFORMING TO"
210 4.4BSD, SVr4, POSIX.1-2001 (these calls first appeared in 4.2BSD).
211 .\" POSIX.1-1996 has
212 .\" .BR ftruncate ().
213 .\" POSIX.1-2001 also has
214 .\" .BR truncate (),
215 .\" as an XSI extension.
216 .\" .LP
217 .\" SVr4 documents additional
218 .\" .BR truncate ()
219 .\" error conditions EMFILE, EMULTIHP, ENFILE, ENOLINK. SVr4 documents for
220 .\" .BR ftruncate ()
221 .\" an additional EAGAIN error condition.
222 .SH NOTES
223 The above description is for XSI-compliant systems.
224 For non-XSI-compliant systems, the POSIX standard allows
225 two behaviors for
226 .BR ftruncate ()
227 when
228 .I length
229 exceeds the file length
230 (note that
231 .BR truncate ()
232 is not specified at all in such an environment):
233 either returning an error, or extending the file.
234 Like most Unix implementations, Linux follows the XSI requirement
235 when dealing with native file systems.
236 However, some nonnative file systems do not permit
237 .BR truncate ()
238 and
239 .BR ftruncate ()
240 to be used to extend a file beyond its current length:
241 a notable example on Linux is VFAT.
242 .\" At the very least: OSF/1, Solaris 7, and FreeBSD conform, mtk, Jan 2002
243 .SH "SEE ALSO"
244 .BR open (2),
245 .BR stat (2),
246 .BR path_resolution (7)