source: trunk/libs/newlib/src/newlib/libc/stdio/flags.c @ 450

Last change on this file since 450 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.2 KB
Line 
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 <stdio.h>
21#include <time.h>
22#include <fcntl.h>
23#include <errno.h>
24#include <sys/types.h>
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
32int
33__sflags (struct _reent *ptr,
34       register char *mode,
35       int *optr)
36{
37  register int ret, m, o;
38
39  switch (mode[0])
40    {
41    case 'r':                   /* open for reading */
42      ret = __SRD;
43      m = O_RDONLY;
44      o = 0;
45      break;
46
47    case 'w':                   /* open for writing */
48      ret = __SWR;
49      m = O_WRONLY;
50      o = O_CREAT | O_TRUNC;
51      break;
52
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      ptr->_errno = EINVAL;
60      return (0);
61    }
62  while (*++mode)
63    {
64      switch (*mode)
65        {
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;
90        }
91    }
92#if defined (O_TEXT) && !defined (__CYGWIN__)
93  if (!(m | O_BINARY))
94    m |= O_TEXT;
95#endif
96  *optr = m | o;
97  return ret;
98}
Note: See TracBrowser for help on using the repository browser.