From 7d356b1ddab0150b7347a5952e3bff2a04704fd4 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Thu, 13 Jul 2017 20:09:53 +0200 Subject: added an exit function added Linux syscall stubs for exit and write adapted all tests added a printf test made stdio work on Linux or AbaOs syscalls --- src/Makefile | 5 +-- src/libc/stdio.c | 93 ++++++++++++++++++++++++++++++++++++++++++------------- src/libc/stdio.h | 4 +++ src/libc/stdlib.c | 20 +++++++++++- src/libc/stdlib.h | 3 +- 5 files changed, 100 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index 4192fd1..b4ceff1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,7 @@ CC := gcc -INCLUDES = -DOS_ABAOS -I. -Ilibc -Ihardware -Idrivers -Idrivers/hdi -Idrivers/hdi/ps2 -Idrivers/video -Ikernel -Igui -CFLAGS := -std=c99 -m32 -march=i486 -ffreestanding -O0 -g -Werror $(INCLUDES) +DEFINES = -DOS_ABAOS +INCLUDES = -I. -Ilibc -Ihardware -Idrivers -Idrivers/hdi -Idrivers/hdi/ps2 -Idrivers/video -Ikernel -Igui +CFLAGS := -std=c99 -m32 -march=i486 -ffreestanding -O0 -g -Werror $(INCLUDES) $(DEFINES) LD := ld NASMFLAGS := -f elf32 NASM := nasm diff --git a/src/libc/stdio.c b/src/libc/stdio.c index 07a047e..631cfbb 100644 --- a/src/libc/stdio.c +++ b/src/libc/stdio.c @@ -3,17 +3,70 @@ #include "stdlib.h" #include "string.h" +#ifdef OS_ABAOS +#include "kernel.h" +#endif + +#ifdef OS_ABAOS console_t *stdio_console = NULL; +#endif -int puts( const char *s ) +static void print_string( const char *s ); +static void print_char( const char c ); +static void print_newline( void ); + +#ifdef OS_ABAOS +static void print_string( const char *s ) { if( stdio_console == NULL ) { - return EOF; + kernel_panic( "stdio_console not set!" ); } - + console_put_string( stdio_console, s ); +} + +static void print_char( const char c ) +{ + if( stdio_console == NULL ) { + kernel_panic( "stdio_console not set!" ); + } + + console_put_char( stdio_console, c ); +} + +static void print_newline( void ) +{ + if( stdio_console == NULL ) { + kernel_panic( "stdio_console not set!" ); + } + console_put_newline( stdio_console ); - +} +#endif + +#ifdef OS_LINUX +static void print_string( const char *s ) +{ + syscall3( __NR_write, STDOUT_FILENO, (long)s, strlen( s ) ); +} + +static void print_char( const char c ) +{ + char s[2]; s[0] = c; + syscall3( __NR_write, STDOUT_FILENO, (long)s, 1 ); +} + +static void print_newline( void ) +{ + syscall3( __NR_write, STDOUT_FILENO, (long)"\n", 1 ); +} +#endif + +int puts( const char *s ) +{ + print_string( s ); + print_newline( ); + return 1; } @@ -32,35 +85,31 @@ int vprintf( const char *format, va_list args ) { const char *s = format; int n = 0; - - if( stdio_console == NULL ) { - return -1; - } while( *s != '\0' ) { switch( *s ) { case '\n': - console_put_newline( stdio_console ); + print_newline( ); n++; break; case '%': s++; if( *s == '\0' ) { - console_put_string( stdio_console, "" ); - console_put_newline( stdio_console ); + print_string( "" ); + print_newline( ); return -1; } switch( *s ) { case '%': - console_put_char( stdio_console, '%' ); + print_char( '%' ); break; case 'X': { char buf[19]; itoa( va_arg( args, int ), (char *)buf, 16 ); - console_put_string( stdio_console, buf ); + print_string( buf ); n += strlen( buf ); } break; @@ -68,30 +117,30 @@ int vprintf( const char *format, va_list args ) case 'd': { char buf[19]; itoa( va_arg( args, int ), (char *)buf, 10 ); - console_put_string( stdio_console, buf ); + print_string( buf ); n += strlen( buf ); } break; case 'c': - console_put_char( stdio_console, va_arg( args, int ) ); + print_char( va_arg( args, int ) ); break; case 's': - console_put_string( stdio_console, va_arg( args, const char * ) ); + print_string( va_arg( args, const char * ) ); break; default: - console_put_string( stdio_console, "" ); - console_put_newline( stdio_console ); + print_string( "" ); + print_newline( ); } break; default: - console_put_char( stdio_console, *s ); + print_char( *s ); n++; } s++; @@ -100,7 +149,9 @@ int vprintf( const char *format, va_list args ) return n; } +#ifdef OS_ABAOS void __stdio_set_console( console_t *console ) { stdio_console = console; } +#endif diff --git a/src/libc/stdio.h b/src/libc/stdio.h index 24632f6..33d7946 100644 --- a/src/libc/stdio.h +++ b/src/libc/stdio.h @@ -9,6 +9,10 @@ #include "console.h" #endif +#ifdef OS_LINUX +#include "kernel_stub.h" +#endif + #define EOF (-1) int puts( const char *s ); diff --git a/src/libc/stdlib.c b/src/libc/stdlib.c index d7d4331..b4c2d84 100644 --- a/src/libc/stdlib.c +++ b/src/libc/stdlib.c @@ -48,11 +48,29 @@ char *itoa( int v, char *s, int base ) return s; } -void abort( void ) +void __attribute__( ( noreturn ) ) exit( int status ) +{ +#ifdef OS_ABAOS + // TODO: this should be done on process level, terminating + // the process (by signalling SIGABRT for instance) + kernel_panic( "exited with exit code %d", status ); +#endif +#ifdef OS_LINUX + syscall1( __NR_exit, status ); +#endif + + // make gcc happy ("error: ‘noreturn’ function does return") + for( ;; ); +} + +void __attribute__( ( noreturn ) ) abort( void ) { // TODO: this should be done on process level, terminating // the process (by signalling SIGABRT for instance) kernel_panic( "aborted" ); + + // make gcc happy ("error: ‘noreturn’ function does return") + for( ;; ); } // TODO: we should have a global memory manager and one per diff --git a/src/libc/stdlib.h b/src/libc/stdlib.h index 9ff28a3..5b2521f 100644 --- a/src/libc/stdlib.h +++ b/src/libc/stdlib.h @@ -7,7 +7,8 @@ char *itoa( int v, char *s, int base ); -void abort( void ); +void __attribute__( (noreturn ) ) exit( int status ); +void __attribute__( (noreturn ) ) abort( void ); void *malloc( size_t size ); void free( void *p ); -- cgit v1.2.3-54-g00ecf