Date: Mon, 9 Jan 2012 04:59:47 +0000 (UTC) From: David Schultz <das@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org Subject: svn commit: r229846 - stable/8/lib/libc/stdio Message-ID: <201201090459.q094xlMY023804@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: das Date: Mon Jan 9 04:59:47 2012 New Revision: 229846 URL: http://svn.freebsd.org/changeset/base/229846 Log: MFC r226604: Add support for the 'x' mode option in fopen() as specified in the C1X draft standard. The option is equivalent to O_EXCL. Also merge doc improvements r197045 and r207942. Modified: stable/8/lib/libc/stdio/flags.c stable/8/lib/libc/stdio/fopen.3 Directory Properties: stable/8/lib/libc/ (props changed) Modified: stable/8/lib/libc/stdio/flags.c ============================================================================== --- stable/8/lib/libc/stdio/flags.c Mon Jan 9 04:59:44 2012 (r229845) +++ stable/8/lib/libc/stdio/flags.c Mon Jan 9 04:59:47 2012 (r229846) @@ -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); } Modified: stable/8/lib/libc/stdio/fopen.3 ============================================================================== --- stable/8/lib/libc/stdio/fopen.3 Mon Jan 9 04:59:44 2012 (r229845) +++ stable/8/lib/libc/stdio/fopen.3 Mon Jan 9 04:59:47 2012 (r229846) @@ -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,51 +60,57 @@ 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 a third 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. .Pp Any created files will have mode -.Pf \\*q Dv S_IRUSR +.Do Dv S_IRUSR \&| .Dv S_IWUSR \&| @@ -114,7 +120,7 @@ Any created files will have mode \&| .Dv S_IROTH \&| -.Dv S_IWOTH Ns \\*q +.Dv S_IWOTH Dc .Pq Li 0666 , as modified by the process' umask value (see @@ -135,6 +141,9 @@ function associates a stream with the ex .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 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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201201090459.q094xlMY023804>