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():
18 _XOPEN_SOURCE >= 500
19 || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
20 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
21
22 ftruncate():
23 _XOPEN_SOURCE >= 500
24 || /* Since glibc 2.3.5: */ _POSIX_C_SOURCE >= 200112L
25 || /* Glibc versions <= 2.19: */ _BSD_SOURCE
26
28 The truncate() and ftruncate() functions cause the regular file named
29 by path or referenced by fd to be truncated to a size of precisely
30 length bytes.
31
32 If the file previously was larger than this size, the extra data is
33 lost. If the file previously was shorter, it is extended, and the
34 extended part reads as null bytes ('\0').
35
36 The file offset is not changed.
37
38 If the size changed, then the st_ctime and st_mtime fields (respecā
39 tively, time of last status change and time of last modification; see
40 inode(7)) for the file are updated, and the set-user-ID and set-group-
41 ID mode bits may be cleared.
42
43 With ftruncate(), the file must be open for writing; with truncate(),
44 the file must be writable.
45
47 On success, zero is returned. On error, -1 is returned, and errno is
48 set appropriately.
49
51 For truncate():
52
53 EACCES Search permission is denied for a component of the path prefix,
54 or the named file is not writable by the user. (See also
55 path_resolution(7).)
56
57 EFAULT The argument path points outside the process's allocated address
58 space.
59
60 EFBIG The argument length is larger than the maximum file size. (XSI)
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 EINVAL The argument length is negative or larger than the maximum file
66 size.
67
68 EIO An I/O error occurred updating the inode.
69
70 EISDIR The named file is a directory.
71
72 ELOOP Too many symbolic links were encountered in translating the
73 pathname.
74
75 ENAMETOOLONG
76 A component of a pathname exceeded 255 characters, or an entire
77 pathname exceeded 1023 characters.
78
79 ENOENT The named file does not exist.
80
81 ENO