Add support for the 'x' mode option in fopen() as specified in the C1X
draft standard. The option is equivalent to O_EXCL. MFC after: 1 month
This commit is contained in:
parent
6fcec4dd2b
commit
bd26fb812d
@ -80,11 +80,30 @@ __sflags(mode, optr)
|
||||
return (0);
|
||||
}
|
||||
|
||||
/* [rwa]\+ or [rwa]b\+ means read and write */
|
||||
if (*mode == '+' || (*mode == 'b' && mode[1] == '+')) {
|
||||
/* 'b' (binary) is ignored */
|
||||
if (*mode == 'b')
|
||||
mode++;
|
||||
|
||||
/* [rwa][b]\+ means read and write */
|
||||
if (*mode == '+') {
|
||||
mode++;
|
||||
ret = __SRW;
|
||||
m = O_RDWR;
|
||||
}
|
||||
|
||||
/* 'b' (binary) can appear here, too -- and is ignored again */
|
||||
if (*mode == 'b')
|
||||
mode++;
|
||||
|
||||
/* 'x' means exclusive (fail if the file exists) */
|
||||
if (*mode == 'x') {
|
||||
if (m == O_RDONLY) {
|
||||
errno = EINVAL;
|
||||
return (0);
|
||||
}
|
||||
o |= O_EXCL;
|
||||
}
|
||||
|
||||
*optr = m | o;
|
||||
return (ret);
|
||||
}
|
||||
|
@ -32,7 +32,7 @@
|
||||
.\" @(#)fopen.3 8.1 (Berkeley) 6/4/93
|
||||
.\" $FreeBSD$
|
||||
.\"
|
||||
.Dd January 26, 2003
|
||||
.Dd October 17, 2011
|
||||
.Dt FOPEN 3
|
||||
.Os
|
||||
.Sh NAME
|
||||
@ -60,45 +60,51 @@ and associates a stream with it.
|
||||
.Pp
|
||||
The argument
|
||||
.Fa mode
|
||||
points to a string beginning with one of the following
|
||||
sequences (Additional characters may follow these sequences.):
|
||||
points to a string beginning with one of the following letters:
|
||||
.Bl -tag -width indent
|
||||
.It Dq Li r
|
||||
Open text file for reading.
|
||||
The stream is positioned at the beginning of the file.
|
||||
.It Dq Li r+
|
||||
Open for reading and writing.
|
||||
Open for reading.
|
||||
The stream is positioned at the beginning of the file.
|
||||
Fail if the file does not exist.
|
||||
.It Dq Li w
|
||||
Truncate to zero length or create text file for writing.
|
||||
The stream is positioned at the beginning of the file.
|
||||
.It Dq Li w+
|
||||
Open for reading and writing.
|
||||
The file is created if it does not exist, otherwise it is truncated.
|
||||
Open for writing.
|
||||
The stream is positioned at the beginning of the file.
|
||||
Create the file if it does not exist.
|
||||
.It Dq Li a
|
||||
Open for writing.
|
||||
The file is created if it does not exist.
|
||||
The stream is positioned at the end of the file.
|
||||
Subsequent writes to the file will always end up at the then current
|
||||
end of file, irrespective of any intervening
|
||||
.Xr fseek 3
|
||||
or similar.
|
||||
.It Dq Li a+
|
||||
Open for reading and writing.
|
||||
The file is created if it does not exist.
|
||||
The stream is positioned at the end of the file.
|
||||
Subsequent writes to the file will always end up at the then current
|
||||
end of file, irrespective of any intervening
|
||||
.Xr fseek 3
|
||||
or similar.
|
||||
Create the file if it does not exist.
|
||||
.El
|
||||
.Pp
|
||||
An optional
|
||||
.Dq Li +
|
||||
following
|
||||
.Dq Li r ,
|
||||
.Dq Li w ,
|
||||
or
|
||||
.Dq Li a
|
||||
opens the file for both reading and writing.
|
||||
An optional
|
||||
.Dq Li x
|
||||
following
|
||||
.Dq Li w
|
||||
or
|
||||
.Dq Li w+
|
||||
causes the
|
||||
.Fn fopen
|
||||
call to fail if the file already exists.
|
||||
.Pp
|
||||
The
|
||||
.Fa mode
|
||||
string can also include the letter ``b'' either as last character or
|
||||
as a character between the characters in any of the two-character strings
|
||||
described above.
|
||||
string can also include the letter
|
||||
.Dq Li b
|
||||
after either the
|
||||
.Dq Li +
|
||||
or the first letter.
|
||||
This is strictly for compatibility with
|
||||
.St -isoC
|
||||
and has no effect; the ``b'' is ignored.
|
||||
@ -135,6 +141,9 @@ function associates a stream with the existing file descriptor,
|
||||
.Fa fildes .
|
||||
The mode
|
||||
of the stream must be compatible with the mode of the file descriptor.
|
||||
The
|
||||
.Dq Li x
|
||||
mode option is ignored.
|
||||
When the stream is closed via
|
||||
.Xr fclose 3 ,
|
||||
.Fa fildes
|
||||
@ -165,29 +174,12 @@ attempts to re-open the file associated with
|
||||
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
|
||||
Streams open for reading can only be re-opened for reading,
|
||||
streams open for writing can only be re-opened for writing,
|
||||
and streams open for reading and writing can be re-opened in any mode.
|
||||
The
|
||||
.Dq Li x
|
||||
mode option is not meaningful in this context.
|
||||
.Pp
|
||||
The primary use of the
|
||||
.Fn freopen
|
||||
|
Loading…
Reference in New Issue
Block a user