Add zealbooter library files for strcpy, strncpy, strcmp, strncmp, strlen.

This commit is contained in:
TomAwezome 2022-09-08 18:47:59 -04:00
parent b0626d623b
commit a02dca9e41
11 changed files with 149 additions and 0 deletions

View file

@ -6,6 +6,11 @@
#include <memset.h>
#include <memmove.h>
#include <memcmp.h>
#include <strcpy.h>
#include <strncpy.h>
#include <strcmp.h>
#include <strncmp.h>
#include <strlen.h>
#include <print.h>
uint64_t div_roundup_u64(uint64_t a, uint64_t b);

19
zealbooter/lib/strcmp.c Normal file
View file

@ -0,0 +1,19 @@
/* strcmp( const char *, const char * )
This file is part of the Public Domain C Library (PDCLib).
Permission is granted to use, modify, and / or redistribute at will.
*/
#include <stddef.h>
int strcmp(const char *s1, const char *s2)
{
while ((*s1) && (*s1 == *s2))
{
++s1;
++s2;
}
return (*(unsigned char *)s1 - *(unsigned char *)s2);
}

6
zealbooter/lib/strcmp.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef __STRCMP_H__
#define __STRCMP_H__
int strcmp(const char *s1, const char *s2);
#endif // __STRCMP_H__

19
zealbooter/lib/strcpy.c Normal file
View file

@ -0,0 +1,19 @@
/* strcpy( char *, const char * )
This file is part of the Public Domain C Library (PDCLib).
Permission is granted to use, modify, and / or redistribute at will.
*/
#include <stddef.h>
char *strcpy(char *s1, const char *s2)
{
char *rc = s1;
while ((*s1++ = *s2++))
{
/* EMPTY */
}
return rc;
}

6
zealbooter/lib/strcpy.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef __STRCPY_H__
#define __STRCPY_H__
char *strcpy(char *s1, const char *s2);
#endif // __STRCPY_H__

19
zealbooter/lib/strlen.c Normal file
View file

@ -0,0 +1,19 @@
/* strlen( const char * )
This file is part of the Public Domain C Library (PDCLib).
Permission is granted to use, modify, and / or redistribute at will.
*/
#include <stddef.h>
size_t strlen(const char *s)
{
size_t rc = 0;
while (s[rc])
{
++rc;
}
return rc;
}

6
zealbooter/lib/strlen.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef __STRLEN_H__
#define __STRLEN_H__
size_t strlen(const char *s);
#endif // __STRLEN_H__

26
zealbooter/lib/strncmp.c Normal file
View file

@ -0,0 +1,26 @@
/* strncmp( const char *, const char *, size_t )
This file is part of the Public Domain C Library (PDCLib).
Permission is granted to use, modify, and / or redistribute at will.
*/
#include <stddef.h>
int strncmp(const char *s1, const char *s2, size_t n)
{
while (n && *s1 && (*s1 == *s2))
{
++s1;
++s2;
--n;
}
if (n == 0)
{
return 0;
}
else
{
return (*(unsigned char *)s1 - *(unsigned char *)s2);
}
}

6
zealbooter/lib/strncmp.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef __STRNCMP_H__
#define __STRNCMP_H__
int strncmp(const char *s1, const char *s2, size_t n);
#endif // __STRNCMP_H__

31
zealbooter/lib/strncpy.c Normal file
View file

@ -0,0 +1,31 @@
/* strncpy( char *, const char *, size_t )
This file is part of the Public Domain C Library (PDCLib).
Permission is granted to use, modify, and / or redistribute at will.
*/
#include <stddef.h>
char *strncpy(char *s1, const char *s2, size_t n)
{
char *rc = s1;
while (n && (*s1++ = *s2++))
{
/* Cannot do "n--" in the conditional as size_t is unsigned and we have
to check it again for >0 in the next loop below, so we must not risk
underflow.
*/
--n;
}
/* Checking against 1 as we missed the last --n in the loop above. */
while (n-- > 1)
{
*s1++ = '\0';
}
return rc;
}

6
zealbooter/lib/strncpy.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef __STRNCPY_H__
#define __STRNCPY_H__
char *strncpy(char *s1, const char *s2, size_t n);
#endif // __STRNCPY_H__