xref: /netbsd-src/lib/libc/stdio/fdopen.c (revision 9b7a6414b8d42d3c40b64c2435a515f0a8898ae6)
1*9b7a6414Schristos /*	$NetBSD: fdopen.c,v 1.18 2017/11/09 20:30:02 christos Exp $	*/
2255db7b2Sjtc 
361f28255Scgd /*-
4255db7b2Sjtc  * Copyright (c) 1990, 1993
5255db7b2Sjtc  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Chris Torek.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
18eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
3523312f88Schristos #include <sys/cdefs.h>
3661f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
37255db7b2Sjtc #if 0
38255db7b2Sjtc static char sccsid[] = "@(#)fdopen.c	8.1 (Berkeley) 6/4/93";
3923312f88Schristos #else
40*9b7a6414Schristos __RCSID("$NetBSD: fdopen.c,v 1.18 2017/11/09 20:30:02 christos Exp $");
41255db7b2Sjtc #endif
4261f28255Scgd #endif /* LIBC_SCCS and not lint */
4361f28255Scgd 
448b3da7b7Skleink #include "namespace.h"
4593438ac9Schristos 
4661f28255Scgd #include <sys/types.h>
4793438ac9Schristos #include <sys/stat.h>
48b48252f3Slukem 
49b48252f3Slukem #include <assert.h>
50b48252f3Slukem #include <errno.h>
5161f28255Scgd #include <fcntl.h>
5261f28255Scgd #include <unistd.h>
5361f28255Scgd #include <stdio.h>
54749de7f2Schristos #include <limits.h>
55b48252f3Slukem 
563fdac2b8Sthorpej #include "reentrant.h"
5761f28255Scgd #include "local.h"
5861f28255Scgd 
598b3da7b7Skleink #ifdef __weak_alias
__weak_alias(fdopen,_fdopen)6060549036Smycroft __weak_alias(fdopen,_fdopen)
618b3da7b7Skleink #endif
628b3da7b7Skleink 
6361f28255Scgd FILE *
64526d9427Schristos fdopen(int fd, const char *mode)
6561f28255Scgd {
66c8bafd62Sperry 	FILE *fp;
6761f28255Scgd 	int flags, oflags, fdflags, tmp;
6861f28255Scgd 
69b48252f3Slukem 	_DIAGASSERT(fd != -1);
70b48252f3Slukem 
71749de7f2Schristos 	/*
72749de7f2Schristos 	 * File descriptors are a full int, but _file is only a short.
73749de7f2Schristos 	 * If we get a valid file descriptor that is greater or equal to
74749de7f2Schristos 	 * USHRT_MAX, then the fd will get sign-extended into an
75749de7f2Schristos 	 * invalid file descriptor.  Handle this case by failing the
76749de7f2Schristos 	 * open. (We treat the short as unsigned, and special-case -1).
77749de7f2Schristos 	 */
78749de7f2Schristos 	if (fd >= USHRT_MAX) {
79749de7f2Schristos 		errno = EMFILE;
80749de7f2Schristos 		return NULL;
81749de7f2Schristos 	}
82749de7f2Schristos 
8361f28255Scgd 	if ((flags = __sflags(mode, &oflags)) == 0)
84526d9427Schristos 		return NULL;
8561f28255Scgd 
8661f28255Scgd 	/* Make sure the mode the user wants is a subset of the actual mode. */
8761f28255Scgd 	if ((fdflags = fcntl(fd, F_GETFL, 0)) < 0)
88526d9427Schristos 		return NULL;
8961f28255Scgd 	tmp = fdflags & O_ACCMODE;
9061f28255Scgd 	if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE))) {
9161f28255Scgd 		errno = EINVAL;
92526d9427Schristos 		return NULL;
9361f28255Scgd 	}
9461f28255Scgd 
95*9b7a6414Schristos 	if (oflags & O_REGULAR) {
96ebb980fcSchristos 		struct stat st;
97ebb980fcSchristos 		if (fstat(fd, &st) == -1) {
98526d9427Schristos 			return NULL;
99ebb980fcSchristos 		}
100ebb980fcSchristos 		if (!S_ISREG(st.st_mode)) {
101ebb980fcSchristos 			errno = EFTYPE;
102526d9427Schristos 			return NULL;
103ebb980fcSchristos 		}
104ebb980fcSchristos 	}
105ebb980fcSchristos 
10661f28255Scgd 	if ((fp = __sfp()) == NULL)
107526d9427Schristos 		return NULL;
10861f28255Scgd 	fp->_flags = flags;
10961f28255Scgd 	/*
11061f28255Scgd 	 * If opened for appending, but underlying descriptor does not have
11161f28255Scgd 	 * O_APPEND bit set, assert __SAPP so that __swrite() will lseek to
11261f28255Scgd 	 * end before each write.
11361f28255Scgd 	 */
11461f28255Scgd 	if ((oflags & O_APPEND) && !(fdflags & O_APPEND))
11561f28255Scgd 		fp->_flags |= __SAPP;
11661f28255Scgd 	fp->_file = fd;
11761f28255Scgd 	fp->_cookie = fp;
11861f28255Scgd 	fp->_read = __sread;
11961f28255Scgd 	fp->_write = __swrite;
12061f28255Scgd 	fp->_seek = __sseek;
12161f28255Scgd 	fp->_close = __sclose;
122526d9427Schristos 	return fp;
12361f28255Scgd }
124