xref: /titanic_44/usr/src/lib/libc/port/stdio/fdopen.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 /*
34  * Unix routine to do an "fopen" on file descriptor
35  * The mode has to be repeated because you can't query its
36  * status
37  */
38 
39 #define	_LARGEFILE64_SOURCE 1
40 
41 #pragma weak fdopen = _fdopen
42 
43 #include <sys/feature_tests.h>
44 
45 #define	fdopen		_fdopen
46 #if !defined(_LP64)
47 #define	lseek64		_lseek64
48 #endif
49 
50 #include "lint.h"
51 #include <mtlib.h>
52 #include "file64.h"
53 #include <sys/types.h>
54 #include <unistd.h>
55 #include <stdio.h>
56 #include <limits.h>
57 #include <thread.h>
58 #include <synch.h>
59 #include "stdiom.h"
60 #include <errno.h>
61 #include <fcntl.h>
62 
63 FILE *
64 fdopen(int fd, const char *type) /* associate file desc. with stream */
65 {
66 	/* iop doesn't need locking since this function is creating it */
67 	FILE *iop;
68 	char plus;
69 	unsigned char flag;
70 
71 
72 	/* Sets EBADF for bad fds */
73 	if (fcntl(fd, F_GETFD) == -1)
74 		return (NULL);
75 
76 #ifdef	_LP64
77 	if ((iop = _findiop()) == 0) {
78 		errno = ENOMEM;
79 		return (NULL);
80 	}
81 	iop->_file = fd;
82 #else
83 	if (fd > UCHAR_MAX) {
84 		errno = EMFILE;
85 		return (NULL);
86 	}
87 	if ((iop = _findiop()) == 0) {
88 		errno = ENOMEM;
89 		return (NULL);
90 	}
91 	iop->_file = (unsigned char)fd;
92 #endif	/*	_LP64	*/
93 
94 	switch (type[0]) {
95 	default:
96 		iop->_flag = 0; /* release iop */
97 		errno = EINVAL;
98 		return (NULL);
99 	case 'r':
100 		flag = _IOREAD;
101 		break;
102 	case 'a':
103 		(void) lseek64(fd, (off64_t)0, SEEK_END);
104 		/*FALLTHROUGH*/
105 	case 'w':
106 		flag = _IOWRT;
107 		break;
108 	}
109 	if ((plus = type[1]) == 'b')	/* Unix ignores 'b' ANSI std */
110 		plus = type[2];
111 	if (plus == '+')
112 		flag = _IORW;
113 	iop->_flag = flag;
114 
115 	return (iop);
116 }
117