1.\" $NetBSD: fopen.3,v 1.6 1997/10/14 07:27:57 mycroft 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 SYNOPSIS 49.Fd #include <stdio.h> 50.Ft FILE * 51.Fn fopen "char *path" "char *mode" 52.Ft FILE * 53.Fn fdopen "int fildes" "char *mode" 54.Ft FILE * 55.Fn freopen "char *path" "char *mode" "FILE *stream" 56.Sh DESCRIPTION 57The 58.Fn fopen 59function 60opens the file whose name is the string pointed to by 61.Fa path 62and associates a stream with it. 63.Pp 64The argument 65.Fa mode 66points to a string beginning with one of the following 67sequences (Additional characters may follow these sequences.): 68.Bl -tag -width indent 69.It Dq Li r 70Open text file for reading. 71The stream is positioned at the beginning of the file. 72.It Dq Li r+ 73Open for reading and writing. 74The stream is positioned at the beginning of the file. 75.It Dq Li w 76Truncate file to zero length or create text file for writing. 77The stream is positioned at the beginning of the file. 78.It Dq Li w+ 79Open for reading and writing. 80The file is created if it does not exist, otherwise it is truncated. 81The stream is positioned at the beginning of the file. 82.It Dq Li a 83Open for writing. 84The file is created if it does not exist. 85The stream is positioned at the end of the file. 86.It Dq Li a+ 87Open for reading and writing. 88The file is created if it does not exist. 89The stream is positioned at the end of the file. 90.El 91.Pp 92The 93.Fa mode 94string can also include the letter ``b'' either as a third character or 95as a character between the characters in any of the two-character strings 96described above. 97This is strictly for compatibility with 98.St -ansiC 99and has no effect; the ``b'' is ignored. 100.Pp 101Any created files will have mode 102.Pf \\*q Dv S_IRUSR 103\&| 104.Dv S_IWUSR 105\&| 106.Dv S_IRGRP 107\&| 108.Dv S_IWGRP 109\&| 110.Dv S_IROTH 111\&| 112.Dv S_IWOTH Ns \\*q 113.Pq Li 0666 , 114as modified by the process' 115umask value (see 116.Xr umask 2 ) . 117.Pp 118Reads and writes may be intermixed on read/write streams in any order, 119and do not require an intermediate seek as in previous versions of 120.Em stdio . 121This is not portable to other systems, however; 122.Tn ANSI C 123requires that 124a file positioning function intervene between output and input, unless 125an input operation encounters end-of-file. 126.Pp 127The 128.Fn fdopen 129function associates a stream with the existing file descriptor, 130.Fa fildes . 131The 132.Fa mode 133of the stream must be compatible with the mode of the file descriptor. 134.Pp 135The 136.Fn freopen 137function 138opens the file whose name is the string pointed to by 139.Fa path 140and associates the stream pointed to by 141.Fa stream 142with it. 143The original stream (if it exists) is closed. 144The 145.Fa mode 146argument is used just as in the 147.Xr fopen 148function. 149The primary use of the 150.Fn freopen 151function 152is to change the file associated with a 153standard text stream 154.Pf ( Em stderr , 155.Em stdin , 156or 157.Em stdout ) . 158.Sh RETURN VALUES 159Upon successful completion 160.Fn fopen , 161.Fn fdopen 162and 163.Fn freopen 164return a 165.Tn FILE 166pointer. 167Otherwise, 168.Dv NULL 169is returned and the global variable 170.Va errno 171is set to indicate the error. 172.Sh ERRORS 173.Bl -tag -width Er 174.It Bq Er EINVAL 175The 176.Fa mode 177provided to 178.Fn fopen , 179.Fn fdopen , 180or 181.Fn freopen 182was invalid. 183.El 184.Pp 185The 186.Fn fopen , 187.Fn fdopen 188and 189.Fn freopen 190functions 191may also fail and set 192.Va errno 193for any of the errors specified for the routine 194.Xr malloc 3 . 195.Pp 196The 197.Fn fopen 198function 199may also fail and set 200.Va errno 201for any of the errors specified for the routine 202.Xr open 2 . 203.Pp 204The 205.Fn fdopen 206function 207may also fail and set 208.Va errno 209for any of the errors specified for the routine 210.Xr fcntl 2 . 211.Pp 212The 213.Fn freopen 214function 215may also fail and set 216.Va errno 217for any of the errors specified for the routines 218.Xr open 2 , 219.Xr fclose 3 220and 221.Xr fflush 3 . 222.Sh SEE ALSO 223.Xr open 2 , 224.Xr fclose 3 , 225.Xr fseek 3 , 226.Xr funopen 3 227.Sh STANDARDS 228The 229.Fn fopen 230and 231.Fn freopen 232functions 233conform to 234.St -ansiC . 235The 236.Fn fdopen 237function conforms to 238.St -p1003.1-90 . 239