Initial commit

This commit is contained in:
g1n 2021-12-19 10:35:56 +02:00
commit fd21b5a8c2
29 changed files with 615 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*~
builds

10
LICENSE Normal file
View File

@ -0,0 +1,10 @@
MIT License
Copyright (c) 2021 GRU
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

16
README.org Normal file
View File

@ -0,0 +1,16 @@
#+TITLE: GRU olibc - standart C11 library implementation
* Dependencies
- GRU liblinux
* Setup
- run ~make~ with path to liblinux as arguments (LIBLINUXHEADERS for path with liblinux header files and LIBLINUXDIR for path to directory where built liblinux (.a or .so file) is stored) in ~src/~ (~olibc.a~ will be in ~builds/~ directory)
- to build examples run ~make examples~ with same arguments
* Tests
- run ~make tests~ with same arguments
- then run ~../tests/test.sh~ if you are in ~src/~ directory
- this will compare result from olibc with libc that installed by default on your machine
* Usage
- to use this library add ~-Ipath/to/olibcheadersdir -Lpath/to/olibcdir -l:olibc.a~ to your ~CFLAGS~

7
examples/hello.c Normal file
View File

@ -0,0 +1,7 @@
#include <stdio.h>
#include <string.h>
int main() {
puts("Hello, World!");
return 0;
}

46
src/Makefile Normal file
View File

@ -0,0 +1,46 @@
CC=cc
LIBLINUXHEADERS=
LIBLINUXDIR=
LIBLINUXFILE=liblinux.a
CFILES=stdio.c string.c
OBJFILES=../builds/olibc/stdio.o ../builds/olibc/string.o
LIBFILE=../builds/olibc.a
OLIBCHEADERS=include/
OLIBCDIR=../builds
DEBUGFLAGS=-fsanitize=address -fsanitize=leak -fsanitize=undefined -fsanitize=pointer-compare -lasan
LIBFLAGS=-l:$(LIBLINUXFILE)
CFLAGS=--std=c11 -ffreestanding -fno-stack-protector -nostdlib -pedantic -Wall -Wextra -I$(LIBLINUXHEADERS) -I$(OLIBCHEADERS) -L$(LIBLINUXDIR) -L$(OLIBCDIR)
lib:
mkdir -p ../builds/olibc
$(CC) -c stdio.c $(CFLAGS) -o ../builds/olibc/stdio.o $(LIBFLAGS)
$(CC) -c string.c $(CFLAGS) -o ../builds/olibc/string.o $(LIBFLAGS)
ar ruv $(LIBFILE) $(OBJFILES)
ranlib $(LIBFILE)
examples:
mkdir -p ../builds/examples
$(CC) ../examples/hello.c $(CFLAGS) -o ../builds/examples/hello -l:olibc.a $(LIBFLAGS)
tests:
mkdir -p ../builds/tests/string/
$(CC) ../tests/string/strlen.c $(CFLAGS) -o ../builds/tests/string/strlen -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strcmp.c $(CFLAGS) -o ../builds/tests/string/strcmp -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strncmp.c $(CFLAGS) -o ../builds/tests/string/strncmp -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/memcmp.c $(CFLAGS) -o ../builds/tests/string/memcmp -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strcpy.c $(CFLAGS) -o ../builds/tests/string/strcpy -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strncpy.c $(CFLAGS) -o ../builds/tests/string/strncpy -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/memcpy.c $(CFLAGS) -o ../builds/tests/string/memcpy -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strchr.c $(CFLAGS) -o ../builds/tests/string/strchr -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strrchr.c $(CFLAGS) -o ../builds/tests/string/strrchr -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/memchr.c $(CFLAGS) -o ../builds/tests/string/memchr -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strcat.c $(CFLAGS) -o ../builds/tests/string/strcat -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strncat.c $(CFLAGS) -o ../builds/tests/string/strncat -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/memmove.c $(CFLAGS) -o ../builds/tests/string/memmove -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strstr.c $(CFLAGS) -o ../builds/tests/string/strstr -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strspn.c $(CFLAGS) -o ../builds/tests/string/strspn -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strcspn.c $(CFLAGS) -o ../builds/tests/string/strcspn -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/memset.c $(CFLAGS) -o ../builds/tests/string/memset -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strpbrk.c $(CFLAGS) -o ../builds/tests/string/strpbrk -l:olibc.a $(LIBFLAGS)
$(CC) ../tests/string/strtok.c $(CFLAGS) -o ../builds/tests/string/strtok -l:olibc.a $(LIBFLAGS)

