1TRUNCATE(2)                Linux Programmer's Manual               TRUNCATE(2)
2
3
4

NAME

6       truncate, ftruncate - truncate a file to a specified length
7

SYNOPSIS

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           _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
19           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
20           || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
21
22       ftruncate():
23           _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
24           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
25           || /* Since glibc 2.3.5: */ _POSIX_C_SOURCE >= 200112L
26

DESCRIPTION

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       stat(2)) for the file are updated, and the set-user-ID and set-group-ID
41       permission bits may be cleared.
42
43       With ftruncate(), the file must be open for writing;  with  truncate(),
44       the file must be writable.
45

RETURN VALUE

47       On  success,  zero is returned.  On error, -1 is returned, and errno is
48       set appropriately.
49

ERRORS

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 Path points outside the process's allocated address space.
58
59       EFBIG  The argument length is larger than the maximum file size. (XSI)
60
61       EINTR  While blocked waiting to complete, the call was interrupted by a
62              signal handler; see fcntl(2) and signal(7).
63
64       EINVAL The  argument length is negative or larger than the maximum file
65              size.
66
67       EIO    An I/O error occurred updating the inode.
68
69       EISDIR The named file is a directory.
70
71       ELOOP  Too many symbolic links  were  encountered  in  translating  the
72              pathname.
73
74       ENAMETOOLONG
75              A  component of a pathname exceeded 255 characters, or an entire
76              pathname exceeded 1023 characters.
77
78       ENOENT The named file does not exist.
79
80       ENOTDIR
81              A component of the path prefix is not a directory.
82
83       EPERM  The underlying file system does not  support  extending  a  file
84              beyond its current size.
85
86       EROFS  The named file resides on a read-only file system.
87
88       ETXTBSY
89              The  file  is  a pure procedure (shared text) file that is being
90              executed.
91
92       For ftruncate() the same errors apply, but instead of things  that  can
93       be  wrong with path, we now have things that can be wrong with the file
94       descriptor, fd:
95
96       EBADF  fd is not a valid descriptor.
97
98       EBADF or EINVAL
99              fd is not open for writing.
100
101       EINVAL fd does not reference a regular file.
102

CONFORMING TO

104       4.4BSD, SVr4, POSIX.1-2001 (these calls first appeared in 4.2BSD).
105

NOTES

107       The details in DESCRIPTION are for XSI-compliant systems.  For non-XSI-
108       compliant  systems,  the POSIX standard allows two behaviors for ftrun‐
109       cate() when length exceeds the file length (note that truncate() is not
110       specified at all in such an environment): either returning an error, or
111       extending the file.  Like most UNIX implementations, Linux follows  the
112       XSI  requirement  when dealing with native file systems.  However, some
113       nonnative file systems do not permit truncate() and ftruncate()  to  be
114       used  to  extend a file beyond its current length: a notable example on
115       Linux is VFAT.
116
117       The original Linux truncate() and ftruncate()  system  calls  were  not
118       designed  to  handle large file offsets.  Consequently, Linux 2.4 added
119       truncate64() and ftruncate64() system calls that  handle  large  files.
120       However,  these  details  can  be  ignored by applications using glibc,
121       whose wrapper functions transparently employ  the  more  recent  system
122       calls where they are available.
123
124       On  some  32-bit  architectures, the calling signature for these system
125       calls differ, for the reasons described in syscall(2).
126

BUGS

128       A header file bug in  glibc  2.12  meant  that  the  minimum  value  of
129       _POSIX_C_SOURCE  required  to expose the declaration of ftruncate() was
130       200809L instead of 200112L.  This has been fixed in  later  glibc  ver‐
131       sions.
132

SEE ALSO

134       open(2), stat(2), path_resolution(7)
135

COLOPHON

137       This  page  is  part of release 3.53 of the Linux man-pages project.  A
138       description of the project, and information about reporting  bugs,  can
139       be found at http://www.kernel.org/doc/man-pages/.
140
141
142
143Linux                             2013-04-01                       TRUNCATE(2)
Impressum