fdopen.c revision 1.17
11.17Schristos/*	$NetBSD: fdopen.c,v 1.17 2017/01/10 17:00:58 christos Exp $	*/
21.5Sjtc
31.1Scgd/*-
41.5Sjtc * Copyright (c) 1990, 1993
51.5Sjtc *	The Regents of the University of California.  All rights reserved.
61.1Scgd *
71.1Scgd * This code is derived from software contributed to Berkeley by
81.1Scgd * Chris Torek.
91.1Scgd *
101.1Scgd * Redistribution and use in source and binary forms, with or without
111.1Scgd * modification, are permitted provided that the following conditions
121.1Scgd * are met:
131.1Scgd * 1. Redistributions of source code must retain the above copyright
141.1Scgd *    notice, this list of conditions and the following disclaimer.
151.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
161.1Scgd *    notice, this list of conditions and the following disclaimer in the
171.1Scgd *    documentation and/or other materials provided with the distribution.
181.14Sagc * 3. Neither the name of the University nor the names of its contributors
191.1Scgd *    may be used to endorse or promote products derived from this software
201.1Scgd *    without specific prior written permission.
211.1Scgd *
221.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321.1Scgd * SUCH DAMAGE.
331.1Scgd */
341.1Scgd
351.6Schristos#include <sys/cdefs.h>
361.1Scgd#if defined(LIBC_SCCS) && !defined(lint)
371.5Sjtc#if 0
381.5Sjtcstatic char sccsid[] = "@(#)fdopen.c	8.1 (Berkeley) 6/4/93";
391.6Schristos#else
401.17Schristos__RCSID("$NetBSD: fdopen.c,v 1.17 2017/01/10 17:00:58 christos Exp $");
411.5Sjtc#endif
421.1Scgd#endif /* LIBC_SCCS and not lint */
431.1Scgd
441.8Skleink#include "namespace.h"
451.17Schristos
461.1Scgd#include <sys/types.h>
471.17Schristos#include <sys/stat.h>
481.9Slukem
491.9Slukem#include <assert.h>
501.9Slukem#include <errno.h>
511.1Scgd#include <fcntl.h>
521.1Scgd#include <unistd.h>
531.1Scgd#include <stdio.h>
541.15Schristos#include <limits.h>
551.9Slukem
561.13Sthorpej#include "reentrant.h"
571.1Scgd#include "local.h"
581.8Skleink
591.8Skleink#ifdef __weak_alias
601.11Smycroft__weak_alias(fdopen,_fdopen)
611.8Skleink#endif
621.1Scgd
631.1ScgdFILE *
641.16Schristosfdopen(int fd, const char *mode)
651.1Scgd{
661.7Sperry	FILE *fp;
671.1Scgd	int flags, oflags, fdflags, tmp;
681.9Slukem
691.9Slukem	_DIAGASSERT(fd != -1);
701.1Scgd
711.15Schristos	/*
721.15Schristos	 * File descriptors are a full int, but _file is only a short.
731.15Schristos	 * If we get a valid file descriptor that is greater or equal to
741.15Schristos	 * USHRT_MAX, then the fd will get sign-extended into an
751.15Schristos	 * invalid file descriptor.  Handle this case by failing the
761.15Schristos	 * open. (We treat the short as unsigned, and special-case -1).
771.15Schristos	 */
781.15Schristos	if (fd >= USHRT_MAX) {
791.15Schristos		errno = EMFILE;
801.15Schristos		return NULL;
811.15Schristos	}
821.15Schristos
831.1Scgd	if ((flags = __sflags(mode, &oflags)) == 0)
841.16Schristos		return NULL;
851.1Scgd
861.1Scgd	/* Make sure the mode the user wants is a subset of the actual mode. */
871.1Scgd	if ((fdflags = fcntl(fd, F_GETFL, 0)) < 0)
881.16Schristos		return NULL;
891.1Scgd	tmp = fdflags & O_ACCMODE;
901.1Scgd	if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE))) {
911.1Scgd		errno = EINVAL;
921.16Schristos		return NULL;
931.12Schristos	}
941.12Schristos
951.12Schristos	if (oflags & O_NONBLOCK) {
961.12Schristos		struct stat st;
971.12Schristos		if (fstat(fd, &st) == -1) {
981.16Schristos			return NULL;
991.12Schristos		}
1001.12Schristos		if (!S_ISREG(st.st_mode)) {
1011.12Schristos			errno = EFTYPE;
1021.16Schristos			return NULL;
1031.12Schristos		}
1041.1Scgd	}
1051.1Scgd
1061.1Scgd	if ((fp = __sfp()) == NULL)
1071.16Schristos		return NULL;
1081.1Scgd	fp->_flags = flags;
1091.1Scgd	/*
1101.1Scgd	 * If opened for appending, but underlying descriptor does not have
1111.1Scgd	 * O_APPEND bit set, assert __SAPP so that __swrite() will lseek to
1121.1Scgd	 * end before each write.
1131.1Scgd	 */
1141.1Scgd	if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
1151.1Scgd		fp->_flags |= __SAPP;
1161.1Scgd	fp->_file = fd;
1171.1Scgd	fp->_cookie = fp;
1181.1Scgd	fp->_read = __sread;
1191.1Scgd	fp->_write = __swrite;
1201.1Scgd	fp->_seek = __sseek;
1211.1Scgd	fp->_close = __sclose;
1221.16Schristos	return fp;
1231.1Scgd}
124