xref: /netbsd-src/lib/libc/stdio/fseek.3 (revision ae9172d6cd9432a6a1a56760d86b32c57a66c39c)
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek and the American National Standards Committee X3,
6.\" on Information Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software
17.\"    must display the following acknowledgement:
18.\"	This product includes software developed by the University of
19.\"	California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\"    may be used to endorse or promote products derived from this software
22.\"    without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\"     from: @(#)fseek.3	6.11 (Berkeley) 6/29/91
37.\"	$Id: fseek.3,v 1.5 1993/11/30 21:54:05 jtc Exp $
38.\"
39.Dd June 29, 1991
40.Dt FSEEK 3
41.Os
42.Sh NAME
43.Nm fgetpos ,
44.Nm fseek ,
45.Nm fsetpos ,
46.Nm ftell ,
47.Nm rewind
48.Nd reposition a stream
49.Sh SYNOPSIS
50.Fd #include <stdio.h>
51.Ft int
52.Fn fseek "FILE *stream" "long offset" "int whence"
53.Ft long
54.Fn ftell "FILE *stream"
55.Ft void
56.Fn rewind "FILE *stream"
57.Ft int
58.Fn fgetpos "FILE *stream" "fpos_t *pos"
59.Ft int
60.Fn fsetpos "FILE *stream" "fpos_t *pos"
61.Sh DESCRIPTION
62The
63.Fn fseek
64function sets the file position indicator for the stream pointed
65to by
66.Fa stream .
67The new position, measured in bytes, is obtained by adding
68.Fa offset
69bytes to the position specified by
70.Fa whence .
71If
72.Fa whence
73is set to
74.Dv SEEK_SET ,
75.Dv SEEK_CUR ,
76or
77.Dv SEEK_END ,
78the offset is relative to the
79start of the file, the current position indicator, or end-of-file,
80respectively.
81A successful call to the
82.Fn fseek
83function clears the end-of-file indicator for the stream and undoes
84any effects of the
85.Xr ungetc 3
86function on the same stream.
87.Pp
88The
89.Fn ftell
90function
91obtains the current value of the file position indicator for the
92stream pointed to by
93.Fa stream .
94.Pp
95The
96.Fn rewind
97function sets the file position indicator for the stream pointed
98to by
99.Fa stream
100to the beginning of the file.
101It is equivalent to:
102.Pp
103.Dl (void)fseek(stream, 0L, SEEK_SET)
104.Pp
105except that the error indicator for the stream is also cleared
106(see
107.Xr clearerr 3 ) .
108.Pp
109The
110.Fn fgetpos
111and
112.Fn fsetpos
113functions
114are alternate interfaces equivalent to
115.Fn ftell
116and
117.Fn fseek
118(with whence set to
119.Dv SEEK_SET
120), setting and storing the current value of
121the file offset into or from the object referenced by
122.Fa pos .
123On some
124.Pq non- Ns Tn UNIX
125systems an
126.Dq Fa fpos_t
127object may be a complex object
128and these routines may be the only way to portably reposition a text stream.
129.Sh RETURN VALUES
130The
131.Fn rewind
132function
133returns no value.
134Upon successful completion,
135.Fn fgetpos ,
136.Fn fseek ,
137.Fn fsetpos
138return 0,
139and
140.Fn ftell
141returns the current offset.
142Otherwise,
143.Fn fseek
144returns \-1 and
145the others
146return a nonzero value and the global variable
147.Va errno
148is set to indicate the error.
149.Sh ERRORS
150.Bl -tag -width Er
151.It Bq Er EBADF
152The
153.Fa stream
154specified
155is not a seekable stream.
156.It Bq Er EINVAL
157The
158.Fa whence
159argument to
160.Fn fseek
161was not
162.Dv SEEK_SET ,
163.Dv SEEK_END ,
164or
165.Dv SEEK_CUR .
166.El
167.Pp
168The function
169.Fn fgetpos ,
170.Fn fseek ,
171.Fn fsetpos ,
172and
173.Fn ftell
174may also fail and set
175.Va errno
176for any of the errors specified for the routines
177.Xr fflush 3 ,
178.Xr fstat 2 ,
179.Xr lseek 2 ,
180and
181.Xr malloc 3 .
182.Sh SEE ALSO
183.Xr lseek 2
184.Sh STANDARDS
185The
186.Fn fgetpos ,
187.Fn fsetpos ,
188.Fn fseek ,
189.Fn ftell ,
190and
191.Fn rewind
192functions
193conform to
194.St -ansiC .
195