metal-cos/sys/kern/libc.c

132 lines
1.8 KiB
C
Raw Normal View History

2014-02-12 21:47:13 +00:00
/*
* Copyright (c) 2006-2018 Ali Mashtizadeh
2014-02-12 21:47:13 +00:00
* All rights reserved.
*/
2014-07-13 21:09:36 +00:00
#include <stddef.h>
2014-07-10 22:55:32 +00:00
#include <stdint.h>
2014-07-06 07:07:32 +00:00
#include <string.h>
2014-02-12 21:47:13 +00:00
2014-07-13 21:09:36 +00:00
char *
strchr(const char *s, int c)
{
int i;
for (i = 0; ; i++)
{
if (s[i] == c)
return (char *)s + i;
if (s[i] == '\0')
return NULL;
}
}
2014-02-12 21:47:13 +00:00
char *
strcpy(char *to, const char *from)
{
char *save = to;
for (; (*to = *from); ++from, ++to);
return save;
}
2014-07-10 22:55:32 +00:00
char *
strncpy(char *to, const char *from, size_t length)
{
char *save = to;
for (; (*to = *from) != '\0' && length > 0; ++from, ++to, length--);
*to = '\0';
return save;
}
2014-02-12 21:47:13 +00:00
int
strcmp(const char *s1, const char *s2)
{
while (*s1 == *s2++)
if (*s1++ == 0)
return 0;
return (*(const uint8_t *)s1 - *(const uint8_t *)(s2 - 1));
}
2014-10-14 21:07:55 +00:00
int
strncmp(const char *s1, const char *s2, size_t len)
{
if (len == 0)
return 0;
while (*s1 == *s2) {
if (*s1 == 0)
return 0;
s1++;
s2++;
len--;
if (len == 0)
return 0;
}
return (*(const uint8_t *)s1 - *(const uint8_t *)s2);
}
2014-02-12 21:47:13 +00:00
size_t
strlen(const char *str)
{
const char *s;
for (s = str; *s; ++s);
return (s - str);
}
void *
2014-07-06 07:07:32 +00:00
memset(void *dst, int c, size_t length)
2014-02-12 21:47:13 +00:00
{
uint8_t *p = (uint8_t *)dst;
2014-10-13 03:22:52 +00:00
while (length-- != 0) {
2014-02-12 21:47:13 +00:00
*p = c;
p += 1;
2014-10-13 03:22:52 +00:00
};
2014-02-12 21:47:13 +00:00
return dst;
}
2014-07-02 23:18:40 +00:00
void *
memcpy(void *dst, const void *src, size_t length)
{
uint8_t *d = (uint8_t *)dst;
const uint8_t *s = (const uint8_t *)src;
2014-10-13 03:22:52 +00:00
while (length-- != 0) {
2014-07-02 23:18:40 +00:00
*d = *s;
d += 1;
s += 1;
2014-10-13 03:22:52 +00:00
};
2014-07-02 23:18:40 +00:00
return dst;
}
2014-08-01 01:16:08 +00:00
int
memcmp(const void *b1, const void *b2, size_t length)
{
int i;
const char *c1 = (const char *)b1;
const char *c2 = (const char *)b2;
for (i = 0; i < length; i++)
{
if (*c1 != *c2)
return *c2 - *c1;
c1++;
c2++;
}
return 0;
}