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 <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22 #include <errno.h>
23 #include "stdio_private.h"
24 
25 /*
26  * Return the (stdio) flags for a given mode.  Store the flags
27  * to be passed to an open() syscall through *optr.
28  * Return 0 on error.
29  */
30 
31 int
__posix_sflags(const char * mode,int * optr)32 __posix_sflags (const char *mode, int *optr)
33 {
34   int ret, m, o;
35 
36   switch (mode[0])
37     {
38     case 'r':			/* open for reading */
39       ret = __SRD;
40       m = O_RDONLY;
41       o = 0;
42       break;
43 
44     case 'w':			/* open for writing */
45       ret = __SWR;
46       m = O_WRONLY;
47       o = O_CREAT | O_TRUNC;
48       break;
49 
50     case 'a':			/* open for appending */
51       ret = __SWR;
52       m = O_WRONLY;
53       o = O_CREAT | O_APPEND;
54       break;
55     default:			/* illegal mode */
56       errno = EINVAL;
57       return (0);
58     }
59   while (*++mode)
60     {
61       switch (*mode)
62 	{
63 	case '+':
64 	  ret |= (__SRD | __SWR);
65 	  m = (m & ~O_ACCMODE) | O_RDWR;
66 	  break;
67 	default:
68 	  break;
69 	}
70     }
71   *optr = m | o;
72   return ret;
73 }
74