cynix

x86 UNIX-like OS
git clone git://git.2f30.org/cynix
Log | Files | Refs | README | LICENSE

string.c (1783B)


      1 /* This file is released under the GPL v2 public license */
      2 
      3 #include <string.h>
      4 
      5 void *
      6 memcpy(void *dest, const void *src, size_t n)
      7 {
      8 	char *tmp = dest;
      9 	const char *s = src;
     10 
     11 	while (n--)
     12 		*tmp++ = *s++;
     13 	return dest;
     14 }
     15 
     16 void *
     17 memmove(void *dest, const void *src, size_t count)
     18 {
     19 	char *tmp;
     20 	const char *s;
     21 
     22 	if (dest <= src) {
     23 		tmp = dest;
     24 		s = src;
     25 		while (count--)
     26 			*tmp++ = *s++;
     27 	} else {
     28 		tmp = dest;
     29 		tmp += count;
     30 		s = src;
     31 		s += count;
     32 		while (count--)
     33 			*--tmp = *--s;
     34 	}
     35 	return dest;
     36 }
     37 
     38 void *
     39 memset(void *s, int c, size_t n)
     40 {
     41 	char *ptr = s;
     42 	while (n--)
     43 		*ptr++ = c;
     44 	return s;
     45 }
     46 
     47 int
     48 memcmp(const void *cs, const void *ct, size_t count)
     49 {
     50 	const unsigned char *su1, *su2;
     51 	int res = 0;
     52 
     53 	for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
     54 		if ((res = *su1 - *su2) != 0)
     55 			break;
     56 	return res;
     57 }
     58 
     59 size_t
     60 strlen(const char *s)
     61 {
     62 	size_t len = 0;
     63 
     64 	while (*s++) ++len;
     65 	return len;
     66 }
     67 
     68 size_t
     69 strnlen(const char *s, size_t count)
     70 {
     71 	const char *sc;
     72 
     73 	for (sc = s; count-- && *sc != '\0'; ++sc)
     74 		;
     75 	return sc - s;
     76 }
     77 
     78 int
     79 strcmp(const char *cs, const char *ct)
     80 {
     81 	signed char res;
     82 
     83 	while (1) {
     84 		if ((res = *cs - *ct++) != 0 || !*cs++)
     85 			break;
     86 	}
     87 	return res;
     88 }
     89 
     90 char *
     91 strcpy(char *dest, const char *src)
     92 {
     93 	char *tmp = dest;
     94 
     95 	while ((*dest++ = *src++) != '\0')
     96 		/* nothing */;
     97 	return tmp;
     98 }
     99 
    100 char *
    101 strncpy(char *dest, const char *src, size_t count)
    102 {
    103 	char *tmp = dest;
    104 
    105 	while (count) {
    106 		if ((*tmp = *src) != 0)
    107 			src++;
    108 		tmp++;
    109 		count--;
    110 	}
    111 	return dest;
    112 }
    113 
    114 int
    115 strncmp(const char *cs, const char *ct, size_t count)
    116 {
    117 	signed char res = 0;
    118 
    119 	while (count) {
    120 		if ((res = *cs - *ct++) != 0 || !*cs++)
    121 			break;
    122 		count--;
    123 	}
    124 	return res;
    125 }
    126 
    127 void
    128 bzero(void *s, size_t n)
    129 {
    130 	unsigned char *ptr = s;
    131 
    132 	while (n--)
    133 		*ptr++ = 0;
    134 }
    135