]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/truncate.2
accept.2, access.2, acct.2, clock_nanosleep.2, mbind.2, mincore.2, remap_file_pages...
[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 2009-02-28 "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 .sp
59 .BR truncate ():
60 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500
61 .br
62 .BR ftruncate ():
63 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 || _POSIX_C_SOURCE\ >=\ 200112L
64 .ad b
65 .SH DESCRIPTION
66 The
67 .BR truncate ()
68 and
69 .BR ftruncate ()
70 functions cause the regular file named by
71 .I path
72 or referenced by
73 .I fd
74 to be truncated to a size of precisely
75 .I length
76 bytes.
77 .LP
78 If the file previously was larger than this size, the extra data is lost.
79 If the file previously was shorter, it is extended, and
80 the extended part reads as null bytes (\(aq\\0\(aq).
81 .LP
82 The file offset is not changed.
83 .LP
84 If the size changed, then the st_ctime and st_mtime fields
85 (respectively, time of last status change and
86 time of last modification; see
87 .BR stat (2))
88 for the file are updated,
89 and the set-user-ID and set-group-ID permission bits may be cleared.
90 .LP
91 With
92 .BR ftruncate (),
93 the file must be open for writing; with
94 .BR truncate (),
95 the file must be writable.
96 .SH "RETURN VALUE"
97 On success, zero is returned.
98 On error, \-1 is returned, and
99 .I errno
100 is set appropriately.
101 .SH ERRORS
102 For
103 .BR truncate ():
104 .TP
105 .B EACCES
106 Search permission is denied for a component of the path prefix,
107 or the named file is not writable by the user.
108 (See also
109 .BR path_resolution (7).)
110 .TP
111 .B EFAULT
112 .I Path
113 points outside the process's allocated address space.
114 .TP
115 .B EFBIG
116 The argument
117 .I length
118 is larger than the maximum file size. (XSI)
119 .TP
120 .B EINTR
121 A signal was caught during execution.
122 .TP
123 .B EINVAL
124 The argument
125 .I length
126 is negative or larger than the maximum file size.
127 .TP
128 .B EIO
129 An I/O error occurred updating the inode.
130 .TP
131 .B EINTR
132 While blocked waiting to complete,
133 the call was interrupted by a signal handler; see
134 .BR fcntl (2)
135 and
136 .BR signal (7).
137 .TP
138 .B EISDIR
139 The named file is a directory.
140 .TP
141 .B ELOOP
142 Too many symbolic links were encountered in translating the pathname.
143 .TP
144 .B ENAMETOOLONG
145 A component of a pathname exceeded 255 characters,
146 or an entire pathname exceeded 1023 characters.
147 .TP
148 .B ENOENT
149 The named file does not exist.
150 .TP
151 .B ENOTDIR
152 A component of the path prefix is not a directory.
153 .TP
154 .B EPERM
155 .\" This happens for at least MSDOS and VFAT file systems
156 .\" on kernel 2.6.13
157 The underlying file system does not support extending
158 a file beyond its current size.
159 .TP
160 .B EROFS
161 The named file resides on a read-only file system.
162 .TP
163 .B ETXTBSY
164 The file is a pure procedure (shared text) file that is being executed.
165 .PP
166 For
167 .BR ftruncate ()
168 the same errors apply, but instead of things that can be wrong with
169 .IR path ,
170 we now have things that can be wrong with the file descriptor,
171 .IR fd :
172 .TP
173 .B EBADF
174 .I fd
175 is not a valid descriptor.
176 .TP
177 .BR EBADF " or " EINVAL
178 .I fd
179 is not open for writing.
180 .TP
181 .B EINVAL
182 .I fd
183 does not reference a regular file.
184 .SH "CONFORMING TO"
185 4.4BSD, SVr4, POSIX.1-2001 (these calls first appeared in 4.2BSD).
186 .\" POSIX.1-1996 has
187 .\" .BR ftruncate ().
188 .\" POSIX.1-2001 also has
189 .\" .BR truncate (),
190 .\" as an XSI extension.
191 .\" .LP
192 .\" SVr4 documents additional
193 .\" .BR truncate ()
194 .\" error conditions EMFILE, EMULTIHP, ENFILE, ENOLINK. SVr4 documents for
195 .\" .BR ftruncate ()
196 .\" an additional EAGAIN error condition.
197 .SH NOTES
198 The above description is for XSI-compliant systems.
199 For non-XSI-compliant systems, the POSIX standard allows
200 two behaviors for
201 .BR ftruncate ()
202 when
203 .I length
204 exceeds the file length
205 (note that
206 .BR truncate ()
207 is not specified at all in such an environment):
208 either returning an error, or extending the file.
209 Like most Unix implementations, Linux follows the XSI requirement
210 when dealing with native file systems.
211 However, some nonnative file systems do not permit
212 .BR truncate ()
213 and
214 .BR ftruncate ()
215 to be used to extend a file beyond its current length:
216 a notable example on Linux is VFAT.
217 .\" At the very least: OSF/1, Solaris 7, and FreeBSD conform, mtk, Jan 2002
218 .SH "SEE ALSO"
219 .BR open (2),
220 .BR stat (2),
221 .BR path_resolution (7)