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