xref: /original-bsd/lib/libc/stdio/fdopen.c (revision 56abee86)
1 /* @(#)fdopen.c	4.2 (Berkeley) 03/09/81 */
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|_IORW); 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 	if (mode[1] == '+') {
31 		iop->_flag &= ~(_IOREAD|_IOWRT);
32 		iop->_flag |= _IORW;
33 	}
34 	return(iop);
35 }
36