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 #define _DEFAULT_SOURCE
20 #include <_ansi.h>
21 #include <stdio.h>
22 #include <time.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <sys/types.h>
26 #include "local.h"
27 
28 /*
29  * Return the (stdio) flags for a given mode.  Store the flags
30  * to be passed to an open() syscall through *optr.
31  * Return 0 on error.
32  */
33 
34 int
__sflags(const char * mode,int * optr)35 __sflags (
36        const char *mode,
37        int *optr)
38 {
39   register int ret, m, o;
40 
41   switch (mode[0])
42     {
43     case 'r':			/* open for reading */
44       ret = __SRD;
45       m = O_RDONLY;
46       o = 0;
47       break;
48 
49     case 'w':			/* open for writing */
50       ret = __SWR;
51       m = O_WRONLY;
52       o = O_CREAT | O_TRUNC;
53       break;
54 
55     case 'a':			/* open for appending */
56       ret = __SWR | __SAPP;
57       m = O_WRONLY;
58       o = O_CREAT | O_APPEND;
59       break;
60     default:			/* illegal mode */
61       _REENT_ERRNO(ptr) = EINVAL;
62       return (0);
63     }
64   while (*++mode)
65     {
66       switch (*mode)
67 	{
68 	case '+':
69 	  ret = (ret & ~(__SRD | __SWR)) | __SRW;
70 	  m = (m & ~O_ACCMODE) | O_RDWR;
71 	  break;
72 	case 'b':
73 #ifdef O_BINARY
74 	  m |= O_BINARY;
75 #endif
76 	  break;
77 #ifdef __CYGWIN__
78 	case 't':
79 	  m |= O_TEXT;
80 	  break;
81 #endif
82 #if defined (O_CLOEXEC) && defined (_GLIBC_EXTENSION)
83 	case 'e':
84 	  m |= O_CLOEXEC;
85 	  break;
86 #endif
87 	case 'x':
88 	  m |= O_EXCL;
89 	  break;
90 	default:
91 	  break;
92 	}
93     }
94   *optr = m | o;
95   return ret;
96 }
97