#include <klib.h>
#include <klib-macros.h>
#include <stdint.h>
#if !defined(__ISA_NATIVE__) || defined(__NATIVE_USE_KLIB__)
size_t strlen(const char *s) {
// panic("Not implemented");
assert(s != NULL);
size_t l = 0;
while(*s ++) {
l ++;
}
return l;
}
char *strcpy(char *dst, const char *src) {
// panic("Not implemented");
if(dst == NULL || src == NULL) return NULL;
// size_t l = strlen(dst);
size_t l = sizeof(dst);
char *tmp = dst;
memset(dst, 0, l);
while(*(unsigned char*)src != '\0' && l --) {
*(unsigned char*)tmp = *(unsigned char*)src;
tmp ++;
src ++;
}
// + '\0'
*(unsigned char*)tmp = '\0';
return dst;
}
char *strncpy(char *dst, const char *src, size_t n) {
// panic("Not implemented");
if(dst == NULL || src == NULL) return NULL;
size_t l1 = sizeof(dst);
size_t l2 = sizeof(src);
assert(l1 >= l2);
assert(l1 >= n);
assert(l2 >= n);
size_t cnt = 0;
char *tmp = dst;
while(*(unsigned char*)src != '\0' && cnt < n) {
*(unsigned char*)dst ++ = *(unsigned char*)src ++;
cnt ++;
}
*(unsigned char*)dst = '\0';
return tmp;
}
char *strcat(char *dst, const char *src) {
// panic("Not implemented");
if(dst == NULL || src == NULL) {
return dst;
}
char *tmp = dst;
size_t l = sizeof(dst);
size_t lr = strlen(dst);
size_t lc = l - lr + 1;
while(*tmp != '\0' ) {
tmp ++;
}
while(*src != '\0' && lc > 1) {
*tmp = *src;
tmp ++;
src ++;
// lc --;
}
*tmp = '\0';
return dst;
}
int strcmp(const char *s1, const char *s2) {
// panic("Not implemented");
assert(s1 != NULL);
assert(s2 != NULL);
while(*(unsigned char*)s1 == *(unsigned char*)s2 && *(unsigned char*)s1 != '\0'){
s1 ++;
s2 ++;
}
return *(unsigned char*)s1 - *(unsigned char*)s2;
}
int strncmp(const char *s1, const char *s2, size_t n) {
panic("Not implemented");
}
// void *memset(void *s, int c, size_t n) {
// // panic("Not implemented");
// void *tmp = s;
// while(n --) {
// *tmp++ = c;
// }
// return s;
// }
void *memset(void *s, int c, size_t n) {
unsigned char *p = (unsigned char *)s;
unsigned char uc = (unsigned char)c;
while (n-- > 0) {
*p++ = uc;
}
return s;
}
void *memmove(void *dst, const void *src, size_t n) {
panic("Not implemented");
}
void *memcpy(void *out, const void *in, size_t n) {
panic("Not implemented");
}
int memcmp(const void *s1, const void *s2, size_t n) {
// panic("Not implemented");
// can not operator on void pointer
const unsigned char *su1 = s1;
const unsigned char *su2 = s2;
while((*su1 != '\0' || *su2 != '\0') && n --){
if(*su1 == '\0')
{
if(*su1 == *su2) {
su2 ++;
}
else break;
}
else if(*su2 == '\0'){
if(*su1 == *su2) {
su1 ++;
}
else break;
}
}
return *su1 - *su2;
}
#endif
标签:src,++,dst,memset,unsigned,strcat,char,strcpy,size
From: https://www.cnblogs.com/seedlesscute-shadow/p/18281403