Orion
Barry Importing existing Orion kernel d41a53c (3 years, 2 months ago)
/*
* This file contains a few routines for the manipulation of memory and strings.
* The functions would normally be part of a C library, but this is for the
* Kernel. The functions are standalone, and have no dependencies - they can be
* called immediately after boot.
*/
#include <stdint.h>
#include <stddef.h>
/* Fill a region of memory with the specified byte */
void *
memset(void *s, int c, size_t n)
{
unsigned char *a = s;
if (n > 0) {
while (n-- > 0)
*a++ = c;
}
return s;
}
/* Copy one region of memory to another */
void *
memcpy(void *dest, void *src, size_t n)
{
unsigned char *a = (unsigned char *) dest,
*b = (unsigned char *) src;
while (n-- > 0)
*a++ = *b++;
return dest;
}
/* Compare two regions of memory */
int
memcmp(void *s1, void *s2, size_t n)
{
unsigned char *a = (unsigned char *) s1,
*b = (unsigned char *) s2;
while (n-- > 0)
if (*a++ != *b++)
return a[-1] - b[-1];
return 0;
}
/* Find the length of a string */
size_t
strlen(char *s)
{
if (!s)
return 0;
size_t i;
for (i = 0; s[i]; i++);
return i;
}
/* Find the length of a string up to maximum */
size_t
strnlen(char *s, size_t maxlen)
{
if (!s)
return 0;
size_t i;
for (i = 0; s[i] && i <= maxlen; i++);
return i;
}
/* Compare two strings */
int
strcmp(char *s1, char *s2)
{
for (; *s1 == *s2 && *s1 && *s2; s1++, s2++);
return *(unsigned char *) s1 - *(unsigned char *) s2;
}
/* Compare two limited strings */
int
strncmp(char *s1, char *s2, size_t n)
{
if (!n--) return 0;
for (; *s1 == *s2 && *s1 && *s2 && n; s1++, s2++, n--);
return *(unsigned char *) s1 - *(unsigned char *) s2;
}
/* Copy a string */
char *
strcpy(char *dest, const char *src)
{
char *ret = dest;
while (*src)
*dest++ = *src++;
*dest = '\0';
return ret;
}
/* Copy a limited string */
char *
strncpy(char *dest, const char *src, size_t n)
{
char *ret = dest;
while (*src && n--)
*dest++ = *src++;
*dest = '\0';
return ret;
}