summaryrefslogtreecommitdiff
path: root/usr/src/man/man3c/fseek.3c
blob: fbcdaac52c14b7533831675c05354b42d7376a95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
.\"
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for
.\" permission to reproduce portions of its copyrighted documentation.
.\" Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\"
.\" The Institute of Electrical and Electronics Engineers and The Open
.\" Group, have given us permission to reprint portions of their
.\" documentation.
.\"
.\" In the following statement, the phrase ``this text'' refers to portions
.\" of the system documentation.
.\"
.\" Portions of this text are reprinted and reproduced in electronic form
.\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition,
.\" Standard for Information Technology -- Portable Operating System
.\" Interface (POSIX), The Open Group Base Specifications Issue 6,
.\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics
.\" Engineers, Inc and The Open Group.  In the event of any discrepancy
.\" between these versions and the original IEEE and The Open Group
.\" Standard, the original IEEE and The Open Group Standard is the referee
.\" document.  The original Standard can be obtained online at
.\" http://www.opengroup.org/unix/online.html.
.\"
.\" This notice shall appear on any product containing this material.
.\"
.\" The contents of this file are subject to the terms of the
.\" Common Development and Distribution License (the "License").
.\" You may not use this file except in compliance with the License.
.\"
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
.\" or http://www.opensolaris.org/os/licensing.
.\" See the License for the specific language governing permissions
.\" and limitations under the License.
.\"
.\" When distributing Covered Code, include this CDDL HEADER in each
.\" file and include the License file at usr/src/OPENSOLARIS.LICENSE.
.\" If applicable, add the following below this CDDL HEADER, with the
.\" fields enclosed by brackets "[]" replaced with your own identifying
.\" information: Portions Copyright [yyyy] [name of copyright owner]
.\"
.\"
.\" Copyright 1989 AT&T.
.\" Portions Copyright (c) 1992, X/Open Company Limited.  All Rights Reserved.
.\" Copyright (c) 2003, Sun Microsystems, Inc.  All Rights Reserved.
.\"
.TH FSEEK 3C "March 25, 2020"
.SH NAME
fseek, fseeko \- reposition a file-position indicator in a stream
.SH SYNOPSIS
.nf
#include <stdio.h>

\fBint\fR \fBfseek\fR(\fBFILE *\fR\fIstream\fR, \fBlong\fR \fIoffset\fR, \fBint\fR \fIwhence\fR);
.fi

.LP
.nf
\fBint\fR \fBfseeko\fR(\fBFILE *\fR\fIstream\fR, \fBoff_t\fR \fIoffset\fR, \fBint\fR \fIwhence\fR);
.fi

.SH DESCRIPTION
The \fBfseek()\fR function sets the file-position indicator for the stream
pointed to by \fIstream\fR. The \fBfseeko()\fR function is identical to
\fBfseek()\fR except for the type of \fIoffset\fR.
.sp
.LP
The new position, measured in bytes from the beginning of the file, is obtained
by adding \fIoffset\fR to the position specified by \fIwhence\fR, whose values
are defined in <\fBstdio.h\fR> as follows:
.sp
.ne 2
.na
\fB\fBSEEK_SET\fR\fR
.ad
.RS 12n
Set position equal to \fIoffset\fR bytes.
.RE

.sp
.ne 2
.na
\fB\fBSEEK_CUR\fR\fR
.ad
.RS 12n
Set position to current location plus \fIoffset\fR.
.RE

.sp
.ne 2
.na
\fB\fBSEEK_END\fR\fR
.ad
.RS 12n
Set position to \fBEOF\fR plus \fIoffset\fR.
.RE

