]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/truncate.2
xnu-1456.1.26.tar.gz
[apple/xnu.git] / bsd / man / man2 / truncate.2
CommitLineData
9bccf70c
A
1.\" $NetBSD: truncate.2,v 1.7 1995/02/27 12:39:00 cgd 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. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" @(#)truncate.2 8.1 (Berkeley) 6/4/93
35.\"
36.Dd June 4, 1993
37.Dt TRUNCATE 2
38.Os BSD 4.2
39.Sh NAME
2d21ac55
A
40.Nm ftruncate ,
41.Nm truncate
9bccf70c
A
42.Nd truncate or extend a file to a specified length
43.Sh SYNOPSIS
44.Fd #include <unistd.h>
45.Ft int
2d21ac55
A
46.Fo ftruncate
47.Fa "int fildes"
48.Fa "off_t length"
49.Fc
9bccf70c 50.Ft int
2d21ac55
A
51.Fo truncate
52.Fa "const char *path"
53.Fa "off_t length"
54.Fc
9bccf70c 55.Sh DESCRIPTION
b0d623f7
A
56.Fn ftruncate
57and
58.Fn truncate
59cause the file named by
60.Fa path ,
9bccf70c 61or referenced by
b0d623f7
A
62.Fa fildes ,
63to be truncated (or extended) to
9bccf70c 64.Fa length
b0d623f7
A
65bytes in size. If the file size exceeds
66.Fa length ,
67any extra data is discarded. If the file size is smaller than
68.Fa length ,
69the file is extended and filled with zeros to the indicated length.
70The
71.Fn ftruncate
72form requires the file to be open for writing.
73.Pp
74Note:
75.Fn ftruncate
76and
77.Fn truncate
78do not modify the current file offset for any open file descriptions associated with the file.
9bccf70c
A
79.Sh RETURN VALUES
80A value of 0 is returned if the call succeeds. If the call
81fails a -1 is returned, and the global variable
82.Va errno
83specifies the error.
84.Sh ERRORS
2d21ac55
A
85.Pp
86The
87.Fn ftruncate
88system call will fail if:
9bccf70c 89.Bl -tag -width Er
2d21ac55
A
90.\" ===========
91.It Bq Er EBADF
92.Fa fildes
93is not a valid descriptor open for writing.
94.\" ===========
95.It Bq Er EFBIG
96The file is a regular file and
97.Fa length
98is greater than the offset maximum established
99in the open file description associated with
100.Fa fildes .
101.\" ===========
102.It Bq Er EINVAL
103.Fa fildes
104references a socket, not a file.
105.\" ===========
106.It Bq Er EINVAL
107.Fa fildes
108is not open for writing.
109.\" ===========
110.It Bq Er EROFS
111The named file resides on a read-only file system.
112.El
113.Pp
114The
115.Fn truncate
116system call will fail if:
117.Bl -tag -width Er
118.\" ===========
9bccf70c
A
119.It Bq Er EACCES
120Search permission is denied for a component of the path prefix.
2d21ac55 121.\" ===========
9bccf70c
A
122.It Bq Er EACCES
123The named file is not writable by the user.
2d21ac55
A
124.\" ===========
125.It Bq Er EFAULT
126.Fa Path
127points outside the process's allocated address space.
128.\" ===========
9bccf70c
A
129.It Bq Er EISDIR
130The named file is a directory.
2d21ac55
A
131.\" ===========
132.It Bq Er ELOOP
133Too many symbolic links are encountered in translating the pathname.
134This is taken to be indicative of a looping symbolic link.
135.\" ===========
136.It Bq Er ENAMETOOLONG
137A component of a pathname exceeds
138.Dv {NAME_MAX}
139characters, or an entire path name exceeds
140.Dv {PATH_MAX}
141characters.
142.\" ===========
143.It Bq Er ENOENT
144The named file does not exist.
145.\" ===========
146.It Bq Er ENOTDIR
147A component of the path prefix is not a directory.
148.\" ===========
9bccf70c
A
149.It Bq Er EROFS
150The named file resides on a read-only file system.
2d21ac55 151.\" ===========
9bccf70c
A
152.It Bq Er ETXTBSY
153The file is a pure procedure (shared text) file that is being executed.
9bccf70c
A
154.El
155.Pp
9bccf70c 156The
2d21ac55
A
157.Fn ftruncate
158and
159.Fn truncate
160system calls will fail if:
161.Bl -tag -width Er
162.\" ===========
163.It Bq Er EFBIG
164The length argument was greater than the maximum file size.
165.\" ===========
166.It Bq Er EINTR
167A signal is caught during execution.
168.\" ===========
9bccf70c
A
169.It Bq Er EINVAL
170The
2d21ac55
A
171.Fa length
172argument is less than 0.
173.\" ===========
174.It Bq Er EIO
175An I/O error occurred while reading from or writing to a file system.
9bccf70c
A
176.El
177.Sh SEE ALSO
178.Xr open 2
179.Sh BUGS
180These calls should be generalized to allow ranges
181of bytes in a file to be discarded.
182.Pp
183Use of
184.Fn truncate
185to extend a file is not portable.
186.Sh HISTORY
187The
188.Fn truncate
189and
190.Fn ftruncate
191function calls appeared in
192.Bx 4.2 .