diff options
author | Jakob Kaivo <jkk@ung.org> | 2020-08-12 16:32:02 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2020-08-12 16:32:02 -0400 |
commit | 5bd54dca15ac7a1d5b823d2eeb20779780424485 (patch) | |
tree | 9f54f20c88939785b0a65fea71c4ce7e1f1664d3 /src/stdio | |
parent | 556843458a3b38f1efe536fdb7ff3b0f65538e0f (diff) |
handle locked/unlocked i/o better
Diffstat (limited to 'src/stdio')
-rw-r--r-- | src/stdio/_stdio.h | 6 | ||||
-rw-r--r-- | src/stdio/fgetc.c | 1 | ||||
-rw-r--r-- | src/stdio/fputc.c | 25 | ||||
-rw-r--r-- | src/stdio/getc_unlocked.c | 12 | ||||
-rw-r--r-- | src/stdio/putc_unlocked.c | 35 | ||||
-rw-r--r-- | src/stdio/puts.c | 22 |
6 files changed, 73 insertions, 28 deletions
diff --git a/src/stdio/_stdio.h b/src/stdio/_stdio.h index dac25c13..43db57a0 100644 --- a/src/stdio/_stdio.h +++ b/src/stdio/_stdio.h @@ -61,8 +61,10 @@ struct __stdio { extern struct __stdio __stdio; #if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 199506L -#define flockfile(_file) (void)(_file) -#define funlockfile(_file) (void)(_file) +#define flockfile(_file) (void)(_file) +#define funlockfile(_file) (void)(_file) +#define putc_unlocked(_c, _stream) fputc(_c, _stream) +#define getc_unlocked(_stream) fgetc(_stream) #endif #endif diff --git a/src/stdio/fgetc.c b/src/stdio/fgetc.c index 08fe4e64..f03fd545 100644 --- a/src/stdio/fgetc.c +++ b/src/stdio/fgetc.c @@ -2,6 +2,7 @@ #include "_stdio.h" #if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 199506L +#undef getc_unlocked #include "getc_unlocked.c" #endif diff --git a/src/stdio/fputc.c b/src/stdio/fputc.c index 20cf5c6b..3123103b 100644 --- a/src/stdio/fputc.c +++ b/src/stdio/fputc.c @@ -1,32 +1,19 @@ #include <stdio.h> #include "_stdio.h" -#if defined _POSIX_SOURCE || defined _POSIX_C_SOURCE || defined _XOPEN_SOURCE -#include "sys/types.h" -#include "unistd.h" -#else -#include "../_syscall.h" -#define write(_fd, _buf, _size) __syscall(__syscall_lookup(write), _fd, _buf, _size) +#if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 199506L +#undef putc_unlocked +#include "putc_unlocked.c" #endif /** write a character to a file stream **/ int fputc(int c, FILE *stream) { - unsigned char ch = (unsigned char)c; + int ret = EOF; flockfile(stream); - - if (!stream) { - return EOF; - } - - if (write(stream->fd, &ch, sizeof(ch)) != 1) { - /* error */ - return EOF; - } - + ret = putc_unlocked(c, stream); funlockfile(stream); - - return ch; + return ret; } /*** diff --git a/src/stdio/getc_unlocked.c b/src/stdio/getc_unlocked.c index 8abae5de..8e688a64 100644 --- a/src/stdio/getc_unlocked.c +++ b/src/stdio/getc_unlocked.c @@ -1,6 +1,18 @@ #include <stdio.h> #include "_stdio.h" + +#if defined _POSIX_SOURCE || defined _POSIX_C_SOURCE || defined _XOPEN_SOURCE +#include "sys/types.h" #include "unistd.h" +#else +#include "../_syscall.h" +#define read(_fd, _buf, _size) __syscall(__syscall_lookup(read), _fd, _buf, _size) +#endif + +#if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 199506 +#define getc_unlocked __getc_unlocked + static +#endif int getc_unlocked(FILE * stream) { diff --git a/src/stdio/putc_unlocked.c b/src/stdio/putc_unlocked.c index 77068408..4f658eb5 100644 --- a/src/stdio/putc_unlocked.c +++ b/src/stdio/putc_unlocked.c @@ -1,10 +1,41 @@ #include <stdio.h> +#include "_stdio.h" -int putc_unlocked(int c, FILE * stream) +#if defined _POSIX_SOURCE || defined _POSIX_C_SOURCE || defined _XOPEN_SOURCE +#include "sys/types.h" +#include "unistd.h" +#else +#include "../_syscall.h" +#define write(_fd, _buf, _size) __syscall(__syscall_lookup(write), _fd, _buf, _size) +#endif + +#if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 199506 +#define putc_unlocked __putc_unlocked + static +#endif + +/** write a character to a file stream with explicit client locking **/ +int putc_unlocked(int c, FILE *stream) { - return 0; + unsigned char ch = (unsigned char)c; + + if (!stream) { + return EOF; + } + + if (write(stream->fd, &ch, sizeof(ch)) != 1) { + /* error */ + return EOF; + } + + return ch; } +/*** +***/ + /* +RETURN_SUCCESS(ARGUMENT(c)) +RETURN_FAILURE(CONSTANT(EOF)) POSIX(199506) */ diff --git a/src/stdio/puts.c b/src/stdio/puts.c index 7aeb8d83..9e6c4c5b 100644 --- a/src/stdio/puts.c +++ b/src/stdio/puts.c @@ -1,21 +1,33 @@ #include <stdio.h> +#include "_stdio.h" /** write a string to stoud **/ int puts(const char *s) { - if (fputs(s, stdout) == EOF) { - return EOF; + int ret = 1; + + flockfile(stdout); + + while (*s) { + if (putc_unlocked(*s, stdout) == EOF) { + ret = EOF; + break; + } + s++; } - if (putc('\n', stdout) == EOF) { - return EOF; + if (putc_unlocked('\n', stdout) == EOF) { + ret = EOF; } + funlockfile(stdout); + /* RETURN_SUCCESS(NONNEGATIVE()); RETURN_FAILURE(CONSTANT(EOF)); */ - return 1; + + return ret; } /*** |