OSDN Git Service

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