xref: /openbsd/lib/libc/stdio/fseek.3 (revision a6445c1d)
1.\"	$OpenBSD: fseek.3,v 1.16 2013/07/17 05:42:11 schwarze Exp $
2.\"
3.\" Copyright (c) 1990, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" This code is derived from software contributed to Berkeley by
7.\" Chris Torek and the American National Standards Committee X3,
8.\" on Information Processing Systems.
9.\"
10.\" Redistribution and use in source and binary forms, with or without
11.\" modification, are permitted provided that the following conditions
12.\" are met:
13.\" 1. Redistributions of source code must retain the above copyright
14.\"    notice, this list of conditions and the following disclaimer.
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in the
17.\"    documentation and/or other materials provided with the distribution.
18.\" 3. 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.Dd $Mdocdate: July 17 2013 $
35.Dt FSEEK 3
36.Os
37.Sh NAME
38.Nm fgetpos ,
39.Nm fseek ,
40.Nm fseeko ,
41.Nm fsetpos ,
42.Nm ftell ,
43.Nm ftello ,
44.Nm rewind
45.Nd reposition a stream
46.Sh SYNOPSIS
47.In stdio.h
48.Ft int
49.Fn fgetpos "FILE *stream" "fpos_t *pos"
50.Ft int
51.Fn fseek "FILE *stream" "long offset" "int whence"
52.Ft int
53.Fn fseeko "FILE *stream" "off_t offset" "int whence"
54.Ft int
55.Fn fsetpos "FILE *stream" "const fpos_t *pos"
56.Ft long
57.Fn ftell "FILE *stream"
58.Ft off_t
59.Fn ftello "FILE *stream"
60.Ft void
61.Fn rewind "FILE *stream"
62.Sh DESCRIPTION
63The
64.Fn fseek
65function sets the file position indicator for the stream pointed to 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 fseeko
90function is identical to
91.Fn fseek
92except that it takes an
93.Li off_t
94as its
95.Fa offset .
96.Pp
97The
98.Fn ftell
99function obtains the current value of the file position indicator for the
100stream pointed to by
101.Fa stream .
102.Pp
103The
104.Fn ftello
105function is identical to
106.Fn ftell
107except that its return value is of type
108.Li off_t .
109.Pp
110The
111.Fn rewind
112function sets the file position indicator for the stream pointed
113to by
114.Fa stream
115to the beginning of the file.
116It is equivalent to:
117.Pp
118.Dl (void)fseek(stream, 0L, SEEK_SET)
119.Pp
120except that the error indicator for the stream is also cleared
121(see
122.Xr clearerr 3 ) .
123.Pp
124The
125.Fn fgetpos
126and
127.Fn fsetpos
128functions are alternate interfaces equivalent to
129.Fn ftell
130and
131.Fn fseek
132(with whence set to
133.Dv SEEK_SET ) ,
134setting and storing the current value of
135the file offset into or from the object referenced by
136.Fa pos .
137On some systems an
138.Dq Fa fpos_t
139object may be a complex object
140and these routines may be the only way to portably reposition a text stream.
141.Sh RETURN VALUES
142The
143.Fn rewind
144function returns no value.
145Upon successful completion,
146.Fn fgetpos ,
147.Fn fseek ,
148.Fn fseeko ,
149and
150.Fn fsetpos
151return 0 and
152.Fn ftell
153and
154.Fn ftello
155return the current offset.
156Otherwise,
157.Fn fseek ,
158.Fn fseeko ,
159.Fn ftell ,
160and
161.Fn ftello
162return \-1 and
163.Fn fgetpos
164and
165.Fn fsetpos
166return a non-zero value and the global variable
167.Va errno
168is set to indicate the error.
169.Sh ERRORS
170The
171.Fn fgetpos ,
172.Fn fseek ,
173.Fn fseeko ,
174.Fn fsetpos ,
175.Fn ftell ,
176.Fn ftello ,
177and
178.Fn rewind
179functions will fail if:
180.Bl -tag -width Er
181.It Bq Er EBADF
182The
183.Fa stream
184specified is not a seekable stream.
185.El
186.Pp
187Additionally, the
188.Fn fseek
189and
190.Fn fseeko
191functions will fail if:
192.Bl -tag -width Er
193.It Bq Er EINVAL
194The
195.Fa whence
196argument was not
197.Dv SEEK_SET ,
198.Dv SEEK_END ,
199or
200.Dv SEEK_CUR .
201.El
202.Pp
203Additionally, the
204.Fn ftell
205function will fail if:
206.Bl -tag -width Er
207.It Bq Er EOVERFLOW
208The value of the file position indicator is too large to be represented by a
209.Vt long .
210.El
211.Pp
212Finally, the functions
213.Fn fgetpos ,
214.Fn fseek ,
215.Fn fseeko ,
216.Fn fsetpos ,
217.Fn ftell ,
218and
219.Fn ftello
220may also fail and set
221.Va errno
222for any of the errors specified for the routines
223.Xr fflush 3 ,
224.Xr fstat 2 ,
225.Xr lseek 2 ,
226and
227.Xr malloc 3 .
228.Sh SEE ALSO
229.Xr lseek 2
230.Sh STANDARDS
231The
232.Fn fgetpos ,
233.Fn fsetpos ,
234.Fn fseek ,
235.Fn ftell ,
236and
237.Fn rewind
238functions conform to
239.St -ansiC
240and
241.St -xpg4 .
242.Pp
243The
244.Fn fseeko
245and
246.Fn ftello
247functions conform to
248.St -xpg4 .
249.Sh HISTORY
250The functions
251.Fn fseek ,
252.Fn ftell ,
253and
254.Fn rewind
255first appeared in
256.At v7 .
257