xref: /openbsd/lib/libc/sys/truncate.2 (revision 17df1aa7)
1.\"	$OpenBSD: truncate.2,v 1.13 2007/05/31 19:19:34 jmc 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: May 31 2007 $
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
68succeeds unless:
69.Bl -tag -width Er
70.It Bq Er ENOTDIR
71A component of the path prefix is not a directory.
72.It Bq Er ENAMETOOLONG
73A component of a pathname exceeded
74.Dv {NAME_MAX}
75characters, or an entire path name exceeded
76.Dv {PATH_MAX}
77characters.
78.It Bq Er ENOENT
79The named file does not exist.
80.It Bq Er EACCES
81Search permission is denied for a component of the path prefix.
82.It Bq Er EACCES
83The named file is not writable by the user.
84.It Bq Er ELOOP
85Too many symbolic links were encountered in translating the pathname.
86.It Bq Er EISDIR
87The named file is a directory.
88.It Bq Er EROFS
89The named file resides on a read-only file system.
90.It Bq Er ETXTBSY
91The file is a pure procedure (shared text) file that is being executed.
92.It Bq Er EIO
93An I/O error occurred updating the inode.
94.It Bq Er EFAULT
95.Fa path
96points outside the process's allocated address space.
97.It Bq Er EPERM
98The effective user ID does not match the owner of the file and
99the effective user ID is not the superuser.
100.El
101.Pp
102.Fn ftruncate
103succeeds unless:
104.Bl -tag -width Er
105.It Bq Er EBADF
106The
107.Fa fd
108is not a valid descriptor.
109.It Bq Er EINVAL
110The
111.Fa fd
112references a socket, not a file.
113.It Bq Er EINVAL
114The
115.Fa fd
116is not open for writing.
117.It Bq Er EINVAL
118The
119.Fa length
120is a negative value.
121.El
122.Sh SEE ALSO
123.Xr open 2
124.Sh HISTORY
125The
126.Fn truncate
127and
128.Fn ftruncate
129function calls appeared in
130.Bx 4.2 .
131.Sh BUGS
132These calls should be generalized to allow ranges of bytes in a file
133to be discarded.
134.Pp
135Use of
136.Fn truncate
137to extend a file is not portable.
138