summaryrefslogtreecommitdiffstats
path: root/man-pages-posix-2003/man3p/freopen.3p
blob: b6a766ae7c10cb21c3c6d4794ed01d0737c0ef1f (plain) (blame)
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
.\" Copyright (c) 2001-2003 The Open Group, All Rights Reserved 
.TH "FREOPEN" 3P 2003 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\" freopen 
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.SH NAME
freopen \- open a stream
.SH SYNOPSIS
.LP
\fB#include <stdio.h>
.br
.sp
FILE *freopen(const char *restrict\fP \fIfilename\fP\fB, const char
*restrict\fP \fImode\fP\fB,
.br
\ \ \ \ \ \  FILE *restrict\fP \fIstream\fP\fB);
.br
\fP
.SH DESCRIPTION
.LP
The \fIfreopen\fP() function shall first attempt to flush the stream
and close any file descriptor associated with
\fIstream\fP. Failure to flush or close the file descriptor successfully
shall be ignored. The error and end-of-file indicators
for the stream shall be cleared.
.LP
The \fIfreopen\fP() function shall open the file whose pathname is
the string pointed to by \fIfilename\fP and associate the
stream pointed to by \fIstream\fP with it. The \fImode\fP argument
shall be used just as in \fIfopen\fP().
.LP
The original stream shall be closed regardless of whether the subsequent
open succeeds.
.LP
If \fIfilename\fP is a null pointer, the \fIfreopen\fP() function
shall attempt to change the mode of the stream to that
specified by \fImode\fP, as if the name of the file currently associated
with the stream had been used. It is
implementation-defined which changes of mode are permitted (if any),
and under what circumstances.
.LP
After a successful call to the \fIfreopen\fP() function, the orientation
of the stream shall be cleared,  the encoding
rule shall be cleared,  and the associated \fBmbstate_t\fP object
shall be set to describe an initial conversion state.
.LP
The
largest value that can be represented correctly in an object of type
\fBoff_t\fP shall be established as the offset maximum in the
open file description. 
.SH RETURN VALUE
.LP
Upon successful completion, \fIfreopen\fP() shall return the value
of \fIstream\fP. Otherwise, a null pointer shall be
returned,  and \fIerrno\fP shall be set to indicate the error.
.SH ERRORS
.LP
The \fIfreopen\fP() function shall fail if:
.TP 7
.B EACCES
Search permission is denied on a component of the path prefix, or
the file exists and the permissions specified by \fImode\fP are
denied, or the file does not exist and write permission is denied
for the parent directory of the file to be created. 
.TP 7
.B EINTR
A
signal was caught during \fIfreopen\fP(). 
.TP 7
.B EISDIR
The named file is a directory and \fImode\fP requires write access.
.TP 7
.B ELOOP
A
loop exists in symbolic links encountered during resolution of the
\fIpath\fP argument. 
.TP 7
.B EMFILE
{OPEN_MAX} file descriptors are currently open in the calling process.
.TP 7
.B ENAMETOOLONG
.sp
The length of the \fIfilename\fP argument exceeds {PATH_MAX} or a
pathname component is longer than {NAME_MAX}. 
.TP 7
.B ENFILE
The maximum allowable number of files is currently open in the system.
.TP 7
.B ENOENT
A
component of \fIfilename\fP does not name an existing file or \fIfilename\fP
is an empty string. 
.TP 7
.B ENOSPC
The directory or file system that would contain the new file cannot
be expanded, the file does not exist, and it was to be created.
.TP 7
.B ENOTDIR
A
component of the path prefix is not a directory. 
.TP 7
.B ENXIO
The named file is a character special or block special file, and the
device associated with this special file does not exist. 
.TP 7
.B EOVERFLOW
The named file is a regular file and the size of the file cannot be
represented correctly in an object of type \fBoff_t\fP. 
.TP 7
.B EROFS
The named file resides on a read-only file system and \fImode\fP requires
write access. 
.sp
.LP
The \fIfreopen\fP() function may fail if:
.TP 7
.B EINVAL
The value of the \fImode\fP argument is not valid. 
.TP 7
.B ELOOP
More than {SYMLOOP_MAX} symbolic links were encountered during resolution
of the \fIpath\fP argument. 
.TP 7
.B ENAMETOOLONG
.sp
Pathname resolution of a symbolic link produced an intermediate result
whose length exceeds {PATH_MAX}. 
.TP 7
.B ENOMEM
Insufficient storage space is available. 
.TP 7
.B ENXIO
A
request was made of a nonexistent device, or the request was outside
the capabilities of the device. 
.TP 7
.B ETXTBSY
The file is a pure procedure (shared text) file that is being executed
and \fImode\fP requires write access. 
.sp
.LP
\fIThe following sections are informative.\fP
.SH EXAMPLES
.SS Directing Standard Output to a File
.LP
The following example logs all standard output to the \fB/tmp/logfile\fP
file.
.sp
.RS
.nf

\fB#include <stdio.h>
\&...
FILE *fp;
\&...
fp = freopen ("/tmp/logfile", "a+", stdout);
\&...
\fP
.fi
.RE
.SH APPLICATION USAGE
.LP
The \fIfreopen\fP() function is typically used to attach the preopened
\fIstreams\fP associated with \fIstdin\fP,
\fIstdout\fP, and \fIstderr\fP to other files.
.SH RATIONALE
.LP
None.
.SH FUTURE DIRECTIONS
.LP
None.
.SH SEE ALSO
.LP
\fIfclose\fP(), \fIfopen\fP(), \fIfdopen\fP(),
\fImbsinit\fP(), the Base Definitions volume of IEEE\ Std\ 1003.1-2001,
\fI<stdio.h>\fP
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group. In the
event of any discrepancy between this version 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 .