From: Jay Freeman (saurik) Date: Mon, 15 Oct 2007 05:51:31 +0000 (+0000) Subject: Added minimal C library. X-Git-Url: https://git.saurik.com/minimal.git/commitdiff_plain/d4987f7dbb59d7657c5a04786883503a9b88a1be Added minimal C library. --- d4987f7dbb59d7657c5a04786883503a9b88a1be diff --git a/mapping.h b/mapping.h new file mode 100644 index 0000000..6882d03 --- /dev/null +++ b/mapping.h @@ -0,0 +1,25 @@ +#include + +#include +#include + +#include +#include +#include + +void *Map(const char *path, size_t *size, bool ro) { + int fd; + _syscall(fd = open(path, ro ? O_RDONLY : O_RDWR)); + + struct stat stat; + _syscall(fstat(fd, &stat)); + + if (size != NULL) + *size = stat.st_size; + + void *base; + _syscall(base = mmap(0, stat.st_size, ro ? PROT_READ : PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); + + _syscall(close(fd)); + return base; +} diff --git a/minimal.h b/minimal.h new file mode 100644 index 0000000..9d3bd20 --- /dev/null +++ b/minimal.h @@ -0,0 +1,26 @@ +#define _assert(expr) \ + do if (!(expr)) { \ + fprintf(stderr, "_assert(%s)\n", #expr); \ + exit(1); \ + } while (false) + +#define _syscall(expr) \ + do if ((long) (expr) != -1) \ + break; \ + else switch (errno) { \ + case EINTR: \ + continue; \ + default: \ + _assert(false); \ + } while (true) + +#define _forever \ + for (;;) + +#define _trace() \ + printf("_trace(%s:%u)\n", __FILE__, __LINE__) + +#include +#include +#include +#include