xref: /onnv-gate/usr/src/lib/libbc/libc/stdio/sys5/fopen.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*      Copyright (c) 1984 AT&T */
230Sstevel@tonic-gate /*        All Rights Reserved   */
240Sstevel@tonic-gate 
25*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*LINTLIBRARY*/
28*722Smuffin 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <fcntl.h>
310Sstevel@tonic-gate 
32*722Smuffin extern FILE	*_findiop();
33*722Smuffin static FILE	*_endopen();
340Sstevel@tonic-gate 
350Sstevel@tonic-gate FILE *
fopen(char * file,char * mode)36*722Smuffin fopen(char *file, char *mode)
370Sstevel@tonic-gate {
380Sstevel@tonic-gate 	return (_endopen(file, mode, _findiop()));
390Sstevel@tonic-gate }
400Sstevel@tonic-gate 
410Sstevel@tonic-gate FILE *
freopen(char * file,char * mode,FILE * iop)42*722Smuffin freopen(char *file, char *mode, FILE *iop)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate 	(void) fclose(iop); /* doesn't matter if this fails */
450Sstevel@tonic-gate 	return (_endopen(file, mode, iop));
460Sstevel@tonic-gate }
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static FILE *
_endopen(char * file,char * mode,FILE * iop)49*722Smuffin _endopen(char *file, char *mode, FILE *iop)
500Sstevel@tonic-gate {
51*722Smuffin 	int	plus, oflag, fd;
520Sstevel@tonic-gate 
530Sstevel@tonic-gate 	if (iop == NULL || file == NULL || file[0] == '\0')
540Sstevel@tonic-gate 		return (NULL);
550Sstevel@tonic-gate 	plus = (mode[1] == '+');
560Sstevel@tonic-gate 	switch (mode[0]) {
570Sstevel@tonic-gate 	case 'w':
580Sstevel@tonic-gate 		oflag = (plus ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT;
590Sstevel@tonic-gate 		break;
600Sstevel@tonic-gate 	case 'a':
610Sstevel@tonic-gate 		oflag = (plus ? O_RDWR : O_WRONLY) | O_APPEND | O_CREAT;
620Sstevel@tonic-gate 		break;
630Sstevel@tonic-gate 	case 'r':
640Sstevel@tonic-gate 		oflag = plus ? O_RDWR : O_RDONLY;
650Sstevel@tonic-gate 		break;
660Sstevel@tonic-gate 	default:
670Sstevel@tonic-gate 		return (NULL);
680Sstevel@tonic-gate 	}
690Sstevel@tonic-gate 	if ((fd = open(file, oflag, 0666)) < 0)
700Sstevel@tonic-gate 		return (NULL);
710Sstevel@tonic-gate 	iop->_cnt = 0;
720Sstevel@tonic-gate 	iop->_file = fd;
730Sstevel@tonic-gate 	iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT;
740Sstevel@tonic-gate 	if (mode[0] == 'a')   {
750Sstevel@tonic-gate 		if (!plus)  {
760Sstevel@tonic-gate 			/* if update only mode, move file pointer to the end
770Sstevel@tonic-gate 			   of the file */
780Sstevel@tonic-gate 			if ((lseek(fd,0L,2)) < 0)  {
790Sstevel@tonic-gate 				(void) close(fd);
800Sstevel@tonic-gate 				return NULL;
810Sstevel@tonic-gate 			}
820Sstevel@tonic-gate 		}
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 	iop->_base = iop->_ptr = NULL;
850Sstevel@tonic-gate 	iop->_bufsiz = 0;
860Sstevel@tonic-gate 	return (iop);
870Sstevel@tonic-gate }
88