12
src/include/stdio.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef _STDIO_H
#define _STDIO_H
#define stdin 0
#define stdout 1
#define stderr 2
int putchar(int c);
int puts(const char *s);
void put_int(long long value); // FIXME: delete this after implemented printf
#endif

26
src/include/string.h Normal file
View File

@ -0,0 +1,26 @@
#ifndef _STRING_H
#define _STRING_H
#include <stddef.h>
size_t strlen(const char *s);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
char *strcpy(char *restrict dest, const char *src);
char *strncpy(char *restrict dest, const char *restrict src, size_t n);
void *memcpy(void *restrict dest, const void *restrict src, size_t n);
char *strchr(const char *s, int c);
void *memchr(const void *s, int c, size_t n);
char *strrchr(const char *s, int c);
char *strcat(char *restrict s1, const char *restrict s2);
char *strncat(char *restrict s1, const char *restrict s2, size_t n);
void *memmove(void *dest, const void *src, size_t n);
char *strstr(const char *s1, const char *s2);
size_t strspn(const char *s1, const char *s2);
size_t strcspn(const char *s1, const char *s2);
void *memset(void *s, int c, size_t n);
char *strpbrk(const char *s1, const char *s2);
char *strtok(char *restrict str, const char *restrict delim);
#endif

44
src/stdio.c Normal file
View File

@ -0,0 +1,44 @@
#include <liblinux.h>
#include <stdio.h>
int putchar(int c) {
sys_write(stdout, &c, 1);
return (unsigned char)c;
}
int puts(const char *s) {
int i = 0;
while(s[i] != '\0') {
putchar(s[i]);
i++;
}
putchar('\n');
return 1; // FIXME
}
void put_int(long long value) { // FIXME: delete this after implemented printf
if (value == 0)
putchar('0');
if (value < 0) {
putchar('-');
value *= (-1);
}
long long power = 1;
while (value > power)
power *= 10;
if (power >= 10)
power /= 10;
while (value != 0) {
int digit = (int)(value / power);
putchar('0' + digit);
if (digit != 0)
value = value - digit * power;
if (power != 1)
power /= 10;
}
putchar('\n');
}

209
src/string.c Normal file
View File

