xref: /original-bsd/lib/libc/sys/truncate.2 (revision 179d6f6f)
1.\" Copyright (c) 1983, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" %sccs.include.redist.man%
5.\"
6.\"     @(#)truncate.2	6.9 (Berkeley) 03/10/91
7.\"
8.Dd
9.Dt TRUNCATE 2
10.Os BSD 4.2
11.Sh NAME
12.Nm truncate ,
13.Nm ftruncate
14.Nd truncate a file to a specified length
15.Sh SYNOPSIS
16.Fd #include <unistd.h>
17.Ft int
18.Fn truncate "const char *path" "off_t length"
19.Ft int
20.Fn ftruncate "int fd" "off_t length"
21.Sh DESCRIPTION
22.Fn Truncate
23causes the file named by
24.Fa path
25or referenced by
26.Fa fd
27to be truncated to at most
28.Fa length
29bytes in size.  If the file previously
30was larger than this size, the extra data
31is lost.
32With
33.Fn ftruncate ,
34the file must be open for writing.
35.Sh RETURN VALUES
36A value of 0 is returned if the call succeeds.  If the call
37fails a -1 is returned, and the global variable
38.Va errno
39specifies the error.
40.Sh ERRORS
41.Fn Truncate
42succeeds unless:
43.Bl -tag -width [ENOTDIR]
44.It Bq Er ENOTDIR
45A component of the path prefix is not a directory.
46.It Bq Er EINVAL
47The pathname contains a character with the high-order bit set.
48.It Bq Er ENAMETOOLONG
49A component of a pathname exceeded 255 characters,
50or an entire path name exceeded 1023 characters.
51.It Bq Er ENOENT
52The named file does not exist.
53.It Bq Er EACCES
54Search permission is denied for a component of the path prefix.
55.It Bq Er EACCES
56The named file is not writable by the user.
57.It Bq Er ELOOP
58Too many symbolic links were encountered in translating the pathname.
59.It Bq Er EISDIR
60The named file is a directory.
61.It Bq Er EROFS
62The named file resides on a read-only file system.
63.It Bq Er ETXTBSY
64The file is a pure procedure (shared text) file that is being executed.
65.It Bq Er EIO
66An I/O error occurred updating the inode.
67.It Bq Er EFAULT
68.Fa Path
69points outside the process's allocated address space.
70.El
71.Pp
72.Fn Ftruncate
73succeeds unless:
74.Bl -tag -width [ENOTDIR]
75.It Bq Er EBADF
76The
77.Fa fd
78is not a valid descriptor.
79.It Bq Er EINVAL
80The
81.Fa fd
82references a socket, not a file.
83.It Bq Er EINVAL
84The
85.Fa fd
86is not open for writing.
87.El
88.Sh SEE ALSO
89.Xr open 2
90.Sh BUGS
91These calls should be generalized to allow ranges
92of bytes in a file to be discarded.
93.Sh HISTORY
94The
95.Nm
96function call appeared in
97.Bx 4.2 .
98