xref: /csrg-svn/lib/libc/stdio/fdopen.c (revision 13495)
1*13495Ssam /* @(#)fdopen.c	4.3 (Berkeley) 06/30/83 */
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*13495Ssam 	if ((unsigned)fd >= getdtablesize())
20*13495Ssam 		return (NULL);
213163Stoy 	for (iop = _iob; iop->_flag&(_IOREAD|_IOWRT|_IORW); iop++)
221999Swnj 		if (iop >= _lastbuf)
231999Swnj 			return(NULL);
241999Swnj 	iop->_cnt = 0;
251999Swnj 	iop->_file = fd;
261999Swnj 	if (*mode != 'r') {
271999Swnj 		iop->_flag |= _IOWRT;
281999Swnj 		if (*mode == 'a')
291999Swnj 			lseek(fd, 0L, 2);
301999Swnj 	} else
311999Swnj 		iop->_flag |= _IOREAD;
323163Stoy 	if (mode[1] == '+') {
333163Stoy 		iop->_flag &= ~(_IOREAD|_IOWRT);
343163Stoy 		iop->_flag |= _IORW;
353163Stoy 	}
361999Swnj 	return(iop);
371999Swnj }
38