xref: /csrg-svn/lib/libc/stdio/flags.c (revision 46111)
1*46111Sbostic /*-
2*46111Sbostic  * Copyright (c) 1990 The Regents of the University of California.
3*46111Sbostic  * All rights reserved.
4*46111Sbostic  *
5*46111Sbostic  * This code is derived from software contributed to Berkeley by
6*46111Sbostic  * Chris Torek.
7*46111Sbostic  *
8*46111Sbostic  * %sccs.include.redist.c%
9*46111Sbostic  */
10*46111Sbostic 
11*46111Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*46111Sbostic static char sccsid[] = "@(#)flags.c	5.1 (Berkeley) 01/20/91";
13*46111Sbostic #endif /* LIBC_SCCS and not lint */
14*46111Sbostic 
15*46111Sbostic #include <sys/types.h>
16*46111Sbostic #include <sys/file.h>
17*46111Sbostic #include <stdio.h>
18*46111Sbostic #include <errno.h>
19*46111Sbostic 
20*46111Sbostic /*
21*46111Sbostic  * Return the (stdio) flags for a given mode.  Store the flags
22*46111Sbostic  * to be passed to an open() syscall through *optr.
23*46111Sbostic  * Return 0 on error.
24*46111Sbostic  */
25*46111Sbostic __sflags(mode, optr)
26*46111Sbostic 	register char *mode;
27*46111Sbostic 	int *optr;
28*46111Sbostic {
29*46111Sbostic 	register int ret, m, o;
30*46111Sbostic 
31*46111Sbostic 	switch (*mode++) {
32*46111Sbostic 
33*46111Sbostic 	case 'r':	/* open for reading */
34*46111Sbostic 		ret = __SRD;
35*46111Sbostic 		m = O_RDONLY;
36*46111Sbostic 		o = 0;
37*46111Sbostic 		break;
38*46111Sbostic 
39*46111Sbostic 	case 'w':	/* open for writing */
40*46111Sbostic 		ret = __SWR;
41*46111Sbostic 		m = O_WRONLY;
42*46111Sbostic 		o = O_CREAT | O_TRUNC;
43*46111Sbostic 		break;
44*46111Sbostic 
45*46111Sbostic 	case 'a':	/* open for appending */
46*46111Sbostic 		ret = __SWR;
47*46111Sbostic 		m = O_WRONLY;
48*46111Sbostic 		o = O_CREAT | O_APPEND;
49*46111Sbostic 		break;
50*46111Sbostic 
51*46111Sbostic 	default:	/* illegal mode */
52*46111Sbostic 		errno = EINVAL;
53*46111Sbostic 		return (0);
54*46111Sbostic 	}
55*46111Sbostic 
56*46111Sbostic 	/* [rwa]\+ or [rwa]b\+ means read and write */
57*46111Sbostic 	if (*mode == '+' || (*mode == 'b' && mode[1] == '+')) {
58*46111Sbostic 		ret = __SRW;
59*46111Sbostic 		m = O_RDWR;
60*46111Sbostic 	}
61*46111Sbostic 	*optr = m | o;
62*46111Sbostic 	return (ret);
63*46111Sbostic }
64