summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/stdio/setbuf.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/setbuf.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/setbuf.c')
-rw-r--r--usr/src/lib/libc/port/stdio/setbuf.c28
1 files changed, 13 insertions, 15 deletions
diff --git a/usr/src/lib/libc/port/stdio/setbuf.c b/usr/src/lib/libc/port/stdio/setbuf.c
index 78c4c8cc76..10eb21d5b5 100644
--- a/usr/src/lib/libc/port/stdio/setbuf.c
+++ b/usr/src/lib/libc/port/stdio/setbuf.c
@@ -25,9 +25,11 @@
*/
/* Copyright (c) 1988 AT&T */
-/* All Rights Reserved */
+/* All Rights Reserved */
-#pragma ident "%Z%%M% %I% %E% SMI"
+/*
+ * Copyright 2020 Robert Mustacchi
+ */
#include "lint.h"
#include "file64.h"
@@ -39,41 +41,37 @@
#include <synch.h>
#include "stdiom.h"
-
void
setbuf(FILE *iop, char *abuf)
{
Uchar *buf = (Uchar *)abuf;
- int fno = GET_FD(iop); /* file number */
+ int fno = _get_fd(iop); /* file number */
int size = BUFSIZ - _SMBFSZ;
Uchar *temp;
rmutex_t *lk;
FLOCKFILE(lk, iop);
- if ((iop->_base != 0) && (iop->_flag & _IOMYBUF))
+ if ((iop->_base != NULL) && (iop->_flag & _IOMYBUF))
free((char *)iop->_base - PUSHBACK);
iop->_flag &= ~(_IOMYBUF | _IONBF | _IOLBF);
- if (buf == 0) {
+ if (buf == NULL) {
iop->_flag |= _IONBF;
-#ifndef _STDIO_ALLOCATE
- if (fno < 2) {
+ if (fno == 0 || fno == 1) {
/* use special buffer for std{in,out} */
buf = (fno == 0) ? _sibuf : _sobuf;
- } else /* needed for ifndef */
-#endif
- if (fno < _NFILE) {
+ } else if (fno >= 2 && fno < _NFILE) {
buf = _smbuf[fno];
size = _SMBFSZ - PUSHBACK;
- } else
- if ((buf = (Uchar *)malloc(_SMBFSZ * sizeof (Uchar))) != 0) {
+ } else if ((buf = (Uchar *)malloc(_SMBFSZ *
+ sizeof (Uchar))) != NULL) {
iop->_flag |= _IOMYBUF;
size = _SMBFSZ - PUSHBACK;
}
} else { /* regular buffered I/O, standard buffer size */
- if (isatty(fno))
+ if (fno != -1 && isatty(fno))
iop->_flag |= _IOLBF;
}
- if (buf == 0) {
+ if (buf == NULL) {
FUNLOCKFILE(lk);
return; /* malloc() failed */
}