xref: /onnv-gate/usr/src/lib/libbc/libc/stdio/4.2/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 /*
230Sstevel@tonic-gate  * Copyright 1986 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*      Copyright (c) 1984 AT&T */
280Sstevel@tonic-gate /*        All Rights Reserved   */
290Sstevel@tonic-gate 
30*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*LINTLIBRARY*/
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate 
36*722Smuffin extern int	fclose();
37*722Smuffin extern FILE	*_findiop();
38*722Smuffin 
39*722Smuffin static FILE	*_endopen(char *, char *, FILE *);
400Sstevel@tonic-gate 
410Sstevel@tonic-gate FILE *
fopen(char * file,char * mode)42*722Smuffin fopen(char *file, char *mode)
430Sstevel@tonic-gate {
440Sstevel@tonic-gate 	return (_endopen(file, mode, _findiop()));
450Sstevel@tonic-gate }
460Sstevel@tonic-gate 
470Sstevel@tonic-gate FILE *
freopen(char * file,char * mode,FILE * iop)48*722Smuffin freopen(char *file, char *mode, FILE *iop)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate 	(void) fclose(iop); /* doesn't matter if this fails */
510Sstevel@tonic-gate 	return (_endopen(file, mode, iop));
520Sstevel@tonic-gate }
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static FILE *
_endopen(char * file,char * mode,FILE * iop)55*722Smuffin _endopen(char *file, char *mode, FILE *iop)
560Sstevel@tonic-gate {
57*722Smuffin 	int	plus, oflag, fd;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	if (iop == NULL || file == NULL || file[0] == '\0')
600Sstevel@tonic-gate 		return (NULL);
610Sstevel@tonic-gate 	plus = (mode[1] == '+');
620Sstevel@tonic-gate 	switch (mode[0]) {
630Sstevel@tonic-gate 	case 'w':
640Sstevel@tonic-gate 		oflag = (plus ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT;
650Sstevel@tonic-gate 		break;
660Sstevel@tonic-gate 	case 'a':
670Sstevel@tonic-gate 		oflag = (plus ? O_RDWR : O_WRONLY) | O_CREAT;
680Sstevel@tonic-gate 		break;
690Sstevel@tonic-gate 	case 'r':
700Sstevel@tonic-gate 		oflag = plus ? O_RDWR : O_RDONLY;
710Sstevel@tonic-gate 		break;
720Sstevel@tonic-gate 	default:
730Sstevel@tonic-gate 		return (NULL);
740Sstevel@tonic-gate 	}
750Sstevel@tonic-gate 	if ((fd = open(file, oflag, 0666)) < 0)
760Sstevel@tonic-gate 		return (NULL);
770Sstevel@tonic-gate 	iop->_cnt = 0;
780Sstevel@tonic-gate 	iop->_file = fd;
790Sstevel@tonic-gate 	iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT;
800Sstevel@tonic-gate 	if (mode[0] == 'a')   {
810Sstevel@tonic-gate 		if ((lseek(fd,0L,2)) < 0)  {
820Sstevel@tonic-gate 			(void) close(fd);
830Sstevel@tonic-gate 			return NULL;
840Sstevel@tonic-gate 		}
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 	iop->_base = iop->_ptr = NULL;
870Sstevel@tonic-gate 	iop->_bufsiz = 0;
880Sstevel@tonic-gate 	return (iop);
890Sstevel@tonic-gate }
90