xref: /freebsd/lib/libc/stdio/fseek.3 (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
1.\" Copyright (c) 1990, 1991, 1993
2.\"	The Regents of the University of California.  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.\"     @(#)fseek.3	8.1 (Berkeley) 6/4/93
37.\"	$Id$
38.\"
39.Dd Mar 5, 1999
40.Dt FSEEK 3
41.Os
42.Sh NAME
43.Nm fgetpos ,
44.Nm fseek ,
45.Nm fseeko ,
46.Nm fsetpos ,
47.Nm ftell ,
48.Nm ftello ,
49.Nm rewind
50.Nd reposition a stream
51.Sh SYNOPSIS
52.Fd #include <stdio.h>
53.Ft int
54.Fn fseek "FILE *stream" "long offset" "int whence"
55.Ft long
56.Fn ftell "FILE *stream"
57.Ft void
58.Fn rewind "FILE *stream"
59.Ft int
60.Fn fgetpos "FILE *stream" "fpos_t *pos"
61.Ft int
62.Fn fsetpos "FILE *stream" "const fpos_t *pos"
63.Fd #include <sys/types.h>
64.Ft int
65.Fn fseeko "FILE *stream" "off_t offset" "int whence"
66.Ft off_t
67.Fn ftello "FILE *stream"
68.Sh DESCRIPTION
69The
70.Fn fseek
71function sets the file position indicator for the stream pointed
72to by
73.Fa stream .
74The new position, measured in bytes, is obtained by adding
75.Fa offset
76bytes to the position specified by
77.Fa whence .
78If
79.Fa whence
80is set to
81.Dv SEEK_SET ,
82.Dv SEEK_CUR ,
83or
84.Dv SEEK_END ,
85the offset is relative to the
86start of the file, the current position indicator, or end-of-file,
87respectively.
88A successful call to the
89.Fn fseek
90function clears the end-of-file indicator for the stream and undoes
91any effects of the
92.Xr ungetc 3
93function on the same stream.
94.Pp
95The
96.Fn ftell
97function
98obtains the current value of the file position indicator for the
99stream pointed to by
100.Fa stream .
101.Pp
102The
103.Fn rewind
104function sets the file position indicator for the stream pointed
105to by
106.Fa stream
107to the beginning of the file.
108It is equivalent to:
109.Pp
110.Dl (void)fseek(stream, 0L, SEEK_SET)
111.Pp
112except that the error indicator for the stream is also cleared
113(see
114.Xr clearerr 3 ) .
115.Pp
116The
117.Fn fseeko
118function is identical to
119.Fn fseek ,
120except it takes an
121.Fa off_t
122argument
123instead of a
124.Fa long .
125Likewise, the
126.Fn ftello
127function is identical to
128.Fn ftell ,
129except it returns an
130.Fa off_t .
131.Pp
132The
133.Fn fgetpos
134and
135.Fn fsetpos
136functions
137are alternate interfaces equivalent to
138.Fn ftell
139and
140.Fn fseek
141(with whence set to
142.Dv SEEK_SET ) ,
143setting and storing the current value of
144the file offset into or from the object referenced by
145.Fa pos .
146On some
147.Pq non- Ns Tn UNIX
148systems an
149.Dq Fa fpos_t
150object may be a complex object
151and these routines may be the only way to portably reposition a text stream.
152.Sh RETURN VALUES
153The
154.Fn rewind
155function
156returns no value.
157Upon successful completion,
158.Fn fgetpos ,
159.Fn fseek ,
160.Fn fseeko ,
161and
162.Fn fsetpos
163return 0,
164and
165.Fn ftell
166and
167.Fn ftello
168return the current offset.
169Otherwise, \-1 is returned and the global variable errno is set to
170indicate the error.
171.Sh ERRORS
172.Bl -tag -width [EINVAL]
173.It Bq Er EBADF
174The
175.Fa stream
176specified
177is not a seekable stream.
178.It Bq Er EINVAL
179The
180.Fa whence
181argument to
182.Fn fseek
183was not
184.Dv SEEK_SET ,
185.Dv SEEK_END ,
186or
187.Dv SEEK_CUR .
188.It Bq Er EOVERFLOW
189For
190.Fn ftell ,
191the resulting file offset would be a value which
192cannot be represented correctly in an object of type long.
193.El
194.Pp
195The functions
196.Fn fgetpos ,
197.Fn fseek ,
198.Fn fseeko ,
199.Fn fsetpos ,
200.Fn ftell ,
201and
202.Fn ftello
203may also fail and set
204.Va errno
205for any of the errors specified for the routines
206.Xr fflush 3 ,
207.Xr fstat 2 ,
208.Xr lseek 2 ,
209and
210.Xr malloc 3 .
211.Sh SEE ALSO
212.Xr lseek 2
213.Sh STANDARDS
214The
215.Fn fgetpos ,
216.Fn fsetpos ,
217.Fn fseek ,
218.Fn ftell ,
219and
220.Fn rewind
221functions
222conform to
223.St -ansiC .
224.Pp
225The
226.Fn fseeko
227and
228.Fn ftello
229functions conform to
230.St -susv2 .
231