]> git.saurik.com Git - minimal.git/commitdiff
Added minimal C library.
authorJay Freeman (saurik) <saurik@saurik.com>
Mon, 15 Oct 2007 05:51:31 +0000 (05:51 +0000)
committerJay Freeman (saurik) <saurik@saurik.com>
Mon, 15 Oct 2007 05:51:31 +0000 (05:51 +0000)
mapping.h [new file with mode: 0644]
minimal.h [new file with mode: 0644]

diff --git a/mapping.h b/mapping.h
new file mode 100644 (file)
index 0000000..6882d03
--- /dev/null
+++ b/mapping.h
@@ -0,0 +1,25 @@
+#include <sys/mman.h>
+
+#include <errno.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+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 (file)
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 <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <stdint.h>