]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/truncate.2
Expand text noting that ftruncate()/truncate() may fail
[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@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>
40 .\"
41 .TH TRUNCATE 2 2004-06-23 "Linux 2.6.7" "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 .SH DESCRIPTION
53 The
54 .BR truncate ()
55 and
56 .BR ftruncate ()
57 functions cause the regular file named by
58 .I path
59 or referenced by
60 .I fd
61 to be truncated to a size of precisely
62 .I length
63 bytes.
64 .LP
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').
68 .LP
69 The file offset is not changed.
70 .LP
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
74 .BR stat (2))
75 for the file are updated,
76 and the set-user-ID and set-group-ID permission bits may be cleared.
77 .LP
78 With
79 .BR ftruncate (),
80 the file must be open for writing; with
81 .BR truncate (),
82 the file must be writable.
83 .SH "RETURN VALUE"
84 On success, zero is returned. On error, \-1 is returned, and
85 .I errno
86 is set appropriately.
87 .SH ERRORS
88 For
89 .BR truncate ():
90 .TP
91 .B EACCES
92 Search permission is denied for a component of the path prefix,
93 or the named file is not writable by the user.
94 (See also
95 .BR path_resolution (2).)
96 .TP
97 .B EFAULT
98 .I Path
99 points outside the process's allocated address space.
100 .TP
101 .B EFBIG
102 The argument
103 .I length
104 is larger than the maximum file size. (XSI)
105 .TP
106 .B EINTR
107 A signal was caught during execution.
108 .TP
109 .B EINVAL
110 The argument
111 .I length
112 is negative or larger than the maximum file size.
113 .TP
114 .B EIO
115 An I/O error occurred updating the inode.
116 .TP
117 .B EISDIR
118 The named file is a directory.
119 .TP
120 .B ELOOP
121 Too many symbolic links were encountered in translating the pathname.
122 .TP
123 .B ENAMETOOLONG
124 A component of a pathname exceeded 255 characters,
125 or an entire pathname exceeded 1023 characters.
126 .TP
127 .B ENOENT
128 The named file does not exist.
129 .TP
130 .B ENOTDIR
131 A component of the path prefix is not a directory.
132 .TP
133 .B EPERM
134 .\" This happens for at leas MSDOS and VFAT fil systems
135 .\" on kernel 2.6.13
136 The underlying file system does not support extending
137 a file beyond its current size.
138 .TP
139 .B EROFS
140 The named file resides on a read-only file system.
141 .TP
142 .B ETXTBSY
143 The file is a pure procedure (shared text) file that is being executed.
144 .PP
145 For
146 .BR ftruncate ()
147 the same errors apply, but instead of things that can be wrong with
148 .IR path ,
149 we now have things that can be wrong with
150 .IR fd :
151 .TP
152 .B EBADF
153 The
154 .I fd
155 is not a valid descriptor.
156 .TP
157 .BR EBADF " or " EINVAL
158 The
159 .I fd
160 is not open for writing.
161 .TP
162 .B EINVAL
163 The
164 .I fd
165 does not reference a regular file.
166 .SH "CONFORMING TO"
167 4.4BSD, SVr4 (these function calls first appeared in 4.2BSD).
168 POSIX 1003.1-1996 has
169 .BR ftruncate ().
170 POSIX 1003.1-2001 also has
171 .BR truncate (),
172 as an XSI extension.
173 .LP
174 SVr4 documents additional
175 .BR truncate ()
176 error conditions EMFILE, EMULTIHP, ENFILE, ENOLINK. SVr4 documents for
177 .BR ftruncate ()
178 an additional EAGAIN error condition.
179 .SH NOTES
180 The above description is for XSI-compliant systems.
181 For non-XSI-compliant systems, the POSIX standard allows
182 two behaviours for
183 .BR ftruncate ()
184 when
185 .I length
186 exceeds the file length
187 (note that
188 .BR truncate ()
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
194 .BR truncate ()
195 and
196 .BR ftruncate ()
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
200 .SH "SEE ALSO"
201 .BR open (2),
202 .BR path_resolution (2),
203 .BR stat (2)