summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2024-05-28 12:34:30 -0400
committerJakob Kaivo <jkk@ung.org>2024-05-28 12:34:30 -0400
commitdb616c2d5cff8477336b318d889e02112fe97f9a (patch)
tree4827495015891d4492ac50e05c6fe2d430b194b0
parente6ef806ef40a421435b1209f0f79d778c03fe04a (diff)
handle stream orientation behavior
-rw-r--r--src/stdio/_stdio.h6
-rw-r--r--src/stdio/fputs.c1
-rw-r--r--src/stdio/putc_unlocked.c8
-rw-r--r--src/stdio/puts.c1
-rw-r--r--src/wchar/fputwc.c5
-rw-r--r--src/wchar/fputws.c6
-rw-r--r--src/wchar/fwide.c15
-rw-r--r--src/wchar/wcrtomb.c6
8 files changed, 22 insertions, 26 deletions
diff --git a/src/stdio/_stdio.h b/src/stdio/_stdio.h
index f02715c6..6302ee83 100644
--- a/src/stdio/_stdio.h
+++ b/src/stdio/_stdio.h
@@ -43,8 +43,8 @@
if (((__operation) == OP_OUTPUT) && !(__stream)->write) { \
UNDEFINED("In call to %s: Attempted output operation on input stream", __func__); \
} \
- if (((__orientation) && (__stream)->orientation) && ((__orientation) != (__stream)->orientation)) { \
- UNDEFINED("In call to %s(): Requested %s operation on %s oriented stream", __func__, (__orientation) > 0 ? "wide" : "byte", (stream->orientation) > 0 ? "wide" : "byte"); \
+ if ((__orientation) && (__stream)->orientation && ((__orientation) != (__stream)->orientation)) { \
+ UNDEFINED("In call to %s(): Requested %s operation on %s oriented stream", __func__, (__orientation) > 0 ? "wide" : "byte", ((__stream)->orientation) > 0 ? "wide" : "byte"); \
} \
} while (0)
#endif
@@ -68,7 +68,7 @@ struct __FILE {
int nlocks; /* in multithreaded, used by flockfile() */
int thread; /* the owning thread if locked */
- unsigned int orientation:2; /* 0 = undetermind, < 0 = byte, > 0 = wide */
+ int orientation:2; /* 0 = undetermind, < 0 = byte, > 0 = wide */
unsigned int operation:2; /* TODO: previous operation, NONE, INPUT, OUTPUT (are there others?) */
unsigned int eof:1; /* eof indicator */
unsigned int err:1; /* error indicator */
diff --git a/src/stdio/fputs.c b/src/stdio/fputs.c
index c4d32dec..87523b43 100644
--- a/src/stdio/fputs.c
+++ b/src/stdio/fputs.c
@@ -7,6 +7,7 @@
int fputs(const char * restrict s, FILE * restrict stream)
{
SIGNAL_SAFE(0);
+ ASSERT_STREAM(stream, ORIENT_BYTE, OP_OUTPUT);
ASSERT_NOOVERLAP(s, strlen(s), stream, sizeof(*stream));
flockfile(stream);
diff --git a/src/stdio/putc_unlocked.c b/src/stdio/putc_unlocked.c
index 9a7b3a60..f3714af8 100644
--- a/src/stdio/putc_unlocked.c
+++ b/src/stdio/putc_unlocked.c
@@ -23,6 +23,14 @@ int putc_unlocked(int c, FILE *stream)
}
stream->operation = OP_OUTPUT;
+ if (stream->buf == NULL) {
+ if (write(stream->fd, &ch, 1) != 1) {
+ stream->err = 1;
+ return EOF;
+ }
+ return ch;
+ }
+
stream->buf[stream->bpos++] = ch;
if (stream->bpos == stream->bsize ||
(stream->bmode == _IOLBF && ch == '\n') ||
diff --git a/src/stdio/puts.c b/src/stdio/puts.c
index 3392228e..ad9eaa84 100644
--- a/src/stdio/puts.c
+++ b/src/stdio/puts.c
@@ -8,6 +8,7 @@ int puts(const char *s)
int ret = 1;
SIGNAL_SAFE(0);
+ ASSERT_STREAM(stdout, ORIENT_BYTE, OP_OUTPUT);
flockfile(stdout);
diff --git a/src/wchar/fputwc.c b/src/wchar/fputwc.c
index afbefedb..303e6c54 100644
--- a/src/wchar/fputwc.c
+++ b/src/wchar/fputwc.c
@@ -1,5 +1,3 @@
-#if 0
-
#include <wchar.h>
#include <stdio.h>
#include <limits.h>
@@ -41,6 +39,3 @@ wint_t fputwc(wchar_t c, FILE * stream)
/*
STDC(199409)
*/
-
-
-#endif
diff --git a/src/wchar/fputws.c b/src/wchar/fputws.c
index 4da7b13c..ad48fbfe 100644
--- a/src/wchar/fputws.c
+++ b/src/wchar/fputws.c
@@ -1,7 +1,6 @@
-#if 0
-
#include <wchar.h>
#include <stdio.h>
+#include "_safety.h"
int fputws(const wchar_t * restrict s, FILE * restrict stream)
{
@@ -21,6 +20,3 @@ int fputws(const wchar_t * restrict s, FILE * restrict stream)
/*
STDC(199409)
*/
-
-
-#endif
diff --git a/src/wchar/fwide.c b/src/wchar/fwide.c
index 9623d956..4dc965be 100644
--- a/src/wchar/fwide.c
+++ b/src/wchar/fwide.c
@@ -1,5 +1,3 @@
-#if 0
-
#include <wchar.h>
#include <stdio.h>
#include "stdio/_stdio.h"
@@ -9,10 +7,14 @@ int fwide(FILE * stream, int mode)
{
SIGNAL_SAFE(0);
- ASSERT_NONNULL(stream);
+ ASSERT_STREAM(stream, 0, 0);
- if (stream->orientation == 0) {
- stream->orientation = mode;
+ if (mode != 0) {
+ if (mode < 0) {
+ stream->orientation = ORIENT_BYTE;
+ } else if (mode > 0) {
+ stream->orientation = ORIENT_WIDE;
+ }
}
return stream->orientation;
}
@@ -20,6 +22,3 @@ int fwide(FILE * stream, int mode)
/*
STDC(199409)
*/
-
-
-#endif
diff --git a/src/wchar/wcrtomb.c b/src/wchar/wcrtomb.c
index 1c48df7f..20cbe235 100644
--- a/src/wchar/wcrtomb.c
+++ b/src/wchar/wcrtomb.c
@@ -1,7 +1,6 @@
-#if 0
-
#include <wchar.h>
#include <limits.h>
+#include "_safety.h"
size_t wcrtomb(char * restrict s, wchar_t wc, mbstate_t * restrict ps)
{
@@ -22,6 +21,3 @@ size_t wcrtomb(char * restrict s, wchar_t wc, mbstate_t * restrict ps)
/*
STDC(199409)
*/
-
-
-#endif