1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
29
30 /* Copyright (c) 2013 OmniTI Computer Consulting, Inc. All rights reserved. */
31 /*
32 * Copyright 2020 Robert Mustacchi
33 */
34
35 /*
36 * Commonized processing of the 'mode' string for stdio.
37 */
38
39 #include "mtlib.h"
40 #include "file64.h"
41 #include <stdio.h>
42 #include <errno.h>
43 #include <sys/types.h>
44 #include <fcntl.h>
45
46 int
_stdio_flags(const char * type,int * oflagsp,int * fflagsp)47 _stdio_flags(const char *type, int *oflagsp, int *fflagsp)
48 {
49 int oflag, fflag, plusflag, eflag, xflag;
50 const char *echr;
51
52 oflag = fflag = 0;
53 switch (type[0]) {
54 default:
55 errno = EINVAL;
56 return (-1);
57 case 'r':
58 oflag = O_RDONLY;
59 fflag = _IOREAD;
60 break;
61 case 'w':
62 oflag = O_WRONLY | O_TRUNC | O_CREAT;
63 fflag = _IOWRT;
64 break;
65 case 'a':
66 oflag = O_WRONLY | O_APPEND | O_CREAT;
67 fflag = _IOWRT;
68 break;
69 }
70
71 plusflag = 0;
72 eflag = 0;
73 xflag = 0;
74 for (echr = type + 1; *echr != '\0'; echr++) {
75 switch (*echr) {
76 /* UNIX ignores 'b' and treats text and binary the same */
77 default:
78 break;
79 case '+':
80 plusflag = 1;
81 break;
82 case 'e':
83 eflag = 1;
84 break;
85 case 'x':
86 xflag = 1;
87 break;
88 }
89 }
90
91 if (eflag) {
92 /* Subsequent to a mode flag, 'e' indicates O_CLOEXEC */
93 oflag = oflag | O_CLOEXEC;
94 }
95
96 if (plusflag) {
97 oflag = (oflag & ~(O_RDONLY | O_WRONLY)) | O_RDWR;
98 fflag = _IORW;
99 }
100
101 if (xflag) {
102 oflag |= O_EXCL;
103 }
104
105 *oflagsp = oflag;
106 *fflagsp = fflag;
107
108 return (0);
109 }
110