summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2013-09-06 13:47:16 +0000
committerjilles <jilles@FreeBSD.org>2013-09-06 13:47:16 +0000
commit68907dc5984d1396b8cd4013146c968206aeb2ad (patch)
tree2b2e89172b7fb247a006e2a0782d3d9599aded24 /lib
parentdfd17597dbfd8db62df39a4c1a2591a3a41d505d (diff)
downloadFreeBSD-src-68907dc5984d1396b8cd4013146c968206aeb2ad.zip
FreeBSD-src-68907dc5984d1396b8cd4013146c968206aeb2ad.tar.gz
libc/stdio: Allow fopen/freopen modes in any order (except initial r/w/a).
Austin Group issue #411 requires 'e' to be accepted before and after 'x', and encourages accepting the characters in any order, except the initial 'r', 'w' or 'a'. Given that glibc accepts the characters after r/w/a in any order and that diagnosing this problem may be hard, change our libc to behave that way as well.
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/stdio/flags.c55
1 files changed, 28 insertions, 27 deletions
diff --git a/lib/libc/stdio/flags.c b/lib/libc/stdio/flags.c
index 1878c2f..b7552a4 100644
--- a/lib/libc/stdio/flags.c
+++ b/lib/libc/stdio/flags.c
@@ -51,7 +51,7 @@ __FBSDID("$FreeBSD$");
int
__sflags(const char *mode, int *optr)
{
- int ret, m, o;
+ int ret, m, o, known;
switch (*mode++) {
@@ -78,34 +78,35 @@ __sflags(const char *mode, int *optr)
return (0);
}
- /* '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') {
- mode++;
- if (m == O_RDONLY) {
- errno = EINVAL;
- return (0);
+ do {
+ known = 1;
+ switch (*mode++) {
+ case 'b':
+ /* 'b' (binary) is ignored */
+ break;
+ case '+':
+ /* [rwa][b]\+ means read and write */
+ ret = __SRW;
+ m = O_RDWR;
+ break;
+ case 'x':
+ /* 'x' means exclusive (fail if the file exists) */
+ o |= O_EXCL;
+ break;
+ case 'e':
+ /* set close-on-exec */
+ o |= O_CLOEXEC;
+ break;
+ default:
+ known = 0;
+ break;
}
- o |= O_EXCL;
- }
+ } while (known);
- /* set close-on-exec */
- if (*mode == 'e')
- o |= O_CLOEXEC;
+ if ((o & O_EXCL) != 0 && m == O_RDONLY) {
+ errno = EINVAL;
+ return (0);
+ }
*optr = m | o;
return (ret);
OpenPOWER on IntegriCloud