Loading...
stdio/fopen.3 Libc-262 Libc-498
--- Libc/Libc-262/stdio/fopen.3
+++ Libc/Libc-498/stdio/fopen.3
@@ -34,14 +34,14 @@
 .\" SUCH DAMAGE.
 .\"
 .\"     @(#)fopen.3	8.1 (Berkeley) 6/4/93
-.\" $FreeBSD: src/lib/libc/stdio/fopen.3,v 1.12 2001/10/01 16:08:59 ru Exp $
-.\"
-.Dd June 4, 1993
+.\" $FreeBSD: src/lib/libc/stdio/fopen.3,v 1.18 2003/01/26 10:01:59 tjr Exp $
+.\"
+.Dd January 26, 2003
 .Dt FOPEN 3
 .Os
 .Sh NAME
+.Nm fdopen ,
 .Nm fopen ,
-.Nm fdopen ,
 .Nm freopen
 .Nd stream open functions
 .Sh LIBRARY
@@ -49,17 +49,27 @@
 .Sh SYNOPSIS
 .In stdio.h
 .Ft FILE *
-.Fn fopen "const char *path" "const char *mode"
+.Fo fdopen
+.Fa "int fildes"
+.Fa "const char *mode"
+.Fc
 .Ft FILE *
-.Fn fdopen "int fildes" "const char *mode"
+.Fo fopen
+.Fa "const char *restrict filename"
+.Fa "const char *restrict mode"
+.Fc
 .Ft FILE *
-.Fn freopen "const char *path" "const char *mode" "FILE *stream"
+.Fo freopen
+.Fa "const char *restrict filename"
+.Fa "const char *restrict mode"
+.Fa "FILE *restrict stream"
+.Fc
 .Sh DESCRIPTION
 The
 .Fn fopen
 function
 opens the file whose name is the string pointed to by
-.Fa path
+.Fa filename
 and associates a stream with it.
 .Pp
 The argument
@@ -107,6 +117,17 @@
 .St -isoC
 and has no effect; the ``b'' is ignored.
 .Pp
+Finally, as an extension to the standards (and thus may not be portable),
+.Fa mode
+string may end with the letter ``x'', which insists on creating a new file
+when used with ``w'' or ``a''.
+If
+.Fa path
+exists, then an error is returned (this is the equivalent of specifying
+.Dv O_EXCL
+with
+.Xr open 2 ) .
+.Pp
 Any created files will have mode
 .Pf \\*q Dv S_IRUSR
 \&|
@@ -137,8 +158,7 @@
 .Fn fdopen
 function associates a stream with the existing file descriptor,
 .Fa fildes .
-The
-.Fa mode
+The mode
 of the stream must be compatible with the mode of the file descriptor.
 When the stream is closed via
 .Xr fclose 3 ,
@@ -149,7 +169,7 @@
 .Fn freopen
 function
 opens the file whose name is the string pointed to by
-.Fa path
+.Fa filename
 and associates the stream pointed to by
 .Fa stream
 with it.
@@ -159,19 +179,53 @@
 argument is used just as in the
 .Fn fopen
 function.
+.Pp
+If the
+.Fa filename
+argument is
+.Dv NULL ,
+.Fn freopen
+attempts to re-open the file associated with
+.Fa stream
+with a new mode.
+The new mode must be compatible with the mode that the stream was originally
+opened with:
+.Bl -bullet -offset indent
+.It
+Streams originally opened with mode
+.Dq Li r
+can only be reopened with that same mode.
+.It
+Streams originally opened with mode
+.Dq Li a
+can be reopened with the same mode, or mode
+.Dq Li w .
+.It
+Streams originally opened with mode
+.Dq Li w
+can be reopened with the same mode, or mode
+.Dq Li a .
+.It
+Streams originally opened with mode
+.Dq Li r+ ,
+.Dq Li w+ ,
+or
+.Dq Li a+
+can be reopened with any mode.
+.El
+.Pp
 The primary use of the
 .Fn freopen
 function
 is to change the file associated with a
 standard text stream
-.Pf ( Em stderr ,
-.Em stdin ,
+.Dv ( stderr , stdin ,
 or
-.Em stdout ) .
+.Dv stdout ) .
 .Sh RETURN VALUES
 Upon successful completion
 .Fn fopen ,
-.Fn fdopen
+.Fn fdopen ,
 and
 .Fn freopen
 return a
@@ -187,7 +241,8 @@
 .It Bq Er EINVAL
 The
 .Fa mode
-provided to
+argument
+to
 .Fn fopen ,
 .Fn fdopen ,
 or
@@ -235,6 +290,7 @@
 .Sh SEE ALSO
 .Xr open 2 ,
 .Xr fclose 3 ,
+.Xr fileno 3 ,
 .Xr fseek 3 ,
 .Xr funopen 3
 .Sh STANDARDS