Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. #ifndef NULL
  3. #define NULL ((void*)0)
  4. #endif
  5.  
  6. void* memset(void *mem, int c, unsigned size)
  7. {
  8.         unsigned i;
  9.         for ( i = 0; i < size; i++ )
  10.                 *((char *)mem+i) = (char) c;
  11.         return NULL;   
  12. }
  13.  
  14.  
  15. void* memcpy(void *dst, const void *src, unsigned size)
  16. {
  17.         unsigned i;
  18.         for ( i = 0; i < size; i++)
  19.                 *(char *)(dst+i) = *(char *)(src+i);
  20.         return NULL;
  21. }
  22.  
  23.  
  24. int memcmp(const void* buf1, const void* buf2, int count)
  25. {
  26.         int i;
  27.         for (i=0;i<count;i++)
  28.                 {
  29.                 if (*(unsigned char*)buf1<*(unsigned char*)buf2)
  30.                         return -1;
  31.                 if (*(unsigned char*)buf1>*(unsigned char*)buf2)                       
  32.                         return 1;
  33.                 }
  34.         return 0;
  35. }
  36.  
  37. char *strcat(char strDest[], char strSource[])
  38. {
  39.         int i, j;
  40.         i = j = 0;
  41.         while (strDest[i] != '\0') i++;
  42.         while ((strDest[i++] = strSource[j++]) != '\0');
  43.         return strDest;
  44. }
  45.  
  46.  
  47. /*int strcmp(char* string1,char* string2)
  48. {
  49.  
  50.         while (1)
  51.         {
  52.                 if (*string1<*string2)
  53.                         return -1;
  54.                 if (*string1>*string2)
  55.                         return 1;
  56.                 if (*string1=='\0')
  57.                         return 0;
  58.                 string1++;
  59.                 string2++;
  60.         }
  61. }*/
  62.  
  63. int strcmp(const char* s1, const char* s2)
  64. {
  65.     while(*s1 && (*s1==*s2))
  66.         s1++,s2++;
  67.     return *(const unsigned char*)s1-*(const unsigned char*)s2;
  68. }
  69.  
  70. char *strcpy(char strDest[], const char strSource[])
  71. {
  72.         unsigned i;
  73.         i = 0;
  74.         while ((strDest[i] = strSource[i]) != '\0') i++;
  75.         return strDest;
  76. }
  77.  
  78.  
  79. char* strncpy(char *strDest, const char *strSource, unsigned n)
  80. {
  81.         unsigned i;
  82.         if (! n )
  83.                 return strDest;
  84.         i = 0;
  85.         while ((strDest[i] = strSource[i]) != '\0')
  86.                 if ( (n-1) == i )
  87.                         break;
  88.                 else
  89.                         i++;
  90.         return strDest;
  91. }
  92.  
  93.  
  94. int strlen(const char* string)
  95. {
  96.         int i;
  97.         i=0;
  98.         while (*string++) i++;
  99.         return i;
  100. }
  101.  
  102.  
  103.  
  104. char* strchr(const char* string, int c)
  105. {
  106.         while (*string)
  107.         {
  108.                 if (*string==c)
  109.                         return (char*)string;
  110.                 string++;
  111.         }      
  112.         return (char*)0;
  113. }
  114.  
  115.  
  116. char* strrchr(const char* string, int c)
  117. {
  118.         char* last_found;
  119.         while (*string)
  120.         {
  121.                 if (*string==c)
  122.                 {
  123.                         last_found = (char*)string;
  124.                 }
  125.                 string++;
  126.         }      
  127.         return last_found;
  128. }
  129.  
  130.  
  131.  
  132. void _itoa(int i, char *s)
  133. {
  134.         int a, b, c, d;
  135.         a = (i - i%1000)/1000;
  136.         b = (i - i%100)/100 - a*10;
  137.         c = (i - i%10)/10 - a*100 - b*10;
  138.         d = i%10;
  139.         s[0] = a + '0';
  140.         s[1] = b + '0';
  141.         s[2] = c + '0';
  142.         s[3] = d + '0';
  143.         s[4] = 0;
  144. }
  145.  
  146.  
  147.  /* reverse:  ïåðåâîðà÷èâàåì ñòðîêó s íà ìåñòå */
  148. void reverse(char s[])
  149.  {
  150.      int i, j;
  151.      char c;
  152.  
  153.      for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
  154.          c = s[i];
  155.          s[i] = s[j];
  156.          s[j] = c;
  157.      }
  158.  }
  159.  
  160.  
  161.  /* itoa:  êîíâåðòèðóåì n â ñèìâîëû â s */
  162. void itoa(int n, char s[])
  163.  {
  164.      int i, sign;
  165.  
  166.      if ((sign = n) < 0)
  167.          n = -n;
  168.      i = 0;
  169.      do {
  170.          s[i++] = n % 10 + '0';
  171.      } while ((n /= 10) > 0);
  172.      if (sign < 0)
  173.          s[i++] = '-';
  174.      s[i] = '\0';
  175.      reverse(s);
  176.  }
  177.  
  178.  
  179.  
  180. int atoi ( char *s )
  181. {
  182.         int i, n;
  183.  
  184.         n = 0;
  185.         for ( i = 0; s[i]!= '\0'; ++i)
  186.                 if ((s[i]<'0') || (s[i]>'9'))
  187.                         return 0;
  188.                 else
  189.                         n = 10 * n + s[i] - '0';
  190.  
  191.         return n;
  192. }
  193.  
  194.  
  195.