voron

experimental ARM OS
git clone git://git.2f30.org/voron
Log | Files | Refs | README | LICENSE

string.c (866B)


      1 #include <inttypes.h>
      2 #include <string.h>
      3 
      4 void *
      5 memset(void *s, int c, size_t n)
      6 {
      7 	unsigned char *us = s;
      8 
      9 	while (n) {
     10 		*us = c;
     11 		us++;
     12 		n--;
     13 	}
     14 
     15 	return s;
     16 }
     17 
     18 void *
     19 memcpy(void *s1, const void *s2, size_t n)
     20 {
     21 	unsigned char *us1 = s1;
     22 	const unsigned char *us2 = s2;
     23 
     24 	while (n) {
     25 		*us1 = *us2;
     26 		us1++;
     27 		us2++;
     28 		n--;
     29 	}
     30 
     31 	return s1;
     32 }
     33 
     34 void *
     35 memmove(void *dest, const void *src, size_t count)
     36 {
     37 	char *tmp;
     38 	const char *s;
     39 
     40 	if (dest <= src) {
     41 		tmp = dest;
     42 		s = src;
     43 		while (count--)
     44 			*tmp++ = *s++;
     45 	} else {
     46 		tmp = dest;
     47 		tmp += count;
     48 		s = src;
     49 		s += count;
     50 		while (count--)
     51 			*--tmp = *--s;
     52 	}
     53 	return dest;
     54 }
     55 
     56 int
     57 memcmp(const void *s1, const void *s2, size_t n)
     58 {
     59 	const unsigned char *us1, *us2;
     60 
     61 	us1 = s1;
     62 	us2 = s2;
     63 
     64 	while (n) {
     65 		if (*us1 != *us2)
     66 			return (*us1 - *us2 < 0) ? -1 : 1;
     67 		us1++;
     68 		us2++;
     69 		n--;
     70 	}
     71 
     72 	return 0;
     73 }