xref: /csrg-svn/lib/libc/stdio/fdopen.c (revision 61178)
146069Sbostic /*-
2*61178Sbostic  * Copyright (c) 1990, 1993
3*61178Sbostic  *	The Regents of the University of California.  All rights reserved.
446069Sbostic  *
546069Sbostic  * This code is derived from software contributed to Berkeley by
646069Sbostic  * Chris Torek.
746069Sbostic  *
846069Sbostic  * %sccs.include.redist.c%
921400Sdist  */
1021400Sdist 
1126642Sdonn #if defined(LIBC_SCCS) && !defined(lint)
12*61178Sbostic static char sccsid[] = "@(#)fdopen.c	8.1 (Berkeley) 06/04/93";
1346069Sbostic #endif /* LIBC_SCCS and not lint */
1421400Sdist 
1517952Sserge #include <sys/types.h>
1646611Sbostic #include <fcntl.h>
1746611Sbostic #include <unistd.h>
1817952Sserge #include <stdio.h>
1946209Sbostic #include <errno.h>
2046069Sbostic #include "local.h"
211999Swnj 
221999Swnj FILE *
fdopen(fd,mode)231999Swnj fdopen(fd, mode)
2446069Sbostic 	int fd;
2546270Storek 	const char *mode;
261999Swnj {
2746209Sbostic 	register FILE *fp;
2846069Sbostic 	static int nofile;
2946209Sbostic 	int flags, oflags, fdflags, tmp;
301999Swnj 
3146069Sbostic 	if (nofile == 0)
3217952Sserge 		nofile = getdtablesize();
3317952Sserge 
3446209Sbostic 	if ((flags = __sflags(mode, &oflags)) == 0)
3513495Ssam 		return (NULL);
3617952Sserge 
3746069Sbostic 	/* Make sure the mode the user wants is a subset of the actual mode. */
3846209Sbostic 	if ((fdflags = fcntl(fd, F_GETFL, 0)) < 0)
3946209Sbostic 		return (NULL);
4046209Sbostic 	tmp = fdflags & O_ACCMODE;
4146209Sbostic 	if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE))) {
4246069Sbostic 		errno = EINVAL;
4317952Sserge 		return (NULL);
4446069Sbostic 	}
4517952Sserge 
4646069Sbostic 	if ((fp = __sfp()) == NULL)
4746069Sbostic 		return (NULL);
4846209Sbostic 	fp->_flags = flags;
4946069Sbostic 	/*
5046069Sbostic 	 * If opened for appending, but underlying descriptor does not have
5146069Sbostic 	 * O_APPEND bit set, assert __SAPP so that __swrite() will lseek to
5246069Sbostic 	 * end before each write.
5346069Sbostic 	 */
5446069Sbostic 	if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
5546069Sbostic 		fp->_flags |= __SAPP;
5646069Sbostic 	fp->_file = fd;
5746069Sbostic 	fp->_cookie = fp;
5846069Sbostic 	fp->_read = __sread;
5946069Sbostic 	fp->_write = __swrite;
6046069Sbostic 	fp->_seek = __sseek;
6146069Sbostic 	fp->_close = __sclose;
6246069Sbostic 	return (fp);
631999Swnj }
64