KraKern

C++ Kernel
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/KraKern

baselib.cpp (1675B)


      1 #include <os.h>
      2 
      3 extern "C" {
      4 	u32		strlen(const char *s) {
      5 		u32		i;
      6 
      7 		for (i = 0; s[i]; i++);
      8 		return i;
      9 	}
     10 
     11 	u32		strcmp(const char *s1, const char *s2) {
     12 		while (s1 && s2 && *s1 && *s2 && *s1 == *s2) {
     13 			s1++;
     14 			s2++;
     15 		}
     16 		return (u32)*s1 - *s2;
     17 	}
     18 
     19 	u32		strncmp(const char *s1, const char *s2, u32 n) {
     20 		while (s1 && s2 && *s1 && *s2 && *s1 == *s2 && n--) {
     21 			s1++;
     22 			s2++;
     23 		}
     24 		return (u32)*s1 - *s2;
     25 	}
     26 
     27 	u32		strcpy(char *dst, const char *src) {
     28 		u32		i;
     29 
     30 		for (i = 0; src[i]; dst[i] = src[i], i++);
     31 		dst[i] = 0x0;
     32 		return i;
     33 	}
     34 
     35 	u32		strncpy(char *dst, const char *src, u32 n) {
     36 		u32		i;
     37 
     38 		for (i = 0; i < n && src[i]; dst[i] = src[i], i++);
     39 		if (i < n)
     40 			dst[i] = 0x0;
     41 		return i;
     42 	}
     43 
     44 	char	*strcat(char *s1, char *s2) {
     45 		return (char *)memcpy(s1 + strlen(s1), s2, strlen(s2));
     46 	}
     47 
     48 	char	*strncat(char *s1, char *s2, u32 n) {
     49 		return (char *)memcpy(s1 + strlen(s1), s2, n);
     50 	}
     51 
     52 	void	*memcpy(void *dst, const void *src, u32 n) {
     53 		char	*p = (char *)dst;
     54 		char	*s = (char *)src;
     55 
     56 		while (n--)
     57 			*p++ = *s++;
     58 		return dst;
     59 	}
     60 
     61 	void	*memset(void *dst, int c, u32 n) {
     62 		char	*p = (char *)dst;
     63 
     64 		while (n--)
     65 			*p++ = c;
     66 		return dst;
     67 	}
     68 	
     69 	void	itoa(char *buf, u32 n, int base) {
     70 		u32 tmp;
     71 		int i, j;
     72 
     73 		tmp = n;
     74 		i = 0;
     75 		do {
     76 			tmp = n % base;
     77 			buf[i++] = (tmp < 10) ? (tmp + '0') : (tmp + 'a' - 10);
     78 		} while (n /= base);
     79 		buf[i--] = 0;
     80 
     81 		for (j = 0; j < i; j++, i--) {
     82 			tmp = buf[j];
     83 			buf[j] = buf[i];
     84 			buf[i] = tmp;
     85 		}
     86 	}
     87 
     88 	void	strreplace(char *s, char a, char b) {
     89 		if (!s)
     90 			return ;
     91 		while (*s) {
     92 			if (*s == a)
     93 				*s = b;
     94 			s++;
     95 		}
     96 	}
     97 
     98 	void	bzero(char *s) {
     99 		for (int i = 0; s[i]; i++)
    100 			s[i] = 0;
    101 	}
    102 }
    103 
    104