OpenCores
URL https://opencores.org/ocsvn/mlite/mlite/trunk

Subversion Repositories mlite

[/] [mlite/] [trunk/] [kernel/] [libc.c] - Diff between revs 144 and 149

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 144 Rev 149
Line 87... Line 87...
   }
   }
   return 0;
   return 0;
}
}
 
 
 
 
char *strstr(char *string, char *find)
char *strstr(const char *string, const char *find)
{
{
   int i;
   int i;
   for(;;)
   for(;;)
   {
   {
      for(i = 0; string[i] == find[i] && find[i]; ++i) ;
      for(i = 0; string[i] == find[i] && find[i]; ++i) ;
      if(find[i] == 0)
      if(find[i] == 0)
         return string;
         return (char*)string;
      if(*string++ == 0)
      if(*string++ == 0)
         return NULL;
         return NULL;
   }
   }
}
}
 
 
Line 170... Line 170...
   return n>=0 ? n : -n;
   return n>=0 ? n : -n;
}
}
 
 
 
 
static uint32 Rand1=0x1f2bcda3, Rand2=0xdeafbeef, Rand3=0xc5134306;
static uint32 Rand1=0x1f2bcda3, Rand2=0xdeafbeef, Rand3=0xc5134306;
unsigned int rand(void)
int rand(void)
{
{
   int shift;
   int shift;
   Rand1 += 0x13423123 + Rand2;
   Rand1 += 0x13423123 + Rand2;
   Rand2 += 0x2312fdea + Rand3;
   Rand2 += 0x2312fdea + Rand3;
   Rand3 += 0xf2a12de1;
   Rand3 += 0xf2a12de1;
Line 233... Line 233...
{
{
   return strtol(s, NULL, 10);
   return strtol(s, NULL, 10);
}
}
 
 
 
 
void itoa(char *dst, int num, int base, int width)
char *itoa(int num, char *dst, int base)
{
{
   int digit, negate=0, digits, widthSave;
   int digit, negate=0, place;
   char c;
   char c, text[20];
 
 
   if(width == 0)
 
      width = 16;
 
   widthSave = width;
 
   digits = width - 1;
 
   if(base == 10 && num < 0)
   if(base == 10 && num < 0)
   {
   {
      num = -num;
      num = -num;
      negate = 1;
      negate = 1;
   }
   }
   dst[width] = 0;
   text[16] = 0;
   while(width-- > 0)
   for(place = 15; place >= 0; --place)
   {
   {
      if(base == 10)
      if(base == 10)
         digit = num % base;
         digit = num % base;
      else
      else
         digit = (unsigned int)num % (unsigned int)base;
         digit = (unsigned int)num % (unsigned int)base;
      if(base == 10 && num == 0 && dst[width+1] != 0)
      if(num == 0 && place < 15 && base == 10 && negate)
      {
      {
         if(negate)
 
            c = '-';
            c = '-';
         else
 
            c = ' ';
 
         negate = 0;
         negate = 0;
      }
      }
      else if(digit < 10)
      else if(digit < 10)
         c = (char)('0' + digit);
         c = (char)('0' + digit);
      else
      else
         c = (char)('a' + digit - 10);
         c = (char)('a' + digit - 10);
      dst[width] = c;
      text[place] = c;
      if(base == 10)
 
         num /= base;
 
      else
 
         num = (unsigned int)num / (unsigned int)base;
         num = (unsigned int)num / (unsigned int)base;
      if(c != ' ' && c != '0')
      if(num == 0 && negate == 0)
         digits = width;
         break;
   }
   }
   if(widthSave > 15)
   strcpy(dst, text + place);
      strcpy(dst, dst + digits);
   return dst;
}
}
 
 
 
 
int sprintf(char *s, const char *format,
int sprintf(char *s, const char *format,
            int arg0, int arg1, int arg2, int arg3,
            int arg0, int arg1, int arg2, int arg3,
            int arg4, int arg5, int arg6, int arg7)
            int arg4, int arg5, int arg6, int arg7)
{
{
   int argv[8];
   int argv[8];
   int argc=0, width, length;
   int argc=0, width, length;
   char f;
   char f, text[20];
 
 
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
   argv[0] = arg0; argv[1] = arg1; argv[2] = arg2; argv[3] = arg3;
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
   argv[4] = arg4; argv[5] = arg5; argv[6] = arg6; argv[7] = arg7;
 
 
   for(;;)
   for(;;)
Line 312... Line 302...
            if('0' <= f && f <= '9')
            if('0' <= f && f <= '9')
               width = width * 10 + f - '0';
               width = width * 10 + f - '0';
         }
         }
 
 
         if(f == 'd')
         if(f == 'd')
            itoa(s, argv[argc++], 10, width);
         {
 
            memset(s, ' ', width);
 
            itoa(argv[argc++], text, 10);
 
            length = (int)strlen(text);
 
            if(width < length)
 
               width = length;
 
            strcpy(s + width - length, text);
 
         }
         else if(f == 'x' || f == 'f')
         else if(f == 'x' || f == 'f')
            itoa(s, argv[argc++], 16, width);
         {
 
            memset(s, '0', width);
 
            itoa(argv[argc++], text, 16);
 
            length = (int)strlen(text);
 
            if(width < length)
 
               width = length;
 
            strcpy(s + width - length, text);
 
         }
         else if(f == 'c')
         else if(f == 'c')
         {
         {
            *s++ = (char)argv[argc++];
            *s++ = (char)argv[argc++];
            *s = 0;
            *s = 0;
         }
         }
Line 413... Line 417...
            ++s;
            ++s;
         if(*s)
         if(*s)
            ++s;
            ++s;
      }
      }
   }
   }
   return argc;
 
}
}
 
 
 
 
 No newline at end of file
 No newline at end of file
 
void dump(const unsigned char *data, int length)
 
{
 
   int i, index=0, value;
 
   char string[80];
 
   memset(string, 0, sizeof(string));
 
   for(i = 0; i < length; ++i)
 
   {
 
      if((i & 15) == 0)
 
      {
 
         if(strlen(string))
 
            printf("%s\n", string);
 
         printf("%4x ", i);
 
         memset(string, 0, sizeof(string));
 
         index = 0;
 
      }
 
      value = data[i];
 
      printf("%2x ", value);
 
      if(isprint(value))
 
         string[index] = (char)value;
 
      else
 
         string[index] = '.';
 
      ++index;
 
   }
 
   for(; index < 16; ++index)
 
      printf("   ");
 
   printf("%s\n", string);
 
}
 
 
 
 
 
 
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.