sl@0: /* FLAGS.C
sl@0:  * 
sl@0:  * Portions Copyright (c) 1990-1999 Nokia Corporation and/or its subsidiary(-ies).
sl@0:  * All rights reserved.
sl@0:  */
sl@0: 
sl@0: /* No user fns here. Pesch 15apr92 */
sl@0: 
sl@0: /*
sl@0:  * Copyright (c) 1990 Regents of the University of California.
sl@0:  * All rights reserved.
sl@0:  *
sl@0:  * Redistribution and use in source and binary forms are permitted
sl@0:  * provided that the above copyright notice and this paragraph are
sl@0:  * duplicated in all such forms and that any documentation,
sl@0:  * advertising materials, and other materials related to such
sl@0:  * distribution and use acknowledge that the software was developed
sl@0:  * by the University of California, Berkeley.  The name of the
sl@0:  * University may not be used to endorse or promote products derived
sl@0:  * from this software without specific prior written permission.
sl@0:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
sl@0:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
sl@0:  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
sl@0:  */
sl@0: 
sl@0: #include <stdio_r.h>
sl@0: #include <time.h>
sl@0: #include <fcntl.h>
sl@0: #include "LOCAL.H"
sl@0: 
sl@0: #include <errno.h>
sl@0: #include <sys/types.h>
sl@0: 
sl@0: /*
sl@0:  * Return the (stdio) flags for a given mode.  Store the flags
sl@0:  * to be passed to an open() syscall through *optr.
sl@0:  * Return 0 on error.
sl@0:  */
sl@0: 
sl@0: 
sl@0: int __sflags (struct _reent *ptr, const wchar_t *mode, int *optr)
sl@0: {
sl@0:   register int ret, m, o;
sl@0: 
sl@0:   switch (mode[0])
sl@0:     {
sl@0:     case L'r':			/* open for reading */
sl@0:       ret = __SRD;
sl@0:       m = O_RDONLY;
sl@0:       o = 0;
sl@0:       break;
sl@0: 
sl@0:     case L'w':			/* open for writing */
sl@0:       ret = __SWR;
sl@0:       m = O_WRONLY;
sl@0:       o = O_CREAT | O_TRUNC;
sl@0:       break;
sl@0: 
sl@0:     case L'a':			/* open for appending */
sl@0:       ret = __SWR | __SAPP;
sl@0:       m = O_WRONLY;
sl@0:       o = O_CREAT | O_APPEND;
sl@0:       break;
sl@0: 
sl@0:     default:			/* illegal mode */
sl@0:       ptr->_errno = EINVAL;
sl@0:       return (0);
sl@0:     }
sl@0:   if (mode[1] == L'+' || mode[2] == L'+')
sl@0:     {
sl@0:       ret = __SRW;
sl@0:       m = O_RDWR;
sl@0:     }
sl@0:   if (mode[1] == L'b' || mode[2] == L'b')
sl@0:     {
sl@0:       m |= O_BINARY;
sl@0:     }
sl@0:   else
sl@0:     {
sl@0:       m |= O_TEXT;
sl@0:     }
sl@0:   *optr = m | o;
sl@0:   return ret;
sl@0: }