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