diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2017-06-10 21:26:24 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2017-06-10 21:26:24 +0200 |
commit | d6d1bdfefafff50b7b6d15d218c0a188570be541 (patch) | |
tree | 15ee8de727d0be5d126efda146b2879de0a72773 /src/libc | |
parent | eea5bf4b859eb56c5772c58ca54937a90a10e7ee (diff) | |
download | abaos-d6d1bdfefafff50b7b6d15d218c0a188570be541.tar.gz abaos-d6d1bdfefafff50b7b6d15d218c0a188570be541.tar.bz2 |
some big renames into subdirs of aspects
updated README
removed size_t in sys/types.h and sys/types.h itself, size_t is in stddef.h
Diffstat (limited to 'src/libc')
-rw-r--r-- | src/libc/limits.h | 7 | ||||
-rw-r--r-- | src/libc/setjmp.asm | 44 | ||||
-rw-r--r-- | src/libc/setjmp.h | 24 | ||||
-rw-r--r-- | src/libc/stddef.h | 10 | ||||
-rw-r--r-- | src/libc/stdio.c | 106 | ||||
-rw-r--r-- | src/libc/stdio.h | 20 | ||||
-rw-r--r-- | src/libc/stdlib.c | 48 | ||||
-rw-r--r-- | src/libc/stdlib.h | 6 | ||||
-rw-r--r-- | src/libc/string.c | 75 | ||||
-rw-r--r-- | src/libc/string.h | 12 |
10 files changed, 352 insertions, 0 deletions
diff --git a/src/libc/limits.h b/src/libc/limits.h new file mode 100644 index 0000000..1fc4e37 --- /dev/null +++ b/src/libc/limits.h @@ -0,0 +1,7 @@ +#ifndef LIMITS_H +#define LIMITS_H + +#define INT_MAX 2147483647 +#define INT_MIN -2147483648 + +#endif // LIMITS_H diff --git a/src/libc/setjmp.asm b/src/libc/setjmp.asm new file mode 100644 index 0000000..a20a0af --- /dev/null +++ b/src/libc/setjmp.asm @@ -0,0 +1,44 @@ +[bits 32] + +global setjmp +global longjmp + +;typedef struct { +; uint32_t ebx; +; uint32_t esp; +; uint32_t ebp; +; uint32_t esi; +; uint32_t edi; +; uint32_t eip; +;} jmp_buf[1]; + +; int setjmp(jmp_buf env) +setjmp: + mov eax, [esp+4] ; the adress of the jump buffer + mov [eax], ebx ; safe registers + mov [eax+4], esp + mov [eax+8], ebp + mov [eax+12], esi + mov [eax+16], edi + mov edx, [esp] ; get return address from the stack (pushed here by call) + mov [eax+20], edx + mov eax, 0 ; indicate that we come from setjmp, not from a longjmp + ret + +; void longjmp(jmp_buf env, int value); +longjmp: + mov eax, [esp+4] ; the address of the jump buffer + mov ecx, [esp+8] ; the return value for setjmp + mov ebx, [eax] ; restore registers + mov esp, [eax+4] + mov ebp, [eax+8] + mov esi, [eax+12] + mov edi, [eax+16] + mov edx, [eax+20] ; get jump address and store it on the stack for 'ret' + mov [esp], edx + mov eax, ecx ; get return value + cmp eax, 0 + jnz .return ; non zero, ok, return it + mov eax, 1 ; setjmp called with 0, not good, return 1 +.return: + ret diff --git a/src/libc/setjmp.h b/src/libc/setjmp.h new file mode 100644 index 0000000..5f987f1 --- /dev/null +++ b/src/libc/setjmp.h @@ -0,0 +1,24 @@ +#ifndef SETJMP_H +#define SETJMP_H + +#include <stdint.h> + +// C99 states this should be an array so we can address it without +// the & operator, see prototypes of setjmp and longjmp +// eax, ecx and edx are scratch registers, save the others +// also store the jump address (eip) +typedef struct { + uint32_t ebx; + uint32_t esp; + uint32_t ebp; + uint32_t esi; + uint32_t edi; + uint32_t eip; +} jmp_buf[1]; + +int setjmp( jmp_buf env ); + +void longjmp( jmp_buf env, int val ); + +#endif // SETJMP_H + diff --git a/src/libc/stddef.h b/src/libc/stddef.h new file mode 100644 index 0000000..4333661 --- /dev/null +++ b/src/libc/stddef.h @@ -0,0 +1,10 @@ +#ifndef STDDEF_H +#define STDDEF_H + +#include <stdint.h> + +#define NULL ( (void *)0 ) + +typedef uint32_t size_t; + +#endif /* STDDEF_H */ diff --git a/src/libc/stdio.c b/src/libc/stdio.c new file mode 100644 index 0000000..1d53f7a --- /dev/null +++ b/src/libc/stdio.c @@ -0,0 +1,106 @@ +#include "stdio.h" +#include "stddef.h" +#include "stdlib.h" +#include "string.h" + +console_t *global_console = NULL; + +int puts( const char *s ) +{ + if( global_console == NULL ) { + return EOF; + } + + console_put_string( global_console, s ); + console_put_newline( global_console ); + + return 1; +} + +int printf( const char *format, ... ) +{ + va_list args; + + va_start( args, format ); + int res = vprintf( format, args ); + va_end( args ); + + return res; +} + +int vprintf( const char *format, va_list args ) +{ + const char *s = format; + int n = 0; + + if( global_console == NULL ) { + return -1; + } + + while( *s != '\0' ) { + switch( *s ) { + case '\n': + console_put_newline( global_console ); + n++; + break; + + case '%': + s++; + if( *s == '\0' ) { + console_put_string( global_console, "<truncated % found at end of format string>" ); + console_put_newline( global_console ); + return -1; + } + + switch( *s ) { + case '%': + console_put_char( global_console, '%' ); + break; + + case 'X': { + char buf[19]; + itoa( va_arg( args, int ), (char *)buf, 16 ); + console_put_string( global_console, buf ); + n += strlen( buf ); + } + break; + + case 'd': { + char buf[19]; + itoa( va_arg( args, int ), (char *)buf, 10 ); + console_put_string( global_console, buf ); + n += strlen( buf ); + } + break; + + case 'c': + console_put_char( global_console, va_arg( args, int ) ); + break; + + case 's': + console_put_string( global_console, va_arg( args, const char * ) ); + break; + + default: + console_put_string( global_console, "<illegal format string %" ); + console_put_char( global_console, *s ); + console_put_string( global_console, ">" ); + console_put_newline( global_console ); + } + + break; + + default: + console_put_char( global_console, *s ); + n++; + } + s++; + } + + return n; +} + +void stdio_set_console( console_t *console ) +{ + global_console = console; +} diff --git a/src/libc/stdio.h b/src/libc/stdio.h new file mode 100644 index 0000000..bc7fac0 --- /dev/null +++ b/src/libc/stdio.h @@ -0,0 +1,20 @@ +#ifndef STDIO_H +#define STDIO_H + +#include <stdarg.h> + +#include "stddef.h" + +#include "console.h" + +#define EOF (-1) + +extern console_t *global_console; + +int puts( const char *s ); +int printf( const char *format, ... ); +int vprintf( const char *format, va_list args ); + +void stdio_set_console( console_t *console ); + +#endif //STDIO_H diff --git a/src/libc/stdlib.c b/src/libc/stdlib.c new file mode 100644 index 0000000..46bdce8 --- /dev/null +++ b/src/libc/stdlib.c @@ -0,0 +1,48 @@ +#include <stdbool.h> +#include "string.h" +#include "stdlib.h" +#include "stddef.h" + +static void strreverse( char *s ) +{ + char *end = s + strlen( s ) - 1; + + while( s < end ) { + // XOR swap; + *s ^= *end; + *end ^= *s; + *s ^= *end; + s++; + end--; + } +} + +char *itoa( int v, char *s, int base ) +{ + static char digit[] = "0123456789ABCDEF"; + bool sign = false; + char *p = s; + + if( base < 2 || base > 16 ) { + return NULL; + } + + if( v < 0 ) { + v = -v; + sign = true; + } + + do { + *p++ = digit[v % base]; + } while( ( v /= base ) > 0 ); + + if( sign ) { + *p++ = '-'; + } + *p = '\0'; + + strreverse( s ); + + return s; +} + diff --git a/src/libc/stdlib.h b/src/libc/stdlib.h new file mode 100644 index 0000000..331012e --- /dev/null +++ b/src/libc/stdlib.h @@ -0,0 +1,6 @@ +#ifndef STDLIB_H +#define STDLIB_H + +char *itoa( int v, char *s, int base ); + +#endif // STDLIB_H diff --git a/src/libc/string.c b/src/libc/string.c new file mode 100644 index 0000000..8d4f876 --- /dev/null +++ b/src/libc/string.c @@ -0,0 +1,75 @@ +#include "string.h" + +void *memset( void *s, int c, size_t n ) +{ + for( size_t i = 0; i < n; i++ ) { + ((char *)s)[i] = c; + } + + return s; +} + +void *memmove( void *d, const void *s, size_t n ) +{ + const char *ss = (const char *)s; + char *dd = (char *)d; + + if( dd == ss ) return d; + + if( dd <= ss ) { + while( n > 0 ) { + *dd++ = *ss++; + n--; + } + } else { + dd += n; + ss += n; + while( n > 0 ) { + *--dd = *--ss; + n--; + } + } + + return d; +} + +size_t strlen( const char *s ) +{ + size_t len; + const char *p = s; + + for( len = 0; *p; len++, p++ ); + + return len; +} + +int strcmp( const char *s1, const char *s2 ) +{ + while( *s1 && *s2 && *s1 == *s2 ) { + s1++; + s2++; + } + + return *s1 - *s2; +} + +size_t strlcpy( char *d, const char *s, size_t n ) +{ + size_t len = 0; + + while( len < n && s[len] != '\0' ) { + d[len] = s[len]; + len++; + } + d[len] = '\0'; + + while( s[len] != '\0' ) { + len++; + } + + if( len >= n ) { + d[n-1] = '\0'; + } + + return len; +} diff --git a/src/libc/string.h b/src/libc/string.h new file mode 100644 index 0000000..476c8c0 --- /dev/null +++ b/src/libc/string.h @@ -0,0 +1,12 @@ +#ifndef STRING_H +#define STRING_H + +#include "stddef.h" + +void *memset( void *s, int c, size_t n ); +void *memmove( void *d, const void *s, size_t n ); +size_t strlen( const char *s ); +int strcmp( const char *s1, const char *s2 ); +size_t strlcpy( char *d, const char *s, size_t n ); + +#endif // STRING_H |