Index: trunk/libs/mini-libc/stdio.c
===================================================================
--- trunk/libs/mini-libc/stdio.c	(revision 443)
+++ trunk/libs/mini-libc/stdio.c	(revision 444)
@@ -22,377 +22,294 @@
  */
 
-#include <shared_syscalls.h>
-#include <hal_user.h>
 #include <stdio.h>
-#include <stdlib.h>
-
-#define  reg_t     int
-
-///////////// POSIX standard system calls ////////////////////////////////////
-
-///////////////////////
-void exit( int status )
-{
-    hal_user_syscall( SYS_EXIT,
-                      (reg_t)status, 0, 0, 0 );
+#include <stdarg.h>
+#include <unistd.h>
+#include <almos-mkh.h>
+//////////////////////////////////////////
+static int xprintf( char         * string,
+                    unsigned int   length,
+                    const char   * format, 
+                    va_list      * args ) 
+{
+    unsigned int ps = 0;    // write index to the string buffer
+
+#define TO_STREAM(x) do { string[ps] = (x); ps++; if(ps==length) return -1; } while(0);
+
+xprintf_text:
+
+    while ( *format != 0 ) 
+    {
+
+        if (*format == '%')   // copy argument to string
+        {
+            format++;
+            goto xprintf_arguments;
+        }
+        else                  // copy one char to string
+        {
+            TO_STREAM( *format );
+            format++;
+        }
+    }
+
+    return ps;
+
+xprintf_arguments:
+
+    {
+        char              buf[30];    // buffer to display one number
+        char *            pbuf;       // pointer on first char to display
+        unsigned int      len = 0;    // number of char to display
+        static const char HexaTab[] = "0123456789ABCDEF";
+        unsigned int      i;
+        
+        // Ignore fields width and precision 
+        for ( ; (*format >= '0' && *format <= '9') || (*format == '.') ; format++ );
+
+        switch (*format) 
+        {
+            case ('c'):             // char conversion 
+            {
+                int val = va_arg( *args, int );
+                buf[0] = val;
+                pbuf   = buf;
+                len    = 1;
+                break;
+            }
+            case ('d'):             // decimal signed integer
+            {
+                int val = va_arg( *args, int );
+                if (val < 0) 
+                {
+                    TO_STREAM( '-' );
+                    val = -val;
+                }
+                for(i = 0; i < 10; i++) 
+                {
+
+                    buf[9 - i] = HexaTab[val % 10];
+                    if (!(val /= 10)) break;
+                }
+                len =  i + 1;
+                pbuf = &buf[9 - i];
+                break;
+            }
+            case ('u'):             // decimal unsigned integer
+            {
+                unsigned int val = va_arg( *args, unsigned int );
+                for(i = 0; i < 10; i++) 
+                {
+                    buf[9 - i] = HexaTab[val % 10];
+                    if (!(val /= 10)) break;
+                }
+                len =  i + 1;
+                pbuf = &buf[9 - i];
+                break;
+            }
+            case ('x'):             // 32 bits hexadecimal 
+            case ('l'):             // 64 bits hexadecimal 
+            {
+                unsigned int       imax;
+                unsigned long long val;
+                
+                if ( *format == 'l' )   // 64 bits
+                {
+                    val = va_arg( *args, unsigned long long);
+                    imax = 16;
+                }
+                else                    // 32 bits
+                {
+                    val = va_arg( *args, unsigned int);
+                    imax = 8;
+                }
+                
+                TO_STREAM( '0' );
+                TO_STREAM( 'x' );
+                
+                for(i = 0; i < imax; i++) 
+                {
+                    buf[(imax-1) - i] = HexaTab[val % 16];
+                    if (!(val /= 16))  break;
+                }
+                len =  i + 1;
+                pbuf = &buf[(imax-1) - i];
+                break;
+            }
+            case ('s'):             /* string */
+            {
+                char* str = va_arg( *args, char* );
+                while (str[len]) { len++; }
+                pbuf = str;
+                break;
+            }
+/*
+            case ('f'):             // IEEE754 64 bits
+                                    // integer part : up to 10 decimal digits
+                                    // decimal part : 9 decimal digits
+            {
+                union
+                {
+                    double d;
+                    unsigned long long ull;
+                } val;
+                
+                val.d = va_arg( *args, double );
+                
+                unsigned long long mantisse;
+                mantisse = val.ull & 0xFFFFFFFFFFFFFULL;    // mantisse
+                
+                unsigned int exp;
+                exp = (unsigned int)((val.ull & 0x7FF0000000000000ULL) >> 52); // exp 
+
+                if (exp == 0x7FF) // special values
+                {
+                    if (mantisse & 0xFFFFFFFFFFFFFULL)  // Not a Number
+                    {
+                        buf[0] = 'N';
+                        buf[1] = 'a';
+                        buf[2] = 'N';
+                        len = 3;
+                        pbuf = buf;
+                    }
+                    else                              // infinite
+                    {
+                        // inf
+                        buf[0] = (val.ull & 0x8000000000000000ULL) ? '-' : '+';
+                        buf[1] = 'i';
+                        buf[2] = 'n';
+                        buf[3] = 'f';
+                        len = 4;
+                        pbuf = buf;
+                    }
+                    break;
+                }
+
+                // display sign & analyse overflow 
+                unsigned int overflow = 0;
+                if (val.ull & 0x8000000000000000ULL)  // negative 
+                {
+                    TO_STREAM( '-' );
+                    val.d = val.d * -1;
+                    if( val.d < -9999999999.0) overflow = 1;
+                }
+                else                                  // positive
+                {
+                    TO_STREAM( '+' );
+                    if( val.d > 9999999999.0) overflow = 1;
+                }
+                
+                // check overflow caused by the 10.9 format
+                if ( overflow )    
+                {
+                    buf[0] = 'o';
+                    buf[1] = 'v';
+                    buf[2] = 'r';
+                    len = 3;
+                    pbuf = buf;
+                    break;
+                }
+
+                // compute integer & decimal parts
+                unsigned int intp;                  // integer part
+                unsigned int decp;                  // decimal part
+                intp = (unsigned int)val.d;      
+                val.d -= (double)intp;
+                decp = (unsigned int)(val.d * 1000000000);
+                
+                // display decimal value in 10.9 format
+                for(i = 0; i < 10; i++) 
+                {
+                    buf[9 - i] = HexaTab[intp % 10];
+                    if (!(intp /= 10)) break;
+                }
+                pbuf = &buf[9 - i];
+                len = i+11;
+                buf[10] = '.';
+                for(i = 0; i < 9; i++)
+                {
+                    buf[19 - i] = HexaTab[decp % 10];
+                    decp /= 10;
+                }
+                break;
+            }
+*/                    
+            default:       // unsupported argument type
+            {
+                return -1;
+            }
+        }  // end switch on  argument type
+
+        format++;
+
+        // copy argument to string
+        for( i = 0 ; i < len ; i++ )
+        {
+            TO_STREAM( pbuf[i] );
+        }
+        
+        goto xprintf_text;
+    }
+} // end xprintf()
+
+//////////////////////////////////////
+int printf( const char * format, ... )
+{
+    char      string[4096];
+    va_list   args;
+    int       count;
+    
+    va_start( args, format );
+    count = xprintf( string , 4095 , format , &args ); 
+    va_end( args );
+
+    if ( count == -1 ) 
+    {
+        display_string( "stdlib : xprintf failure" );
+        return -1;
+    }
+    else 
+    {
+        string[count] = 0;
+        return write( 1 , &string , count + 1 );
+    }
 }
 
-/////////////////////////////////
-int munmap( void         * addr,
-            unsigned int   size )
-{
-    return hal_user_syscall( SYS_MUNMAP,
-                             (reg_t)addr,
-                             (reg_t)size, 0, 0 );
+/////////////
+int getchar()
+{
+    char byte;
+
+    if ( read( 0 , &byte , 1 ) != 1 ) return 0;
+    else                              return (int)byte; 
 }
 
-////////////////////////////////
-int open( const char * pathname,
-          int          flags,
-          int          mode )
-{
-    return hal_user_syscall( SYS_OPEN,
-                             (reg_t)pathname,
-                             (reg_t)flags,
-                             (reg_t)mode, 0 );
+////////////////////
+int putchar( int c )
+{
+    char byte = (char)c;
+
+    if( write( 1 , &byte , 1 ) != 1 ) return 0;
+    else                              return c; 
 }
 
-///////////////////////////////
-void * mmap( void       * addr,
-             unsigned int length,
-             int          prot,
-             int          flags,
-             int          fd,
-             unsigned int offset )
-{
-    mmap_attr_t attr;
-    attr.addr   = addr;
-    attr.length = length;
-    attr.prot   = prot;
-    attr.flags  = flags;
-    attr.fdid   = fd;
-    attr.offset = offset;
-
-    if( hal_user_syscall( SYS_MMAP,
-                          (reg_t)&attr, 0, 0, 0 ) ) return NULL;
-    else                                            return attr.addr;
+///////////////////////////////////////
+int snprintf( char           * string,
+              unsigned int     length,
+              const char     * format, ... )
+{
+    va_list   args;
+    int       count;
+
+    va_start( args, format );
+    count = xprintf( string , length , format , &args ); 
+    va_end( args );
+
+    if( count < length ) string[count] = 0;
+
+    return count;
 }
 
-//////////////////////////
-int read( int          fd,
-          void       * buf,
-          unsigned int count )
-{
-    return hal_user_syscall( SYS_READ,
-                             (reg_t)fd,
-                             (reg_t)buf,
-                             (reg_t)count, 0 );
-}
-
-///////////////////////////
-int write( int          fd,
-           const void * buf,
-           unsigned int count )
-{
-    return hal_user_syscall( SYS_WRITE,
-                             (reg_t)fd,
-                             (reg_t)buf,
-                             (reg_t)count, 0 );
-}
-
-///////////////////////////
-int lseek( int          fd,
-           unsigned int offset,
-           int          whence )
-{
-    return hal_user_syscall( SYS_LSEEK,
-                             (reg_t)fd,
-                             (reg_t)offset,
-                             (reg_t)whence, 0 );
-}
-
-///////////////////
-int close( int fd )
-{
-    return hal_user_syscall( SYS_CLOSE,
-                             (reg_t)fd, 0, 0, 0 );
-}
-
-///////////////////////////////////
-int unlink( const char * pathname )
-{
-    return hal_user_syscall( SYS_UNLINK,
-                             (reg_t)pathname, 0, 0, 0 );
-}
-
-/////////////////////
-int pipe( int fd[2] )
-{
-    return -1;
-}
-
-//////////////////////////////////
-int chdir( const char * pathname )
-{
-    return hal_user_syscall( SYS_CHDIR,
-                             (reg_t)pathname, 0, 0, 0 );
-}
-
-/////////////////////////////////
-int mkdir( const char * pathname,
-           int          mode )
-{
-    return hal_user_syscall( SYS_MKDIR,
-                             (reg_t)pathname,
-                             (reg_t)mode, 0, 0 );
-}
-
-//////////////////////////////////
-int mkfifo( const char * pathname,
-            int          mode )
-{
-    return hal_user_syscall( SYS_MKFIFO,
-                             (reg_t)pathname,
-                             (reg_t)mode, 0, 0 );
-}
-
-//////////////////////////////////////
-DIR * opendir( const char * pathname )
-{
-    DIR   * dirp; 
-    int     error;
-    error = hal_user_syscall( SYS_OPENDIR,
-                              (reg_t)pathname,
-                              (reg_t)&dirp, 0, 0 );
-    if( error ) return NULL;
-    else        return dirp;
-}
-
-/////////////////////////////////////
-struct dirent * readdir( DIR * dirp )
-{
-    struct dirent * dentp;
-    int             error;
-    error = hal_user_syscall( SYS_READDIR,
-                              (reg_t)dirp,
-                              (reg_t)&dentp, 0, 0 );
-    if( error ) return NULL;
-    else        return dentp;
-}
-
-//////////////////////////
-int closedir( DIR * dirp )
-{
-    return hal_user_syscall( SYS_CLOSEDIR,
-                             (reg_t)dirp, 0, 0, 0 );
-}
-
-/////////////////////////////
-int getcwd( char       * buf,
-            unsigned int bytes )
-{
-    return hal_user_syscall( SYS_GETCWD,
-                             (reg_t)buf,
-                             (reg_t)bytes, 0, 0 );
-}
-
-////////////////////////////
-int rmdir( char * pathname )
-{
-    return hal_user_syscall( SYS_RMDIR,
-                             (reg_t)pathname, 0, 0, 0 );
-} 
-
-/////////////////////////////////
-int utls( unsigned int operation,
-          unsigned int value )
-{
-    return hal_user_syscall( SYS_UTLS,
-                             (reg_t)operation,
-                             (reg_t)value, 0, 0 );
-}
-
-///////////////////////////////
-int chmod( char     * pathname,
-           unsigned int   rights )
-{
-    return hal_user_syscall( SYS_CHMOD,
-                             (reg_t)pathname,
-                             (reg_t)rights, 0, 0 );
-}
-
-/////////////////////////////////
-int signal( unsigned int   sigid,
-            void         * handler )
-{
-    return hal_user_syscall( SYS_SIGNAL,
-                             (reg_t)sigid,
-                             (reg_t)handler, 0, 0 );
-}
-
-///////////////////////////////////////
-int gettimeofday( struct timeval  * tv,
-                  struct timezone * tz )
-{
-    return hal_user_syscall( SYS_SIGNAL,
-                             (reg_t)tv,
-                             (reg_t)tz, 0, 0 );
-}
-
-///////////////////////////
-int kill( unsigned int pid,
-          unsigned int sig_id )
-{
-    return hal_user_syscall( SYS_KILL,
-                             (reg_t)pid,
-                             (reg_t)sig_id, 0, 0 );
-}
-
-////////////
-int getpid()
-{
-    return hal_user_syscall( SYS_GETPID, 0, 0, 0, 0 );
-}
-
-//////////
-int fork()
-{
-    return hal_user_syscall( SYS_FORK, 0, 0, 0, 0 );
-}
-
-///////////////////////////
-int exec( char  * pathname,
-          char ** argv,
-          char ** envp )
-{
-    return hal_user_syscall( SYS_EXEC,
-                             (reg_t)pathname,
-                             (reg_t)argv,
-                             (reg_t)envp, 0 );
-}
-
-/////////////////////////////////
-int stat( const char  * pathname,
-          struct stat * stat )
-{
-    return hal_user_syscall( SYS_STAT,
-                             (reg_t)pathname,
-                             (reg_t)stat, 0, 0 );
-}
-
-////////////////////////
-int wait( int * status )
-{
-    return hal_user_syscall( SYS_WAIT,
-                             (reg_t)status, 0, 0, 0 );
-}
-
-
-
-/////////////     Non standard system calls ////////////////////////////////////
-
-
-//////////////////////////
-int fg( unsigned int pid )
-{
-    return hal_user_syscall( SYS_FG,
-                             (reg_t)pid, 0, 0, 0 );
-}
-
-//////////////////////////////////////
-int get_config( unsigned int * x_size,
-                unsigned int * y_size,
-                unsigned int * ncores )
-{
-    return hal_user_syscall( SYS_GET_CONFIG,
-                             (reg_t)x_size,
-                             (reg_t)y_size,
-                             (reg_t)ncores, 0 );
-}
-
-/////////////////////////////////
-int get_core( unsigned int * cxy,
-              unsigned int * lid )
-{
-    return hal_user_syscall( SYS_GET_CORE,
-                             (reg_t)cxy,
-                             (reg_t)lid, 0, 0 );
-}
-
-////////////////////////////////////
-void display_string( char * string )
-{
-    hal_user_syscall( SYS_DISPLAY,
-                      DISPLAY_STRING,
-                      (reg_t)string, 0, 0 );
-}
-
-//////////////////////////////////
-int display_vmm( unsigned int cxy,
-                 unsigned int pid )
-{
-    return hal_user_syscall( SYS_DISPLAY,
-                             DISPLAY_VMM,
-                             (reg_t)cxy,
-                             (reg_t)pid, 0 );
-} 
-
-////////////////////////////////////
-int display_sched( unsigned int cxy,
-                   unsigned int lid )
-{
-    return hal_user_syscall( SYS_DISPLAY,
-                             DISPLAY_SCHED,
-                             (reg_t)cxy,
-                             (reg_t)lid, 0 );
-} 
-
-/////////////////////////////////////////////////
-int display_cluster_processes( unsigned int cxy )
-{
-    return hal_user_syscall( SYS_DISPLAY,
-                             DISPLAY_CLUSTER_PROCESSES,
-                             (reg_t)cxy, 0, 0 );
-} 
-
-///////////////////
-int display_chdev()
-{
-    return hal_user_syscall( SYS_DISPLAY,
-                             DISPLAY_CHDEV, 0, 0, 0 );
-} 
-
-/////////////////
-int display_vfs()
-{
-    return hal_user_syscall( SYS_DISPLAY,
-                             DISPLAY_VFS, 0, 0, 0 );
-} 
-
-////////////////////////////////////////////////
-int display_txt_processes( unsigned int txt_id )
-{
-    return hal_user_syscall( SYS_DISPLAY,
-                             DISPLAY_TXT_PROCESSES,
-                             (reg_t)txt_id, 0, 0 );
-} 
-
-///////////////////////////////////////////
-int get_cycle( unsigned long long * cycle )
-{
-    return hal_user_syscall( SYS_GET_CYCLE,
-                             (reg_t)cycle, 0, 0, 0 );
-}
-
-///////////////////////////////
-int trace( unsigned int active,
-           unsigned int cxy, 
-           unsigned int lid )
-{
-    return hal_user_syscall( SYS_TRACE,
-                             (reg_t)active,
-                             (reg_t)cxy,
-                             (reg_t)lid, 0 );
-}
-
-
+
+
+
+
