/* $NetBSD: fdopen.c,v 1.15 2008/03/13 15:40:00 christos Exp $ */ /*- * Copyright (c) 1990, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Chris Torek. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #if defined(LIBC_SCCS) && !defined(lint) #if 0 static char sccsid[] = "@(#)fdopen.c 8.1 (Berkeley) 6/4/93"; #else __RCSID("$NetBSD: fdopen.c,v 1.15 2008/03/13 15:40:00 christos Exp $"); #endif #endif /* LIBC_SCCS and not lint */ #include "namespace.h" #include #include #include #include #include #include #include #include "reentrant.h" #include "local.h" #ifdef __weak_alias __weak_alias(fdopen,_fdopen) #endif FILE * fdopen(fd, mode) int fd; const char *mode; { FILE *fp; int flags, oflags, fdflags, tmp; _DIAGASSERT(fd != -1); /* * File descriptors are a full int, but _file is only a short. * If we get a valid file descriptor that is greater or equal to * USHRT_MAX, then the fd will get sign-extended into an * invalid file descriptor. Handle this case by failing the * open. (We treat the short as unsigned, and special-case -1). */ if (fd >= USHRT_MAX) { errno = EMFILE; return NULL; } if ((flags = __sflags(mode, &oflags)) == 0) return (NULL); /* Make sure the mode the user wants is a subset of the actual mode. */ if ((fdflags = fcntl(fd, F_GETFL, 0)) < 0) return (NULL); tmp = fdflags & O_ACCMODE; if (tmp != O_RDWR && (tmp != (oflags & O_ACCMODE))) { errno = EINVAL; return (NULL); } if (oflags & O_NONBLOCK) { struct stat st; if (fstat(fd, &st) == -1) { return (NULL); } if (!S_ISREG(st.st_mode)) { errno = EFTYPE; return (NULL); } } if ((fp = __sfp()) == NULL) return (NULL); fp->_flags = flags; /* * If opened for appending, but underlying descriptor does not have * O_APPEND bit set, assert __SAPP so that __swrite() will lseek to * end before each write. */ if ((oflags & O_APPEND) && !(fdflags & O_APPEND)) fp->_flags |= __SAPP; fp->_file = fd; fp->_cookie = fp; fp->_read = __sread; fp->_write = __swrite; fp->_seek = __sseek; fp->_close = __sclose; return (fp); } 8f127096765a73ce06d19b77c74ddd3'>a round of autoconf cleanup:drochner 2003-10-26Fix unitialized variable use.fvdl 2003-07-14add missing __KERNEL_RCSID()lukem 2003-02-02guarantee, not guarentee. Idea from miod@openbsd.wiz 2003-02-01make compileichiro 2002-06-25tabifyitojun 2002-05-23Fix bug in the way the config_found loop runs. Passing NULL doesn't work well.jmc 2002-04-02Add another exception condition (XXX...I'm gonna have to give in and justjmc 2002-02-27SBP is a firewire/ieee1394 specific protocol, not a generic standard. Move thisjmc 2002-02-27Initial checkin.jmc 2002-02-27Add a few more extraction macros and begin the ALLOW list of flags forjmc 2001-06-29Add some additional values to the keyvalue list from draft specs as some roms...jmc 2001-04-23Add comment set spec id to macro typesjmc 2001-04-23Add Model id as a valid generic typejmc 2001-04-16Add macro for extracting the whole key as one byte value.jmc 2001-04-15Correct some typos. Add 2 more macro's to extract data from directory entries...jmc 2001-04-11Only install headers which are actually used by our userland. Thisjdolecek 2001-01-18No-op commit to force update to a non-"-kk" revision.tv 2000-11-08Initial import of some of the definitions for the Serial Bus Protocol V2.matt 2000-06-15Add preliminary ieee1212reg.h (csr architecture which is the base spec formatt