xref: /csrg-svn/lib/libc/stdio/fdopen.c (revision 3163)
1*3163Stoy /* @(#)fdopen.c	4.2 (Berkeley) 03/09/81 */
21999Swnj /*
31999Swnj  * Unix routine to do an "fopen" on file descriptor
41999Swnj  * The mode has to be repeated because you can't query its
51999Swnj  * status
61999Swnj  */
71999Swnj 
81999Swnj #include	<stdio.h>
91999Swnj #include	<errno.h>
101999Swnj 
111999Swnj FILE *
121999Swnj fdopen(fd, mode)
131999Swnj register char *mode;
141999Swnj {
151999Swnj 	extern int errno;
161999Swnj 	register FILE *iop;
171999Swnj 	extern FILE *_lastbuf;
181999Swnj 
19*3163Stoy 	for (iop = _iob; iop->_flag&(_IOREAD|_IOWRT|_IORW); iop++)
201999Swnj 		if (iop >= _lastbuf)
211999Swnj 			return(NULL);
221999Swnj 	iop->_cnt = 0;
231999Swnj 	iop->_file = fd;
241999Swnj 	if (*mode != 'r') {
251999Swnj 		iop->_flag |= _IOWRT;
261999Swnj 		if (*mode == 'a')
271999Swnj 			lseek(fd, 0L, 2);
281999Swnj 	} else
291999Swnj 		iop->_flag |= _IOREAD;
30*3163Stoy 	if (mode[1] == '+') {
31*3163Stoy 		iop->_flag &= ~(_IOREAD|_IOWRT);
32*3163Stoy 		iop->_flag |= _IORW;
33*3163Stoy 	}
341999Swnj 	return(iop);
351999Swnj }
36