metal-cos/sys/kern/libc.c

296 lines
4.6 KiB
C

/*
* Copyright (c) 2006-2018 Ali Mashtizadeh
* All rights reserved.
*/
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
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;
}
}
char *
strtok_r(char *str, const char *delim, char **last)
{
char *rval;
if (str == NULL)
str = *last;
// Skip deliminaters in the front
while ((str[0] != '\0') && strchr(delim, str[0])) {
str++;
}
// We've reached the end of the string
if (str[0] == '\0')
return NULL;
// Return first non-delim until the next
rval = str;
// Skip deliminaters in the front
while ((str[0] != '\0') && !strchr(delim, str[0])) {
str++;
}
if (str[0] != '\0') {
str[0] = '\0';
str++;
}
*last = str;
return rval;
}
char *
strcpy(char *to, const char *from)
{
char *save = to;
for (; (*to = *from); ++from, ++to);
return save;
}
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;
}
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));
}
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);
}
size_t
strlen(const char *str)
{
const char *s;
for (s = str; *s; ++s);
return (s - str);
}
void *
memset(void *dst, int c, size_t length)
{
uint8_t *p = (uint8_t *)dst;
while (length-- != 0) {
*p = c;
p += 1;
};
return dst;
}
void *
memcpy(void *dst, const void *src, size_t length)
{
uint8_t *d = (uint8_t *)dst;
const uint8_t *s = (const uint8_t *)src;
while (length-- != 0) {
*d = *s;
d += 1;
s += 1;
};
return dst;
}
int
memcmp(const void *b1, const void *b2, size_t length)
{
size_t 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;
}
//
// glibc
//
void *
memmove (void *dest, const void *src, size_t len)
{
char *d = dest;
const char *s = src;
if (d < s)
while (len--)
*d++ = *s++;
else
{
const char *lasts = s + (len-1);
char *lastd = d + (len-1);
while (len--)
*lastd-- = *lasts--;
}
return dest;
}
//
// glibc
//
char *
strrchr (register const char *s, int c)
{
char *rtnval = 0;
do {
if (*s == c)
rtnval = (char*) s;
} while (*s++);
return (rtnval);
}
//
// glibc
//
void *
memchr (register const void *src_void, int c, size_t length)
{
const unsigned char *src = (const unsigned char *)src_void;
while (length-- > 0)
{
if (*src == c)
return (void *)src;
src++;
}
return NULL;
}
//
// https://android.googlesource.com/platform/bionic/+/ics-mr0/libc/string/strnlen.c
//
size_t strnlen(const char* str, size_t maxlen)
{
char* p = memchr(str, 0, maxlen);
if (p == NULL)
return maxlen;
else
return (p - str);
}
//
// https://android.googlesource.com/platform/bionic/+/ics-mr0/libc/stdlib/strtoul.c
//
unsigned long
strtoul(const char *nptr, char **endptr, int base)
{
const char *s;
unsigned long acc, cutoff;
int c;
int neg, any, cutlim;
/*
* See strtol for comments as to the logic used.
*/
s = nptr;
do {
c = (unsigned char) *s++;
} while (isspace(c));
if (c == '-') {
neg = 1;
c = *s++;
} else {
neg = 0;
if (c == '+')
c = *s++;
}
if ((base == 0 || base == 16) &&
c == '0' && (*s == 'x' || *s == 'X')) {
c = s[1];
s += 2;
base = 16;
}
if (base == 0)
base = c == '0' ? 8 : 10;
cutoff = ULONG_MAX / (unsigned long)base;
cutlim = ULONG_MAX % (unsigned long)base;
for (acc = 0, any = 0;; c = (unsigned char) *s++) {
if (isdigit(c))
c -= '0';
else if (isalpha(c))
c -= isupper(c) ? 'A' - 10 : 'a' - 10;
else
break;
if (c >= base)
break;
if (any < 0)
continue;
if (acc > cutoff || (acc == cutoff && c > cutlim)) {
any = -1;
acc = ULONG_MAX;
// errno = ERANGE;
} else {
any = 1;
acc *= (unsigned long)base;
acc += c;
}
}
if (neg && any > 0)
acc = -acc;
if (endptr != 0)
*endptr = (char *) (any ? s - 1 : nptr);
return (acc);
}