xref: /csrg-svn/lib/libc/stdio/flags.c (revision 61180)
146111Sbostic /*-
2*61180Sbostic  * Copyright (c) 1990, 1993
3*61180Sbostic  *	The Regents of the University of California.  All rights reserved.
446111Sbostic  *
546111Sbostic  * This code is derived from software contributed to Berkeley by
646111Sbostic  * Chris Torek.
746111Sbostic  *
846111Sbostic  * %sccs.include.redist.c%
946111Sbostic  */
1046111Sbostic 
1146111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*61180Sbostic static char sccsid[] = "@(#)flags.c	8.1 (Berkeley) 06/04/93";
1346111Sbostic #endif /* LIBC_SCCS and not lint */
1446111Sbostic 
1546111Sbostic #include <sys/types.h>
1646111Sbostic #include <sys/file.h>
1746111Sbostic #include <stdio.h>
1846111Sbostic #include <errno.h>
1946111Sbostic 
2046111Sbostic /*
2146111Sbostic  * Return the (stdio) flags for a given mode.  Store the flags
2246111Sbostic  * to be passed to an open() syscall through *optr.
2346111Sbostic  * Return 0 on error.
2446111Sbostic  */
__sflags(mode,optr)2546111Sbostic __sflags(mode, optr)
2646111Sbostic 	register char *mode;
2746111Sbostic 	int *optr;
2846111Sbostic {
2946111Sbostic 	register int ret, m, o;
3046111Sbostic 
3146111Sbostic 	switch (*mode++) {
3246111Sbostic 
3346111Sbostic 	case 'r':	/* open for reading */
3446111Sbostic 		ret = __SRD;
3546111Sbostic 		m = O_RDONLY;
3646111Sbostic 		o = 0;
3746111Sbostic 		break;
3846111Sbostic 
3946111Sbostic 	case 'w':	/* open for writing */
4046111Sbostic 		ret = __SWR;
4146111Sbostic 		m = O_WRONLY;
4246111Sbostic 		o = O_CREAT | O_TRUNC;
4346111Sbostic 		break;
4446111Sbostic 
4546111Sbostic 	case 'a':	/* open for appending */
4646111Sbostic 		ret = __SWR;
4746111Sbostic 		m = O_WRONLY;
4846111Sbostic 		o = O_CREAT | O_APPEND;
4946111Sbostic 		break;
5046111Sbostic 
5146111Sbostic 	default:	/* illegal mode */
5246111Sbostic 		errno = EINVAL;
5346111Sbostic 		return (0);
5446111Sbostic 	}
5546111Sbostic 
5646111Sbostic 	/* [rwa]\+ or [rwa]b\+ means read and write */
5746111Sbostic 	if (*mode == '+' || (*mode == 'b' && mode[1] == '+')) {
5846111Sbostic 		ret = __SRW;
5946111Sbostic 		m = O_RDWR;
6046111Sbostic 	}
6146111Sbostic 	*optr = m | o;
6246111Sbostic 	return (ret);
6346111Sbostic }
64