xref: /titanic_51/usr/src/lib/libbc/libc/stdio/sys5/fopen.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*      Copyright (c) 1984 AT&T */
23 /*        All Rights Reserved   */
24 
25 #pragma ident	"%Z%%M%	%I%	%E% SMI"
26 
27 /*LINTLIBRARY*/
28 
29 #include <stdio.h>
30 #include <fcntl.h>
31 
32 extern FILE	*_findiop();
33 static FILE	*_endopen();
34 
35 FILE *
36 fopen(char *file, char *mode)
37 {
38 	return (_endopen(file, mode, _findiop()));
39 }
40 
41 FILE *
42 freopen(char *file, char *mode, FILE *iop)
43 {
44 	(void) fclose(iop); /* doesn't matter if this fails */
45 	return (_endopen(file, mode, iop));
46 }
47 
48 static FILE *
49 _endopen(char *file, char *mode, FILE *iop)
50 {
51 	int	plus, oflag, fd;
52 
53 	if (iop == NULL || file == NULL || file[0] == '\0')
54 		return (NULL);
55 	plus = (mode[1] == '+');
56 	switch (mode[0]) {
57 	case 'w':
58 		oflag = (plus ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT;
59 		break;
60 	case 'a':
61 		oflag = (plus ? O_RDWR : O_WRONLY) | O_APPEND | O_CREAT;
62 		break;
63 	case 'r':
64 		oflag = plus ? O_RDWR : O_RDONLY;
65 		break;
66 	default:
67 		return (NULL);
68 	}
69 	if ((fd = open(file, oflag, 0666)) < 0)
70 		return (NULL);
71 	iop->_cnt = 0;
72 	iop->_file = fd;
73 	iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT;
74 	if (mode[0] == 'a')   {
75 		if (!plus)  {
76 			/* if update only mode, move file pointer to the end
77 			   of the file */
78 			if ((lseek(fd,0L,2)) < 0)  {
79 				(void) close(fd);
80 				return NULL;
81 			}
82 		}
83 	}
84 	iop->_base = iop->_ptr = NULL;
85 	iop->_bufsiz = 0;
86 	return (iop);
87 }
88