xref: /csrg-svn/lib/libc/stdio/fdopen.c (revision 1999)
1 /* @(#)fdopen.c	4.1 (Berkeley) 12/21/80 */
2 /*
3  * Unix routine to do an "fopen" on file descriptor
4  * The mode has to be repeated because you can't query its
5  * status
6  */
7 
8 #include	<stdio.h>
9 #include	<errno.h>
10 
11 FILE *
12 fdopen(fd, mode)
13 register char *mode;
14 {
15 	extern int errno;
16 	register FILE *iop;
17 	extern FILE *_lastbuf;
18 
19 	for (iop = _iob; iop->_flag&(_IOREAD|_IOWRT); iop++)
20 		if (iop >= _lastbuf)
21 			return(NULL);
22 	iop->_cnt = 0;
23 	iop->_file = fd;
24 	if (*mode != 'r') {
25 		iop->_flag |= _IOWRT;
26 		if (*mode == 'a')
27 			lseek(fd, 0L, 2);
28 	} else
29 		iop->_flag |= _IOREAD;
30 	return(iop);
31 }
32