Fixed vsnprintf
authorJohn Hodge <[email protected]>
Tue, 1 Mar 2011 06:40:01 +0000 (14:40 +0800)
committerJohn Hodge <[email protected]>
Tue, 1 Mar 2011 06:40:01 +0000 (14:40 +0800)
- '-' is padding, not '+'
- Added precision

Kernel/lib.c
Kernel/logging.c

index 3e57d00..5f277b8 100644 (file)
@@ -199,7 +199,7 @@ void itoa(char *buf, Uint64 num, int base, int minLength, char pad)
 int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
 {
        char    c, pad = ' ';
-        int    minSize = 0, len;
+        int    minSize = 0, precision = -1, len;
        char    tmpBuf[34];     // For Integers
        const char      *p = NULL;
         int    isLongLong = 0;
@@ -231,7 +231,7 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
                }
                
                // - Padding Side Flag
-               if(c == '+') {
+               if(c == '-') {
                        bPadLeft = 1;
                        c = *__format++;
                }
@@ -262,6 +262,27 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
                else
                        minSize = 1;
                
+               // - Precision
+               precision = -1;
+               if( c == '.' ) {
+                       c = *__format++;
+                       
+                       if(c == '*') {  // Dynamic length
+                               precision = va_arg(args, unsigned int);
+                               c = *__format++;
+                       }
+                       else if('1' <= c && c <= '9')
+                       {
+                               precision = 0;
+                               while('0' <= c && c <= '9')
+                               {
+                                       precision *= 10;
+                                       precision += c - '0';
+                                       c = *__format++;
+                               }
+                       }
+               }
+               
                // - Default, Long or LongLong?
                isLongLong = 0;
                if(c == 'l')    // Long is actually the default on x86
@@ -323,12 +344,12 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
                // String - Null Terminated Array
                case 's':
                        p = va_arg(args, char*);        // Get Argument
-                       if( !CheckString(p) )   continue;       // Avoid #PFs  
+                       if( !CheckString(p) )   p = "(inval)";  // Avoid #PFs  
                printString:
                        if(!p)          p = "(null)";
                        len = strlen(p);
                        if( !bPadLeft ) while(len++ < minSize)  PUTCH(pad);
-                       while(*p)       PUTCH(*p++);
+                       while(*p && precision--)        PUTCH(*p++);
                        if( bPadLeft )  while(len++ < minSize)  PUTCH(pad);
                        break;
                
index 8132e5c..c00746a 100644 (file)
@@ -113,7 +113,7 @@ void Log_AddEvent(const char *Ident, int Level, const char *Format, va_list Args
                char    newData[ LOG_HDR_LEN + len + 2 + 1 ];
                char    _ident[9];
                strncpy(_ident, Ident, 9);
-               sprintf( newData, "%014lli%s [%+8s] ",
+               sprintf( newData, "%014lli%s [%-8s] ",
                        ent->Time, csaLevelCodes[Level], Ident);
                strcpy( newData + LOG_HDR_LEN, ent->Data );
                strcpy( newData + LOG_HDR_LEN + len, "\r\n" );
@@ -153,7 +153,7 @@ void Log_AddEvent(const char *Ident, int Level, const char *Format, va_list Args
 void Log_Int_PrintMessage(tLogEntry *Entry)
 {
        SHORTLOCK( &glLogOutput );
-       LogF("%s%014lli%s [%+8s] %s",
+       LogF("%s%014lli%s [%-8s] %s",
                csaLevelColours[Entry->Level],
                Entry->Time,
                csaLevelCodes[Entry->Level],

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