xref: /netbsd-src/lib/libc/sys/truncate.2 (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1.\"	$NetBSD: truncate.2,v 1.21 2004/05/13 10:20:58 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 August 18, 2002
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
73.Fn truncate
74succeeds unless:
75.Bl -tag -width Er
76.It Bq Er ENOTDIR
77A component of the path prefix is not a directory.
78.It Bq Er ENAMETOOLONG
79A component of a pathname exceeded
80.Dv {NAME_MAX}
81characters, or an entire path name exceeded
82.Dv {PATH_MAX}
83characters.
84.It Bq Er ENOENT
85The named file does not exist.
86.It Bq Er EACCES
87Search permission is denied for a component of the path prefix, or
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
100.Fa path
101points outside the process's allocated address space.
102.El
103.Pp
104.Fn ftruncate
105succeeds unless:
106.Bl -tag -width Er
107.It Bq Er EBADF
108The
109.Fa fd
110is not a valid descriptor.
111.It Bq Er EINVAL
112The
113.Fa fd
114references a socket, not a file, or
115the
116.Fa fd
117is not open for writing.
118.El
119.Sh SEE ALSO
120.Xr open 2
121.Sh HISTORY
122The
123.Fn truncate
124and
125.Fn ftruncate
126function calls appeared in
127.Bx 4.2 .
128.Sh BUGS
129These calls should be generalized to allow ranges
130of bytes in a file to be discarded.
131.Pp
132Use of
133.Fn truncate
134to extend a file is not portable.
135