xref: /titanic_44/usr/src/lib/libc/port/stdio/fdopen.c (revision 94bc75770001bfdc49b11467deff2235fc9927f9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 /*	Copyright (c) 1988 AT&T	*/
31 /*	  All Rights Reserved  	*/
32 
33 
34 /*
35  * Unix routine to do an "fopen" on file descriptor
36  * The mode has to be repeated because you can't query its
37  * status
38  */
39 
40 #define	_LARGEFILE64_SOURCE 1
41 
42 #pragma weak fdopen = _fdopen
43 
44 #include "synonyms.h"
45 #include <mtlib.h>
46 #include "file64.h"
47 #include <sys/types.h>
48 #include <unistd.h>
49 #include <stdio.h>
50 #include <limits.h>
51 #include <thread.h>
52 #include <synch.h>
53 #include "stdiom.h"
54 #include <errno.h>
55 #include <fcntl.h>
56 
57 FILE *
58 fdopen(int fd, const char *type) /* associate file desc. with stream */
59 {
60 	/* iop doesn't need locking since this function is creating it */
61 	FILE *iop;
62 	char plus;
63 	unsigned char flag;
64 
65 
66 	/* Sets EBADF for bad fds */
67 	if (fcntl(fd, F_GETFD) == -1)
68 		return (NULL);
69 
70 #ifdef	_LP64
71 	if ((iop = _findiop()) == 0) {
72 		errno = ENOMEM;
73 		return (NULL);
74 	}
75 	iop->_file = fd;
76 #else
77 	if (fd > UCHAR_MAX) {
78 		errno = EMFILE;
79 		return (NULL);
80 	}
81 	if ((iop = _findiop()) == 0) {
82 		errno = ENOMEM;
83 		return (NULL);
84 	}
85 	iop->_file = (unsigned char)fd;
86 #endif	/*	_LP64	*/
87 
88 	switch (type[0]) {
89 	default:
90 		iop->_flag = 0; /* release iop */
91 		errno = EINVAL;
92 		return (NULL);
93 	case 'r':
94 		flag = _IOREAD;
95 		break;
96 	case 'a':
97 		(void) lseek64(fd, (off64_t)0, SEEK_END);
98 		/*FALLTHROUGH*/
99 	case 'w':
100 		flag = _IOWRT;
101 		break;
102 	}
103 	if ((plus = type[1]) == 'b')	/* Unix ignores 'b' ANSI std */
104 		plus = type[2];
105 	if (plus == '+')
106 		flag = _IORW;
107 	iop->_flag = flag;
108 
109 	return (iop);
110 }
111