summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/stdio/fopen.c
diff options
context:
space:
mode:
authorRobert Mustacchi <rm@fingolfin.org>2020-03-02 05:43:45 +0000
committerRobert Mustacchi <rm@fingolfin.org>2020-03-26 07:42:53 +0000
commitcd62a92d4a964bfe61d35ba2301b69e65e22a509 (patch)
tree8e346d9037f7c654ffe58ed0d5e27f34025dd672 /usr/src/lib/libc/port/stdio/fopen.c
parent1470234269f4edea4cbf270cb2475e4988b788d5 (diff)
downloadillumos-gate-cd62a92d4a964bfe61d35ba2301b69e65e22a509.tar.gz
7092 Want support for stdio memory streams
12360 fwrite can loop forever on zero byte write 12392 ftello64 doesn't handle ungetc() correctly when unbuffered Reviewed by: John Levon <john.levon@joyent.com> Reviewed by: Yuri Pankov <ypankov@fastmail.com> Approved by: Dan McDonald <danmcd@joyent.com>
Diffstat (limited to 'usr/src/lib/libc/port/stdio/fopen.c')
-rw-r--r--usr/src/lib/libc/port/stdio/fopen.c19
1 files changed, 16 insertions, 3 deletions
diff --git a/usr/src/lib/libc/port/stdio/fopen.c b/usr/src/lib/libc/port/stdio/fopen.c
index eea8093aca..728a0f4978 100644
--- a/usr/src/lib/libc/port/stdio/fopen.c
+++ b/usr/src/lib/libc/port/stdio/fopen.c
@@ -25,7 +25,7 @@
*/
/* Copyright (c) 1988 AT&T */
-/* All Rights Reserved */
+/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
@@ -37,7 +37,9 @@
* contributors.
*/
-#pragma ident "%Z%%M% %I% %E% SMI"
+/*
+ * Copyright 2020 Robert Mustacchi
+ */
#include "lint.h"
#include "file64.h"
@@ -88,6 +90,17 @@ _freopen_null(const char *type, FILE *iop)
return (NULL);
}
+ /*
+ * If this is not a file-based stream (as in we have no file
+ * descriptor), then we need to close this, but still actually return an
+ * error.
+ */
+ if (_get_fd(iop) == -1) {
+ (void) close_fd(iop);
+ errno = EBADF;
+ return (NULL);
+ }
+
if (!(iop->_flag & _IONBF) && (iop->_flag & (_IOWRT | _IOREAD | _IORW)))
(void) _fflush_u(iop);
@@ -140,7 +153,7 @@ _freopen_null(const char *type, FILE *iop)
}
#ifdef _LP64
- iop->_flag &= ~0377; /* clear lower 8-bits */
+ iop->_flag &= ~_DEF_FLAG_MASK; /* clear lower 8-bits */
if (mode == 'r') {
iop->_flag |= _IOREAD;
nflag = oflag & ~O_APPEND;