1 /*
2  * Copyright (c) 1990 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * and/or other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 /* No user fns here. Pesch 15apr92 */
18 
19 #include "stdio_private.h"
20 
21 /*
22  * In POSIX_IO mode, this is called __posix_sflags and __stdio_sflags
23  * is a static inline on top of this. Otherwise, this is called
24  * __stdio_sflags.
25  *
26  * Return the (stdio) flags for a given mode. When POSIX_IO is
27  * available, also store the flags to be passed to an open() syscall
28  * through *optr.
29  *
30  * Return 0 on error.
31  */
32 
33 #ifdef POSIX_IO
34 int
__posix_sflags(const char * mode,int * optr)35 __posix_sflags (const char *mode, int *optr)
36 #else
37 int
38 __stdio_sflags (const char *mode)
39 #endif
40 {
41   int ret;
42 #ifdef POSIX_IO
43   int m, o;
44 #endif
45 
46   switch (mode[0])
47     {
48     case 'r':			/* open for reading */
49       ret = __SRD;
50 #ifdef POSIX_IO
51       m = O_RDONLY;
52       o = 0;
53 #endif
54       break;
55 
56     case 'w':			/* open for writing */
57       ret = __SWR;
58 #ifdef POSIX_IO
59       m = O_WRONLY;
60       o = O_CREAT | O_TRUNC;
61 #endif
62       break;
63 
64     case 'a':			/* open for appending */
65       ret = __SWR;
66 #ifdef POSIX_IO
67       m = O_WRONLY;
68       o = O_CREAT | O_APPEND;
69 #endif
70       break;
71     default:			/* illegal mode */
72       errno = EINVAL;
73       return (0);
74     }
75   while (*++mode)
76     {
77       switch (*mode)
78 	{
79 	case '+':
80 	  ret |= (__SRD | __SWR);
81 #ifdef POSIX_IO
82 	  m = (m & ~O_ACCMODE) | O_RDWR;
83 #endif
84 	  break;
85 	default:
86 	  break;
87 	}
88     }
89 #ifdef POSIX_IO
90   *optr = m | o;
91 #endif
92   return ret;
93 }
94