Implemenations for the cstring header.

meesbs
Marko Semet 2020-05-21 04:19:56 +02:00
parent 15ec4e90f2
commit d5bd2531b0
5 changed files with 193 additions and 5 deletions

View File

@ -1,3 +1,2 @@
/Makefile
/lib
/tests
/libc.a
/tcc

View File

@ -0,0 +1,4 @@
#ifndef CONFIG_TCCDIR
# define CONFIG_TCCDIR "/usr/lib/tcc"
#endif
#define TCC_VERSION "0.9.27"

View File

@ -1,5 +1,14 @@
#! /usr/bin/env sh
cd "$(dirname "$0")" &&
../configure &&
gcc -o tcc -static -nostdlib -DNDEBUG -DTCC_NOT_NATIVE -DCONFIG_LDDIR="\"lib64\"" -DTCC_TARGET_X86_64 -DONE_SOURCE=0 -I. ../tcc.c ../libtcc.c ../tccpp.c ../tccgen.c ../tccelf.c ../tccasm.c ../tccrun.c ../x86_64-gen.c ../x86_64-link.c ../i386-asm.c
# Compile dietlibc
gcc -r -nostdlib -o libc.a -Ilibc/include -I../include \
libc/src/cstring.c &&
# Build compiler
ln -fs base_config.h config.h &&
gcc -o tcc -static -nostdlib \
-DNDEBUG -DTCC_NOT_NATIVE -DCONFIG_LDDIR="\"lib64\"" -DTCC_TARGET_X86_64 -DONE_SOURCE=0 -I. -I../include -Ilibc/include \
../tcc.c ../libtcc.c ../tccpp.c ../tccgen.c ../tccelf.c ../tccasm.c ../tccrun.c ../x86_64-gen.c ../x86_64-link.c ../i386-asm.c\
./libc.a

View File

@ -0,0 +1,22 @@
#ifndef __STRING_H__
#define __STRING_H__
#include <stddef.h>
// Memory
const void* memchr(const void* ptr, int value, size_t size);
int memcmp(const void* ptr1, const void* ptr2, size_t num);
void* memcpy(void* destination, const void* source, size_t num);
void* memmove(void* destination, const void* source, size_t num);
void* memset(void* ptr, int value, size_t num);
// Strings
char* strchr(const char* str, int character);
int strcmp(const char* str1, const char* str2);
char* strcpy(char* destination, const char* source);
size_t strlen(const char* str);
int strncmp(const char* str1, const char* str2, size_t num);
char* strrchr(const char* str, int character);
char* strstr(const char* str1, const char* str2);
#endif

View File

@ -0,0 +1,154 @@
#include <stddef.h>
// Tools
size_t min(size_t a, size_t b) {
if(a < b) {
return a;
}
else {
return b;
}
}
// Memory
const void* memchr(const void* ptr, int value, size_t size) {
const char* ptr_real = (const char*) ptr;
char value_real = value;
for(size_t i = 0; i < size; i++) {
if(ptr_real[i] == value_real) {
return &(ptr_real[i]);
}
}
return NULL;
}
int memcmp(const void* ptr1, const void* ptr2, size_t num) {
const char* ptr1_real = (const char*) ptr1;
const char* ptr2_real = (const char*) ptr2;
for(size_t i = 0; i < num; i++) {
if(ptr1_real[i] < ptr2_real[i]) {
return -1;
}
else if(ptr1_real[i] > ptr2_real[i]) {
return 1;
}
}
return 0;
}
void* memcpy(void* destination, const void* source, size_t num) {
char* destination_real = (char*) destination;
const char* source_real = (const char*) source;
if(source > destination) {
for(size_t i = 0; i < num; i++) {
destination_real[i] = source_real[i];
}
}
else {
for(size_t i = 0; i < num; i++) {
destination_real[num - i - 1] = source_real[num - i - 1];
}
}
return destination;
}
void* memmove(void* destination, const void* source, size_t num) {
return memcpy(destination, source, num);
}
void* memset(void* ptr, int value, size_t num) {
char* ptr_real = (char*) ptr;
char value_real = value;
for(size_t i = 0; i < num; i++) {
ptr_real[i] = value_real;
}
return ptr;
}
// String
size_t strlen(const char* str);
char* strchr(const char* str, int character) {
while((*str) == 0) {
if((*str) == character) {
return (char*) str;
}
str++;
}
return NULL;
}
int strcmp(const char* str1, const char* str2) { // TODO: Make faster
size_t len_str1 = strlen(str1);
size_t len_str2 = strlen(str2);
size_t min_len = min(len_str1, len_str2);
int result = memcmp(str1, str2, min_len);
if(result != 0) {
return result;
}
else if(len_str1 > min_len) {
return 1;
}
else {
return -1;
}
}
char* strcpy(char* destination, const char* source) {
char* result = destination;
while((*source) == 0) {
(*destination) = (*source);
destination++;
source++;
}
return result;
}
size_t strlen(const char* str) {
size_t size = 0;
while(str[size] != 0) {
size++;
}
return size;
}
int strncmp(const char* str1, const char* str2, size_t num) { // TODO: Make faster
size_t len_str1 = strlen(str1);
size_t len_str2 = strlen(str2);
size_t min_len = min(min(len_str1, len_str2), num);
int result = memcmp(str1, str2, min_len);
if(min_len == num) {
return result;
}
else if(result != 0) {
return result;
}
else if(len_str1 > min_len) {
return 1;
}
else {
return -1;
}
}
char* strrchr(const char* str, int character) {
char* result = NULL;
while((*str) != 0) {
if((*str) == character) {
result = (char*) str;
}
str++;
}
if(character == 0) {
result = (char*) str;
}
return result;
}
char* strstr(const char* str1, const char* str2) {
size_t len_str1 = strlen(str1);
size_t len_str2 = strlen(str2);
for(size_t i = 0; i <= len_str1 - len_str2; i++) {
int valid = 1;
for(size_t j = 0; j < len_str2; j++) {
if(str1[i + j] != str2[j]) {
valid = 0;
break;
}
}
if(valid != 0) {
return (char*) &(str1[i]);
}
}
return NULL;
}