os/ossrv/genericopenlibs/cstdlib/LSTDIO/FLAGS.C
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* FLAGS.C
     2  * 
     3  * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
     4  * All rights reserved.
     5  */
     6 
     7 /* No user fns here. Pesch 15apr92 */
     8 
     9 /*
    10  * Copyright (c) 1990 Regents of the University of California.
    11  * All rights reserved.
    12  *
    13  * Redistribution and use in source and binary forms are permitted
    14  * provided that the above copyright notice and this paragraph are
    15  * duplicated in all such forms and that any documentation,
    16  * advertising materials, and other materials related to such
    17  * distribution and use acknowledge that the software was developed
    18  * by the University of California, Berkeley.  The name of the
    19  * University may not be used to endorse or promote products derived
    20  * from this software without specific prior written permission.
    21  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
    22  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
    23  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    24  */
    25 
    26 #include <stdio_r.h>
    27 #include <time.h>
    28 #include <fcntl.h>
    29 #include "LOCAL.H"
    30 
    31 #include <errno.h>
    32 #include <sys/types.h>
    33 
    34 /*
    35  * Return the (stdio) flags for a given mode.  Store the flags
    36  * to be passed to an open() syscall through *optr.
    37  * Return 0 on error.
    38  */
    39 
    40 
    41 int __sflags (struct _reent *ptr, const wchar_t *mode, int *optr)
    42 {
    43   register int ret, m, o;
    44 
    45   switch (mode[0])
    46     {
    47     case L'r':			/* open for reading */
    48       ret = __SRD;
    49       m = O_RDONLY;
    50       o = 0;
    51       break;
    52 
    53     case L'w':			/* open for writing */
    54       ret = __SWR;
    55       m = O_WRONLY;
    56       o = O_CREAT | O_TRUNC;
    57       break;
    58 
    59     case L'a':			/* open for appending */
    60       ret = __SWR | __SAPP;
    61       m = O_WRONLY;
    62       o = O_CREAT | O_APPEND;
    63       break;
    64 
    65     default:			/* illegal mode */
    66       ptr->_errno = EINVAL;
    67       return (0);
    68     }
    69   if (mode[1] == L'+' || mode[2] == L'+')
    70     {
    71       ret = __SRW;
    72       m = O_RDWR;
    73     }
    74   if (mode[1] == L'b' || mode[2] == L'b')
    75     {
    76       m |= O_BINARY;
    77     }
    78   else
    79     {
    80       m |= O_TEXT;
    81     }
    82   *optr = m | o;
    83   return ret;
    84 }