/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 19934* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Chris Torek.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#if defined(LIBC_SCCS) && !defined(lint)35static char sccsid[] = "@(#)flags.c 8.1 (Berkeley) 6/4/93";36#endif /* LIBC_SCCS and not lint */37#include "bsd_compat.h"38__FBSDID("$FreeBSD: head/lib/libc/stdio/flags.c 326025 2017-11-20 19:49:47Z pfg $");3940#include <sys/types.h>41#include <sys/file.h>42#include <stdio.h>43#include <errno.h>44#include <fcntl.h>4546/*47* Return the (stdio) flags for a given mode. Store the flags48* to be passed to an _open() syscall through *optr.49* Return 1 on error.50*/51int52checkflags(const char *mode, int *optr)53{54int ret, m, o, known;55ret = 0;5657switch (*mode++) {5859case 'r': /* open for reading */60ret = 1;61m = O_RDONLY;62o = 0;63break;6465case 'w': /* open for writing */66ret = 1;67m = O_WRONLY;68o = O_CREAT | O_TRUNC;69break;7071case 'a': /* open for appending */72ret = 1;73m = O_WRONLY;74o = O_CREAT | O_APPEND;75break;7677default: /* illegal mode */78errno = EINVAL;79return (0);80}8182do {83known = 1;84switch (*mode++) {85case 'b':86/* 'b' (binary) is ignored */87break;88case '+':89/* [rwa][b]\+ means read and write */90ret = 1;91m = O_RDWR;92break;93case 'x':94/* 'x' means exclusive (fail if the file exists) */95o |= O_EXCL;96break;97case 'e':98/* set close-on-exec */99o |= O_CLOEXEC;100break;101default:102known = 0;103break;104}105} while (known);106107if ((o & O_EXCL) != 0 && m == O_RDONLY) {108errno = EINVAL;109return (1);110}111112*optr = m | o;113return (ret);114}115116117