xref: /csrg-svn/lib/libc/stdio/fopen.c (revision 17951)
1 /* @(#)fopen.c	4.4 (Berkeley) 02/13/85 */
2 #include <sys/types.h>
3 #include <sys/file.h>
4 #include <stdio.h>
5 
6 FILE *
7 fopen(file, mode)
8 	char *file;
9 	register char *mode;
10 {
11 	register FILE *iop;
12 	register f, rw, oflags;
13 	extern FILE *_findiop();
14 
15 	iop = _findiop();
16 	if (iop == NULL)
17 		return (NULL);
18 
19 	rw = (mode[1] == '+');
20 
21 	switch (*mode) {
22 	case 'a':
23 		oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY);
24 		break;
25 	case 'r':
26 		oflags = rw ? O_RDWR : O_RDONLY;
27 		break;
28 	case 'w':
29 		oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY);
30 		break;
31 	default:
32 		return (NULL);
33 	}
34 
35 	f = open(file, oflags, 0666);
36 	if (f < 0)
37 		return (NULL);
38 
39 	if (*mode == 'a')
40 		lseek(f, (off_t)0, L_XTND);
41 
42 	iop->_cnt = 0;
43 	iop->_file = f;
44 	iop->_bufsiz = 0;
45 	if (rw)
46 		iop->_flag = _IORW;
47 	else if (*mode == 'r')
48 		iop->_flag = _IOREAD;
49 	else
50 		iop->_flag = _IOWRT;
51 	iop->_base = iop->_ptr = NULL;
52 	return (iop);
53 }
54