mirror of
https://github.com/Tom-on64/kern.git
synced 2026-11-03 18:22:22 +00:00
Added the 'n' functions (ie. strNcpy) to string.h and some minor changes
This commit is contained in:
+2
-1
@@ -1,8 +1,9 @@
|
|||||||
#ifndef _FILEIO_H
|
#ifndef _FILEIO_H
|
||||||
#define _FILEIO_H
|
#define _FILEIO_H
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <syscall.h>
|
#include <syscall.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
// Nobody should have to interface with the internals of this structure
|
// Nobody should have to interface with the internals of this structure
|
||||||
typedef struct _iobuf { // 24B
|
typedef struct _iobuf { // 24B
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <syscall.h>
|
#include <syscall.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
/* Declarations */
|
/* Declarations */
|
||||||
void putc(char);
|
void putc(char);
|
||||||
|
|||||||
@@ -3,6 +3,10 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#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/
|
// Source: http://www.strudel.org.uk/itoa/
|
||||||
char* itoa(uint32_t val, uint8_t base) {
|
char* itoa(uint32_t val, uint8_t base) {
|
||||||
static char buf[32] = { 0 };
|
static char buf[32] = { 0 };
|
||||||
@@ -47,6 +51,19 @@ char* strcpy(char* str1, const char* str2) {
|
|||||||
return str1;
|
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* strcat(char* str1, const char* str2) {
|
||||||
char* s = str1;
|
char* s = str1;
|
||||||
while (*s != '\0') { s++; }
|
while (*s != '\0') { s++; }
|
||||||
@@ -54,12 +71,21 @@ char* strcat(char* str1, const char* str2) {
|
|||||||
return str1;
|
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) {
|
char* strchr(const char* str, char c) {
|
||||||
const char* s = str;
|
const char* s = str;
|
||||||
while (*s != '\0' && *s != c) { s++; }
|
while (*s != '\0' && *s != c) { s++; }
|
||||||
return (*s == c) ? (char*)s : NULL;
|
return (*s == c) ? (char*)s : NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// char* strrchr(const char* str, char c);
|
||||||
|
|
||||||
int strcmp(const char* str1, const char* str2) {
|
int strcmp(const char* str1, const char* str2) {
|
||||||
while (*str1 != '\0' && *str1 == *str2) {
|
while (*str1 != '\0' && *str1 == *str2) {
|
||||||
str1++;
|
str1++;
|
||||||
@@ -72,6 +98,20 @@ int strcmp(const char* str1, const char* str2) {
|
|||||||
return ((c1 < c2) ? -1 : (c1 > c2));
|
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) {
|
uint32_t strlen(const char* str) {
|
||||||
const char* p = str;
|
const char* p = str;
|
||||||
while (*++p != '\0');
|
while (*++p != '\0');
|
||||||
|
|||||||
Reference in New Issue
Block a user