TIOCGPGRP copies an int out of the kernel
[unix-history] / usr / src / lib / libc / stdio / fopen.3
CommitLineData
411867e7
KB
1.\" Copyright (c) 1990 The Regents of the University of California.
2.\" All rights reserved.
251f9c2d 3.\"
411867e7
KB
4.\" This code is derived from software contributed to Berkeley by
5.\" Chris Torek.
251f9c2d 6.\"
411867e7
KB
7.\" %sccs.include.redist.man%
8.\"
a4b80657 9.\" @(#)fopen.3 6.6 (Berkeley) %G%
411867e7
KB
10.\"
11.TH FOPEN 3 ""
12.UC 7
251f9c2d 13.SH NAME
411867e7 14fopen, fdopen, freopen \- open a stream
251f9c2d 15.SH SYNOPSIS
411867e7
KB
16.nf
17.ft B
18#include <stdio.h>
19
20FILE *
21fopen(char *path, char *type);
22
23FILE *
24fdopen(int fildes, char *type);
25
26FILE *
27freopen(char *path, char *type, FILE *stream);
28.ft R
29.fi
251f9c2d
KM
30.SH DESCRIPTION
31.I Fopen
32opens the file named by
411867e7
KB
33.I path
34and associates a stream with it, returning a pointer for identifying
35the stream in subsequent operations.
251f9c2d
KM
36.PP
37.I Type
411867e7
KB
38is one of the following character strings:
39.TP
40``r''
41Open for reading.
42The stream is positioned at the beginning of the file.
43.TP
44``r+''
45Open for reading and writing.
46The stream is positioned at the beginning of the file.
47.TP
48``w''
49Open for writing.
50The file is created if it does not exist, otherwise it is truncated.
51The stream is positioned at the beginning of the file.
52.TP
53``w+''
54Open for reading and writing.
55The file is created if it does not exist, otherwise it is truncated.
56The stream is positioned at the beginning of the file.
57.TP
58``a''
59Open for writing.
60The file is created if it does not exist.
61The stream is positioned at the end of the file.
62.TP
63``a+''
64Open for reading and writing.
65The file is created if it does not exist.
66The stream is positioned at the end of the file.
251f9c2d 67.PP
411867e7 68The
251f9c2d 69.I type
411867e7
KB
70string can also include the letter ``b'' either as a third character or
71as a character between the characters in any of the two-character strings
72described above.
73This is strictly for compatibility with ANSI X3.159-1989 (``ANSI C'')
74and has no effect; the ``b'' is ignored.
251f9c2d 75.PP
a4b80657
KB
76Any created files will have mode ``S_IRUSR | S_IWUSR | S_IRGRP |
77S_IWGRP | S_IROTH | S_IWOTH'' (0666), as modified by the process'
78umask value (see
411867e7 79.IR umask (2)).
251f9c2d 80.PP
411867e7
KB
81Reads and writes may be intermixed on read/write streams in any order,
82and do not require an intermediate seek as in previous versions of
83stdio.
84This is not portable to other systems, however; ANSI C requires that
85a file positioning function intervene between output and input, unless
86an input operation encounters end-of-file.
251f9c2d
KM
87.PP
88.I Fdopen
89associates a stream with a file descriptor obtained from
411867e7
KB
90.IR creat (2),
91.IR dup (2),
92.IR open (2),
251f9c2d
KM
93or
94.IR pipe (2).
95The
96.I type
411867e7
KB
97of the stream must be compatible with the mode of the file descriptor.
98.PP
99.I Freopen
100substitutes the named file in place of the open
101.IR stream
102and returns a pointer to it.
103The original stream is closed.
104.I Freopen
105is typically used to attach the preopened constant names,
106.IR stdin ,
107.IR stdout ,
108and
109.I stderr
110to files.
251f9c2d 111.SH "SEE ALSO"
a4b80657 112open(2), fclose(3), fseek(3), funopen(3)
411867e7
KB
113.SH "RETURN VALUES"
114Upon successful completion
115.IR fopen ,
116.I fdopen
117and
118.I freopen
119return a FILE pointer.
120Otherwise, NULL is returned and the global variable
121.I errno
122is set to indicate the error.
123.SH ERRORS
124.TP 15
125[EINVAL]
126The
127.I type
128provided to
129.IR fopen ,
130.IR fdopen ,
131or
132.IR freopen
133was invalid.
a4b80657
KB
134.PP
135.IR Fopen ,
136.I fdopen
137and
138.I freopen
139may also fail and set
140.IR errno
141for any of the errors specified for the routine
142.IR malloc (3).
411867e7 143.PP
251f9c2d 144.I Fopen
411867e7
KB
145may also fail and set
146.IR errno
147for any of the errors specified for the routine
148.IR open (2).
149.PP
150.I Fdopen
151may also fail and set
152.IR errno
153for any of the errors specified for the routine
154.IR fcntl (2).
155.PP
156.I Freopen
157may also fail and set
158.IR errno
159for any of the errors specified for the routines
160.IR open (2),
161.IR fclose (3)
162and
163.IR fflush (3).
164.SH STANDARDS
165.I Fopen
166and
251f9c2d 167.I freopen
411867e7
KB
168conform to ANSI X3.159-1989 (``ANSI C'').
169.I Fdopen
170conforms to IEEE Std 1003.1-1988 (``POSIX'').
251f9c2d
KM
171.SH BUGS
172.I Fdopen
411867e7 173may not be portable to systems other than UNIX.