summaryrefslogtreecommitdiff
path: root/src/libc/stdio.c
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2017-06-10 21:26:24 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2017-06-10 21:26:24 +0200
commitd6d1bdfefafff50b7b6d15d218c0a188570be541 (patch)
tree15ee8de727d0be5d126efda146b2879de0a72773 /src/libc/stdio.c
parenteea5bf4b859eb56c5772c58ca54937a90a10e7ee (diff)
downloadabaos-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/stdio.c')
-rw-r--r--src/libc/stdio.c106
1 files changed, 106 insertions, 0 deletions
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;
+}