@ -0,0 +1,209 @@
#include <string.h>
size_t strlen(const char *s) {
int i = 0;
while(s[i] != '\0') {
i++;
}
return i;
}
int strcmp(const char *s1, const char *s2) {
int status = sizeof(s1) - sizeof(s2);
if (status > 0 || status < 0) {
return status;
} else {
for (size_t i = 0; i < strlen(s1); i++) {
if (s1[i] != s2[i])
return s1[i] - s2[i];
}
return 0;
}
}
int strncmp(const char *s1, const char *s2, size_t n) {
for (size_t i = 0; i < n; i++) {
if (s1[i] != s2[i])
return s1[i] - s2[i];
}
return 0;
}
int memcmp(const void *s1, const void *s2, size_t n) {
int status = sizeof(s1) - sizeof(s2);
if (status > 0 || status < 0) {
return status;
} else {
const unsigned char* first = s1;
const unsigned char* second = s2;
for (size_t i = 0; i < n; i++) {
if (first[i] != second[i])
return first[i] - second[i];
}
return 0;
}
}
char *strcpy(char *restrict dest, const char *src) {
int i = 0;
while(src[i] != '\0') {
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return dest;
}
char *strncpy(char *restrict dest, const char *restrict src, size_t n) {
size_t i;
for (i = 0; i < n && src[i] != '\0'; i++) {
dest[i] = src[i];
}
for (; i < n; i++) {
dest[i] = '\0';
}
return dest;
}
void *memcpy(void *restrict dest, const void *restrict src, size_t n) {
size_t i;
unsigned char* chardest = dest;
const unsigned char* charsrc = src;
for (i = 0; i < n && charsrc[i] != '\0'; i++) {
chardest[i] = charsrc[i];
}
for (; i < n; i++) {
chardest[i] = '\0';
}
return (void*)chardest;
}
char *strchr(const char *s, int c) {
if (s == NULL) return NULL;
char cc = c;
size_t i = 0;
for (; s[i] != cc && s[i] != '\0'; i++);
if (i == strlen(s)) return NULL;
if (s[i] == '\0' && i == 1) return NULL;
return (char*)(s+i);
}
char *strrchr(const char *s, int c) {
char cc = (char)c;
size_t i = sizeof(s);
for (; i >= sizeof(s) && s[i] != cc; i--);
return (char*)(s+i);
}
void *memchr(const void *s, int c, size_t n) {
const unsigned char* ss = s;
char uc = (unsigned char)c;
size_t i = 0;
for (; i < n && ss[i] != uc; i++);
return (void*)(ss+i);
}
char *strcat(char *restrict s1, const char *restrict s2) {
size_t i = 0;
size_t j = 0;
for (; s1[i] != '\0'; i++);
for (; s2[j] != '\0'; j++) {
s1[i] = s2[j];
i++;
}
return s1;
}
char *strncat(char *restrict s1, const char *restrict s2, size_t n) {
size_t i = 0;
size_t j = 0;
for (; s1[i] != '\0'; i++);
for (; j < n-1; j++) {
s1[i] = s2[j];
i++;
}
s1[i] = '\0';
return s1;
}
void *memmove(void *dest, const void *src, size_t n) {
unsigned char* chardest = dest;
const unsigned char* charsrc = src;
if (chardest == charsrc) {
return (void*)chardest;
} else if (chardest > charsrc) {
for (size_t i = n; i > sizeof(charsrc); i--)
chardest[i] = charsrc[i];
} else {
for (int i = 0; i < chardest - charsrc; i++)
chardest[i] = charsrc[i];
}
memcpy(chardest, charsrc, n);
return (void*)chardest;
}
char *strstr(const char *s1, const char *s2) {
if (strncmp(strchr(s1, s2[0]), s2, sizeof(s2))) {
return strchr(s1, s2[0]);
} else {
return (char*)s1;
}
}
size_t strspn(const char *s1, const char *s2) {
size_t i = 0;
size_t counter = 0;
for( ; i < strlen(s2); i++) {
if (strchr(s2, s1[i])) {
counter++;
} else
break;
}
return counter;
}
size_t strcspn(const char *s1, const char *s2) {
if (s1 == NULL) return 0;
size_t i = 0;
size_t counter = 0;
for( ; i < strlen(s1); i++) {
if (strchr(s2, s1[i]) == NULL) {
counter++;
} else
break;
}
return counter;
}
void *memset(void *s, int c, size_t n) {
unsigned char uc = c;
unsigned char* ss = s;
for (size_t i = 0; i < n; i++)
ss[i] = uc;
return (void*)ss;
}
char *strpbrk(const char *s1, const char *s2) {
for(size_t i = 0; i < sizeof(s2); i++) {
if (strchr(s1, s2[i]))
return strchr(s1, s2[i]);
}
return NULL;
}
char *strtok(char *restrict str, const char *restrict delim) {
static char *ptr;
if (str == NULL && !(str = ptr))
return NULL;
str += strspn(str, delim);
if (!*str) return ptr = NULL;
ptr = str + strcspn(str, delim);
if (*ptr) *ptr++ = 0;
else ptr = 0;
return str;
}

10
tests/string/memchr.c Normal file
View File

@ -0,0 +1,10 @@
// This is test program for memchr function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s[] = "Hi.Hello.Hey!";
puts(memchr(s, '.', 9));
return 0;
}

12
tests/string/memcmp.c Normal file
View File

@ -0,0 +1,12 @@
// This is test program for memcmp function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s1[] = "This is test string";
char s2[] = "This is second test string";
put_int(memcmp(s1, s2, strlen(s1)));
put_int(memcmp(s2, s1, strlen(s1)));
return 0;
}

11
tests/string/memcpy.c Normal file
View File

@ -0,0 +1,11 @@
// This is test program for memcpy function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s1[] = "FIRSTT";
char s2[] = "SECOND";
puts(memcpy(s1, s2, 4));
return 0;
}

11
tests/string/memmove.c Normal file
View File

@ -0,0 +1,11 @@
// This is test program for memmove function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s1[] = "FIRSTT";
char s2[] = "SECOND";
puts(memmove(s1, s2, 4));
return 0;
}

10
tests/string/memset.c Normal file
View File

@ -0,0 +1,10 @@
// This is test program for memset function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s[] = "TEST HELLO";
puts(memset(s, '$', 4));
return 0;
}

11
tests/string/strcat.c Normal file
View File

