/* * This file is part of musl-mod: modified version of Musl libc library. * musl-mod in general and this file specifically are available under * MIT license. See COPYRIGHT file. */ #ifndef COMMON_H #define COMMON_H #include #include extern int main(int argc, char **argv, char **envp); #define NULL ((void *)0) #define _S(s) (s), (sizeof(s) - 1) #define CSTR(s) ((struct str) { (s), (s) + sizeof(s) - 1 }) #define STR(s) ((struct str) { (s), (s) + strzlen(s) }) struct str *debug2str(char *s); struct str { char *start; char *end; }; enum { heap_saves_max = 16 }; extern int errno; extern char *progname; void exit(int code); int chdir(char *path); int read(int fd, char *buf, int len); int write(int fd, char *buf, int len); #define O_RDONLY 0 #define O_DIRECTORY 0200000 int open(char *fname, int flags, int mode); /* * Taken from linux/arch/x86/include/uapi/asm/stat.h * Matches arch/x86_64/bits/stat.h */ struct time { unsigned long sec; unsigned long usec; }; struct stat { unsigned long dev; unsigned long ino; unsigned long nlink; unsigned int mode; unsigned int uid; unsigned int gid; unsigned int __pad0; unsigned long rdev; unsigned long size; unsigned long blksize; unsigned long blocks; struct time atime; struct time mtime; struct time ctime; long __unused[3]; }; int stat(char *fname, struct stat *st); int fstat(int fd, struct stat *st); struct dir { int fd; char *buf; char *cur; char *end; char *path; }; struct dirent { unsigned long inode; unsigned long offset; unsigned short dsize; unsigned char type; char name[1]; }; int opendir(struct dir *dir, char *path); struct dirent *readdir(struct dir *dir); void closedir(struct dir *dir); #define F_OK 0 int access(char *path, int mode); int dup2(int oldfd, int newfd); int pipe(int *fds); int close(int fd); #define MAP_PRIVATE 0x02 #define MAP_ANON 0x20 #define PROT_NONE 0 #define PROT_READ 1 #define PROT_WRITE 2 #define PROT_EXEC 4 void *mmap(char *addr, unsigned long length, int prot, int flags, int fd, unsigned long offset); int munmap(char *addr, int length); #define MREMAP_MAYMOVE 1 void *mremap(void *old_addr, unsigned long old_size, unsigned long new_size, int flags, void *new_addr); int fork(); #define WIFEXITED(s) (!WTERMSIG(s)) #define WEXITSTATUS(s) (((s) >> 8) & 0xff) #define WIFSIGNALED(s) (((s) & 0xffff) -1 < 0xff) #define WTERMSIG(s) ((s) & 0x7f) int wait4(int pid, int *wstatus, int options, void *rusage); int execve(char *path, char **argv, char **envp); struct timeval { unsigned long long sec; unsigned long long msec; }; struct rusage { struct timeval utime; struct timeval stime; long maxrss; long ixrss; long idrss; long isrss; long minflt; long majflt; long nswap; long inblock; long oublock; long msgsnd; long msgrcv; long nsignals; long nvcsw; long nivcsw; }; #define RUSAGE_SELF 0 int getrusage(int who, struct rusage *rusage); void hexit(int code); extern struct iobuf stdout; extern struct iobuf stderr; void print_strz(struct iobuf *buf, char *s); void print_str(struct iobuf *buf, struct str *s); void printf(char *fmt, ...); void eprintf(char *fmt, ...); void evprintf(char *fmt, va_list vl); void error(char *fmt, ...); struct arena; typedef void arena_grow_func(struct arena *a, int size); struct arena { char *start; char *cur; char *end; arena_grow_func *grow; }; struct heap { struct arena a; char *saved[heap_saves_max]; int snum; char *last_alloc; }; extern struct heap heap; void heap_offset_save(); void heap_offset_remove(); void heap_offset_restore(); void *mem_alloc(int size); void *amalloc(struct arena *a, int size); void *malloc(int size); void realloc(void *p, int newsize); int strzlen(char *s); int strzeq(char *s1, char *s2); int streq(struct str *s1, struct str *s2); int strge(struct str *s1, struct str *s2); void flush(struct iobuf *buf); void perror(char *s); void syscall_error(char *fname); void memmove(char *dest, char *src, int len); void memcpy(void *restrict dest, void *restrict src, int len); void memset(void *dest, char c, int len); int memeq(void *m1, void *m2, int len); #endif