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