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