| 1 | /* |
|---|
| 2 | * libcaca |
|---|
| 3 | * libcaca Colour ASCII-Art library |
|---|
| 4 | * Copyright (c) 2006 Sam Hocevar <sam@hocevar.net> |
|---|
| 5 | * 2009 Jean-Yves Lamoureux <jylam@lnxscene.org> |
|---|
| 6 | * All Rights Reserved |
|---|
| 7 | * |
|---|
| 8 | * $Id: kernel.h 4154 2009-12-20 13:33:11Z jylam $ |
|---|
| 9 | * |
|---|
| 10 | * This library is free software. It comes without any warranty, to |
|---|
| 11 | * the extent permitted by applicable law. You can redistribute it |
|---|
| 12 | * and/or modify it under the terms of the Do What The Fuck You Want |
|---|
| 13 | * To Public License, Version 2, as published by Sam Hocevar. See |
|---|
| 14 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
|---|
| 15 | */ |
|---|
| 16 | |
|---|
| 17 | /* |
|---|
| 18 | * This file contains replacement functions for the standard C library |
|---|
| 19 | * that must be used when building libcaca and libcaca into a kernel. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /* Various typedefs -- some are x86-specific */ |
|---|
| 24 | #define CUSTOM_INTTYPES |
|---|
| 25 | |
|---|
| 26 | #define outb(port,value) __asm__ __volatile__ ("outb %%al,%%dx"::"d" (port), "a" (value)); |
|---|
| 27 | #define outbp(port,value) __asm __volatile__ ("outb %%al,%%dx; jmp 1f; 1:"::"d" (port), "a" (value)); |
|---|
| 28 | #define inb(port) ({unsigned char _v; __asm__ __volatile__ ("inb %%dx,%%al" : "=a" (_v) : "d" (port)); _v; }) |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | /* Various defines */ |
|---|
| 32 | #define NULL ((void *)0) |
|---|
| 33 | #define EOF (-1) |
|---|
| 34 | #define BUFSIZ 4096 |
|---|
| 35 | #define RAND_MAX ((unsigned int)0x8000000) |
|---|
| 36 | #define INT_MAX ((int)0x7fffffff) |
|---|
| 37 | #define M_PI 3.14159265358979323846 |
|---|
| 38 | #define __BYTE_ORDER 1 |
|---|
| 39 | #define __BIG_ENDIAN 2 |
|---|
| 40 | |
|---|
| 41 | typedef unsigned char u8; |
|---|
| 42 | typedef unsigned short u16; |
|---|
| 43 | typedef unsigned int u32; |
|---|
| 44 | typedef unsigned long long u64; |
|---|
| 45 | |
|---|
| 46 | #ifndef size_t |
|---|
| 47 | typedef unsigned int size_t; |
|---|
| 48 | #endif |
|---|
| 49 | typedef struct file |
|---|
| 50 | { |
|---|
| 51 | void *mem; |
|---|
| 52 | } FILE; |
|---|
| 53 | |
|---|
| 54 | struct timeval |
|---|
| 55 | { |
|---|
| 56 | int tv_sec; |
|---|
| 57 | int tv_usec; |
|---|
| 58 | }; |
|---|
| 59 | |
|---|
| 60 | struct timezone |
|---|
| 61 | { |
|---|
| 62 | int tz_minuteswest; |
|---|
| 63 | int tz_dsttime; |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | /* stdlib.h functions */ |
|---|
| 67 | void *malloc(size_t size); |
|---|
| 68 | void free(void *ptr); |
|---|
| 69 | void *realloc(void *ptr, size_t size); |
|---|
| 70 | char *getenv(const char *name); |
|---|
| 71 | int rand(void); |
|---|
| 72 | int abs(int j); |
|---|
| 73 | void exit(int status); |
|---|
| 74 | void srand(unsigned int s); |
|---|
| 75 | int atexit(void (*function) (void)); |
|---|
| 76 | FILE *stdin, *stdout, *stderr; |
|---|
| 77 | |
|---|
| 78 | /* string.h functions */ |
|---|
| 79 | void *memset(void *s, int c, size_t n); |
|---|
| 80 | void *memcpy(void *dest, const void *src, size_t n); |
|---|
| 81 | void *memmove(void *dest, const void *src, size_t n); |
|---|
| 82 | size_t strlen(const char *s); |
|---|
| 83 | int strcmp(const char *s1, const char *s2); |
|---|
| 84 | int strcasecmp(const char *s1, const char *s2); |
|---|
| 85 | int memcmp(const void *s1, const void *s2, size_t n); |
|---|
| 86 | char *strdup(const char *s); |
|---|
| 87 | char *strchr(const char *s, int c); |
|---|
| 88 | |
|---|
| 89 | /* stdarg.h functions */ |
|---|
| 90 | typedef void *va_list; |
|---|
| 91 | #define va_start(v,a) v = (void *)((uintptr_t)(&a) + sizeof(a)) |
|---|
| 92 | #define va_end(v) |
|---|
| 93 | int vsnprintf(char *str, size_t size, const char *format, va_list ap); |
|---|
| 94 | /* va_arg */ |
|---|
| 95 | #define args_list char * |
|---|
| 96 | #define _arg_stack_size(type) (((sizeof(type)-1)/sizeof(int)+1)*sizeof(int)) |
|---|
| 97 | #define args_start(ap, fmt) do { \ |
|---|
| 98 | ap = (char *)((unsigned int)&fmt + _arg_stack_size(&fmt)); \ |
|---|
| 99 | } while (0) |
|---|
| 100 | #define args_end(ap) |
|---|
| 101 | #define args_next(ap, type) (((type *)(ap+=_arg_stack_size(type)))[-1]) |
|---|
| 102 | |
|---|
| 103 | /* stdio.h functions */ |
|---|
| 104 | FILE *fopen(const char *path, const char *mode); |
|---|
| 105 | int feof(FILE * stream); |
|---|
| 106 | char *fgets(char *s, int size, FILE * stream); |
|---|
| 107 | size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE * stream); |
|---|
| 108 | int fclose(FILE * fp); |
|---|
| 109 | |
|---|
| 110 | int printf(const char *format, ...); |
|---|
| 111 | |
|---|
| 112 | int fprintf(FILE * stream, const char *format, ...); |
|---|
| 113 | int fflush(FILE * stream); |
|---|
| 114 | int sprintf(char *str, const char *format, ...); |
|---|
| 115 | int sscanf(const char *str, const char *format, ...); |
|---|
| 116 | void itoa(int n, char s[]); |
|---|
| 117 | |
|---|
| 118 | void clearscreen(void); |
|---|
| 119 | |
|---|
| 120 | /* unistd.h functions */ |
|---|
| 121 | void usleep(unsigned long usec); |
|---|
| 122 | void sleep(unsigned long sec); |
|---|
| 123 | int getpid(void); |
|---|
| 124 | |
|---|
| 125 | /* time.h functions */ |
|---|
| 126 | int gettimeofday(struct timeval *tv, struct timezone *tz); |
|---|
| 127 | int time(void *); |
|---|
| 128 | |
|---|
| 129 | /* math.h functions */ |
|---|
| 130 | double cos(double x); |
|---|
| 131 | double sin(double x); |
|---|
| 132 | double sqrt(double x); |
|---|
| 133 | |
|---|
| 134 | /* errno.h functions */ |
|---|
| 135 | #define ENOENT 2 /* No such file or directory */ |
|---|
| 136 | #define ENOMEM 12 /* Out of memory */ |
|---|
| 137 | #define EBUSY 16 /* Device or resource busy */ |
|---|
| 138 | #define ENODEV 19 /* No such device */ |
|---|
| 139 | #define EINVAL 22 /* Invalid argument */ |
|---|
| 140 | #define ENOTTY 25 /* Not a typewriter */ |
|---|
| 141 | #define ENOSYS 38 /* Function not implemented */ |
|---|
| 142 | extern int errno; |
|---|
| 143 | |
|---|
| 144 | /* arpa/inet.h functions */ |
|---|
| 145 | unsigned int htonl(unsigned int hostlong); |
|---|
| 146 | unsigned short htons(unsigned short hostlong); |
|---|
| 147 | |
|---|
| 148 | void print(char *str); |
|---|