xref: /illumos-gate/usr/src/lib/libc/port/stdio/_endopen.c (revision 8a67df42)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a5f69788Scraigm  * Common Development and Distribution License (the "License").
6a5f69788Scraigm  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28cd62a92dSRobert Mustacchi /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
30b89a2c3eSTheo Schlossnagle /* Copyright (c) 2013 OmniTI Computer Consulting, Inc. All rights reserved. */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *	This routine is a special case, in that it is aware of
347c478bd9Sstevel@tonic-gate  *	both small and large file interfaces. It must be built
357c478bd9Sstevel@tonic-gate  *	in the small compilation environment.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include "lint.h"
397c478bd9Sstevel@tonic-gate #include "file64.h"
407c478bd9Sstevel@tonic-gate #include <mtlib.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <fcntl.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <limits.h>
467c478bd9Sstevel@tonic-gate #include <thread.h>
477c478bd9Sstevel@tonic-gate #include <synch.h>
487c478bd9Sstevel@tonic-gate #include "stdiom.h"
497c478bd9Sstevel@tonic-gate #include <errno.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * open UNIX file name, associate with iop
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate FILE *
_endopen(const char * name,const char * type,FILE * iop,int largefile)567c478bd9Sstevel@tonic-gate _endopen(const char *name, const char *type, FILE *iop, int largefile)
577c478bd9Sstevel@tonic-gate {
58*8a67df42SToomas Soome 	int oflag, fd, fflag;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 	if (iop == NULL)
617c478bd9Sstevel@tonic-gate 		return (NULL);
62b89a2c3eSTheo Schlossnagle 
63cd62a92dSRobert Mustacchi 	if (_stdio_flags(type, &oflag, &fflag) != 0) {
64cd62a92dSRobert Mustacchi 		return (NULL);
65b5e59502SRobert Mustacchi 	}
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	/* select small or large file open based on flag */
687c478bd9Sstevel@tonic-gate 	if (largefile) {
697c478bd9Sstevel@tonic-gate 		fd = open64(name, oflag, 0666);
707c478bd9Sstevel@tonic-gate 	} else {
717c478bd9Sstevel@tonic-gate 		fd = open(name, oflag, 0666);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 	if (fd < 0)
747c478bd9Sstevel@tonic-gate 		return (NULL);
757c478bd9Sstevel@tonic-gate 
76a5f69788Scraigm 	/* As long as we make sure _flag stays != 0, we don't need to lock */
777c478bd9Sstevel@tonic-gate #ifdef	_LP64
787c478bd9Sstevel@tonic-gate 	iop->_file = fd;
79cd62a92dSRobert Mustacchi 	iop->_flag = (iop->_flag & ~_DEF_FLAG_MASK) | fflag;
807c478bd9Sstevel@tonic-gate #else
81a5f69788Scraigm 	if (fd <= _FILE_FD_MAX) {
82a5f69788Scraigm 		SET_FILE(iop, fd);
83a5f69788Scraigm 	} else if (_file_set(iop, fd, type) != 0) {
84a5f69788Scraigm 		/* errno set in _file_set() */
85cd62a92dSRobert Mustacchi 		int err = errno;
867c478bd9Sstevel@tonic-gate 		(void) close(fd);
87cd62a92dSRobert Mustacchi 		errno = err;
887c478bd9Sstevel@tonic-gate 		return (NULL);
897c478bd9Sstevel@tonic-gate 	}
90a5f69788Scraigm 	iop->_flag = fflag;
917c478bd9Sstevel@tonic-gate #endif	/*	_LP64	*/
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	if (oflag == (O_WRONLY | O_APPEND | O_CREAT)) {	/* type == "a" */
94cd62a92dSRobert Mustacchi 		if (_xseek64(iop, (off64_t)0, SEEK_END) < (off64_t)0) {
95cd62a92dSRobert Mustacchi 			(void) _xclose(iop);
967c478bd9Sstevel@tonic-gate 			return (NULL);
977c478bd9Sstevel@tonic-gate 		}
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	return (iop);
1017c478bd9Sstevel@tonic-gate }
102