]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/truncate.2
fmtmsg.3, gethostbyname.3, termios.3: Global fix: s/Unixware/UnixWare/
[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.\"
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>
c11b1abf 37.\" Modified 2002-01-07 by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 38.\" Modified 2002-04-06 by Andries Brouwer <aeb@cwi.nl>
c11b1abf 39.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 40.\"
c18150c2 41.TH TRUNCATE 2 2010-10-07 "Linux" "Linux Programmer's Manual"
fea681da
MK
42.SH NAME
43truncate, 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 );
cc4615cc
MK
52.sp
53.in -4n
54Feature Test Macro Requirements for glibc (see
55.BR feature_test_macros (7)):
56.in
57.ad l
1e2ba090 58.PD 0
cc4615cc 59.sp
3ca7341f 60.BR truncate ():
729166ed 61.RS 4
1e2ba090
MK
62_BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
63_XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
3ba63d80
MK
64.br
65|| /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
729166ed 66.RE
1e2ba090 67.sp
3ca7341f 68.BR ftruncate ():
729166ed 69.RS 4
1e2ba090 70_BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
1e2ba090 71_XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
9dcef0c0
MK
72.br
73|| /* Since glibc 2.3.5: */ _POSIX_C_SOURCE\ >=\ 200112L
729166ed 74.RE
1e2ba090 75.PD
cc4615cc 76.ad b
fea681da
MK
77.SH DESCRIPTION
78The
310b7919 79.BR truncate ()
fea681da 80and
310b7919 81.BR ftruncate ()
fea681da
MK
82functions cause the regular file named by
83.I path
84or referenced by
85.I fd
86to be truncated to a size of precisely
87.I length
88bytes.
89.LP
c13182ef 90If the file previously was larger than this size, the extra data is lost.
fea681da 91If the file previously was shorter, it is extended, and
f81fb444 92the extended part reads as null bytes (\(aq\\0\(aq).
fea681da 93.LP
1e568304 94The file offset is not changed.
fea681da 95.LP
c13182ef 96If the size changed, then the st_ctime and st_mtime fields
310b7919
MK
97(respectively, time of last status change and
98time of last modification; see
99.BR stat (2))
c13182ef 100for the file are updated,
310b7919 101and the set-user-ID and set-group-ID permission bits may be cleared.
fea681da
MK
102.LP
103With
310b7919 104.BR ftruncate (),
fea681da 105the file must be open for writing; with
310b7919 106.BR truncate (),
fea681da
MK
107the file must be writable.
108.SH "RETURN VALUE"
c13182ef
MK
109On success, zero is returned.
110On error, \-1 is returned, and
fea681da
MK
111.I errno
112is set appropriately.
113.SH ERRORS
114For
e511ffb6 115.BR truncate ():
fea681da
MK
116.TP
117.B EACCES
118Search permission is denied for a component of the path prefix,
119or the named file is not writable by the user.
120(See also
ad7cc990 121.BR path_resolution (7).)
fea681da
MK
122.TP
123.B EFAULT
124.I Path
125points outside the process's allocated address space.
126.TP
127.B EFBIG
128The argument
129.I length
130is larger than the maximum file size. (XSI)
131.TP
132.B EINTR
133A signal was caught during execution.
134.TP
135.B EINVAL
136The argument
137.I length
138is negative or larger than the maximum file size.
139.TP
140.B EIO
141An I/O error occurred updating the inode.
142.TP
3ade1aa9
MK
143.B EINTR
144While blocked waiting to complete,
145the call was interrupted by a signal handler; see
146.BR fcntl (2)
147and
148.BR signal (7).
149.TP
fea681da
MK
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
7a5097a1 167.\" This happens for at least MSDOS and VFAT file systems
5b548b31
MK
168.\" on kernel 2.6.13
169The underlying file system does not support extending
170a file beyond its current size.
171.TP
fea681da
MK
172.B EROFS
173The named file resides on a read-only file system.
174.TP
175.B ETXTBSY
176The file is a pure procedure (shared text) file that is being executed.
177.PP
178For
310b7919 179.BR ftruncate ()
fea681da
MK
180the same errors apply, but instead of things that can be wrong with
181.IR path ,
3ade1aa9 182we now have things that can be wrong with the file descriptor,
fea681da
MK
183.IR fd :
184.TP
185.B EBADF
fea681da
MK
186.I fd
187is not a valid descriptor.
188.TP
189.BR EBADF " or " EINVAL
fea681da
MK
190.I fd
191is not open for writing.
192.TP
193.B EINVAL
fea681da
MK
194.I fd
195does not reference a regular file.
196.SH "CONFORMING TO"
97c1eac8
MK
1974.4BSD, SVr4, POSIX.1-2001 (these calls first appeared in 4.2BSD).
198.\" POSIX.1-1996 has
199.\" .BR ftruncate ().
200.\" POSIX.1-2001 also has
201.\" .BR truncate (),
202.\" as an XSI extension.
203.\" .LP
204.\" SVr4 documents additional
205.\" .BR truncate ()
206.\" error conditions EMFILE, EMULTIHP, ENFILE, ENOLINK. SVr4 documents for
207.\" .BR ftruncate ()
208.\" an additional EAGAIN error condition.
fea681da
MK
209.SH NOTES
210The above description is for XSI-compliant systems.
211For non-XSI-compliant systems, the POSIX standard allows
d9bfdb9c 212two behaviors for
310b7919 213.BR ftruncate ()
fea681da
MK
214when
215.I length
216exceeds the file length
217(note that
310b7919 218.BR truncate ()
fea681da
MK
219is not specified at all in such an environment):
220either returning an error, or extending the file.
c13182ef
MK
221Like most Unix implementations, Linux follows the XSI requirement
222when dealing with native file systems.
24b74457 223However, some nonnative file systems do not permit
5b548b31 224.BR truncate ()
c13182ef 225and
5b548b31
MK
226.BR ftruncate ()
227to be used to extend a file beyond its current length:
228a notable example on Linux is VFAT.
fea681da 229.\" At the very least: OSF/1, Solaris 7, and FreeBSD conform, mtk, Jan 2002
9dcef0c0
MK
230.SH BUGS
231A header file bug in glibc 2.12 meant that the minimum value of
232.\" http://sourceware.org/bugzilla/show_bug.cgi?id=12037
233.BR _POSIX_C_SOURCE
234required to expose the declaration of
235.BR ftruncate ()
236was 200809L instead of 200112L.
237This has been fixed in later glibc versions.
fea681da
MK
238.SH "SEE ALSO"
239.BR open (2),
ad7cc990
MK
240.BR stat (2),
241.BR path_resolution (7)