Rev 4921 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4349 | Serge | 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 | * advertising materials, and 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 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
||
16 | */ |
||
17 | /* No user fns here. Pesch 15apr92 */ |
||
18 | |||
19 | #include <_ansi.h> |
||
20 | #include |
||
21 | #include |
||
22 | #include |
||
23 | #include |
||
24 | #include |
||
25 | |||
26 | /* |
||
27 | * Return the (stdio) flags for a given mode. Store the flags |
||
28 | * to be passed to an open() syscall through *optr. |
||
29 | * Return 0 on error. |
||
30 | */ |
||
31 | |||
32 | int |
||
33 | _DEFUN(__sflags, (ptr, mode, optr), |
||
34 | struct _reent *ptr _AND |
||
35 | register char *mode _AND |
||
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 | ptr->_errno = EINVAL; |
||
61 | return (0); |
||
62 | } |
||
4921 | Serge | 63 | while (*++mode) |
4349 | Serge | 64 | { |
4921 | Serge | 65 | switch (*mode) |
66 | { |
||
67 | case '+': |
||
6099 | serge | 68 | ret = (ret & ~(__SRD | __SWR)) | __SRW; |
4921 | Serge | 69 | m = (m & ~O_ACCMODE) | O_RDWR; |
70 | break; |
||
71 | case 'b': |
||
4349 | Serge | 72 | #ifdef O_BINARY |
6099 | serge | 73 | m |= O_BINARY; |
4349 | Serge | 74 | #endif |
4921 | Serge | 75 | break; |
4349 | Serge | 76 | #ifdef __CYGWIN__ |
4921 | Serge | 77 | case 't': |
78 | m |= O_TEXT; |
||
79 | break; |
||
4349 | Serge | 80 | #endif |
4921 | Serge | 81 | #if defined (O_CLOEXEC) && defined (_GLIBC_EXTENSION) |
82 | case 'e': |
||
83 | m |= O_CLOEXEC; |
||
84 | break; |
||
85 | #endif |
||
86 | case 'x': |
||
87 | m |= O_EXCL; |
||
88 | break; |
||
89 | default: |
||
90 | break; |
||
91 | } |
||
92 | } |
||
93 | #if defined (O_TEXT) && !defined (__CYGWIN__) |
||
94 | if (!(m | O_BINARY)) |
||
6099 | serge | 95 | m |= O_TEXT; |
4349 | Serge | 96 | #endif |
97 | *optr = m | o; |
||
98 | return ret; |
||
99 | } |