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