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