17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
5a5f6978craigm * Common Development and Distribution License (the "License").
6a5f6978craigm * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
21e8031f0raf
227c478bdstevel@tonic-gate/*
237257d1braf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1988 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
30a5f6978craigm#pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bdstevel@tonic-gate
327c478bdstevel@tonic-gate/*
337c478bdstevel@tonic-gate * Unix routine to do an "fopen" on file descriptor
347c478bdstevel@tonic-gate * The mode has to be repeated because you can't query its
357c478bdstevel@tonic-gate * status
367c478bdstevel@tonic-gate */
377c478bdstevel@tonic-gate
387c478bdstevel@tonic-gate#define	_LARGEFILE64_SOURCE 1
397c478bdstevel@tonic-gate
407257d1braf#pragma weak _fdopen = fdopen
417c478bdstevel@tonic-gate
427257d1braf#include "lint.h"
437c478bdstevel@tonic-gate#include <mtlib.h>
447c478bdstevel@tonic-gate#include "file64.h"
457c478bdstevel@tonic-gate#include <sys/types.h>
467c478bdstevel@tonic-gate#include <unistd.h>
477c478bdstevel@tonic-gate#include <stdio.h>
487c478bdstevel@tonic-gate#include <limits.h>
497c478bdstevel@tonic-gate#include <thread.h>
507c478bdstevel@tonic-gate#include <synch.h>
517c478bdstevel@tonic-gate#include "stdiom.h"
527c478bdstevel@tonic-gate#include <errno.h>
537c478bdstevel@tonic-gate#include <fcntl.h>
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gateFILE *
567c478bdstevel@tonic-gatefdopen(int fd, const char *type) /* associate file desc. with stream */
577c478bdstevel@tonic-gate{
587c478bdstevel@tonic-gate	/* iop doesn't need locking since this function is creating it */
597c478bdstevel@tonic-gate	FILE *iop;
607c478bdstevel@tonic-gate	char plus;
617c478bdstevel@tonic-gate	unsigned char flag;
627c478bdstevel@tonic-gate
637c478bdstevel@tonic-gate	/* Sets EBADF for bad fds */
647c478bdstevel@tonic-gate	if (fcntl(fd, F_GETFD) == -1)
657c478bdstevel@tonic-gate		return (NULL);
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate	if ((iop = _findiop()) == 0) {
687c478bdstevel@tonic-gate		errno = ENOMEM;
697c478bdstevel@tonic-gate		return (NULL);
707c478bdstevel@tonic-gate	}
717c478bdstevel@tonic-gate
727c478bdstevel@tonic-gate	switch (type[0]) {
737c478bdstevel@tonic-gate	default:
747c478bdstevel@tonic-gate		iop->_flag = 0; /* release iop */
757c478bdstevel@tonic-gate		errno = EINVAL;
767c478bdstevel@tonic-gate		return (NULL);
777c478bdstevel@tonic-gate	case 'r':
787c478bdstevel@tonic-gate		flag = _IOREAD;
797c478bdstevel@tonic-gate		break;
807c478bdstevel@tonic-gate	case 'a':
817c478bdstevel@tonic-gate		(void) lseek64(fd, (off64_t)0, SEEK_END);
827c478bdstevel@tonic-gate		/*FALLTHROUGH*/
837c478bdstevel@tonic-gate	case 'w':
847c478bdstevel@tonic-gate		flag = _IOWRT;
857c478bdstevel@tonic-gate		break;
867c478bdstevel@tonic-gate	}
877c478bdstevel@tonic-gate	if ((plus = type[1]) == 'b')	/* Unix ignores 'b' ANSI std */
887c478bdstevel@tonic-gate		plus = type[2];
897c478bdstevel@tonic-gate	if (plus == '+')
907c478bdstevel@tonic-gate		flag = _IORW;
917c478bdstevel@tonic-gate	iop->_flag = flag;
927c478bdstevel@tonic-gate
93a5f6978craigm#ifdef	_LP64
94a5f6978craigm	iop->_file = fd;
95a5f6978craigm#else
96a5f6978craigm	if (fd <= _FILE_FD_MAX) {
97a5f6978craigm		SET_FILE(iop, fd);
98a5f6978craigm	} else if (_file_set(iop, fd, type) != 0) {
99a5f6978craigm		/* errno set by _file_set () */
100a5f6978craigm		iop->_flag = 0;		/* release iop */
101a5f6978craigm		return (NULL);
102a5f6978craigm	}
103a5f6978craigm#endif	/*	_LP64	*/
104a5f6978craigm
1057c478bdstevel@tonic-gate	return (iop);
1067c478bdstevel@tonic-gate}
107