X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Flib.c;h=27995b0732f6b0b93c77b57b2bfbe58bd057afc9;hb=f3d0d7fcf0496a63625c92e5ab95471e202e958e;hp=4c6ab188b54193f54092e9cee3f0124b132e0472;hpb=09e044c2338f194d0f179aff6358bf9fd0739768;p=tpg%2Facess2.git diff --git a/Kernel/lib.c b/Kernel/lib.c index 4c6ab188..27995b07 100644 --- a/Kernel/lib.c +++ b/Kernel/lib.c @@ -14,11 +14,14 @@ const short DAYS_BEFORE[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 3 #define UNIX_TO_2K ((30*365*3600*24) + (7*3600*24)) //Normal years + leap years // === PROTOTYPES === +#if 0 int atoi(const char *string); 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); int sprintf(char *__s, const char *__format, ...); +#endif int tolower(int c); +#if 0 int strucmp(const char *Str1, const char *Str2); char *strchr(const char *__s, int __c); int strpos(const char *Str, char Ch); @@ -33,7 +36,6 @@ char **str_split(const char *__str, char __ch); int strpos8(const char *str, Uint32 Search); int ReadUTF8(Uint8 *str, Uint32 *Val); int WriteUTF8(Uint8 *str, Uint32 Val); - int DivUp(int num, int dem); Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year); int rand(void); @@ -45,6 +47,7 @@ Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year); int ModUtil_SetIdent(char *Dest, char *Value); int UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString); +#endif // === EXPORTS === EXPORT(atoi); @@ -196,9 +199,9 @@ 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 - char *p = NULL; + const char *p = NULL; int isLongLong = 0; Uint64 val; size_t pos = 0; @@ -222,13 +225,13 @@ 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'); + itoa(tmpBuf, ptr, 16, BITS/4, '0'); p = tmpBuf; - itoa(p, ptr, 16, BITS/4, '0'); goto printString; } // - Padding Side Flag - if(c == '+') { + if(c == '-') { bPadLeft = 1; c = *__format++; } @@ -257,7 +260,28 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args) } } else - minSize = 1; + minSize = 0; + + // - 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; @@ -285,30 +309,30 @@ int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args) PUTCH('-'); val = -(Sint32)val; } - itoa(p, val, 10, minSize, pad); + itoa(tmpBuf, val, 10, minSize, pad); goto printString; case 'u': GETVAL(); - itoa(p, val, 10, minSize, pad); + itoa(tmpBuf, val, 10, minSize, pad); goto printString; case 'X': if(BITS == 64) isLongLong = 1; // TODO: Handle non-x86 64-bit archs GETVAL(); - itoa(p, val, 16, minSize, pad); + itoa(tmpBuf, val, 16, minSize, pad); goto printString; case 'x': GETVAL(); - itoa(p, val, 16, minSize, pad); + itoa(tmpBuf, val, 16, minSize, pad); goto printString; case 'o': GETVAL(); - itoa(p, val, 8, minSize, pad); + itoa(tmpBuf, val, 8, minSize, pad); goto printString; case 'b': GETVAL(); - itoa(p, val, 2, minSize, pad); + itoa(tmpBuf, val, 2, minSize, pad); goto printString; case 'B': //Boolean @@ -320,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; @@ -418,10 +442,11 @@ int strpos(const char *Str, char Ch) * \fn Uint8 ByteSum(void *Ptr, int Size) * \brief Adds the bytes in a memory region and returns the sum */ -Uint8 ByteSum(void *Ptr, int Size) +Uint8 ByteSum(const void *Ptr, int Size) { Uint8 sum = 0; - while(Size--) sum += *(Uint8*)Ptr++; + const Uint8 *data = Ptr; + while(Size--) sum += *(data++); return sum; } @@ -593,7 +618,7 @@ int strpos8(const char *str, Uint32 Search) * \fn int ReadUTF8(Uint8 *str, Uint32 *Val) * \brief Read a UTF-8 character from a string */ -int ReadUTF8(Uint8 *str, Uint32 *Val) +int ReadUTF8(const Uint8 *str, Uint32 *Val) { *Val = 0xFFFD; // Assume invalid character @@ -756,7 +781,7 @@ int rand(void) /** * \brief Checks if a string resides fully in valid memory */ -int CheckString(char *String) +int CheckString(const char *String) { if( !MM_GetPhysAddr( (tVAddr)String ) ) return 0; @@ -792,7 +817,7 @@ int CheckString(char *String) /** * \brief Check if a sized memory region is valid memory */ -int CheckMem(void *Mem, int NumBytes) +int CheckMem(const void *Mem, int NumBytes) { tVAddr addr = (tVAddr)Mem; @@ -826,7 +851,7 @@ int CheckMem(void *Mem, int NumBytes) * \brief Search a string array for \a Needle * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP */ -int ModUtil_LookupString(char **Array, char *Needle) +int ModUtil_LookupString(const char **Array, const char *Needle) { int i; if( !CheckString(Needle) ) return -1; @@ -837,7 +862,7 @@ int ModUtil_LookupString(char **Array, char *Needle) return -1; } -int ModUtil_SetIdent(char *Dest, char *Value) +int ModUtil_SetIdent(char *Dest, const char *Value) { if( !CheckMem(Dest, 32) ) return -1; strncpy(Dest, Value, 32);