diff --git a/example/crc32/crc32.cc b/example/crc32/crc32.cc index 5737fbe8..97b5cc37 100644 --- a/example/crc32/crc32.cc +++ b/example/crc32/crc32.cc @@ -30,7 +30,6 @@ for a C++ compiler $CXX, such as clang++ or g++. #include #include #include -#include // Wuffs ships as a "single file C library" or "header file library" as per // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt @@ -66,22 +65,14 @@ int main(int argc, char** argv) { } while (true) { - const int stdin_fd = 0; - ssize_t n = read(stdin_fd, src_buffer, SRC_BUFFER_SIZE); - if (n < 0) { - if (errno != EINTR) { - fprintf(stderr, "%s\n", strerror(errno)); - return 1; - } - continue; - } - - uint32_t checksum = - h.update(wuffs_base__make_slice_u8(src_buffer, static_cast(n))); - - if (n == 0) { + size_t n = fread(src_buffer, sizeof(uint8_t), SRC_BUFFER_SIZE, stdin); + uint32_t checksum = h.update(wuffs_base__make_slice_u8(src_buffer, n)); + if (feof(stdin)) { printf("%08" PRIx32 "\n", checksum); return 0; + } else if (ferror(stdin)) { + fprintf(stderr, "read error\n"); + return 1; } } } diff --git a/example/gifplayer/gifplayer.c b/example/gifplayer/gifplayer.c index 0bb72074..74ff1bef 100644 --- a/example/gifplayer/gifplayer.c +++ b/example/gifplayer/gifplayer.c @@ -32,15 +32,17 @@ support true color: https://gist.github.com/XVilka/8346728 #include #include #include -#include -#ifndef _POSIX_TIMERS -#error "TODO: timers on non-POSIX systems" -#else +// ---------------- + +#if defined(__unix__) || defined(__MACH__) + #include +#include +#define WUFFS_EXAMPLE_USE_TIMERS bool started = false; -struct timespec start_time; +struct timespec start_time = {0}; int64_t micros_since_start(struct timespec* now) { if (!started) { @@ -53,8 +55,13 @@ int64_t micros_since_start(struct timespec* now) { } return nanos / 1000; } + +#else +#warning "TODO: timers on non-POSIX systems" #endif +// ---------------- + // Wuffs ships as a "single file C library" or "header file library" as per // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt // @@ -98,21 +105,15 @@ wuffs_base__pixel_buffer pb = {0}; wuffs_base__flicks cumulative_delay_micros = 0; -// ignore_return_value suppresses errors from -Wall -Werror. -static void ignore_return_value(int ignored) {} - const char* read_stdin() { while (src_len < SRC_BUFFER_SIZE) { - const int stdin_fd = 0; - ssize_t n = read(stdin_fd, src_buffer + src_len, SRC_BUFFER_SIZE - src_len); - if (n > 0) { - src_len += n; - } else if (n == 0) { + size_t n = fread(src_buffer + src_len, sizeof(uint8_t), + SRC_BUFFER_SIZE - src_len, stdin); + src_len += n; + if (feof(stdin)) { return NULL; - } else if (errno == EINTR) { - // No-op. - } else { - return strerror(errno); + } else if (ferror(stdin)) { + return "read error"; } } return "input is too large"; @@ -380,7 +381,7 @@ const char* play() { } } -#ifdef _POSIX_TIMERS +#if defined(WUFFS_EXAMPLE_USE_TIMERS) if (started) { struct timespec now; if (clock_gettime(CLOCK_MONOTONIC, &now)) { @@ -399,7 +400,7 @@ const char* play() { } #endif - ignore_return_value(write(stdout_fd, printbuf.ptr, n)); + fwrite(printbuf.ptr, sizeof(uint8_t), n, stdout); cumulative_delay_micros += (1000 * wuffs_base__frame_config__duration(&fc)) / @@ -416,13 +417,6 @@ const char* play() { return NULL; } -int fail(const char* msg) { - const int stderr_fd = 2; - ignore_return_value(write(stderr_fd, msg, strnlen(msg, 4095))); - ignore_return_value(write(stderr_fd, "\n", 1)); - return 1; -} - int main(int argc, char** argv) { int i; for (i = 1; i < argc; i++) { @@ -431,14 +425,17 @@ int main(int argc, char** argv) { } } - const char* msg = read_stdin(); - if (msg) { - return fail(msg); + const char* status = read_stdin(); + if (status) { + fprintf(stderr, "%s\n", status); + return 1; } + while (true) { - msg = play(); - if (msg) { - return fail(msg); + status = play(); + if (status) { + fprintf(stderr, "%s\n", status); + return 1; } if (num_loops_remaining == 0) { continue; diff --git a/example/library/library.c b/example/library/library.c index 2b112c67..cd5468a1 100644 --- a/example/library/library.c +++ b/example/library/library.c @@ -32,8 +32,8 @@ rm -f a.out for a C compiler $CC, such as clang or gcc. */ +#include #include -#include // Wuffs ships as a "single file C library" or "header file library" as per // https://github.com/nothings/stb/blob/master/docs/stb_howto.txt @@ -73,9 +73,6 @@ uint8_t work_buffer[WORK_BUFFER_SIZE]; uint8_t work_buffer[1]; #endif -// ignore_return_value suppresses errors from -Wall -Werror. -static void ignore_return_value(int ignored) {} - static const char* decode() { wuffs_base__io_buffer dst; dst.data.ptr = dst_buffer; @@ -115,18 +112,16 @@ static const char* decode() { free(dec); return status; } - ignore_return_value(write(1, dst.data.ptr, dst.meta.wi)); + fwrite(dst.data.ptr, sizeof(uint8_t), dst.meta.wi, stdout); free(dec); return NULL; } int main(int argc, char** argv) { - const char* status_msg = decode(); - int status = 0; - if (status_msg) { - status = 1; - ignore_return_value(write(2, status_msg, strnlen(status_msg, 4095))); - ignore_return_value(write(2, "\n", 1)); + const char* status = decode(); + if (status) { + fprintf(stderr, "%s\n", status); + return 1; } - return status; + return 0; } diff --git a/example/zcat/zcat.c b/example/zcat/zcat.c index a5f73f21..c2bf06ae 100644 --- a/example/zcat/zcat.c +++ b/example/zcat/zcat.c @@ -53,7 +53,7 @@ for a C compiler $CC, such as clang or gcc. // program to generate a stand-alone C file. #include "../../release/c/wuffs-unsupported-snapshot.c" -#ifdef __linux__ +#if defined(__linux__) #include #include #include @@ -160,17 +160,17 @@ int fail(const char* msg) { } int main(int argc, char** argv) { -#ifdef WUFFS_EXAMPLE_USE_SECCOMP +#if defined(WUFFS_EXAMPLE_USE_SECCOMP) prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT); #endif - const char* msg = decode(); - int status = msg ? fail(msg) : 0; + const char* status = decode(); + int status_code = status ? fail(status) : 0; -#ifdef WUFFS_EXAMPLE_USE_SECCOMP +#if defined(WUFFS_EXAMPLE_USE_SECCOMP) // Call SYS_exit explicitly instead of SYS_exit_group implicitly. // SECCOMP_MODE_STRICT allows only the former. - syscall(SYS_exit, status); + syscall(SYS_exit, status_code); #endif - return status; + return status_code; }