xref: /titanic_51/usr/src/lib/libbc/libc/stdio/common/fdopen.c (revision 1e49577a7fcde812700ded04431b49d67cc57d6d)
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 1989 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*      Copyright (c) 1984 AT&T */
28 /*        All Rights Reserved   */
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"  /* from S5R2 1.4 */
31 
32 /*LINTLIBRARY*/
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 #include <stdio.h>
40 #include <sys/errno.h>
41 
42 extern int  errno;
43 extern long lseek();
44 extern FILE *_findiop();
45 
46 FILE *
47 fdopen(fd, mode)
48 int	fd;
49 register char *mode;
50 {
51 	static int nofile = -1;
52 	register FILE *iop;
53 
54 	if(nofile < 0)
55 		nofile = getdtablesize();
56 
57 	if(fd < 0 || fd >= nofile) {
58 		errno = EINVAL;
59 		return(NULL);
60 	}
61 
62 	if((iop = _findiop()) == NULL)
63 		return(NULL);
64 
65 	iop->_cnt = 0;
66 	iop->_file = fd;
67 	iop->_base = iop->_ptr = NULL;
68 	iop->_bufsiz = 0;
69 	switch(*mode) {
70 
71 		case 'r':
72 			iop->_flag = _IOREAD;
73 			break;
74 		case 'a':
75 			(void) lseek(fd, 0L, 2);
76 			/* No break */
77 		case 'w':
78 			iop->_flag = _IOWRT;
79 			break;
80 		default:
81 			errno = EINVAL;
82 			return(NULL);
83 	}
84 
85 	if(mode[1] == '+')
86 		iop->_flag = _IORW;
87 
88 	return(iop);
89 }
90