2021-02-28 02:18:39 +01:00
|
|
|
/* See the end of this file for copyright, license, and warranty information. */
|
2020-06-12 00:01:15 +02:00
|
|
|
|
|
|
|
#include <ardix/types.h>
|
|
|
|
|
2021-01-05 13:59:44 +01:00
|
|
|
#include <stdbool.h>
|
2021-02-28 18:27:43 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <toolchain.h>
|
2021-01-05 13:59:44 +01:00
|
|
|
|
2020-06-12 00:01:15 +02:00
|
|
|
#ifndef __HAVE_ASM_MEMCMP
|
2021-02-28 18:08:08 +01:00
|
|
|
int memcmp(const void *s1, const void *s2, size_t n)
|
2020-06-12 00:01:15 +02:00
|
|
|
{
|
|
|
|
int delta = 0;
|
|
|
|
|
|
|
|
while (n-- > 0) {
|
2020-11-19 16:33:40 +01:00
|
|
|
delta = *(const unsigned char *)s1++ - *(const unsigned char *)s2++;
|
2020-06-12 00:01:15 +02:00
|
|
|
if (delta != 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return delta;
|
|
|
|
}
|
|
|
|
#endif /* __HAVE_ASM_MEMCMP */
|
|
|
|
|
|
|
|
#ifndef __HAVE_ASM_MEMCPY
|
2021-02-28 18:08:08 +01:00
|
|
|
void *memcpy(void *dest, const void *src, size_t n)
|
2020-06-12 00:01:15 +02:00
|
|
|
{
|
|
|
|
uint8_t *tmp = (uint8_t *)dest;
|
|
|
|
|
|
|
|
while (n-- > 0)
|
|
|
|
*tmp++ = *(const uint8_t *)src++;
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
#endif /* __HAVE_ASM_MEMCPY */
|
|
|
|
|
|
|
|
#ifndef __HAVE_ASM_MEMSET
|
2021-02-28 18:08:08 +01:00
|
|
|
void *memset(void *ptr, int c, size_t n)
|
2020-06-12 00:01:15 +02:00
|
|
|
{
|
|
|
|
char *tmp = (char *)ptr;
|
|
|
|
|
|
|
|
while (n-- > 0)
|
|
|
|
*tmp++ = (char)c;
|
|
|
|
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
#endif /* __HAVE_ASM_MEMSET */
|
|
|
|
|
|
|
|
#ifndef __HAVE_ASM_MEMMOVE
|
2021-02-28 18:08:08 +01:00
|
|
|
void *memmove(void *dest, const void *src, size_t n)
|
2020-06-12 00:01:15 +02:00
|
|
|
{
|
|
|
|
char *tmp = (char *)dest;
|
|
|
|
const char *s = (const char *)src;
|
|
|
|
|
|
|
|
if (dest == src)
|
|
|
|
return dest;
|
|
|
|
|
|
|
|
if (dest < src) {
|
|
|
|
while (n-- != 0)
|
|
|
|
*tmp++ = *s++;
|
|
|
|
} else {
|
|
|
|
tmp += n;
|
|
|
|
s += n;
|
|
|
|
while (n-- != 0)
|
|
|
|
*--tmp = *--s;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
#endif /* __HAVE_ASM_MEMMOVE */
|
|
|
|
|
|
|
|
#ifndef __HAVE_ASM_STRCMP
|
2021-02-28 18:08:08 +01:00
|
|
|
int strcmp(const char *s1, const char *s2)
|
2020-06-12 00:01:15 +02:00
|
|
|
{
|
|
|
|
while (*s1++ == *s2++) {
|
|
|
|
if (*s1 == '\0' || *s2 == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return *((const unsigned char *)s1) - *((const unsigned char *)s2);
|
|
|
|
}
|
|
|
|
#endif /* __HAVE_ASM_STRCMP */
|
|
|
|
|
|
|
|
#ifndef __HAVE_ASM_STRCPY
|
2021-02-28 18:08:08 +01:00
|
|
|
char *strcpy(char *dest, const char *src)
|
2020-06-12 00:01:15 +02:00
|
|
|
{
|
|
|
|
char *tmp = dest;
|
|
|
|
|
|
|
|
while ((*tmp++ = *src++) != '\0');
|
|
|
|
/* nothing */
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
#endif /* __HAVE_ASM_STRCPY */
|
|
|
|
|
|
|
|
#ifndef __HAVE_ASM_STRNCPY
|
2021-02-28 18:08:08 +01:00
|
|
|
char *strncpy(char *dest, const char *src, size_t n)
|
2020-06-12 00:01:15 +02:00
|
|
|
{
|
|
|
|
char *tmp = dest;
|
|
|
|
|
|
|
|
while (n-- != 0) {
|
|
|
|
if ((*tmp++ = *src++) == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
#endif /* __HAVE_ASM_STRNCPY */
|
|
|
|
|
|
|
|
#ifndef __HAVE_ASM_STRLEN
|
2021-02-28 18:08:08 +01:00
|
|
|
size_t strlen(const char *s)
|
2020-06-12 00:01:15 +02:00
|
|
|
{
|
|
|
|
const char *tmp = s;
|
|
|
|
|
|
|
|
while (*tmp++ != '\0');
|
|
|
|
/* nothing */
|
|
|
|
|
|
|
|
return (size_t)tmp - (size_t)s - (size_t)1;
|
|
|
|
}
|
|
|
|
#endif /* __HAVE_ASM_STRLEN */
|
2020-10-11 19:35:30 +02:00
|
|
|
|
|
|
|
/*
|
2021-02-28 02:18:39 +01:00
|
|
|
* This file is part of Ardix.
|
|
|
|
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.
|
2020-10-11 19:35:30 +02:00
|
|
|
*
|
2021-05-10 16:19:38 +02:00
|
|
|
* Ardix is non-violent software: you may only use, redistribute,
|
|
|
|
* and/or modify it under the terms of the CNPLv6+ as found in
|
|
|
|
* the LICENSE file in the source code root directory or at
|
|
|
|
* <https://git.pixie.town/thufie/CNPL>.
|
2020-10-11 19:35:30 +02:00
|
|
|
*
|
2021-05-10 16:19:38 +02:00
|
|
|
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
|
|
|
|
* permitted by applicable law. See the CNPLv6+ for details.
|
2020-10-11 19:35:30 +02:00
|
|
|
*/
|