This commit is contained in:
alexvoste
2026-05-07 02:22:25 +03:00
commit 1a9fd27a31
226 changed files with 29188 additions and 0 deletions
+16
View File
@@ -0,0 +1,16 @@
#ifndef _ASSERT_H
#define _ASSERT_H
#ifdef NDEBUG
#define assert(cond) ((void)0)
#else
void __cervus_assert_fail(const char *expr, const char *file, int line, const char *func)
__attribute__((noreturn));
#define assert(cond) \
((cond) ? (void)0 : __cervus_assert_fail(#cond, __FILE__, __LINE__, __func__))
#endif
#endif
+162
View File
@@ -0,0 +1,162 @@
#ifndef _CERVUS_UTIL_H
#define _CERVUS_UTIL_H
#include <stddef.h>
#include <string.h>
#include <unistd.h>
#define C_RESET "\x1b[0m"
#define C_BOLD "\x1b[1m"
#define C_RED "\x1b[1;31m"
#define C_GREEN "\x1b[1;32m"
#define C_YELLOW "\x1b[1;33m"
#define C_BLUE "\x1b[1;34m"
#define C_MAGENTA "\x1b[1;35m"
#define C_CYAN "\x1b[1;36m"
#define C_GRAY "\x1b[90m"
static inline void path_join(const char *base, const char *name,
char *out, size_t sz)
{
if (!name || !name[0]) {
strncpy(out, base ? base : "", sz - 1);
out[sz - 1] = '\0';
return;
}
if (name[0] == '/') {
strncpy(out, name, sz - 1);
out[sz - 1] = '\0';
return;
}
strncpy(out, base ? base : "", sz - 1);
out[sz - 1] = '\0';
size_t bl = strlen(out);
if (bl > 0 && out[bl - 1] != '/' && bl + 1 < sz) {
out[bl++] = '/';
out[bl] = '\0';
}
size_t nl = strlen(name);
if (bl + nl + 1 < sz) memcpy(out + bl, name, nl + 1);
else out[sz - 1] = '\0';
}
static inline void path_norm(char *path)
{
char tmp[512];
strncpy(tmp, path, 511);
tmp[511] = '\0';
char *parts[64];
int np = 0;
char *p = tmp;
while (*p) {
while (*p == '/') p++;
if (!*p) break;
char *s = p;
while (*p && *p != '/') p++;
if (*p) *p++ = '\0';
if (strcmp(s, ".") == 0) continue;
if (strcmp(s, "..") == 0) { if (np > 0) np--; continue; }
if (np < 64) parts[np++] = s;
}
char out[512];
size_t ol = 0;
for (int i = 0; i < np; i++) {
if (ol + 1 >= sizeof(out)) break;
out[ol++] = '/';
size_t pl = strlen(parts[i]);
if (ol + pl >= sizeof(out)) pl = sizeof(out) - 1 - ol;
memcpy(out + ol, parts[i], pl);
ol += pl;
}
out[ol] = '\0';
if (ol == 0) { out[0] = '/'; out[1] = '\0'; }
strncpy(path, out, 512);
}
static inline void resolve_path(const char *cwd, const char *path,
char *out, size_t sz)
{
if (!path || !path[0]) {
strncpy(out, cwd ? cwd : "/", sz - 1);
out[sz - 1] = '\0';
return;
}
if (path[0] == '/') {
strncpy(out, path, sz - 1);
out[sz - 1] = '\0';
return;
}
path_join(cwd, path, out, sz);
path_norm(out);
}
extern int __cervus_argc;
extern char **__cervus_argv;
static inline int is_shell_flag(const char *a)
{
if (!a) return 0;
if (a[0] == '-' && a[1] == '-' &&
a[2] == 'c' && a[3] == 'w' && a[4] == 'd' && a[5] == '=') return 1;
if (a[0] == '-' && a[1] == '-' &&
a[2] == 'e' && a[3] == 'n' && a[4] == 'v' && a[5] == ':') return 1;
return 0;
}
static inline const char *get_cwd_flag(int argc, char **argv)
{
(void)argc; (void)argv;
for (int i = 1; i < __cervus_argc; i++) {
char *a = __cervus_argv[i];
if (a && a[0] == '-' && a[1] == '-' &&
a[2] == 'c' && a[3] == 'w' && a[4] == 'd' && a[5] == '=')
return a + 6;
}
return "/";
}
static inline const char *getenv_argv(int argc, char **argv,
const char *name, const char *def)
{
(void)argc; (void)argv;
if (!name) return def;
size_t nl = strlen(name);
for (int i = 1; i < __cervus_argc; i++) {
const char *a = __cervus_argv[i];
if (!a) continue;
if (a[0] == '-' && a[1] == '-' &&
a[2] == 'e' && a[3] == 'n' && a[4] == 'v' && a[5] == ':') {
const char *kv = a + 6;
if (strncmp(kv, name, nl) == 0 && kv[nl] == '=')
return kv + nl + 1;
}
}
return def;
}
static inline int util_readline(int fd, char *buf, int maxlen)
{
int i = 0;
while (i < maxlen - 1) {
char c;
ssize_t r = read(fd, &c, 1);
if (r <= 0) { buf[i] = '\0'; return i > 0 ? i : -1; }
if (c == '\r') continue;
buf[i++] = c;
if (c == '\n') break;
}
buf[i] = '\0';
return i;
}
static inline void util_write_stdout(const char *s)
{
write(1, s, strlen(s));
}
static inline void util_write_stderr(const char *s)
{
write(2, s, strlen(s));
}
#endif
+19
View File
@@ -0,0 +1,19 @@
#ifndef _CTYPE_H
#define _CTYPE_H
int isdigit(int c);
int isalpha(int c);
int isalnum(int c);
int isupper(int c);
int islower(int c);
int isspace(int c);
int isprint(int c);
int isgraph(int c);
int ispunct(int c);
int isxdigit(int c);
int iscntrl(int c);
int isblank(int c);
int toupper(int c);
int tolower(int c);
#endif
+30
View File
@@ -0,0 +1,30 @@
#ifndef _DIRENT_H
#define _DIRENT_H
#include <sys/types.h>
#define DT_UNKNOWN 0
#define DT_FILE 0
#define DT_REG 0
#define DT_DIR 1
#define DT_CHR 2
#define DT_BLK 3
#define DT_LNK 4
#define DT_PIPE 5
#define DT_FIFO 5
struct dirent {
ino_t d_ino;
uint8_t d_type;
char d_name[256];
};
typedef struct __cervus_DIR DIR;
DIR *opendir(const char *path);
struct dirent *readdir(DIR *dirp);
int closedir(DIR *dirp);
void rewinddir(DIR *dirp);
int dirfd(DIR *dirp);
#endif
+45
View File
@@ -0,0 +1,45 @@
#ifndef _ERRNO_H
#define _ERRNO_H
extern int __cervus_errno;
#define errno __cervus_errno
#define EPERM 1
#define ENOENT 2
#define ESRCH 3
#define EINTR 4
#define EIO 5
#define ENXIO 6
#define E2BIG 7
#define ENOEXEC 8
#define EBADF 9
#define ECHILD 10
#define EAGAIN 11
#define ENOMEM 12
#define EACCES 13
#define EFAULT 14
#define EBUSY 16
#define EEXIST 17
#define EXDEV 18
#define ENODEV 19
#define ENOTDIR 20
#define EISDIR 21
#define EINVAL 22
#define ENFILE 23
#define EMFILE 24
#define ENOTTY 25
#define EFBIG 27
#define ENOSPC 28
#define ESPIPE 29
#define EROFS 30
#define EMLINK 31
#define EPIPE 32
#define EDOM 33
#define ERANGE 34
#define ENAMETOOLONG 36
#define ENOSYS 38
#define ENOTEMPTY 39
#define ELOOP 40
#define EOVERFLOW 75
#endif
+27
View File
@@ -0,0 +1,27 @@
#ifndef _FCNTL_H
#define _FCNTL_H
#include <sys/types.h>
#define O_RDONLY 0x000
#define O_WRONLY 0x001
#define O_RDWR 0x002
#define O_ACCMODE 0x003
#define O_CREAT 0x040
#define O_TRUNC 0x200
#define O_APPEND 0x400
#define O_NONBLOCK 0x800
#define O_DIRECTORY 0x10000
#define O_CLOEXEC 0x80000
#define F_GETFD 1
#define F_SETFD 2
#define F_GETFL 3
#define F_SETFL 4
#define FD_CLOEXEC 1
int open(const char *path, int flags, ...);
int fcntl(int fd, int cmd, ...);
int creat(const char *path, mode_t mode);
#endif
+76
View File
@@ -0,0 +1,76 @@
#ifndef _INTTYPES_H
#define _INTTYPES_H
#include <stdint.h>
#define PRId8 "d"
#define PRId16 "d"
#define PRId32 "d"
#define PRId64 "ld"
#define PRIu8 "u"
#define PRIu16 "u"
#define PRIu32 "u"
#define PRIu64 "lu"
#define PRIx8 "x"
#define PRIx16 "x"
#define PRIx32 "x"
#define PRIx64 "lx"
#define PRIX8 "X"
#define PRIX16 "X"
#define PRIX32 "X"
#define PRIX64 "lX"
#define PRIi8 "i"
#define PRIi16 "i"
#define PRIi32 "i"
#define PRIi64 "li"
#define PRIo8 "o"
#define PRIo16 "o"
#define PRIo32 "o"
#define PRIo64 "lo"
#define PRIdMAX "ld"
#define PRIuMAX "lu"
#define PRIxMAX "lx"
#define PRIiMAX "li"
#define PRIdPTR "ld"
#define PRIuPTR "lu"
#define PRIxPTR "lx"
#define SCNd8 "hhd"
#define SCNd16 "hd"
#define SCNd32 "d"
#define SCNd64 "ld"
#define SCNu8 "hhu"
#define SCNu16 "hu"
#define SCNu32 "u"
#define SCNu64 "lu"
#define SCNx8 "hhx"
#define SCNx16 "hx"
#define SCNx32 "x"
#define SCNx64 "lx"
typedef struct {
intmax_t quot;
intmax_t rem;
} imaxdiv_t;
static inline intmax_t imaxabs(intmax_t j) {
return j < 0 ? -j : j;
}
static inline imaxdiv_t imaxdiv(intmax_t numer, intmax_t denom) {
imaxdiv_t r;
r.quot = numer / denom;
r.rem = numer % denom;
return r;
}
#endif
+30
View File
@@ -0,0 +1,30 @@
#ifndef _LIMITS_H
#define _LIMITS_H
#define CHAR_BIT 8
#define SCHAR_MIN (-128)
#define SCHAR_MAX (127)
#define UCHAR_MAX (255)
#define CHAR_MIN SCHAR_MIN
#define CHAR_MAX SCHAR_MAX
#define SHRT_MIN (-32768)
#define SHRT_MAX (32767)
#define USHRT_MAX (65535)
#define INT_MIN (-2147483647 - 1)
#define INT_MAX (2147483647)
#define UINT_MAX (4294967295u)
#define LONG_MIN (-9223372036854775807L - 1)
#define LONG_MAX (9223372036854775807L)
#define ULONG_MAX (18446744073709551615UL)
#define LLONG_MIN LONG_MIN
#define LLONG_MAX LONG_MAX
#define ULLONG_MAX ULONG_MAX
#define PATH_MAX 512
#define NAME_MAX 255
#endif
+77
View File
@@ -0,0 +1,77 @@
#ifndef _MATH_H
#define _MATH_H
#include <stdint.h>
#define INFINITY (1.0/0.0)
#define NAN (0.0/0.0)
int abs(int x);
double fabs(double x);
double pow(double base, double exp);
double pow10(int n);
int isinf(double x);
int isnan(double x);
static inline double ldexp(double x, int exp) {
union { double d; uint64_t u; } v;
v.d = x;
int e = (int)((v.u >> 52) & 0x7FF);
if (e == 0 || e == 0x7FF)
return x;
e += exp;
if (e <= 0)
return 0.0;
if (e >= 0x7FF)
return INFINITY;
v.u = (v.u & ~((uint64_t)0x7FF << 52)) | ((uint64_t)e << 52);
return v.d;
}
static inline double floor(double x) {
int64_t i = (int64_t)x;
return (double)(i - (x < (double)i));
}
static inline double ceil(double x) {
int64_t i = (int64_t)x;
return (double)(i + (x > (double)i));
}
static inline double round(double x) {
return (x >= 0.0) ? floor(x + 0.5) : ceil(x - 0.5);
}
static inline double sqrt(double x) {
double result;
asm volatile ("sqrtsd %1, %0" : "=x"(result) : "x"(x));
return result;
}
static inline float sqrtf(float x) {
float result;
asm volatile ("sqrtss %1, %0" : "=x"(result) : "x"(x));
return result;
}
static inline double log2(double x) {
double result;
asm volatile (
"fld1\n\t"
"fld %1\n\t"
"fyl2x\n\t"
"fstp %0\n\t"
: "=m"(result) : "m"(x)
);
return result;
}
#define MIN(a, b) ({ __typeof__(a) _a = (a); __typeof__(b) _b = (b); _a < _b ? _a : _b; })
#define MAX(a, b) ({ __typeof__(a) _a = (a); __typeof__(b) _b = (b); _a > _b ? _a : _b; })
#define ALIGN_UP(x, align) (((x) + (align) - 1) & ~((align) - 1))
#define ALIGN_DOWN(x, align) ((x) & ~((align) - 1))
#define IS_POWER_OF_TWO(x) ((x) != 0 && (((x) & ((x) - 1)) == 0))
#endif
+13
View File
@@ -0,0 +1,13 @@
#ifndef _SETJMP_H
#define _SETJMP_H
typedef long jmp_buf[8];
int setjmp(jmp_buf env);
void longjmp(jmp_buf env, int val) __attribute__((noreturn));
#define sigjmp_buf jmp_buf
#define sigsetjmp(env, savesigs) setjmp(env)
#define siglongjmp(env, val) longjmp(env, val)
#endif
+169
View File
@@ -0,0 +1,169 @@
#ifndef _SIGNAL_H
#define _SIGNAL_H
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
typedef int sig_atomic_t;
#define SIGHUP 1
#define SIGINT 2
#define SIGQUIT 3
#define SIGILL 4
#define SIGTRAP 5
#define SIGABRT 6
#define SIGIOT SIGABRT
#define SIGBUS 7
#define SIGFPE 8
#define SIGKILL 9
#define SIGUSR1 10
#define SIGSEGV 11
#define SIGUSR2 12
#define SIGPIPE 13
#define SIGALRM 14
#define SIGTERM 15
#define SIGCHLD 17
#define SIGCONT 18
#define SIGSTOP 19
#define SIGTSTP 20
#define SIGTTIN 21
#define SIGTTOU 22
#define SIGWINCH 28
#define NSIG 32
typedef void (*sighandler_t)(int);
#define SIG_DFL ((sighandler_t)0)
#define SIG_IGN ((sighandler_t)1)
#define SIG_ERR ((sighandler_t)-1)
#define SA_NOCLDSTOP 0x00000001
#define SA_NOCLDWAIT 0x00000002
#define SA_SIGINFO 0x00000004
#define SA_ONSTACK 0x08000000
#define SA_RESTART 0x10000000
#define SA_NODEFER 0x40000000
#define SA_RESETHAND 0x80000000
#define SI_USER 0
#define SI_KERNEL 0x80
#define SI_QUEUE -1
#define SI_TIMER -2
#define SI_MESGQ -3
#define SI_ASYNCIO -4
#define SI_SIGIO -5
#define SI_TKILL -6
#define FPE_INTDIV 1
#define FPE_INTOVF 2
#define FPE_FLTDIV 3
#define FPE_FLTOVF 4
#define FPE_FLTUND 5
#define FPE_FLTRES 6
#define FPE_FLTINV 7
#define FPE_FLTSUB 8
#define ILL_ILLOPC 1
#define ILL_ILLOPN 2
#define ILL_ILLADR 3
#define ILL_ILLTRP 4
#define ILL_PRVOPC 5
#define ILL_PRVREG 6
#define ILL_COPROC 7
#define ILL_BADSTK 8
#define SEGV_MAPERR 1
#define SEGV_ACCERR 2
#define BUS_ADRALN 1
#define BUS_ADRERR 2
#define BUS_OBJERR 3
typedef struct {
unsigned long __bits[2];
} sigset_t;
union sigval {
int sival_int;
void *sival_ptr;
};
typedef struct {
int si_signo;
int si_errno;
int si_code;
union {
struct {
pid_t si_pid;
uid_t si_uid;
} _kill;
struct {
int si_tid;
int si_overrun;
union sigval si_sigval;
} _timer;
struct {
pid_t si_pid;
uid_t si_uid;
union sigval si_sigval;
} _rt;
struct {
pid_t si_pid;
uid_t si_uid;
int si_status;
} _sigchld;
struct {
void *si_addr;
} _sigfault;
struct {
long si_band;
int si_fd;
} _sigpoll;
} _sifields;
} siginfo_t;
#define si_pid _sifields._kill.si_pid
#define si_uid _sifields._kill.si_uid
#define si_status _sifields._sigchld.si_status
#define si_addr _sifields._sigfault.si_addr
#define si_band _sifields._sigpoll.si_band
#define si_fd _sifields._sigpoll.si_fd
typedef struct {
void *ss_sp;
int ss_flags;
size_t ss_size;
} stack_t;
#define SS_ONSTACK 1
#define SS_DISABLE 2
struct sigaction {
union {
sighandler_t sa_handler;
void (*sa_sigaction)(int, siginfo_t *, void *);
};
sigset_t sa_mask;
int sa_flags;
void (*sa_restorer)(void);
};
sighandler_t signal(int signum, sighandler_t handler);
int raise(int sig);
int sigaction(int sig, const struct sigaction *act, struct sigaction *oldact);
int sigemptyset(sigset_t *set);
int sigfillset(sigset_t *set);
int sigaddset(sigset_t *set, int sig);
int sigdelset(sigset_t *set, int sig);
int sigismember(const sigset_t *set, int sig);
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);
int sigaltstack(const stack_t *ss, stack_t *oss);
#define SIG_BLOCK 0
#define SIG_UNBLOCK 1
#define SIG_SETMASK 2
#endif
+11
View File
@@ -0,0 +1,11 @@
#ifndef _STDARG_H
#define _STDARG_H
typedef __builtin_va_list va_list;
#define va_start(ap, last) __builtin_va_start(ap, last)
#define va_arg(ap, t) __builtin_va_arg(ap, t)
#define va_end(ap) __builtin_va_end(ap)
#define va_copy(d, s) __builtin_va_copy(d, s)
#endif
+9
View File
@@ -0,0 +1,9 @@
#ifndef _STDBOOL_H
#define _STDBOOL_H
#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#endif
#define __bool_true_false_are_defined 1
#endif
+15
View File
@@ -0,0 +1,15 @@
#ifndef _STDDEF_H
#define _STDDEF_H
#ifndef NULL
#define NULL ((void*)0)
#endif
typedef unsigned long size_t;
typedef long ssize_t;
typedef long ptrdiff_t;
typedef int wchar_t;
#define offsetof(t, m) __builtin_offsetof(t, m)
#endif
+48
View File
@@ -0,0 +1,48 @@
#ifndef _STDINT_H
#define _STDINT_H
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef short int16_t;
typedef unsigned short uint16_t;
typedef int int32_t;
typedef unsigned int uint32_t;
typedef long int64_t;
typedef unsigned long uint64_t;
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long intmax_t;
typedef unsigned long uintmax_t;
#define INT8_MIN (-128)
#define INT8_MAX (127)
#define INT16_MIN (-32768)
#define INT16_MAX (32767)
#define INT32_MIN (-2147483647 - 1)
#define INT32_MAX (2147483647)
#define INT64_MIN (-9223372036854775807L - 1)
#define INT64_MAX (9223372036854775807L)
#define UINT8_MAX (0xFFu)
#define UINT16_MAX (0xFFFFu)
#define UINT32_MAX (0xFFFFFFFFu)
#define UINT64_MAX (0xFFFFFFFFFFFFFFFFUL)
#define INTPTR_MIN INT64_MIN
#define INTPTR_MAX INT64_MAX
#define UINTPTR_MAX UINT64_MAX
#define SIZE_MAX UINT64_MAX
#define INT8_C(v) v
#define INT16_C(v) v
#define INT32_C(v) v
#define INT64_C(v) v ## L
#define UINT8_C(v) v ## u
#define UINT16_C(v) v ## u
#define UINT32_C(v) v ## u
#define UINT64_C(v) v ## UL
#endif
+66
View File
@@ -0,0 +1,66 @@
#ifndef _STDIO_H
#define _STDIO_H
#include <stddef.h>
#include <stdarg.h>
#define EOF (-1)
#define BUFSIZ 1024
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
typedef struct __cervus_FILE FILE;
extern FILE *stdin;
extern FILE *stdout;
extern FILE *stderr;
int putchar(int c);
int getchar(void);
int puts(const char *s);
int fputs(const char *s, FILE *stream);
int fputc(int c, FILE *stream);
int fgetc(FILE *stream);
char *fgets(char *s, int n, FILE *stream);
int ungetc(int c, FILE *stream);
int printf(const char *fmt, ...);
int fprintf(FILE *stream, const char *fmt, ...);
int sprintf(char *buf, const char *fmt, ...);
int snprintf(char *buf, size_t sz, const char *fmt, ...);
int vsprintf(char *buf, const char *fmt, va_list ap);
int vsnprintf(char *buf, size_t sz, const char *fmt, va_list ap);
int vfprintf(FILE *stream, const char *fmt, va_list ap);
int vprintf(const char *fmt, va_list ap);
int sscanf(const char *str, const char *fmt, ...);
int vsscanf(const char *str, const char *fmt, va_list ap);
int fscanf(FILE *stream, const char *fmt, ...);
int scanf(const char *fmt, ...);
FILE *fopen(const char *path, const char *mode);
FILE *fdopen(int fd, const char *mode);
FILE *tmpfile(void);
int fclose(FILE *stream);
size_t fread(void *buf, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream);
int fflush(FILE *stream);
int fseek(FILE *stream, long off, int whence);
long ftell(FILE *stream);
void rewind(FILE *stream);
int feof(FILE *stream);
int ferror(FILE *stream);
void clearerr(FILE *stream);
int fileno(FILE *stream);
FILE *popen(const char *cmd, const char *type);
int pclose(FILE *stream);
void perror(const char *msg);
int rename(const char *oldp, const char *newp);
int remove(const char *path);
char *tmpnam(char *buf);
#endif
+76
View File
@@ -0,0 +1,76 @@
#ifndef _STDLIB_H
#define _STDLIB_H
#include <stddef.h>
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
#define RAND_MAX 0x7FFFFFFF
void *malloc(size_t n);
void *calloc(size_t nmemb, size_t size);
void *realloc(void *p, size_t n);
void free(void *p);
void exit(int status) __attribute__((noreturn));
void abort(void) __attribute__((noreturn));
int atexit(void (*fn)(void));
int atoi(const char *s);
long atol(const char *s);
long long atoll(const char *s);
long strtol(const char *s, char **end, int base);
unsigned long strtoul(const char *s, char **end, int base);
long long strtoll(const char *s, char **end, int base);
unsigned long long strtoull(const char *s, char **end, int base);
#include <stdint.h>
uint64_t __cervus_strtod_bits(const char *s, char **endptr);
static __inline__ double strtod(const char *s, char **endp)
{
uint64_t b = __cervus_strtod_bits(s, endp);
double d;
__builtin_memcpy(&d, &b, sizeof(d));
return d;
}
static __inline__ float strtof(const char *s, char **endp)
{
return (float)strtod(s, endp);
}
static __inline__ long double strtold(const char *s, char **endp)
{
return (long double)strtod(s, endp);
}
static __inline__ double atof(const char *s)
{
return strtod(s, (char **)0);
}
int abs(int x);
long labs(long x);
long long llabs(long long x);
int rand(void);
void srand(unsigned int seed);
char *getenv(const char *name);
int putenv(char *str);
int setenv(const char *name, const char *value, int overwrite);
int unsetenv(const char *name);
void qsort(void *base, size_t nmemb, size_t size, int (*cmp)(const void *, const void *));
void *bsearch(const void *key, const void *base, size_t nmemb, size_t size,
int (*cmp)(const void *, const void *));
int system(const char *cmd);
int mkstemp(char *template);
char *mkdtemp(char *template);
char *realpath(const char *path, char *resolved);
#endif
+36
View File
@@ -0,0 +1,36 @@
#ifndef _STRING_H
#define _STRING_H
#include <stddef.h>
void *memset(void *dst, int c, size_t n);
void *memcpy(void *dst, const void *src, size_t n);
void *memmove(void *dst, const void *src, size_t n);
int memcmp(const void *a, const void *b, size_t n);
void *memchr(const void *s, int c, size_t n);
void *memmem(const void *haystack, size_t hlen, const void *needle, size_t nlen);
size_t strlen(const char *s);
size_t strnlen(const char *s, size_t max);
int strcmp(const char *a, const char *b);
int strncmp(const char *a, const char *b, size_t n);
int strcasecmp(const char *a, const char *b);
int strncasecmp(const char *a, const char *b, size_t n);
char *strcpy(char *dst, const char *src);
char *strncpy(char *dst, const char *src, size_t n);
char *strcat(char *dst, const char *src);
char *strncat(char *dst, const char *src, size_t n);
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
char *strstr(const char *hay, const char *needle);
char *strdup(const char *s);
char *strndup(const char *s, size_t n);
size_t strspn(const char *s, const char *accept);
size_t strcspn(const char *s, const char *reject);
char *strpbrk(const char *s, const char *accept);
char *strtok(char *str, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr);
char *strerror(int errnum);
char *strsignal(int sig);
#endif
+140
View File
@@ -0,0 +1,140 @@
#ifndef _SYS_CERVUS_H
#define _SYS_CERVUS_H
#include <stddef.h>
#include <stdint.h>
#include <sys/types.h>
#define CAP_IOPORT (1ULL << 0)
#define CAP_RAWMEM (1ULL << 1)
#define CAP_KILL_ANY (1ULL << 4)
#define CAP_SET_PRIO (1ULL << 5)
#define CAP_TASK_SPAWN (1ULL << 6)
#define CAP_TASK_INFO (1ULL << 7)
#define CAP_MMAP_EXEC (1ULL << 8)
#define CAP_SETUID (1ULL << 17)
#define CAP_DBG_SERIAL (1ULL << 20)
typedef struct __attribute__((packed)) {
uint8_t boot_flag;
uint8_t type;
uint32_t lba_start;
uint32_t sector_count;
} cervus_mbr_part_t;
typedef struct __attribute__((packed)) {
char disk_name[32];
char part_name[32];
uint32_t part_num;
uint8_t type;
uint8_t bootable;
uint64_t lba_start;
uint64_t sector_count;
uint64_t size_bytes;
} cervus_part_info_t;
typedef struct {
char name[32];
uint64_t sectors;
uint64_t size_bytes;
char model[41];
uint8_t present;
uint8_t _pad[6];
} cervus_disk_info_t;
typedef struct {
uint32_t pid, ppid, uid, gid;
uint64_t capabilities;
char name[32];
uint32_t state;
uint32_t priority;
uint64_t total_runtime_ns;
} cervus_task_info_t;
typedef struct {
uint64_t total_bytes;
uint64_t free_bytes;
uint64_t used_bytes;
uint64_t usable_bytes;
uint64_t page_size;
} cervus_meminfo_t;
typedef struct {
int64_t tv_sec;
int64_t tv_nsec;
} cervus_timespec_t;
typedef struct {
char path[512];
char device[32];
char fstype[16];
uint32_t flags;
} cervus_mount_info_t;
typedef struct {
uint64_t f_bsize;
uint64_t f_blocks;
uint64_t f_bfree;
uint64_t f_bavail;
uint64_t f_files;
uint64_t f_ffree;
uint32_t f_flag;
uint32_t f_namemax;
} cervus_statvfs_t;
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1
#define WNOHANG 0x1
#define WEXITSTATUS(s) (((s) >> 8) & 0xFF)
#define WIFEXITED(s) (((s) & 0x7F) == 0)
int cervus_task_info(pid_t pid, cervus_task_info_t *out);
int cervus_task_kill(pid_t pid);
uint64_t cervus_cap_get(void);
int cervus_cap_drop(uint64_t mask);
int cervus_meminfo(cervus_meminfo_t *out);
uint64_t cervus_uptime_ns(void);
int cervus_clock_gettime(int id, cervus_timespec_t *ts);
int cervus_nanosleep(uint64_t ns);
int cervus_shutdown(void);
int cervus_reboot(void);
int cervus_disk_info(int index, cervus_disk_info_t *out);
int cervus_disk_mount(const char *dev, const char *path);
int cervus_disk_umount(const char *path);
int cervus_disk_format(const char *dev, const char *label);
int cervus_disk_mkfs_fat32(const char *dev, const char *label);
int cervus_disk_partition(const char *dev, const cervus_mbr_part_t *specs, uint64_t n);
int cervus_disk_read_raw(const char *dev, uint64_t lba, uint64_t count, void *buf);
int cervus_disk_write_raw(const char *dev, uint64_t lba, uint64_t count, const void *buf);
long cervus_disk_list_parts(cervus_part_info_t *out, int max);
long cervus_disk_bios_install(const char *disk, const void *sys_data, uint32_t sys_size);
long cervus_list_mounts(cervus_mount_info_t *out, int max);
long cervus_statvfs(const char *path, cervus_statvfs_t *out);
uint32_t cervus_ioport_read(uint16_t port, int width);
int cervus_ioport_write(uint16_t port, int width, uint32_t val);
#define PROT_NONE 0x0
#define PROT_READ 0x1
#define PROT_WRITE 0x2
#define PROT_EXEC 0x4
#define MAP_PRIVATE 0x02
#define MAP_ANONYMOUS 0x20
#define MAP_FIXED 0x10
#define MAP_FAILED ((void *)-1)
void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off);
int munmap(void *addr, size_t len);
pid_t waitpid(pid_t pid, int *status, int options);
pid_t wait(int *status);
ssize_t cervus_dbg_print(const char *buf, size_t n);
#endif
+29
View File
@@ -0,0 +1,29 @@
#ifndef _SYS_IOCTL_H
#define _SYS_IOCTL_H
#include <stdint.h>
#include <sys/syscall.h>
#define TIOCGWINSZ 0x5413
#define TIOCSWINSZ 0x5414
#define TIOCGPGRP 0x540F
#define TIOCSPGRP 0x5410
#define TIOCGPTN 0x80045430
#define TIOCSPTLCK 0x40045431
#define FIOCLEX 0x5451
#define FIONCLEX 0x5450
#define FIONREAD 0x541B
#define FIONBIO 0x5421
#define FIOASYNC 0x5452
struct winsize {
uint16_t ws_row;
uint16_t ws_col;
uint16_t ws_xpixel;
uint16_t ws_ypixel;
};
int ioctl(int fd, unsigned long request, ...);
#endif
+24
View File
@@ -0,0 +1,24 @@
#ifndef _SYS_MMAN_H
#define _SYS_MMAN_H
#include <stddef.h>
#include <sys/types.h>
#define PROT_NONE 0x0
#define PROT_READ 0x1
#define PROT_WRITE 0x2
#define PROT_EXEC 0x4
#define MAP_SHARED 0x01
#define MAP_PRIVATE 0x02
#define MAP_FIXED 0x10
#define MAP_ANONYMOUS 0x20
#define MAP_ANON MAP_ANONYMOUS
#define MAP_FAILED ((void *)-1)
void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off);
int munmap(void *addr, size_t len);
int mprotect(void *addr, size_t len, int prot);
#endif
+50
View File
@@ -0,0 +1,50 @@
#ifndef _SYS_STAT_H
#define _SYS_STAT_H
#include <sys/types.h>
#define S_IFMT 0170000
#define S_IFREG 0100000
#define S_IFDIR 0040000
#define S_IFCHR 0020000
#define S_IFBLK 0060000
#define S_IFIFO 0010000
#define S_IFLNK 0120000
#define S_IFSOCK 0140000
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
#define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK)
#define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO)
#define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
#define S_IRWXU 00700
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100
#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010
#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002
#define S_IXOTH 00001
struct stat {
ino_t st_ino;
uint32_t st_type;
mode_t st_mode;
uid_t st_uid;
gid_t st_gid;
off_t st_size;
blkcnt_t st_blocks;
};
int stat(const char *path, struct stat *out);
int fstat(int fd, struct stat *out);
int mkdir(const char *path, mode_t mode);
int chmod(const char *path, mode_t mode);
#endif
+98
View File
@@ -0,0 +1,98 @@
#ifndef _SYS_SYSCALL_H
#define _SYS_SYSCALL_H
#include <stdint.h>
#define SYS_EXIT 0
#define SYS_EXIT_GROUP 1
#define SYS_GETPID 2
#define SYS_GETPPID 3
#define SYS_FORK 4
#define SYS_WAIT 5
#define SYS_YIELD 6
#define SYS_GETUID 7
#define SYS_GETGID 8
#define SYS_SETUID 9
#define SYS_SETGID 10
#define SYS_CAP_GET 11
#define SYS_CAP_DROP 12
#define SYS_TASK_INFO 13
#define SYS_EXECVE 14
#define SYS_READ 20
#define SYS_WRITE 21
#define SYS_OPEN 22
#define SYS_CLOSE 23
#define SYS_SEEK 24
#define SYS_STAT 25
#define SYS_FSTAT 26
#define SYS_IOCTL 27
#define SYS_DUP 28
#define SYS_DUP2 29
#define SYS_PIPE 30
#define SYS_FCNTL 31
#define SYS_READDIR 32
#define SYS_MMAP 40
#define SYS_MUNMAP 41
#define SYS_MPROTECT 42
#define SYS_BRK 43
#define SYS_CLOCK_GET 60
#define SYS_SLEEP_NS 61
#define SYS_UPTIME 62
#define SYS_MEMINFO 63
#define SYS_FUTEX_WAIT 80
#define SYS_FUTEX_WAKE 81
#define SYS_DBG_PRINT 512
#define SYS_TASK_KILL 515
#define SYS_IOPORT_READ 521
#define SYS_IOPORT_WRITE 522
#define SYS_SHUTDOWN 523
#define SYS_REBOOT 524
#define SYS_DISK_MOUNT 530
#define SYS_DISK_UMOUNT 531
#define SYS_DISK_FORMAT 532
#define SYS_DISK_INFO 533
#define SYS_UNLINK 534
#define SYS_RMDIR 535
#define SYS_MKDIR 536
#define SYS_RENAME 537
#define SYS_DISK_READ_RAW 540
#define SYS_DISK_WRITE_RAW 541
#define SYS_DISK_PARTITION 542
#define SYS_DISK_MKFS_FAT32 543
#define SYS_DISK_LIST_PARTS 544
#define SYS_DISK_BIOS_INSTALL 545
#define SYS_LIST_MOUNTS 546
#define SYS_STATVFS 547
static inline int64_t
__syscall6(uint64_t nr, uint64_t a1, uint64_t a2, uint64_t a3,
uint64_t a4, uint64_t a5, uint64_t a6)
{
int64_t ret;
register uint64_t r10 asm("r10") = a4;
register uint64_t r8 asm("r8") = a5;
register uint64_t r9 asm("r9") = a6;
asm volatile ("syscall"
: "=a"(ret)
: "0"(nr), "D"(a1), "S"(a2), "d"(a3), "r"(r10), "r"(r8), "r"(r9)
: "rcx", "r11", "memory");
return ret;
}
#define syscall0(n) __syscall6((n), 0, 0, 0, 0, 0, 0)
#define syscall1(n,a) __syscall6((n), (uint64_t)(a), 0, 0, 0, 0, 0)
#define syscall2(n,a,b) __syscall6((n), (uint64_t)(a), (uint64_t)(b), 0, 0, 0, 0)
#define syscall3(n,a,b,c) __syscall6((n), (uint64_t)(a), (uint64_t)(b), (uint64_t)(c), 0, 0, 0)
#define syscall4(n,a,b,c,d) __syscall6((n), (uint64_t)(a), (uint64_t)(b), (uint64_t)(c), (uint64_t)(d), 0, 0)
#define syscall5(n,a,b,c,d,e) __syscall6((n), (uint64_t)(a), (uint64_t)(b), (uint64_t)(c), (uint64_t)(d), (uint64_t)(e), 0)
#define syscall6(n,a,b,c,d,e,f) __syscall6((n), (uint64_t)(a), (uint64_t)(b), (uint64_t)(c), (uint64_t)(d), (uint64_t)(e), (uint64_t)(f))
#endif
+19
View File
@@ -0,0 +1,19 @@
#ifndef _SYS_TIME_H
#define _SYS_TIME_H
#include <sys/types.h>
#include <time.h>
struct timeval {
time_t tv_sec;
long tv_usec;
};
struct timezone {
int tz_minuteswest;
int tz_dsttime;
};
int gettimeofday(struct timeval *tv, struct timezone *tz);
#endif
+18
View File
@@ -0,0 +1,18 @@
#ifndef _SYS_TYPES_H
#define _SYS_TYPES_H
#include <stdint.h>
#include <stddef.h>
typedef int32_t pid_t;
typedef uint32_t uid_t;
typedef uint32_t gid_t;
typedef int64_t off_t;
typedef uint64_t ino_t;
typedef uint32_t mode_t;
typedef uint32_t dev_t;
typedef uint64_t blkcnt_t;
typedef uint64_t blksize_t;
typedef int64_t time_t;
#endif
+67
View File
@@ -0,0 +1,67 @@
#ifndef _SYS_UCONTEXT_H
#define _SYS_UCONTEXT_H
#include <stdint.h>
#include <signal.h>
typedef unsigned long greg_t;
#define NGREG 23
typedef greg_t gregset_t[NGREG];
enum {
REG_R8 = 0,
REG_R9 = 1,
REG_R10 = 2,
REG_R11 = 3,
REG_R12 = 4,
REG_R13 = 5,
REG_R14 = 6,
REG_R15 = 7,
REG_RDI = 8,
REG_RSI = 9,
REG_RBP = 10,
REG_RBX = 11,
REG_RDX = 12,
REG_RAX = 13,
REG_RCX = 14,
REG_RSP = 15,
REG_RIP = 16,
REG_EFL = 17,
REG_CSGSFS = 18,
REG_ERR = 19,
REG_TRAPNO = 20,
REG_OLDMASK = 21,
REG_CR2 = 22,
};
typedef struct {
uint16_t cwd;
uint16_t swd;
uint16_t ftw;
uint16_t fop;
uint64_t rip;
uint64_t rdp;
uint32_t mxcsr;
uint32_t mxcr_mask;
uint32_t st_space[32];
uint32_t xmm_space[64];
uint32_t padding[24];
} fpregset_t;
typedef struct {
gregset_t gregs;
fpregset_t *fpregs;
uint64_t __reserved[8];
} mcontext_t;
typedef struct ucontext {
uint64_t uc_flags;
struct ucontext *uc_link;
stack_t uc_stack;
mcontext_t uc_mcontext;
sigset_t uc_sigmask;
} ucontext_t;
#endif
+16
View File
@@ -0,0 +1,16 @@
#ifndef _SYS_UTSNAME_H
#define _SYS_UTSNAME_H
#define _UTSNAME_LENGTH 65
struct utsname {
char sysname[_UTSNAME_LENGTH];
char nodename[_UTSNAME_LENGTH];
char release[_UTSNAME_LENGTH];
char version[_UTSNAME_LENGTH];
char machine[_UTSNAME_LENGTH];
};
int uname(struct utsname *buf);
#endif
+19
View File
@@ -0,0 +1,19 @@
#ifndef _SYS_WAIT_H
#define _SYS_WAIT_H
#include <sys/types.h>
#define WNOHANG 0x1
#define WUNTRACED 0x2
#define WIFEXITED(s) (((s) & 0x7F) == 0)
#define WEXITSTATUS(s) (((s) >> 8) & 0xFF)
#define WIFSIGNALED(s) (((s) & 0x7F) != 0 && ((s) & 0x7F) != 0x7F)
#define WTERMSIG(s) ((s) & 0x7F)
#define WIFSTOPPED(s) (((s) & 0xFF) == 0x7F)
#define WSTOPSIG(s) (((s) >> 8) & 0xFF)
pid_t wait(int *status);
pid_t waitpid(pid_t pid, int *status, int options);
#endif
+58
View File
@@ -0,0 +1,58 @@
#ifndef _TERMIOS_H
#define _TERMIOS_H
#include <stdint.h>
typedef unsigned int tcflag_t;
typedef unsigned char cc_t;
typedef unsigned int speed_t;
#define NCCS 32
struct termios {
tcflag_t c_iflag;
tcflag_t c_oflag;
tcflag_t c_cflag;
tcflag_t c_lflag;
cc_t c_cc[NCCS];
};
#define BRKINT 0x0001
#define ICRNL 0x0002
#define INPCK 0x0004
#define ISTRIP 0x0008
#define IXON 0x0010
#define OPOST 0x0001
#define CS8 0x0030
#define ISIG 0x0001
#define ICANON 0x0002
#define ECHO 0x0008
#define ECHOE 0x0010
#define ECHOK 0x0020
#define ECHONL 0x0040
#define IEXTEN 0x0100
#define VMIN 6
#define VTIME 5
#define VINTR 0
#define VQUIT 1
#define VERASE 2
#define VKILL 3
#define VEOF 4
#define TCSANOW 0
#define TCSADRAIN 1
#define TCSAFLUSH 2
#define TCGETS 0x5401
#define TCSETS 0x5402
#define TCSETSW 0x5403
#define TCSETSF 0x5404
int tcgetattr(int fd, struct termios *t);
int tcsetattr(int fd, int optional_actions, const struct termios *t);
#endif
+47
View File
@@ -0,0 +1,47 @@
#ifndef _TIME_H
#define _TIME_H
#include <stddef.h>
#include <sys/types.h>
typedef long clock_t;
#define CLOCKS_PER_SEC 1000000L
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
struct timespec {
time_t tv_sec;
long tv_nsec;
};
time_t time(time_t *t);
clock_t clock(void);
struct tm *localtime(const time_t *t);
struct tm *gmtime(const time_t *t);
time_t mktime(struct tm *tm);
size_t strftime(char *s, size_t max, const char *fmt, const struct tm *tm);
char *asctime(const struct tm *tm);
char *ctime(const time_t *t);
int nanosleep(const struct timespec *req, struct timespec *rem);
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 1
int clock_gettime(int clk, struct timespec *tp);
static inline long difftime_l(time_t a, time_t b) { return (long)(a - b); }
#endif
+68
View File
@@ -0,0 +1,68 @@
#ifndef _UNISTD_H
#define _UNISTD_H
#include <stddef.h>
#include <sys/types.h>
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#define F_OK 0
#define X_OK 1
#define W_OK 2
#define R_OK 4
ssize_t read(int fd, void *buf, size_t n);
ssize_t write(int fd, const void *buf, size_t n);
int close(int fd);
off_t lseek(int fd, off_t off, int whence);
int dup(int fd);
int dup2(int oldfd, int newfd);
int pipe(int fds[2]);
int unlink(const char *path);
int rmdir(const char *path);
int access(const char *path, int mode);
int chdir(const char *path);
int fchdir(int fd);
char *getcwd(char *buf, size_t size);
int symlink(const char *target, const char *linkpath);
ssize_t readlink(const char *path, char *buf, size_t bufsiz);
pid_t getpid(void);
pid_t getppid(void);
uid_t getuid(void);
gid_t getgid(void);
int setuid(uid_t uid);
int setgid(gid_t gid);
pid_t fork(void);
int execve(const char *path, char *const argv[], char *const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
void _exit(int status) __attribute__((noreturn));
unsigned int sleep(unsigned int sec);
int usleep(unsigned int usec);
void *sbrk(intptr_t increment);
int brk(void *addr);
void sched_yield_cervus(void);
int sched_yield(void);
int isatty(int fd);
long pathconf(const char *path, int name);
long fpathconf(int fd, int name);
extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;
int getopt(int argc, char *const argv[], const char *optstring);
#endif
@@ -0,0 +1 @@
Hello from sysroot!