xref: /csrg-svn/lib/libc/stdio/fdopen.c (revision 13495)
1 /* @(#)fdopen.c	4.3 (Berkeley) 06/30/83 */
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 	if ((unsigned)fd >= getdtablesize())
20 		return (NULL);
21 	for (iop = _iob; iop->_flag&(_IOREAD|_IOWRT|_IORW); iop++)
22 		if (iop >= _lastbuf)
23 			return(NULL);
24 	iop->_cnt = 0;
25 	iop->_file = fd;
26 	if (*mode != 'r') {
27 		iop->_flag |= _IOWRT;
28 		if (*mode == 'a')
29 			lseek(fd, 0L, 2);
30 	} else
31 		iop->_flag |= _IOREAD;
32 	if (mode[1] == '+') {
33 		iop->_flag &= ~(_IOREAD|_IOWRT);
34 		iop->_flag |= _IORW;
35 	}
36 	return(iop);
37 }
38