Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / flags.c
blobd686fa046f2edc67b3baf576f223a8b467bd81b4
1 /*
2 * Copyright (c) 1990 Regents of the University of California.
3 * All rights reserved.
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.
17 /* No user fns here. Pesch 15apr92 */
19 #include <_ansi.h>
20 #include <stdio.h>
21 #include <time.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <sys/types.h>
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.
32 int
33 __sflags (struct _reent *ptr,
34 register char *mode,
35 int *optr)
37 register int ret, m, o;
39 switch (mode[0])
41 case 'r': /* open for reading */
42 ret = __SRD;
43 m = O_RDONLY;
44 o = 0;
45 break;
47 case 'w': /* open for writing */
48 ret = __SWR;
49 m = O_WRONLY;
50 o = O_CREAT | O_TRUNC;
51 break;
53 case 'a': /* open for appending */
54 ret = __SWR | __SAPP;
55 m = O_WRONLY;
56 o = O_CREAT | O_APPEND;
57 break;
58 default: /* illegal mode */
59 _REENT_ERRNO(ptr) = EINVAL;
60 return (0);
62 while (*++mode)
64 switch (*mode)
66 case '+':
67 ret = (ret & ~(__SRD | __SWR)) | __SRW;
68 m = (m & ~O_ACCMODE) | O_RDWR;
69 break;
70 case 'b':
71 #ifdef O_BINARY
72 m |= O_BINARY;
73 #endif
74 break;
75 #ifdef __CYGWIN__
76 case 't':
77 m |= O_TEXT;
78 break;
79 #endif
80 #if defined (O_CLOEXEC) && defined (_GLIBC_EXTENSION)
81 case 'e':
82 m |= O_CLOEXEC;
83 break;
84 #endif
85 case 'x':
86 m |= O_EXCL;
87 break;
88 default:
89 break;
92 *optr = m | o;
93 return ret;