Merge "Switch to OpenBSD fopen/fclose."

This commit is contained in:
Elliott Hughes 2014-09-24 20:24:47 +00:00 committed by Gerrit Code Review
commit 567d3fcb7e
4 changed files with 15 additions and 59 deletions

View File

@ -242,8 +242,6 @@ libc_upstream_freebsd_src_files := \
upstream-freebsd/lib/libc/gen/ldexp.c \
upstream-freebsd/lib/libc/gen/sleep.c \
upstream-freebsd/lib/libc/gen/usleep.c \
upstream-freebsd/lib/libc/stdio/fclose.c \
upstream-freebsd/lib/libc/stdio/fopen.c \
upstream-freebsd/lib/libc/stdlib/abs.c \
upstream-freebsd/lib/libc/stdlib/getopt_long.c \
upstream-freebsd/lib/libc/stdlib/imaxabs.c \
@ -388,6 +386,7 @@ libc_upstream_openbsd_src_files := \
upstream-openbsd/lib/libc/stdio/asprintf.c \
upstream-openbsd/lib/libc/stdio/clrerr.c \
upstream-openbsd/lib/libc/stdio/dprintf.c \
upstream-openbsd/lib/libc/stdio/fclose.c \
upstream-openbsd/lib/libc/stdio/fdopen.c \
upstream-openbsd/lib/libc/stdio/feof.c \
upstream-openbsd/lib/libc/stdio/ferror.c \
@ -402,6 +401,7 @@ libc_upstream_openbsd_src_files := \
upstream-openbsd/lib/libc/stdio/findfp.c \
upstream-openbsd/lib/libc/stdio/flags.c \
upstream-openbsd/lib/libc/stdio/fmemopen.c \
upstream-openbsd/lib/libc/stdio/fopen.c \
upstream-openbsd/lib/libc/stdio/fprintf.c \
upstream-openbsd/lib/libc/stdio/fpurge.c \
upstream-openbsd/lib/libc/stdio/fputc.c \

View File

@ -38,17 +38,6 @@
#define __usleep usleep
/* Redirect internal C library calls to the public function. */
#define _close close
#define _fcntl fcntl
#define _fstat fstat
#define _nanosleep nanosleep
#define _open open
/* This one is only needed as long as we have a mix of OpenBSD and FreeBSD stdio. */
#define _sseek __sseek
/* This is in BSD's <stdlib.h>. */
#include <stdint.h>
extern uint32_t arc4random_uniform(uint32_t upper_bound);
#endif

View File

@ -1,3 +1,4 @@
/* $OpenBSD: fclose.c,v 1.9 2009/11/09 00:18:27 kurt Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@ -30,19 +31,9 @@
* SUCH DAMAGE.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)fclose.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include "un-namespace.h"
#include <spinlock.h>
#include "libc_private.h"
#include "local.h"
int
@ -55,6 +46,7 @@ fclose(FILE *fp)
return (EOF);
}
FLOCKFILE(fp);
WCIO_FREE(fp);
r = fp->_flags & __SWR ? __sflush(fp) : 0;
if (fp->_close != NULL && (*fp->_close)(fp->_cookie) < 0)
r = EOF;
@ -64,22 +56,8 @@ fclose(FILE *fp)
FREEUB(fp);
if (HASLB(fp))
FREELB(fp);
fp->_file = -1;
fp->_r = fp->_w = 0; /* Mess up if reaccessed. */
/*
* Lock the spinlock used to protect __sglue list walk in
* __sfp(). The __sfp() uses fp->_flags == 0 test as an
* indication of the unused FILE.
*
* Taking the lock prevents possible compiler or processor
* reordering of the writes performed before the final _flags
* cleanup, making sure that we are done with the FILE before
* it is considered available.
*/
STDIO_THREAD_LOCK();
fp->_flags = 0; /* Release this FILE for reuse. */
STDIO_THREAD_UNLOCK();
FUNLOCKFILE(fp);
return (r);
}

View File

@ -1,3 +1,4 @@
/* $OpenBSD: fopen.c,v 1.7 2008/05/03 18:46:41 chl Exp $ */
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
@ -30,26 +31,17 @@
* SUCH DAMAGE.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)fopen.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "namespace.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <stdio.h>
#include <errno.h>
#include <limits.h>
#include "un-namespace.h"
#include <unistd.h>
#include "local.h"
FILE *
fopen(const char * __restrict file, const char * __restrict mode)
fopen(const char *file, const char *mode)
{
FILE *fp;
int f;
@ -59,23 +51,19 @@ fopen(const char * __restrict file, const char * __restrict mode)
return (NULL);
if ((fp = __sfp()) == NULL)
return (NULL);
if ((f = _open(file, oflags, DEFFILEMODE)) < 0) {
if ((f = open(file, oflags, DEFFILEMODE)) < 0) {
fp->_flags = 0; /* release */
return (NULL);
}
/*
* 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.
*/
/* _file is only a short */
if (f > SHRT_MAX) {
fp->_flags = 0; /* release */
_close(f);
close(f);
errno = EMFILE;
return (NULL);
}
fp->_file = f;
fp->_flags = flags;
fp->_cookie = fp;
@ -83,6 +71,7 @@ fopen(const char * __restrict file, const char * __restrict mode)
fp->_write = __swrite;
fp->_seek = __sseek;
fp->_close = __sclose;
/*
* When opening in append mode, even though we use O_APPEND,
* we need to seek to the end so that ftell() gets the right
@ -92,6 +81,6 @@ fopen(const char * __restrict file, const char * __restrict mode)
* fseek and ftell.)
*/
if (oflags & O_APPEND)
(void)_sseek(fp, (fpos_t)0, SEEK_END);
(void) __sseek((void *)fp, (fpos_t)0, SEEK_END);
return (fp);
}