1.\" $NetBSD: fopen.3,v 1.8 1998/04/28 20:58:01 fair 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. All advertising materials mentioning features or use of this software 19.\" must display the following acknowledgement: 20.\" This product includes software developed by the University of 21.\" California, Berkeley and its contributors. 22.\" 4. Neither the name of the University nor the names of its contributors 23.\" may be used to endorse or promote products derived from this software 24.\" without specific prior written permission. 25.\" 26.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36.\" SUCH DAMAGE. 37.\" 38.\" @(#)fopen.3 8.1 (Berkeley) 6/4/93 39.\" 40.Dd June 4, 1993 41.Dt FOPEN 3 42.Os 43.Sh NAME 44.Nm fopen , 45.Nm fdopen , 46.Nm freopen 47.Nd stream open functions 48.Sh LIBRARY 49.Lb libc 50.Sh SYNOPSIS 51.Fd #include <stdio.h> 52.Ft FILE * 53.Fn fopen "char *path" "char *mode" 54.Ft FILE * 55.Fn fdopen "int fildes" "char *mode" 56.Ft FILE * 57.Fn freopen "char *path" "char *mode" "FILE *stream" 58.Sh DESCRIPTION 59The 60.Fn fopen 61function 62opens the file whose name is the string pointed to by 63.Fa path 64and associates a stream with it. 65.Pp 66The argument 67.Fa mode 68points to a string beginning with one of the following 69sequences (Additional characters may follow these sequences.): 70.Bl -tag -width indent 71.It Dq Li r 72Open text file for reading. 73The stream is positioned at the beginning of the file. 74.It Dq Li r+ 75Open for reading and writing. 76The stream is positioned at the beginning of the file. 77.It Dq Li w 78Truncate file to zero length or create text file for writing. 79The stream is positioned at the beginning of the file. 80.It Dq Li w+ 81Open for reading and writing. 82The file is created if it does not exist, otherwise it is truncated. 83The stream is positioned at the beginning of the file. 84.It Dq Li a 85Open for writing. 86The file is created if it does not exist. 87The stream is positioned at the end of the file. 88.It Dq Li a+ 89Open for reading and writing. 90The file is created if it does not exist. 91The stream is positioned at the end of the file. 92.El 93.Pp 94The 95.Fa mode 96string can also include the letter ``b'' either as a third character or 97as a character between the characters in any of the two-character strings 98described above. 99This is strictly for compatibility with 100.St -ansiC 101and has no effect; the ``b'' is ignored. 102.Pp 103Any created files will have mode 104.Pf \\*q Dv S_IRUSR 105\&| 106.Dv S_IWUSR 107\&| 108.Dv S_IRGRP 109\&| 110.Dv S_IWGRP 111\&| 112.Dv S_IROTH 113\&| 114.Dv S_IWOTH Ns \\*q 115.Pq Li 0666 , 116as modified by the process' 117umask value (see 118.Xr umask 2 ) . 119.Pp 120Reads and writes may be intermixed on read/write streams in any order, 121and do not require an intermediate seek as in previous versions of 122.Em stdio . 123This is not portable to other systems, however; 124.Tn ANSI C 125requires that 126a file positioning function intervene between output and input, unless 127an input operation encounters end-of-file. 128.Pp 129The 130.Fn fdopen 131function associates a stream with the existing file descriptor, 132.Fa fildes . 133The 134.Fa mode 135of the stream must be compatible with the mode of the file descriptor. 136.Pp 137The 138.Fn freopen 139function 140opens the file whose name is the string pointed to by 141.Fa path 142and associates the stream pointed to by 143.Fa stream 144with it. 145The original stream (if it exists) is closed. 146The 147.Fa mode 148argument is used just as in the 149.Fn fopen 150function. 151The primary use of the 152.Fn freopen 153function 154is to change the file associated with a 155standard text stream 156.Pf ( Em stderr , 157.Em stdin , 158or 159.Em stdout ) . 160.Sh RETURN VALUES 161Upon successful completion 162.Fn fopen , 163.Fn fdopen 164and 165.Fn freopen 166return a 167.Tn FILE 168pointer. 169Otherwise, 170.Dv NULL 171is returned and the global variable 172.Va errno 173is set to indicate the error. 174.Sh ERRORS 175.Bl -tag -width Er 176.It Bq Er EINVAL 177The 178.Fa mode 179provided to 180.Fn fopen , 181.Fn fdopen , 182or 183.Fn freopen 184was invalid. 185.El 186.Pp 187The 188.Fn fopen , 189.Fn fdopen 190and 191.Fn freopen 192functions 193may also fail and set 194.Va errno 195for any of the errors specified for the routine 196.Xr malloc 3 . 197.Pp 198The 199.Fn fopen 200function 201may also fail and set 202.Va errno 203for any of the errors specified for the routine 204.Xr open 2 . 205.Pp 206The 207.Fn fdopen 208function 209may also fail and set 210.Va errno 211for any of the errors specified for the routine 212.Xr fcntl 2 . 213.Pp 214The 215.Fn freopen 216function 217may also fail and set 218.Va errno 219for any of the errors specified for the routines 220.Xr open 2 , 221.Xr fclose 3 222and 223.Xr fflush 3 . 224.Sh SEE ALSO 225.Xr open 2 , 226.Xr fclose 3 , 227.Xr fseek 3 , 228.Xr funopen 3 229.Sh STANDARDS 230The 231.Fn fopen 232and 233.Fn freopen 234functions 235conform to 236.St -ansiC . 237The 238.Fn fdopen 239function conforms to 240.St -p1003.1-90 . 241