1TRUNCATE(2) Linux Programmer's Manual TRUNCATE(2)
2
3
4
6 truncate, ftruncate - truncate a file to a specified length
7
9 #include <unistd.h>
10 #include <sys/types.h>
11
12 int truncate(const char *path, off_t length);
13 int ftruncate(int fd, off_t length);
14
15 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17 truncate(): _BSD_SOURCE || _XOPEN_SOURCE >= 500
18 ftruncate(): _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
19 _POSIX_C_SOURCE >= 200112L
20
22 The truncate() and ftruncate() functions cause the regular file named
23 by path or referenced by fd to be truncated to a size of precisely
24 length bytes.
25
26 If the file previously was larger than this size, the extra data is
27 lost. If the file previously was shorter, it is extended, and the
28 extended part reads as null bytes ('\0').
29
30 The file offset is not changed.
31
32 If the size changed, then the st_ctime and st_mtime fields (respec‐
33 tively, time of last status change and time of last modification; see
34 stat(2)) for the file are updated, and the set-user-ID and set-group-ID
35 permission bits may be cleared.
36
37 With ftruncate(), the file must be open for writing; with truncate(),
38 the file must be writable.
39
41 On success, zero is returned. On error, -1 is returned, and errno is
42 set appropriately.
43
45 For truncate():
46
47 EACCES Search permission is denied for a component of the path prefix,
48 or the named file is not writable by the user. (See also
49 path_resolution(7).)
50
51 EFAULT Path points outside the process's allocated address space.
52
53 EFBIG The argument length is larger than the maximum file size. (XSI)
54
55 EINTR A signal was caught during execution.
56
57 EINVAL The argument length is negative or larger than the maximum file
58 size.
59
60 EIO An I/O error occurred updating the inode.
61
62 EINTR While blocked waiting to complete, the call was interrupted by a
63 signal handler; see fcntl(2) and signal(7).
64
65 EISDIR The named file is a directory.
66
67 ELOOP Too many symbolic links were encountered in translating the
68 pathname.
69
70 ENAMETOOLONG
71 A component of a pathname exceeded 255 characters, or an entire
72 pathname exceeded 1023 characters.
73
74 ENOENT The named file does not exist.
75
76 ENOTDIR
77 A component of the path prefix is not a directory.
78
79 EPERM The underlying file system does not support extending a file
80 beyond its current size.
81
82 EROFS The named file resides on a read-only file system.
83
84 ETXTBSY
85 The file is a pure procedure (shared text) file that is being
86 executed.
87
88 For ftruncate() the same errors apply, but instead of things that can
89 be wrong with path, we now have things that can be wrong with the file
90 descriptor, fd:
91
92 EBADF fd is not a valid descriptor.
93
94 EBADF or EINVAL
95 fd is not open for writing.
96
97 EINVAL fd does not reference a regular file.
98
100 4.4BSD, SVr4, POSIX.1-2001 (these calls first appeared in 4.2BSD).
101
103 The above description is for XSI-compliant systems. For non-XSI-com‐
104 pliant systems, the POSIX standard allows two behaviors for ftruncate()
105 when length exceeds the file length (note that truncate() is not speci‐
106 fied at all in such an environment): either returning an error, or
107 extending the file. Like most Unix implementations, Linux follows the
108 XSI requirement when dealing with native file systems. However, some
109 nonnative file systems do not permit truncate() and ftruncate() to be
110 used to extend a file beyond its current length: a notable example on
111 Linux is VFAT.
112
114 open(2), stat(2), path_resolution(7)
115
117 This page is part of release 3.25 of the Linux man-pages project. A
118 description of the project, and information about reporting bugs, can
119 be found at http://www.kernel.org/doc/man-pages/.
120
121
122
123Linux 2009-02-28 TRUNCATE(2)