Kernel - Imported userland strtoi file (and other minor changes)
[tpg/acess2.git] / KernelLand / Kernel / libc.c
index 08ebc62..a5f87e4 100644 (file)
@@ -49,6 +49,7 @@ EXPORT(tolower);
 
 EXPORT(strucmp);
 EXPORT(strchr);
+EXPORT(strrchr);
 EXPORT(strpos);
 EXPORT(strlen);
 EXPORT(strcpy);
@@ -66,15 +67,10 @@ EXPORT(CheckString);
 EXPORT(CheckMem);
 
 // === CODE ===
-/**
- * \brief Convert a string into an integer
- */
-int atoi(const char *string)
-{
-       int ret = 0;
-       ParseInt(string, &ret);
-       return ret;
-}
+// - Import userland stroi.c file
+#define _LIB_H_
+#include "../../Usermode/Libraries/libc.so_src/strtoi.c"
+
 int ParseInt(const char *string, int *Val)
 {
         int    ret = 0;
@@ -183,7 +179,14 @@ void itoa(char *buf, Uint64 num, int base, int minLength, char pad)
 /**
  * \brief Append a character the the vsnprintf output
  */
-#define PUTCH(c)       _putch(c)
+#define PUTCH(ch)      do { \
+               if(pos < __maxlen) { \
+                       if(__s) __s[pos] = ch; \
+               } else { \
+                       (void)ch;\
+               } \
+               pos ++; \
+       } while(0)
 #define GETVAL()       do {\
        if(isLongLong)  val = va_arg(args, Uint64);\
        else    val = va_arg(args, unsigned int);\
@@ -202,17 +205,6 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
        size_t  pos = 0;
        // Flags
         int    bPadLeft = 0;
-       
-       auto void _putch(char ch);
-
-       void _putch(char ch)
-       {
-               if(pos < __maxlen)
-               {
-                       if(__s) __s[pos] = ch;
-               }
-               pos ++;
-       }
 
        while((c = *__format++) != 0)
        {
@@ -229,8 +221,14 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
                if(c == 'p') {
                        Uint    ptr = va_arg(args, Uint);
                        PUTCH('*');     PUTCH('0');     PUTCH('x');
-                       for( len = BITS/4; len --; )
-                               PUTCH( cUCDIGITS[ (ptr>>(len*4))&15 ] );
+                       for( len = BITS/4; len -- && ((ptr>>(len*4))&15) == 0; )
+                               ;
+                       len ++;
+                       if( len == 0 )
+                               PUTCH( '0' );
+                       else
+                               while( len -- )
+                                       PUTCH( cUCDIGITS[ (ptr>>(len*4))&15 ] );
                        continue ;
                }
                
@@ -360,7 +358,7 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
                        if(!p)          p = "(null)";
                        len = strlen(p);
                        if( !bPadLeft ) while(len++ < minSize)  PUTCH(pad);
-                       while(*p && precision--)        PUTCH(*p++);
+                       while(*p && precision--) { PUTCH(*p); p++;} 
                        if( bPadLeft )  while(len++ < minSize)  PUTCH(pad);
                        break;
                
@@ -368,7 +366,10 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
                        p = va_arg(args, char*);
                        if( !CheckMem(p, minSize) )     continue;       // No #PFs please
                        if(!p)  goto printString;
-                       while(minSize--)        PUTCH(*p++);
+                       while(minSize--) {
+                               PUTCH(*p);
+                               p ++;
+                       }
                        break;
                
                // Single Character
@@ -529,6 +530,15 @@ char *strchr(const char *__s, int __c)
        return NULL;
 }
 
+char *strrchr(const char *__s, int __c)
+{
+       size_t ofs = strlen(__s);
+       while(--ofs && __s[ofs] != __c);
+       if( __s[ofs] == __c )
+               return (char*)__s + ofs;
+       return NULL;
+}
+
 /**
  * \fn int strpos(const char *Str, char Ch)
  * \brief Search a string for an ascii character
@@ -732,7 +742,8 @@ void *memmove(void *__dest, const void *__src, size_t len)
        
 }
 
-// NOTE: Strictly not libc, but lib.c is used by userland code too
+// NOTE: Strictly not libc, but lib.c is used by userland code too and hence these two
+// can't be in it.
 /**
  * \name Memory Validation
  * \{

UCC git Repository :: git.ucc.asn.au