X-Git-Url: https://git.saurik.com/apple/libc.git/blobdiff_plain/9385eb3d10ebe5eb398c52040ec3dbfba9b0cdcf..70ad1dc8a19d6edd9b97aa81f32cfd65758ae97d:/stdio/FreeBSD/fdopen.c diff --git a/stdio/FreeBSD/fdopen.c b/stdio/FreeBSD/fdopen.c index 15e7e8f..c49e36e 100644 --- a/stdio/FreeBSD/fdopen.c +++ b/stdio/FreeBSD/fdopen.c @@ -13,10 +13,6 @@ * 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. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. * 4. 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. @@ -34,11 +30,20 @@ * SUCH DAMAGE. */ +#ifdef VARIANT_DARWINEXTSN +#define _DARWIN_UNLIMITED_STREAMS +#define COUNT 0 +#elif defined(VARIANT_LEGACY) +#define COUNT 0 +#else +#define COUNT 1 +#endif + #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)fdopen.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include -__FBSDID("$FreeBSD: src/lib/libc/stdio/fdopen.c,v 1.7 2002/03/22 21:53:04 obrien Exp $"); +__FBSDID("$FreeBSD: src/lib/libc/stdio/fdopen.c,v 1.11 2008/05/10 18:39:20 antoine Exp $"); #include "namespace.h" #include @@ -46,6 +51,7 @@ __FBSDID("$FreeBSD: src/lib/libc/stdio/fdopen.c,v 1.7 2002/03/22 21:53:04 obrien #include #include #include +#include #include "un-namespace.h" #include "local.h" @@ -55,11 +61,19 @@ fdopen(fd, mode) const char *mode; { FILE *fp; - static int nofile; int flags, oflags, fdflags, tmp; - if (nofile == 0) - nofile = getdtablesize(); + /* + * File descriptors are a full int, but _file is only a short. + * If we get a valid file descriptor that is greater than + * SHRT_MAX, then the fd will get sign-extended into an + * invalid file descriptor. Handle this case by failing the + * open. + */ + if (fd > SHRT_MAX) { + errno = EMFILE; + return (NULL); + } if ((flags = __sflags(mode, &oflags)) == 0) return (NULL); @@ -73,7 +87,7 @@ fdopen(fd, mode) return (NULL); } - if ((fp = __sfp()) == NULL) + if ((fp = __sfp(COUNT)) == NULL) return (NULL); fp->_flags = flags; /*