summaryrefslogtreecommitdiff
path: root/qstd/plat.c
diff options
context:
space:
mode:
authorquou <quou@disroot.org>2024-12-14 23:15:34 +1100
committerquou <quou@disroot.org>2024-12-14 23:19:17 +1100
commit44e48ddc2785b037abd202a8d38b2ef2e8c36600 (patch)
treef58887ce48f7fdbf6dcca365b2a1b02a34e1b355 /qstd/plat.c
initial commit
Diffstat (limited to 'qstd/plat.c')
-rw-r--r--qstd/plat.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/qstd/plat.c b/qstd/plat.c
new file mode 100644
index 0000000..61385ff
--- /dev/null
+++ b/qstd/plat.c
@@ -0,0 +1,86 @@
+#include "plat.h"
+
+#ifdef plat_posix
+#define _POSIX_SOURCE
+#define _GNU_SOURCE
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <time.h>
+#include <stdarg.h>
+#include <stdio.h>
+
+extern int isatty(int);
+extern int fileno(FILE*);
+
+int imp_assert(
+ int val,
+ const char* expr,
+ const char* file,
+ int line
+) {
+ if (!val) {
+ print_err(
+ "%d:%s: Assertion failed: %s.\n",
+ line,
+ file,
+ expr
+ );
+ pbreak(420);
+ return 0;
+ }
+ return 1;
+}
+
+void print(const char* fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ vfprintf(stdout, fmt, args);
+ va_end(args);
+}
+
+void print_err(const char* fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+
+ if (isatty(fileno(stderr))) {
+ fprintf(stderr, "\033[31;31m");
+ }
+
+ vfprintf(stderr, fmt, args);
+
+ if (isatty(fileno(stderr))) {
+ fprintf(stderr, "\033[0m");
+ }
+
+ va_end(args);
+}
+
+void print_war(const char* fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+
+ if (isatty(fileno(stderr))) {
+ fprintf(stderr, "\033[31;35m");
+ }
+
+ vfprintf(stderr, fmt, args);
+
+ if (isatty(fileno(stderr))) {
+ fprintf(stderr, "\033[0m");
+ }
+
+ va_end(args);
+}
+
+void pbreak(int code) {
+#if defined(DEBUG) && defined(plat_x86)
+ __asm__("int3;");
+ (void)code;
+#else
+ exit(code);
+#endif
+}
+
+#endif