@ -0,0 +1,11 @@
// This is test program for strcat function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s1[] = "Hello,";
char s2[] = " World!";
puts(strcat(s1, s2));
return 0;
}

10
tests/string/strchr.c Normal file
View File

@ -0,0 +1,10 @@
// This is test program for strchr function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s[] = "Hi.Hello.Hey!";
puts(strchr(s, '.'));
return 0;
}

12
tests/string/strcmp.c Normal file
View File

@ -0,0 +1,12 @@
// This is test program for strcmp function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s1[] = "This is test string";
char s2[] = "This is second test string";
put_int(strcmp(s1, s2));
put_int(strcmp(s2, s1));
return 0;
}

11
tests/string/strcpy.c Normal file
View File

@ -0,0 +1,11 @@
// This is test program for strcpy function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s1[] = "FIRSTT";
char s2[] = "SECOND";
puts(strcpy(s1, s2));
return 0;
}

11
tests/string/strcspn.c Normal file
View File

@ -0,0 +1,11 @@
// This is test program for strcspn function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s1[] = "HELLO, this won't be counted";
char s2[] = "ihst";
put_int(strcspn(s1, s2));
return 0;
}

10
tests/string/strlen.c Normal file
View File

@ -0,0 +1,10 @@
// This is test program for strlen function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s[] = "This is test string";
put_int(strlen(s));
return 0;
}

11
tests/string/strncat.c Normal file
View File

@ -0,0 +1,11 @@
// This is test program for strncat function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s1[] = "Hello,";
char s2[] = " World! this should be in output";
puts(strncat(s1, s2, 7));
return 0;
}

12
tests/string/strncmp.c Normal file
View File

@ -0,0 +1,12 @@
// This is test program for strncmp function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s1[] = "This is test string";
char s2[] = "This is second test string";
put_int(strncmp(s1, s2, strlen(s1)));
put_int(strncmp(s2, s1, strlen(s1)));
return 0;
}

11
tests/string/strncpy.c Normal file
View File

@ -0,0 +1,11 @@
// This is test program for strncpy function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s1[] = "FIRSTT";
char s2[] = "SECOND";
puts(strncpy(s1, s2, 4));
return 0;
}

10
tests/string/strpbrk.c Normal file
View File

@ -0,0 +1,10 @@
// This is test program for strpbrk function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s[] = "Hello, World!";
puts(strpbrk(s, "W"));
return 0;
}

10
tests/string/strrchr.c Normal file
View File

@ -0,0 +1,10 @@
// This is test program for strrchr function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s[] = "Hi.Hello.Hey!";
puts(strrchr(s, '.'));
return 0;
}

11
tests/string/strspn.c Normal file
View File

@ -0,0 +1,11 @@
// This is test program for strspn function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s1[] = "HELLO, this won't be counted";
char s2[] = "EHLOL";
put_int(strspn(s1, s2));
return 0;
}

10
tests/string/strstr.c Normal file
View File

@ -0,0 +1,10 @@
// This is test program for strstr function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s[] = "Hi.Hello.Hey!";
puts(strstr(s, "."));
return 0;
}

11
tests/string/strtok.c Normal file
View File

@ -0,0 +1,11 @@
// This is test program for strtok function from string.h
#include <string.h>
#include <stdio.h>
int main() {
char s[] = "Hi.Hey.Hello";
puts(strtok(s, "."));
puts(strtok(NULL, "."));
return 0;
}

38
tests/test.sh Executable file
View File

@ -0,0 +1,38 @@
#!/bin/bash
# Simple script for testing olibc
# Recommended to execute in directory tests/ or src/
CC=cc
cd ../builds/tests
for i in $(ls); do
if [ -d $i ]
then
echo ------------------------
echo --- TESTING $i.h ---
echo ------------------------
echo
cd $i
for j in $(ls); do
if [ -x $j ] && [[ $j != *"-cc"* ]]
then
cat $(cd ../../../tests/$i ; pwd)/$j.c | sed 's/put_int(/printf("%d\\n",/' | cc -x c -o $j-cc -
./$j > tmp
./$j-cc > tmp-cc
if [ "$(diff tmp tmp-cc)" == "" ]
then
echo $j - TEST SUCCESS
else
echo $j - TEST FAILED
echo "---- DIFF OUTPUT ----"
diff -y tmp tmp-cc
fi
echo "---"
fi
done
fi
done