libc.h (4509B)
1 /* 2 * libc.h - core definitions and function prototypes 3 * Copyright (C) 2026. All rights reserved. 4 */ 5 6 #ifndef LIBC_H 7 #define LIBC_H 8 9 /* base types */ 10 typedef unsigned long size_t; 11 typedef unsigned long ulong_t; 12 typedef unsigned long nfds_t; 13 typedef long ssize_t; 14 typedef long off_t; 15 16 #ifndef NULL 17 #define NULL ((void*)0) 18 #endif 19 20 #define stdin 0 21 #define stdout 1 22 #define stderr 2 23 24 #define STDIN_FILENO 0 25 #define STDOUT_FILENO 1 26 #define STDERR_FILENO 2 27 28 struct linux_dirent64 { 29 unsigned long d_ino; 30 long d_off; 31 unsigned short d_reclen; 32 unsigned char d_type; 33 char d_name[]; 34 }; 35 36 struct timespec { 37 long tv_sec; 38 long tv_nsec; 39 }; 40 41 struct stat { 42 unsigned long st_dev; 43 unsigned long st_ino; 44 unsigned long st_nlink; 45 unsigned int st_mode; 46 unsigned int st_uid; 47 unsigned int st_gid; 48 unsigned int __pad0; 49 unsigned long st_rdev; 50 long st_size; 51 long st_blksize; 52 long st_blocks; 53 struct timespec st_atim; 54 struct timespec st_mtim; 55 struct timespec st_ctim; 56 long __reserved[3]; 57 }; 58 59 struct pollfd { 60 int fd; 61 short events; 62 short revents; 63 }; 64 65 struct block_header { 66 size_t size; 67 size_t requested_size; 68 int is_free; 69 struct block_header *next; 70 } __attribute__((aligned(16))); 71 72 typedef struct block_header block_header_t; 73 74 struct utimbuf { 75 long actime; 76 long modtime; 77 }; 78 79 80 #define SIGCHLD 17 81 82 /* errno values used by the libc implementation */ 83 #define ENOENT 2 84 #define EACCES 13 85 #define ENAMETOOLONG 36 86 87 /* open flags */ 88 #define O_RDONLY 00 89 #define O_WRONLY 01 90 #define O_RDWR 02 91 #define O_CREAT 0100 92 #define O_EXCL 0x80 93 #define O_DIRECTORY 0200000 94 95 /* syscall numbers */ 96 #define SYS_read 0 97 #define SYS_write 1 98 #define SYS_open 2 99 #define SYS_close 3 100 #define SYS_stat 4 101 #define SYS_fstat 5 102 #define SYS_lstat 6 103 #define SYS_poll 7 104 #define SYS_lseek 8 105 #define SYS_mmap 9 106 #define SYS_munmap 11 107 #define SYS_brk 12 108 #define SYS_nanosleep 35 109 #define SYS_clone 56 110 #define SYS_fork 57 111 #define SYS_execve 59 112 #define SYS_exit 60 113 #define SYS_wait4 61 114 #define SYS_getcwd 79 115 #define SYS_chdir 80 116 #define SYS_mkdir 83 117 #define SYS_rmdir 84 118 #define SYS_unlink 87 119 #define SYS_utime 132 120 #define SYS_mount 165 121 #define SYS_getdents64 217 122 123 /* mmap flags */ 124 #define PROT_READ 0x1 125 #define PROT_WRITE 0x2 126 #define MAP_PRIVATE 0x02 127 #define MAP_ANONYMOUS 0x20 128 #define MAP_FAILED ((void*)-1) 129 130 /* variables */ 131 extern char **environ; 132 133 /* syscall */ 134 long syscall(long num, long a1, long a2, long a3, long a4, long a5, long a6); 135 136 /* io */ 137 ssize_t read(int fd, void *buf, size_t count); 138 ssize_t write(int fd, const void *buf, size_t count); 139 int open(const char *filename, int flags, int mode); 140 int close(int fd); 141 142 /* proc */ 143 void _exit(int status); 144 void exit(int status); 145 int execve(const char *path, char *const argv[], char *const envp[]); 146 int execvp(const char *file, char *const argv[]); 147 int fork(void); 148 int waitpid(int pid, int *status, int options); 149 int nanosleep(const struct timespec *req_time, struct timespec *rem); 150 unsigned int sleep(unsigned int sec); 151 152 /* fs */ 153 int mount(const char *src, const char *trg, const char *fstype, ulong_t flags, 154 const void *data); 155 int chdir(const char *path); 156 int mkdir(const char *path, unsigned int mode); 157 int rmdir(const char *path); 158 int unlink(const char *path); 159 char *getcwd(char *buf, size_t size); 160 ssize_t getdents64(int fd, struct linux_dirent64 *dirp, size_t count); 161 int stat(const char *pathname, struct stat *statbuf); 162 int fstat(int fd, struct stat *statbuf); 163 int lstat(const char *pathname, struct stat *statbuf); 164 int poll(struct pollfd *fds, nfds_t nfds, int timeout); 165 off_t lseek(int fd, off_t offset, int whence); 166 167 /* memory */ 168 void* mmap(void *addr, size_t length, int prot, int flags, int fd, long offset); 169 int munmap(void *addr, size_t length); 170 void* memset(void *s, int c, size_t n); 171 void* memcpy(void *dest, const void *src, size_t n); 172 173 /* alloc */ 174 void* malloc(size_t size); 175 void free(void *ptr); 176 void* calloc(size_t nmemb, size_t size); 177 void* realloc(void *ptr, size_t size); 178 179 /* utime */ 180 int utime(const char *path, const struct utimbuf *times); 181 182 /* string functions */ 183 size_t strlen(const char *str); 184 char *strchr(const char *s, int c); 185 int strcmp(const char *s1, const char *s2); 186 char* strtok(char *str, const char *delim); 187 188 /* errno */ 189 extern int errno; 190 191 #endif /* LIBC_H */