xref: /freebsd/lib/libc/stdio/flags.c (revision d5fec48956cd44268599f64f7a8825a0a84c77e5)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1990, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
658f0484fSRodney W. Grimes  * Chris Torek.
758f0484fSRodney W. Grimes  *
858f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes  * are met:
1158f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
161d8053c5SEd Maste  * 3. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes  *    without specific prior written permission.
1958f0484fSRodney W. Grimes  *
2058f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes  * SUCH DAMAGE.
3158f0484fSRodney W. Grimes  */
3258f0484fSRodney W. Grimes 
3358f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3458f0484fSRodney W. Grimes static char sccsid[] = "@(#)flags.c	8.1 (Berkeley) 6/4/93";
3558f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
36333fc21eSDavid E. O'Brien #include <sys/cdefs.h>
37333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$");
3858f0484fSRodney W. Grimes 
3958f0484fSRodney W. Grimes #include <sys/types.h>
4058f0484fSRodney W. Grimes #include <sys/file.h>
4158f0484fSRodney W. Grimes #include <stdio.h>
4258f0484fSRodney W. Grimes #include <errno.h>
43d201fe46SDaniel Eischen 
44ce51cf03SJames Raynard #include "local.h"
4558f0484fSRodney W. Grimes 
4658f0484fSRodney W. Grimes /*
4758f0484fSRodney W. Grimes  * Return the (stdio) flags for a given mode.  Store the flags
48d201fe46SDaniel Eischen  * to be passed to an _open() syscall through *optr.
4958f0484fSRodney W. Grimes  * Return 0 on error.
5058f0484fSRodney W. Grimes  */
51ce51cf03SJames Raynard int
525a6307cfSEd Maste __sflags(const char *mode, int *optr)
5358f0484fSRodney W. Grimes {
54ef70de18SJilles Tjoelker 	int ret, m, o, known;
5558f0484fSRodney W. Grimes 
5658f0484fSRodney W. Grimes 	switch (*mode++) {
5758f0484fSRodney W. Grimes 
5858f0484fSRodney W. Grimes 	case 'r':	/* open for reading */
5958f0484fSRodney W. Grimes 		ret = __SRD;
6058f0484fSRodney W. Grimes 		m = O_RDONLY;
6158f0484fSRodney W. Grimes 		o = 0;
6258f0484fSRodney W. Grimes 		break;
6358f0484fSRodney W. Grimes 
6458f0484fSRodney W. Grimes 	case 'w':	/* open for writing */
6558f0484fSRodney W. Grimes 		ret = __SWR;
6658f0484fSRodney W. Grimes 		m = O_WRONLY;
6758f0484fSRodney W. Grimes 		o = O_CREAT | O_TRUNC;
6858f0484fSRodney W. Grimes 		break;
6958f0484fSRodney W. Grimes 
7058f0484fSRodney W. Grimes 	case 'a':	/* open for appending */
7158f0484fSRodney W. Grimes 		ret = __SWR;
7258f0484fSRodney W. Grimes 		m = O_WRONLY;
7358f0484fSRodney W. Grimes 		o = O_CREAT | O_APPEND;
7458f0484fSRodney W. Grimes 		break;
7558f0484fSRodney W. Grimes 
7658f0484fSRodney W. Grimes 	default:	/* illegal mode */
7758f0484fSRodney W. Grimes 		errno = EINVAL;
7858f0484fSRodney W. Grimes 		return (0);
7958f0484fSRodney W. Grimes 	}
8058f0484fSRodney W. Grimes 
81ef70de18SJilles Tjoelker 	do {
82ef70de18SJilles Tjoelker 		known = 1;
83ef70de18SJilles Tjoelker 		switch (*mode++) {
84ef70de18SJilles Tjoelker 		case 'b':
85bd26fb81SDavid Schultz 			/* 'b' (binary) is ignored */
86ef70de18SJilles Tjoelker 			break;
87ef70de18SJilles Tjoelker 		case '+':
88bd26fb81SDavid Schultz 			/* [rwa][b]\+ means read and write */
8958f0484fSRodney W. Grimes 			ret = __SRW;
9058f0484fSRodney W. Grimes 			m = O_RDWR;
91ef70de18SJilles Tjoelker 			break;
92ef70de18SJilles Tjoelker 		case 'x':
93bd26fb81SDavid Schultz 			/* 'x' means exclusive (fail if the file exists) */
94ef70de18SJilles Tjoelker 			o |= O_EXCL;
95ef70de18SJilles Tjoelker 			break;
96ef70de18SJilles Tjoelker 		case 'e':
97ef70de18SJilles Tjoelker 			/* set close-on-exec */
98ef70de18SJilles Tjoelker 			o |= O_CLOEXEC;
99ef70de18SJilles Tjoelker 			break;
100*d5fec489SCraig Rodrigues 		case 'v':
101*d5fec489SCraig Rodrigues 			/* verify */
102*d5fec489SCraig Rodrigues 			o |= O_VERIFY;
103*d5fec489SCraig Rodrigues 			break;
104ef70de18SJilles Tjoelker 		default:
105ef70de18SJilles Tjoelker 			known = 0;
106ef70de18SJilles Tjoelker 			break;
107ef70de18SJilles Tjoelker 		}
108ef70de18SJilles Tjoelker 	} while (known);
109ef70de18SJilles Tjoelker 
110ef70de18SJilles Tjoelker 	if ((o & O_EXCL) != 0 && m == O_RDONLY) {
111bd26fb81SDavid Schultz 		errno = EINVAL;
112bd26fb81SDavid Schultz 		return (0);
113bd26fb81SDavid Schultz 	}
1145ba651f0SJilles Tjoelker 
11558f0484fSRodney W. Grimes 	*optr = m | o;
11658f0484fSRodney W. Grimes 	return (ret);
11758f0484fSRodney W. Grimes }
118