1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group. In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document. The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T
.\" Portions Copyright (c) 1992, X/Open Company Limited All Rights Reserved
.\" Copyright (c) 2002, Sun Microsystems, Inc. All Rights Reserved.
.\"
.TH TRUNCATE 3C "Apr 5, 2002"
.SH NAME
truncate, ftruncate \- set a file to a specified length
.SH SYNOPSIS
.LP
.nf
#include <unistd.h>
\fBint\fR \fBtruncate\fR(\fBconst char *\fR\fIpath\fR, \fBoff_t\fR \fIlength\fR);
.fi
.LP
.nf
\fBint\fR \fBftruncate\fR(\fBint\fR \fIfildes\fR, \fBoff_t\fR \fIlength\fR);
.fi
.SH DESCRIPTION
.sp
.LP
The \fBtruncate()\fR function causes the regular file named by \fIpath\fR to
have a size equal to \fIlength\fR bytes.
.sp
.LP
If the file previously was larger than \fIlength\fR, the extra data is
discarded. If the file was previously shorter than length, its size is
increased, and the extended area appears as if it were zero-filled.
.sp
.LP
The application must ensure that the process has write permission for the file.
.sp
.LP
This function does not modify the file offset for any open file descriptions
associated with the file.
.sp
.LP
The \fBftruncate()\fR function causes the regular file referenced by
\fIfildes\fR to be truncated to \fIlength\fR. If the size of the file
previously exceeded \fIlength\fR, the extra data is no longer available to
reads on the file. If the file previously was smaller than this size,
\fBftruncate()\fR increases the size of the file with the extended area
appearing as if it were zero-filled. The value of the seek pointer is not
modified by a call to \fBftruncate()\fR.
.sp
.LP
The \fBftruncate()\fR function works only with regular files and shared memory.
If \fIfildes\fR refers to a shared memory object, \fBftruncate()\fR sets the
size of the shared memory object to \fIlength\fR. If \fIfildes\fR refers to a
directory or is not a valid file descriptor open for writing, \fBftruncate()\fR
fails.
.sp
.LP
If the effect of \fBftruncate()\fR is to decrease the size of a shared memory
object or memory mapped file and whole pages beyond the new end were previously
mapped, then the whole pages beyond the new end shall be discarded.
.sp
.LP
If the effect of \fBftruncate()\fR is to increase the size of a shared memory
object, it is unspecified if the contents of any mapped pages between the old
end-of-file and the new are flushed to the underlying object.
.sp
.LP
These functions do not modify the file offset for any open file descriptions
associated with the file. On successful completion, if the file size is
changed, these functions will mark for update the \fBst_ctime\fR and
\fBst_mtime\fR fields of the file, and if the file is a regular file, the
\fBS_ISUID\fR and \fBS_ISGID\fR bits of the file mode are left unchanged.
.sp
.LP
If the request would cause the file size to exceed the soft file size limit for
the process, the request will fail and a \fBSIGXFSZ\fR signal will be generated
for the process.
.SH RETURN VALUES
.sp
.LP
Upon successful completion, \fBftruncate()\fR and \fBtruncate()\fR return
\fB0\fR. Otherwise, \fB\(mi1\fR is returned and \fBerrno\fR is set to indicate
the error.
.SH ERRORS
.sp
.LP
The \fBftruncate()\fR and \fBtruncate()\fR functions will fail if:
.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 19n
A signal was caught during execution.
.RE
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 19n
The \fIlength\fR argument was less than 0.
.RE
.sp
.ne 2
.na
\fB\fBEFBIG\fR or \fBEINVAL\fR\fR
.ad
.RS 19n
The \fIlength\fR argument was greater than the maximum file size.
.RE
.sp
.ne 2
.na
\fB\fBEIO\fR\fR
.ad
.RS 19n
An I/O error occurred while reading from or writing to a file system.
.RE
.sp
.ne 2
.na
\fB\fBEROFS\fR\fR
.ad
.RS 19n
The named file resides on a read-only file system.
.RE
.sp
.LP
The \fBtruncate()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEACCES\fR\fR
.ad
.RS 16n
A component of the path prefix denies search permission, or write permission is
denied on the file.
.RE
.sp
.ne 2
.na
\fB\fBEFAULT\fR\fR
.ad
.RS 16n
The \fIpath\fR argument points outside the process' allocated address space.
.RE
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 16n
The \fIpath\fR argument is not an ordinary file.
.RE
.sp
.ne 2
.na
\fB\fBEISDIR\fR\fR
.ad
.RS 16n
The named file is a directory.
.RE
.sp
.ne 2
.na
\fB\fBELOOP\fR\fR
.ad
.RS 16n
Too many symbolic links were encountered in resolving \fIpath\fR.
.RE
.sp
.ne 2
.na
\fB\fBEMFILE\fR\fR
.ad
.RS 16n
The maximum number of file descriptors available to the process has been
reached.
.RE
.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
The length of the specified pathname exceeds {\fBPATH_MAX\fR} bytes, or the
length of a component of the pathname exceeds {\fBNAME_MAX\fR} bytes.
.RE
.sp
.ne 2
.na
\fB\fBENOENT\fR\fR
.ad
.RS 16n
A component of \fIpath\fR does not name an existing file or \fIpath\fR is an
empty string.
.RE
.sp
.ne 2
.na
\fB\fBENFILE\fR\fR
.ad
.RS 16n
Additional space could not be allocated for the system file table.
.RE
.sp
.ne 2
.na
\fB\fBENOTDIR\fR\fR
.ad
.RS 16n
A component of the path prefix of \fIpath\fR is not a directory.
.RE
.sp
.ne 2
.na
\fB\fBENOLINK\fR\fR
.ad
.RS 16n
The \fIpath\fR argument points to a remote machine and the link to that machine
is no longer active.
.RE
.sp
.LP
The \fBftruncate()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 19n
The file exists, mandatory file/record locking is set, and there are
outstanding record locks on the file (see \fBchmod\fR(2)).
.RE
.sp
.ne 2
.na
\fB\fBEBADF\fR or \fBEINVAL\fR\fR
.ad
.RS 19n
The \fIfildes\fR argument is not a file descriptor open for writing.
.RE
.sp
.ne 2
.na
\fB\fBEFBIG\fR\fR
.ad
.RS 19n
The file is a regular file and \fIlength\fR is greater than the offset maximum
established in the open file description associated with \fIfildes\fR.
.RE
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 19n
The \fIfildes\fR argument references a file that was opened without write
permission.
.RE
.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 19n
The \fIfildes\fR argument does not correspond to an ordinary file.
.RE
.sp
.ne 2
.na
\fB\fBENOLINK\fR\fR
.ad
.RS 19n
The \fIfildes\fR argument points to a remote machine and the link to that
machine is no longer active.
.RE
.sp
.LP
The \fBtruncate()\fR function may fail if:
.sp
.ne 2
.na
\fB\fBENAMETOOLONG\fR\fR
.ad
.RS 16n
Pathname resolution of a symbolic link produced an intermediate result whose
length exceeds {\fBPATH_MAX\fR}.
.RE
.SH USAGE
.sp
.LP
The \fBtruncate()\fR and \fBftruncate()\fR functions have transitional
interfaces for 64-bit file offsets. See \fBlf64\fR(7).
.SH ATTRIBUTES
.sp
.LP
See \fBattributes\fR(7) for descriptions of the following attributes:
.sp
.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE ATTRIBUTE VALUE
_
Interface Stability Standard
_
MT-Level MT-Safe
.TE
.SH SEE ALSO
.sp
.LP
.BR chmod (2),
.BR fcntl (2),
.BR open (2),
.BR attributes (7),
.BR lf64 (7),
.BR standards (7)
|