#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/times.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <setjmp.h>
#include <termios.h>

// Globals
char **environ = NULL;

extern void console_write(const void* p, size_t len);

// Stubs
int fstat(int fd, struct stat *buf) { return 0; }
int lstat(const char *path, struct stat *buf) { return 0; }
int stat(const char *path, struct stat *buf) { return 0; }

int sigemptyset(sigset_t *set) { return 0; }
int sigaddset(sigset_t *set, int signum) { return 0; }
int sigprocmask(int how, const sigset_t *set, sigset_t *oldset) { return 0; }
int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact) { return 0; }
int sigsuspend(const sigset_t *mask) { return -1; }
int kill(pid_t pid, int sig) { return 0; }
unsigned int alarm(unsigned int seconds) { return 0; }

off_t lseek(int fd, off_t offset, int whence) { return 0; }
// ssize_t read(int fd, void *buf, size_t count) { return 0; } // In libc.nim
// ssize_t write(int fd, const void *buf, size_t count) { return 0; } // In clib.c

// int open(const char *pathname, int flags, ...) { return -1; } // In libc.nim
// int close(int fd) { return 0; } // In libc.nim
int pipe(int pipefd[2]) { return -1; }
int dup2(int oldfd, int newfd) { return newfd; }
int fcntl(int fd, int cmd, ...) { return 0; }
int ioctl(int fd, unsigned long request, ...) { return 0; }

// int execve(const char *pathname, char *const argv[], char *const envp[]) { return -1; } // In clib.c
// pid_t fork(void) { return -1; } // In clib.c
pid_t waitpid(pid_t pid, int *status, int options) { return -1; }

pid_t getpid(void) { return 1; }
pid_t getppid(void) { return 0; }
uid_t getuid(void) { return 0; }
uid_t geteuid(void) { return 0; }
gid_t getgid(void) { return 0; }
gid_t getegid(void) { return 0; }
pid_t getpgrp(void) { return 1; }
int setuid(uid_t uid) { return 0; }
int setgid(gid_t gid) { return 0; }
int seteuid(uid_t uid) { return 0; }
int setegid(gid_t gid) { return 0; }
int setpgid(pid_t pid, pid_t pgid) { return 0; }

int tcgetattr(int fd, struct termios *termios_p) { return 0; }
int tcsetattr(int fd, int optional_actions, const struct termios *termios_p) { return 0; }
pid_t tcgetpgrp(int fd) { return 1; }
int tcsetpgrp(int fd, pid_t pgrp) { return 0; }
int isatty(int fd) { return 1; }

int access(const char *pathname, int mode) { return -1; }
int unlink(const char *pathname) { return -1; }
int rename(const char *oldpath, const char *newpath) { return -1; }
int rmdir(const char *pathname) { return -1; }
int chdir(const char *path) { return 0; }
char *getcwd(char *buf, size_t size) { 
    if (size > 1) { buf[0]='/'; buf[1]='\0'; return buf; }
    return NULL;
}
int mkdir(const char *pathname, mode_t mode) { return -1; }

time_t time(time_t *tloc) { return 0; }
clock_t times(struct tms *buf) { return 0; }
unsigned int sleep(unsigned int seconds) { return 0; }

DIR *opendir(const char *name) { return NULL; }
struct dirent *readdir(DIR *dirp) { return NULL; }
int closedir(DIR *dirp) { return 0; }

ssize_t readlink(const char *pathname, char *buf, size_t objsiz) { return -1; }
struct passwd *getpwnam(const char *name) { return NULL; }

// qsort
void qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *)) {
    if (nmemb < 2 || size == 0) return;
    char *b = (char *)base;
    char *tmp = malloc(size);
    for (size_t i = 0; i < nmemb - 1; i++) {
        for (size_t j = 0; j < nmemb - i - 1; j++) {
            if (compar(b + j * size, b + (j + 1) * size) > 0) {
                // swap
                memcpy(tmp, b + j * size, size);
                memcpy(b + j * size, b + (j + 1) * size, size);
                memcpy(b + (j + 1) * size, tmp, size);
            }
        }
    }
    free(tmp);
}

// strstr
char *strstr(const char *haystack, const char *needle) {
    if (!*needle) return (char *)haystack;
    for (char *p = (char *)haystack; *p; p++) {
        if (*p == *needle) {
            char *h = p, *n = (char *)needle;
            while (*h && *n && *h == *n) { h++; n++; }
            if (!*n) return p;
        }
    }
    return NULL;
}

mode_t umask(mode_t mask) { return 0; }

int c_ulimit(const char **wp) { return 0; }

void longjmp(jmp_buf env, int val) { while(1); }
int setjmp(jmp_buf env) { return 0; }

void exit(int status) { while(1); }
