Added the 'n' functions (ie. strNcpy) to string.h and some minor changes

This commit is contained in:
2024-08-26 17:01:19 +02:00
parent d6c4682f52
commit 7886fd5599
3 changed files with 43 additions and 1 deletions
+2 -1
View File
@@ -1,8 +1,9 @@
#ifndef _FILEIO_H
#define _FILEIO_H
#include <stdint.h>
#include <syscall.h>
#include <stdlib.h>
#include <stddef.h>
// Nobody should have to interface with the internals of this structure
typedef struct _iobuf { // 24B
+1
View File
@@ -4,6 +4,7 @@
#include <syscall.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
/* Declarations */
void putc(char);
+40
View File
@@ -3,6 +3,10 @@
#include <stdint.h>
void* memcpy(void* src, void* dst, uint32_t length);
void* memcpy32(void* src, void* dst, uint32_t length);
void* memset(void* dst, uint8_t value, uint32_t length);
// Source: http://www.strudel.org.uk/itoa/
char* itoa(uint32_t val, uint8_t base) {
static char buf[32] = { 0 };
@@ -47,6 +51,19 @@ char* strcpy(char* str1, const char* str2) {
return str1;
}
char* strncpy(char* str1, const char* str2, size_t n) {
char* dst = str1;
const char* src = str2;
while (n > 0) {
n--;
if ((*dst++ = *src++) == '\0') {
memset(dst, '\0', n);
break;
}
}
return str1;
}
char* strcat(char* str1, const char* str2) {
char* s = str1;
while (*s != '\0') { s++; }
@@ -54,12 +71,21 @@ char* strcat(char* str1, const char* str2) {
return str1;
}
char* strncat(char* str1, const char* str2, size_t n) {
char* s = str1;
while (*s != '\0') { s++; }
strncpy(s, str2, n);
return str1;
}
char* strchr(const char* str, char c) {
const char* s = str;
while (*s != '\0' && *s != c) { s++; }
return (*s == c) ? (char*)s : NULL;
}
// char* strrchr(const char* str, char c);
int strcmp(const char* str1, const char* str2) {
while (*str1 != '\0' && *str1 == *str2) {
str1++;
@@ -72,6 +98,20 @@ int strcmp(const char* str1, const char* str2) {
return ((c1 < c2) ? -1 : (c1 > c2));
}
int strncmp(const char* str1, const char* str2, size_t n) {
if (n == 0) { return 0; }
while (n-- > 0 && *str1 == *str2) {
if (n == 0 || *str1 == '\0') { return 0; }
str1++;
str2++;
}
uint8_t c1 = (*(uint8_t*)str1);
uint8_t c2 = (*(uint8_t*)str2);
return ((c1 < c2) ? -1 : (c1 > c2));
}
uint32_t strlen(const char* str) {
const char* p = str;
while (*++p != '\0');