1FTRUNCATE(3P) POSIX Programmer's Manual FTRUNCATE(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 ftruncate - truncate a file to a specified length
13
15 #include <unistd.h>
16
17 int ftruncate(int fildes, off_t length);
18
19
21 If fildes is not a valid file descriptor open for writing, the ftrunâ
22 cate() function shall fail.
23
24 If fildes refers to a regular file, the ftruncate() function shall
25 cause the size of the file to be truncated to length. If the size of
26 the file previously exceeded length, the extra data shall no longer be
27 available to reads on the file. If the file previously was smaller than
28 this size, ftruncate() shall either increase the size of the file or
29 fail. XSI-conformant systems shall increase the size of the file. If
30 the file size is increased, the extended area shall appear as if it
31 were zero-filled. The value of the seek pointer shall not be modified
32 by a call to ftruncate().
33
34 Upon successful completion, if fildes refers to a regular file, the
35 ftruncate() function shall mark for update the st_ctime and st_mtime
36 fields of the file and the S_ISUID and S_ISGID bits of the file mode
37 may be cleared. If the ftruncate() function is unsuccessful, the file
38 is unaffected.
39
40 If the request would cause the file size to exceed the soft file size
41 limit for the process, the request shall fail and the implementation
42 shall generate the SIGXFSZ signal for the thread.
43
44 If fildes refers to a directory, ftruncate() shall fail.
45
46 If fildes refers to any other file type, except a shared memory object,
47 the result is unspecified.
48
49 If fildes refers to a shared memory object, ftruncate() shall set the
50 size of the shared memory object to length.
51
52 If the effect of ftruncate() is to decrease the size of a shared memory
53 object or memory mapped file and whole pages beyond the new end were
54 previously mapped, then the whole pages beyond the new end shall be
55 discarded.
56
57 If the Memory Protection option is supported, references to discarded
58 pages shall result in the generation of a SIGBUS signal; otherwise, the
59 result of such references is undefined.
60
61 If the effect of ftruncate() is to increase the size of a shared memory
62 object, it is unspecified whether the contents of any mapped pages
63 between the old end-of-file and the new are flushed to the underlying
64 object.
65
67 Upon successful completion, ftruncate() shall return 0; otherwise, -1
68 shall be returned and errno set to indicate the error.
69
71 The ftruncate() function shall fail if:
72
73 EINTR A signal was caught during execution.
74
75 EINVAL The length argument was less than 0.
76
77 EFBIG or EINVAL
78
79 The length argument was greater than the maximum file size.
80
81 EFBIG The file is a regular file and length is greater than the offset
82 maximum established in the open file description associated with
83 fildes.
84
85 EIO An I/O error occurred while reading from or writing to a file
86 system.
87
88 EBADF or EINVAL
89
90 The fildes argument is not a file descriptor open for writing.
91
92 EINVAL The fildes argument references a file that was opened without
93 write permission.
94
95 EROFS The named file resides on a read-only file system.
96
97
98 The following sections are informative.
99
101 None.
102
104 None.
105
107 The ftruncate() function is part of IEEE Std 1003.1-2001 as it was
108 deemed to be more useful than truncate(). The truncate() function is
109 provided as an XSI extension.
110
112 None.
113
115 open(), truncate(), the Base Definitions volume of
116 IEEE Std 1003.1-2001, <unistd.h>
117
119 Portions of this text are reprinted and reproduced in electronic form
120 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
121 -- Portable Operating System Interface (POSIX), The Open Group Base
122 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
123 Electrical and Electronics Engineers, Inc and The Open Group. In the
124 event of any discrepancy between this version and the original IEEE and
125 The Open Group Standard, the original IEEE and The Open Group Standard
126 is the referee document. The original Standard can be obtained online
127 at http://www.opengroup.org/unix/online.html .
128
129
130
131IEEE/The Open Group 2003 FTRUNCATE(3P)