Main: include/kernaux/libc.h: Add libc functions (closes #37)

This commit is contained in:
Alex Kotov 2022-06-06 05:23:46 +03:00
parent d2befd712a
commit 0e261a7b34
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
4 changed files with 87 additions and 1 deletions

View File

@ -104,10 +104,16 @@ stable options.
* `--with[out]-libc-atoi`
* `--with[out]-libc-isdigit`
* `--with[out]-libc-isspace`
* `--with[out]-libc-memcpy`
* `--with[out]-libc-memmove`
* `--with[out]-libc-memset`
* `--with[out]-libc-strcmp`
* `--with[out]-libc-strcpy`
* `--with[out]-libc-strlen`
* `--with[out]-libc-strncmp`
* `--with[out]-libc-strncpy`
* `--with[out]-libc-strnlen`
* `--with[out]-libc-strstr`
### Default options

View File

@ -47,10 +47,16 @@ AC_ARG_WITH( [libc-all], AS_HELP_STRING([--with-libc-all], [with libc
AC_ARG_WITH( [libc-atoi], AS_HELP_STRING([--with-libc-atoi], [with atoi replacement]))
AC_ARG_WITH( [libc-isdigit], AS_HELP_STRING([--with-libc-isdigit], [with isdigit replacement]))
AC_ARG_WITH( [libc-isspace], AS_HELP_STRING([--with-libc-isspace], [with isspace replacement]))
AC_ARG_WITH( [libc-memcpy], AS_HELP_STRING([--with-libc-memcpy], [with memcpy replacement]))
AC_ARG_WITH( [libc-memmove], AS_HELP_STRING([--with-libc-memmove], [with memmove replacement]))
AC_ARG_WITH( [libc-memset], AS_HELP_STRING([--with-libc-memset], [with memset replacement]))
AC_ARG_WITH( [libc-strcmp], AS_HELP_STRING([--with-libc-strcmp], [with strcmp replacement]))
AC_ARG_WITH( [libc-strcpy], AS_HELP_STRING([--with-libc-strcpy], [with strcpy replacement]))
AC_ARG_WITH( [libc-strlen], AS_HELP_STRING([--with-libc-strlen], [with strlen replacement]))
AC_ARG_WITH( [libc-strncmp], AS_HELP_STRING([--with-libc-strncmp], [with strncmp replacement]))
AC_ARG_WITH( [libc-strncpy], AS_HELP_STRING([--with-libc-strncpy], [with strncpy replacement]))
AC_ARG_WITH( [libc-strnlen], AS_HELP_STRING([--with-libc-strnlen], [with strnlen replacement]))
AC_ARG_WITH( [libc-strstr], AS_HELP_STRING([--with-libc-strstr], [with strstr replacement]))

View File

@ -15,10 +15,16 @@ int isspace(int c);
int atoi(const char *str);
// <string.h>
void *memcpy(void *dest, const void *src, size_t n);
void *memmove(void *dest, const void *src, size_t n);
void *memset(void *s, int c, size_t n);
int strcmp(const char *s1, const char *s2);
char *strcpy(char *dest, const char *src);
size_t strlen(const char *s);
int strncmp(const char *s1, const char *s2, size_t n);
char *strncpy(char *dest, const char *src, size_t n);
size_t strnlen(const char *s, size_t maxlen);
char *strstr(const char *haystack, const char *needle);
#ifdef __cplusplus
}

View File

@ -35,15 +35,49 @@ int isspace(const int c)
}
#endif // WITH_LIBC_ISSPACE
#ifdef WITH_LIBC_MEMCPY
void *memcpy(void *dest, const void *src, size_t n)
{
char *dest_cell = dest;
char *src_cell = src;
while (n--) *dest_cell++ = *src_cell++;
return dest;
}
#endif // WITH_LIBC_MEMCPY
#ifdef WITH_LIBC_MEMMOVE
void *memmove(void *dest, const void *src, size_t n)
{
char *dest_cell = dest;
char *src_cell = src;
if (dest_cell <= src_cell) {
while (n--) *dest_cell++ = *src_cell++;
} else {
dest_cell += n;
src_cell += n;
while (n--) *--dest_cell = *--src_cell;
}
return dest;
}
#endif // WITH_LIBC_MEMMOVE
#ifdef WITH_LIBC_MEMSET
void *memset(void *s, int c, size_t n)
{
char *ss = s;
while (n-- > 0) *ss++ = c;
while (n--) *ss++ = c;
return s;
}
#endif // WITH_LIBC_MEMSET
#ifdef WITH_LIBC_STRCMP
int strcmp(const char *s1, const char *s2)
{
for (; *s1; ++s1, ++s2) if (*s1 != *s2) return *s1 < *s2 ? -1 : 1;
return 0;
}
#endif // WITH_LIBC_STRCMP
#ifdef WITH_LIBC_STRCPY
char *strcpy(char *dest, const char *src)
{
@ -62,6 +96,23 @@ size_t strlen(const char *s)
}
#endif // WITH_LIBC_STRLEN
#ifdef WITH_LIBC_STRNCMP
int strncmp(const char *s1, const char *s2, size_t n)
{
for (; *s1 && n; ++s1, ++s2, --n) if (*s1 != *s2) return *s1 < *s2 ? -1 : 1;
return 0;
}
#endif // WITH_LIBC_STRNCMP
#ifndef WITH_LIBC_STRNCPY
char *strncpy(char *dest, const char *src, size_t n)
{
char *tmp = dest;
while (n-- && (*dest++ = *src++) != '\0');
return tmp;
}
#endif // WITH_LIBC_STRNCPY
#ifdef WITH_LIBC_STRNLEN
size_t strnlen(const char *s, size_t maxlen)
{
@ -70,3 +121,20 @@ size_t strnlen(const char *s, size_t maxlen)
return ss - s;
}
#endif // WITH_LIBC_STRNLEN
#ifdef WITH_LIBC_STRSTR
char *strstr(const char *haystack, const char *needle)
{
const size_t needle_slen = strlen(needle);
if (!needle_slen) return (char*)haystack;
size_t haystack_slen = strlen(haystack);
while (haystack_slen >= needle_slen) {
--haystack_slen;
if (!memcmp(haystack, needle, needle_slen)) return (char*)haystack;
++haystack;
}
return NULL;
}
#endif // WITH_LIBC_STRSTR