xref: /onnv-gate/usr/src/lib/libc/port/stdio/fdopen.c (revision 6812:febeba71273d)
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
51846Scraigm  * Common Development and Distribution License (the "License").
61846Scraigm  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211219Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 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) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
301846Scraigm #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * Unix routine to do an "fopen" on file descriptor
340Sstevel@tonic-gate  * The mode has to be repeated because you can't query its
350Sstevel@tonic-gate  * status
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #define	_LARGEFILE64_SOURCE 1
390Sstevel@tonic-gate 
40*6812Sraf #pragma weak _fdopen = fdopen
410Sstevel@tonic-gate 
42*6812Sraf #include "lint.h"
430Sstevel@tonic-gate #include <mtlib.h>
440Sstevel@tonic-gate #include "file64.h"
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate #include <stdio.h>
480Sstevel@tonic-gate #include <limits.h>
490Sstevel@tonic-gate #include <thread.h>
500Sstevel@tonic-gate #include <synch.h>
510Sstevel@tonic-gate #include "stdiom.h"
520Sstevel@tonic-gate #include <errno.h>
530Sstevel@tonic-gate #include <fcntl.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate FILE *
fdopen(int fd,const char * type)560Sstevel@tonic-gate fdopen(int fd, const char *type) /* associate file desc. with stream */
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	/* iop doesn't need locking since this function is creating it */
590Sstevel@tonic-gate 	FILE *iop;
600Sstevel@tonic-gate 	char plus;
610Sstevel@tonic-gate 	unsigned char flag;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	/* Sets EBADF for bad fds */
640Sstevel@tonic-gate 	if (fcntl(fd, F_GETFD) == -1)
650Sstevel@tonic-gate 		return (NULL);
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	if ((iop = _findiop()) == 0) {
680Sstevel@tonic-gate 		errno = ENOMEM;
690Sstevel@tonic-gate 		return (NULL);
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	switch (type[0]) {
730Sstevel@tonic-gate 	default:
740Sstevel@tonic-gate 		iop->_flag = 0; /* release iop */
750Sstevel@tonic-gate 		errno = EINVAL;
760Sstevel@tonic-gate 		return (NULL);
770Sstevel@tonic-gate 	case 'r':
780Sstevel@tonic-gate 		flag = _IOREAD;
790Sstevel@tonic-gate 		break;
800Sstevel@tonic-gate 	case 'a':
810Sstevel@tonic-gate 		(void) lseek64(fd, (off64_t)0, SEEK_END);
820Sstevel@tonic-gate 		/*FALLTHROUGH*/
830Sstevel@tonic-gate 	case 'w':
840Sstevel@tonic-gate 		flag = _IOWRT;
850Sstevel@tonic-gate 		break;
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 	if ((plus = type[1]) == 'b')	/* Unix ignores 'b' ANSI std */
880Sstevel@tonic-gate 		plus = type[2];
890Sstevel@tonic-gate 	if (plus == '+')
900Sstevel@tonic-gate 		flag = _IORW;
910Sstevel@tonic-gate 	iop->_flag = flag;
920Sstevel@tonic-gate 
931846Scraigm #ifdef	_LP64
941846Scraigm 	iop->_file = fd;
951846Scraigm #else
961846Scraigm 	if (fd <= _FILE_FD_MAX) {
971846Scraigm 		SET_FILE(iop, fd);
981846Scraigm 	} else if (_file_set(iop, fd, type) != 0) {
991846Scraigm 		/* errno set by _file_set () */
1001846Scraigm 		iop->_flag = 0;		/* release iop */
1011846Scraigm 		return (NULL);
1021846Scraigm 	}
1031846Scraigm #endif	/*	_LP64	*/
1041846Scraigm 
1050Sstevel@tonic-gate 	return (iop);
1060Sstevel@tonic-gate }
107