xref: /freebsd-12.1/lib/libc/sys/truncate.2 (revision 7d0fc2f4)
1.\" Copyright (c) 1983, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     @(#)truncate.2	8.1 (Berkeley) 6/4/93
33.\" $FreeBSD$
34.\"
35.Dd June 4, 1993
36.Dt TRUNCATE 2
37.Os
38.Sh NAME
39.Nm truncate ,
40.Nm ftruncate
41.Nd truncate or extend a file to a specified length
42.Sh LIBRARY
43.Lb libc
44.Sh SYNOPSIS
45.In unistd.h
46.Ft int
47.Fn truncate "const char *path" "off_t length"
48.Ft int
49.Fn ftruncate "int fd" "off_t length"
50.Sh DESCRIPTION
51The
52.Fn truncate
53system call
54causes the file named by
55.Fa path
56or referenced by
57.Fa fd
58to be truncated or extended to
59.Fa length
60bytes in size.
61If the file
62was larger than this size, the extra data
63is lost.
64If the file was smaller than this size,
65it will be extended as if by writing bytes
66with the value zero.
67With
68.Fn ftruncate ,
69the file must be open for writing.
70.Sh RETURN VALUES
71.Rv -std
72.Sh ERRORS
73The
74.Fn truncate
75system call
76succeeds unless:
77.Bl -tag -width Er
78.It Bq Er ENOTDIR
79A component of the path prefix is not a directory.
80.It Bq Er ENAMETOOLONG
81A component of a pathname exceeded 255 characters,
82or an entire path name exceeded 1023 characters.
83.It Bq Er ENOENT
84The named file does not exist.
85.It Bq Er EACCES
86Search permission is denied for a component of the path prefix.
87.It Bq Er EACCES
88The named file is not writable by the user.
89.It Bq Er ELOOP
90Too many symbolic links were encountered in translating the pathname.
91.It Bq Er EISDIR
92The named file is a directory.
93.It Bq Er EROFS
94The named file resides on a read-only file system.
95.It Bq Er ETXTBSY
96The file is a pure procedure (shared text) file that is being executed.
97.It Bq Er EIO
98An I/O error occurred updating the inode.
99.It Bq Er EFAULT
100The
101.Fa path
102argument
103points outside the process's allocated address space.
104.El
105.Pp
106The
107.Fn ftruncate
108system call
109succeeds unless:
110.Bl -tag -width Er
111.It Bq Er EBADF
112The
113.Fa fd
114argument
115is not a valid descriptor.
116.It Bq Er EINVAL
117The
118.Fa fd
119argument
120references a socket, not a file.
121.It Bq Er EINVAL
122The
123.Fa fd
124descriptor
125is not open for writing.
126.El
127.Sh SEE ALSO
128.Xr open 2
129.Sh BUGS
130These calls should be generalized to allow ranges
131of bytes in a file to be discarded.
132.Pp
133Use of
134.Fn truncate
135to extend a file is not portable.
136.Sh HISTORY
137The
138.Fn truncate
139system call appeared in
140.Bx 4.2 .
141