worst code ever
0
print.c
48 lines 1.2 kB view raw
1#include <stdio.h> 2#include <stdarg.h> 3#include <stdint.h> 4#include <signal.h> 5#include <setjmp.h> 6 7#define SENTINEL 0xDEADBEEFCAFEBABEULL 8static sigjmp_buf jmp; 9static void handler(int s) { siglongjmp(jmp, 1); } 10 11static void _print(int d, ...) { 12 va_list ap; va_start(ap, d); 13 uint64_t *slot = (uint64_t *)(void *)ap; 14 15 struct sigaction sa = {.sa_handler = handler}, ob, os; 16 sigaction(SIGBUS, &sa, &ob); sigaction(SIGSEGV, &sa, &os); 17 18 for (uint64_t v; (v = *slot++) != SENTINEL;) { 19 int t = 0; 20 if (!sigsetjmp(jmp, 1)) { 21 unsigned char c = *(char *)(uintptr_t)v; 22 t = (c - 9u < 5) || (c - 0x20u < 0x5F); 23 } 24 25 if (!t && ((v >> 52 & 0x7FF) - 1u < 0x7FE)) t = 2; 26 double f; __builtin_memcpy(&f, &v, 8); 27 28 t == 1 29 ? printf("%s", (char *)(uintptr_t)v) 30 : t == 2 ? printf("%g", f) 31 : printf("%d", (int)v); 32 } 33 34 sigaction(SIGBUS, &ob, 0); sigaction(SIGSEGV, &os, 0); 35} 36 37#define print(...) _print(0, __VA_ARGS__, SENTINEL) 38 39int main(void) { 40 print("This print works in c and does not care about types\n"); 41 42 char *name = "Pablo"; 43 int age = 67; 44 double pi = 3.14159; 45 long big = 42L; 46 47 print("name: ", name, " age: ", age, " pi: ", pi, " big: ", big, "\n"); 48}