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