.sp
.LP
If the stream is to be used with wide character input/output functions,
\fIoffset\fR must either be 0 or a value returned by an earlier call to
\fBftell\fR(3C) on the same stream and \fIwhence\fR must be \fBSEEK_SET\fR. This
constraint does not hold for streams created by \fBopen_wmemstream\fR(3C).
.sp
.LP
A successful call to \fBfseek()\fR clears the end-of-file indicator for the
stream and undoes any effects of \fBungetc\fR(3C) and \fBungetwc\fR(3C) on the
same stream.  After an \fBfseek()\fR call, the next operation on an update
stream may be either input or output.
.sp
.LP
If the most recent operation, other than \fBftell\fR(3C), on a given stream is
\fBfflush\fR(3C), the file offset in the underlying open file description will
be adjusted to reflect the location specified by \fBfseek()\fR.
.sp
.LP
The \fBfseek()\fR function allows the file-position indicator to be set beyond
the end of existing data in the file. If data is later written at this point,
subsequent reads of data in the gap will return bytes with the value 0 until
data is actually written into the gap.
.sp
.LP
The value of the file offset returned by \fBfseek()\fR on devices which are
incapable of seeking is undefined.
.sp
.LP
If the stream is writable and buffered data had not been written to the
underlying file, \fBfseek()\fR will cause the unwritten data to be written to
the file and mark the \fBst_ctime\fR and \fBst_mtime\fR fields of the file for
update.
.SS \fBopen_wmemstream\fR(3C)
When using a stream based on
.B open_wmemstream\fR(3C),
the
.B fseek()
and
.B fseeko()
functions no longer operate in terms of bytes. Instead, like the stream itself,
the values used in
.I offset
are measured in units of wide characters, the underlying data unit of the
stream. The values returned by
.B ftell(3C)
or
.B ftello(3C)
are also in these same units, allowing them to be used in the same way. These
streams do not have the constraints of other wide character streams and may the
full range of values in
.I whence
and
.I offset,
the same as would be done for a normal byte-oriented stream.
.SH RETURN VALUES
The \fBfseek()\fR and \fBfseeko()\fR functions return \fB0\fR on success;
otherwise, they returned \fB\(mi1\fR and set \fBerrno\fR to indicate the error.
.SH ERRORS
The \fBfseek()\fR and \fBfseeko()\fR functions will fail if, either the
\fIstream\fR is unbuffered or the \fIstream\fR's buffer needed to be flushed,
and the call to \fBfseek()\fR or \fBfseeko()\fR causes an underlying
\fBlseek\fR(2) or \fBwrite\fR(2) to be invoked:
.sp
.ne 2
.na
\fB\fBEAGAIN\fR\fR
.ad
.RS 10n
The \fBO_NONBLOCK\fR flag is set for the file descriptor and the process would
be delayed in the write operation.
.RE

.sp
.ne 2
.na
\fB\fBEBADF\fR\fR
.ad
.RS 10n
The file descriptor underlying the stream file is not open for writing or the
stream's buffer needed to be flushed and the file is not open.
.RE

.sp
.ne 2
.na
\fB\fBEFBIG\fR\fR
.ad
.RS 10n
An attempt was made to write a file that exceeds the maximum file size or the
process's file size limit, or the file is a regular file and an attempt was
made to write at or beyond the offset maximum associated with the corresponding
stream.
.RE

.sp
.ne 2
.na
\fB\fBEINTR\fR\fR
.ad
.RS 10n
The write operation was terminated due to the receipt of a signal, and no data
was transferred.
.RE

.sp
.ne 2
.na
\fB\fBEINVAL\fR\fR
.ad
.RS 10n
The \fIwhence\fR argument is invalid. The resulting file-position indicator
would be set to a negative value.
.RE

.sp
.ne 2
.na
\fB\fBEIO\fR\fR
.ad
.RS 10n
A physical I/O error has occurred; or the process is a member of a background
process group attempting to perform a \fBwrite\fR(2) operation to its
controlling terminal, \fBTOSTOP\fR is set, the process is neither ignoring nor
blocking \fBSIGTTOU\fR, and the process group of the process is orphaned.
.RE

.sp
.ne 2
.na
\fB\fBENOSPC\fR\fR
.ad
.RS 10n
There was no free space remaining on the device containing the file.
.RE

.sp
.ne 2
.na
\fB\fBENXIO\fR\fR
.ad
.RS 10n
A request was made of a non-existent device, or the request was outside the
capabilities of the device.
.RE

.sp
.ne 2
.na
\fB\fBEPIPE\fR\fR
.ad
.RS 10n
The file descriptor underlying \fIstream\fR is associated with a pipe or FIFO.
.RE

.sp
.ne 2
.na
\fB\fBEPIPE\fR\fR
.ad
.RS 10n
An attempt was made to write to a pipe or \fBFIFO\fR that is not open for
reading by any process. A \fBSIGPIPE\fR signal will also be sent to the calling
thread.
.RE

.sp
.LP
The \fBfseek()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEOVERFLOW\fR\fR
.ad
.RS 13n
The resulting file offset would be a value which cannot be represented
correctly in an object of type \fBlong\fR.
.RE

.sp
.LP
The \fBfseeko()\fR function will fail if:
.sp
.ne 2
.na
\fB\fBEOVERFLOW\fR\fR
.ad
.RS 13n
The resulting file offset would be a value which cannot be represented
correctly in an object of type \fBoff_t\fR.
.RE

.SH USAGE
Although on the UNIX system an offset returned by \fBftell()\fR or
\fBftello()\fR (see \fBftell\fR(3C)) is measured in bytes, and it is
permissible to seek to positions relative to that offset, portability to
non-UNIX systems requires that an offset be used by \fBfseek()\fR directly.
Arithmetic may not meaningfully be performed on such an offset, which is not
necessarily measured in bytes.
.sp
.LP
The \fBfseeko()\fR function has a transitional interface for 64-bit file
offsets.  See \fBlf64\fR(5).
.SH ATTRIBUTES
See \fBattributes\fR(5) for descriptions of the following attributes:
.sp

.sp
.TS
box;
c | c
l | l .
ATTRIBUTE TYPE	ATTRIBUTE VALUE
_
Interface Stability	Standard
_
MT-Level	MT-Safe
.TE

.SH SEE ALSO
\fBgetrlimit\fR(2), \fBulimit\fR(2), \fBftell\fR(3C),
\fBrewind\fR(3C), \fBungetc\fR(3C), \fBungetwc\fR(3C), \fBattributes\fR(5),
\fBlf64\fR(5), \fBstandards\fR(5)