mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-22 16:47:28 +01:00
Take some unistd.h dependencies out of examples
This commit is contained in:
1 parent
c4b8a6a4cc
commit
7e384e079e
4 files changed
+49
-66
No files matched your search
+6
-15
@@ -30,7 +30,6 @@ for a C++ compiler $CXX, such as clang++ or g++.
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
// Wuffs ships as a "single file C library" or "header file library" as per
|
// 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
|
// https://github.com/nothings/stb/blob/master/docs/stb_howto.txt
|
||||||
@@ -66,22 +65,14 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const int stdin_fd = 0;
|
size_t n = fread(src_buffer, sizeof(uint8_t), SRC_BUFFER_SIZE, stdin);
|
||||||
ssize_t n = read(stdin_fd, src_buffer, SRC_BUFFER_SIZE);
|
uint32_t checksum = h.update(wuffs_base__make_slice_u8(src_buffer, n));
|
||||||
if (n < 0) {
|
if (feof(stdin)) {
|
||||||
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<size_t>(n)));
|
|
||||||
|
|
||||||
if (n == 0) {
|
|
||||||
printf("%08" PRIx32 "\n", checksum);
|
printf("%08" PRIx32 "\n", checksum);
|
||||||
return 0;
|
return 0;
|
||||||
|
} else if (ferror(stdin)) {
|
||||||
|
fprintf(stderr, "read error\n");
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,15 +32,17 @@ support true color: https://gist.github.com/XVilka/8346728
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#ifndef _POSIX_TIMERS
|
// ----------------
|
||||||
#error "TODO: timers on non-POSIX systems"
|
|
||||||
#else
|
#if defined(__unix__) || defined(__MACH__)
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#define WUFFS_EXAMPLE_USE_TIMERS
|
||||||
|
|
||||||
bool started = false;
|
bool started = false;
|
||||||
struct timespec start_time;
|
struct timespec start_time = {0};
|
||||||
|
|
||||||
int64_t micros_since_start(struct timespec* now) {
|
int64_t micros_since_start(struct timespec* now) {
|
||||||
if (!started) {
|
if (!started) {
|
||||||
@@ -53,8 +55,13 @@ int64_t micros_since_start(struct timespec* now) {
|
|||||||
}
|
}
|
||||||
return nanos / 1000;
|
return nanos / 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
#warning "TODO: timers on non-POSIX systems"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// ----------------
|
||||||
|
|
||||||
// Wuffs ships as a "single file C library" or "header file library" as per
|
// 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
|
// 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;
|
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() {
|
const char* read_stdin() {
|
||||||
while (src_len < SRC_BUFFER_SIZE) {
|
while (src_len < SRC_BUFFER_SIZE) {
|
||||||
const int stdin_fd = 0;
|
size_t n = fread(src_buffer + src_len, sizeof(uint8_t),
|
||||||
ssize_t n = read(stdin_fd, src_buffer + src_len, SRC_BUFFER_SIZE - src_len);
|
SRC_BUFFER_SIZE - src_len, stdin);
|
||||||
if (n > 0) {
|
src_len += n;
|
||||||
src_len += n;
|
if (feof(stdin)) {
|
||||||
} else if (n == 0) {
|
|
||||||
return NULL;
|
return NULL;
|
||||||
} else if (errno == EINTR) {
|
} else if (ferror(stdin)) {
|
||||||
// No-op.
|
return "read error";
|
||||||
} else {
|
|
||||||
return strerror(errno);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "input is too large";
|
return "input is too large";
|
||||||
@@ -380,7 +381,7 @@ const char* play() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef _POSIX_TIMERS
|
#if defined(WUFFS_EXAMPLE_USE_TIMERS)
|
||||||
if (started) {
|
if (started) {
|
||||||
struct timespec now;
|
struct timespec now;
|
||||||
if (clock_gettime(CLOCK_MONOTONIC, &now)) {
|
if (clock_gettime(CLOCK_MONOTONIC, &now)) {
|
||||||
@@ -399,7 +400,7 @@ const char* play() {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
ignore_return_value(write(stdout_fd, printbuf.ptr, n));
|
fwrite(printbuf.ptr, sizeof(uint8_t), n, stdout);
|
||||||
|
|
||||||
cumulative_delay_micros +=
|
cumulative_delay_micros +=
|
||||||
(1000 * wuffs_base__frame_config__duration(&fc)) /
|
(1000 * wuffs_base__frame_config__duration(&fc)) /
|
||||||
@@ -416,13 +417,6 @@ const char* play() {
|
|||||||
return NULL;
|
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 main(int argc, char** argv) {
|
||||||
int i;
|
int i;
|
||||||
for (i = 1; i < argc; i++) {
|
for (i = 1; i < argc; i++) {
|
||||||
@@ -431,14 +425,17 @@ int main(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* msg = read_stdin();
|
const char* status = read_stdin();
|
||||||
if (msg) {
|
if (status) {
|
||||||
return fail(msg);
|
fprintf(stderr, "%s\n", status);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
msg = play();
|
status = play();
|
||||||
if (msg) {
|
if (status) {
|
||||||
return fail(msg);
|
fprintf(stderr, "%s\n", status);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
if (num_loops_remaining == 0) {
|
if (num_loops_remaining == 0) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ rm -f a.out
|
|||||||
for a C compiler $CC, such as clang or gcc.
|
for a C compiler $CC, such as clang or gcc.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
// Wuffs ships as a "single file C library" or "header file library" as per
|
// 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
|
// 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];
|
uint8_t work_buffer[1];
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// ignore_return_value suppresses errors from -Wall -Werror.
|
|
||||||
static void ignore_return_value(int ignored) {}
|
|
||||||
|
|
||||||
static const char* decode() {
|
static const char* decode() {
|
||||||
wuffs_base__io_buffer dst;
|
wuffs_base__io_buffer dst;
|
||||||
dst.data.ptr = dst_buffer;
|
dst.data.ptr = dst_buffer;
|
||||||
@@ -115,18 +112,16 @@ static const char* decode() {
|
|||||||
free(dec);
|
free(dec);
|
||||||
return status;
|
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);
|
free(dec);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
const char* status_msg = decode();
|
const char* status = decode();
|
||||||
int status = 0;
|
if (status) {
|
||||||
if (status_msg) {
|
fprintf(stderr, "%s\n", status);
|
||||||
status = 1;
|
return 1;
|
||||||
ignore_return_value(write(2, status_msg, strnlen(status_msg, 4095)));
|
|
||||||
ignore_return_value(write(2, "\n", 1));
|
|
||||||
}
|
}
|
||||||
return status;
|
return 0;
|
||||||
}
|
}
|
||||||
+7
-7
@@ -53,7 +53,7 @@ for a C compiler $CC, such as clang or gcc.
|
|||||||
// program to generate a stand-alone C file.
|
// program to generate a stand-alone C file.
|
||||||
#include "../../release/c/wuffs-unsupported-snapshot.c"
|
#include "../../release/c/wuffs-unsupported-snapshot.c"
|
||||||
|
|
||||||
#ifdef __linux__
|
#if defined(__linux__)
|
||||||
#include <linux/prctl.h>
|
#include <linux/prctl.h>
|
||||||
#include <linux/seccomp.h>
|
#include <linux/seccomp.h>
|
||||||
#include <sys/prctl.h>
|
#include <sys/prctl.h>
|
||||||
@@ -160,17 +160,17 @@ int fail(const char* msg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
#ifdef WUFFS_EXAMPLE_USE_SECCOMP
|
#if defined(WUFFS_EXAMPLE_USE_SECCOMP)
|
||||||
prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
|
prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
const char* msg = decode();
|
const char* status = decode();
|
||||||
int status = msg ? fail(msg) : 0;
|
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.
|
// Call SYS_exit explicitly instead of SYS_exit_group implicitly.
|
||||||
// SECCOMP_MODE_STRICT allows only the former.
|
// SECCOMP_MODE_STRICT allows only the former.
|
||||||
syscall(SYS_exit, status);
|
syscall(SYS_exit, status_code);
|
||||||
#endif
|
#endif
|
||||||
return status;
|
return status_code;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user