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