xref: /original-bsd/lib/libc/stdio/fopen.c (revision bdbb8aec)
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[] = "@(#)fopen.c	5.3 (Berkeley) 01/20/91";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <stdio.h>
18 #include <errno.h>
19 #include "local.h"
20 
21 FILE *
22 fopen(file, mode)
23 	char *file;
24 	char *mode;
25 {
26 	register FILE *fp;
27 	register int f;
28 	int flags, oflags;
29 
30 	if ((flags = __sflags(mode, &oflags)) == 0)
31 		return (NULL);
32 	if ((fp = __sfp()) == NULL)
33 		return (NULL);
34 	if ((f = open(file, oflags, DEFFILEMODE)) < 0) {
35 		fp->_flags = 0;			/* release */
36 		return (NULL);
37 	}
38 	fp->_file = f;
39 	fp->_flags = flags;
40 	fp->_cookie = fp;
41 	fp->_read = __sread;
42 	fp->_write = __swrite;
43 	fp->_seek = __sseek;
44 	fp->_close = __sclose;
45 	return (fp);
46 }
47