Cider Isn't Darwin Emulation, Really
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310#include "base.h"#include "simple.h"#include <stdarg.h>#include <stddef.h>#include <linux-syscalls/linux.h>#include <lkm/api.h>
void __simple_vsprintf(char* buf, const char* format, va_list vl);extern char* memchr(char* buf, int c, __SIZE_TYPE__ n);
int __simple_strlen(const char* text){ int len = 0; while (*text++) len++; return len;}
static inline int abs(int n){ return (n < 0) ? -n : n;}
void __simple_vsprintf(char* buf, const char* format, va_list vl){ while (*format) { if (*format == '%') { format++; if (!*format) break; if (*format == 'l') format++; switch (*format) { case '%': *buf++ = '%'; break; case 's': { const char* str = va_arg(vl, const char*); if (!str) str = "(null)";
while (*str) { *buf++ = *str; str++; } break; } case 'd': { int num = va_arg(vl, int); char temp[16]; int count = 0;
if (num < 0) { *buf++ = '-'; num = -num; }
do { temp[count++] = '0' + abs(num % 10); num /= 10; } while (num > 0); while (count--) *buf++ = temp[count];
break; } case 'u': { unsigned int num = va_arg(vl, unsigned int); char temp[16]; int count = 0;
do { temp[count++] = '0' + (num % 10); num /= 10; } while (num > 0); while (count--) *buf++ = temp[count];
break; } case 'p': case 'x': { unsigned long num = va_arg(vl, unsigned long); char temp[40]; int count = 0;
if (*format == 'p') { *buf++ = '0'; *buf++ = 'x'; }
do { int c = (num % 16);
if (c < 10) temp[count++] = '0' + c; else temp[count++] = 'A' + (c - 10); num /= 16; } while (num > 0); while (count--) *buf++ = temp[count];
break;
} }
format++; } else { *buf++ = *format; format++; } }
*buf = 0;}
__attribute__ ((visibility ("default")))void __simple_printf(const char* format, ...){ char buffer[512]; va_list vl;
va_start(vl, format); __simple_vsprintf(buffer, format, vl); va_end(vl);
LINUX_SYSCALL3(__NR_write, 1, buffer, __simple_strlen(buffer));}
__attribute__ ((visibility ("default")))void __simple_kprintf(const char* format, ...){ char buffer[512]; va_list vl;
va_start(vl, format); __simple_vsprintf(buffer, format, vl); va_end(vl);
lkm_call(NR_kernel_printk, buffer);}
void __simple_fprintf(int fd, const char* format, ...){ char buffer[512]; va_list vl;
va_start(vl, format); __simple_vsprintf(buffer, format, vl); va_end(vl);
LINUX_SYSCALL3(__NR_write, fd, buffer, __simple_strlen(buffer));}
void __simple_sprintf(char *buffer, const char* format, ...){ va_list vl;
va_start(vl, format); __simple_vsprintf(buffer, format, vl); va_end(vl);}
#ifdef isdigit#undef isdigit#endif
static int isdigit(char c){ return c >= '0' && c <= '9';}static int isdigit16(char c){ if (isdigit(c)) return 1; if (c >= 'a' && c <= 'f') return 1; if (c >= 'A' && c <= 'F') return 1; return 0;}
unsigned long long __simple_atoi(const char* str, const char** endp){ unsigned long long value = 0;
while (isdigit(*str)) { value *= 10; value += *str - '0'; str++; } if (endp) *endp = str;
return value;}
unsigned long long __simple_atoi16(const char* str, const char** endp){ unsigned long long value = 0;
while (isdigit16(*str)) { value *= 16;
if (*str >= '0' && *str <= '9') value += *str - '0'; else if (*str >= 'a' && *str <= 'f') value += 10 + (*str - 'a'); else if (*str >= 'A' && *str < 'F') value += 10 + (*str - 'A');
str++; } if (endp) *endp = str;
return value;}
extern void *memmove(void *dest, const void *src, __SIZE_TYPE__ n);extern void *memcpy(void *dest, const void *src, __SIZE_TYPE__ n);extern long sys_read(int fd, void* buf, __SIZE_TYPE__ n);
#ifndef min# define min(a,b) ((a) < (b) ? (a) : (b))#endif
void __simple_readline_init(struct simple_readline_buf* buf){ buf->used = 0;}
char* __simple_readline(int fd, struct simple_readline_buf* buf, char* out, int max_out){ char* nl = NULL; int len;
find_nl: if (buf->used == 0 || (nl = memchr(buf->buf, '\n', buf->used)) == NULL) { // Read some more int rd;
if (sizeof(buf->buf) == buf->used) { nl = buf->buf + buf->used; goto have_nl; }
rd = sys_read(fd, buf->buf + buf->used, sizeof(buf->buf) - buf->used); if (rd == 0) { if (!buf->used) return NULL;
nl = buf->buf + buf->used; goto have_nl; } else { buf->used += rd; goto find_nl; } }
have_nl: if (nl == buf->buf) return NULL;
len = min(max_out - 1, nl - buf->buf); memcpy(out, buf->buf, len); out[len] = 0;
buf->used -= len; if (buf->used) buf->used--; // remove NL if (buf->used) memmove(buf->buf, nl+1, buf->used);
return out;}