libc

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 36cb297b57cf1cb24556f000103660379150d927
parent 56f77cf07113c9f744e5b4875db8b47f65e99a0e
Author: Gitea <gitea@fake.local>
Date:   Tue, 11 Feb 2020 23:23:27 -0600

Revert "Revert "Initial commit - work done up through 11 Feb 2020""

This reverts commit 56f77cf07113c9f744e5b4875db8b47f65e99a0e.

oops

Diffstat:
AREADME | 0
Aarch/i386/make.config | 9+++++++++
Ainclude/math.h | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ainclude/stdio.h | 14++++++++++++++
Ainclude/stdlib.h | 9+++++++++
Ainclude/string.h | 15+++++++++++++++
Ainclude/sys/cdefs.h | 6++++++
Ainclude/sys/types.h | 4++++
Amakefile | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astdio/printf.c | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Astdio/printf.libk.d | 6++++++
Astdio/putchar.c | 16++++++++++++++++
Astdio/putchar.libk.d | 3+++
Astdio/puts.c | 5+++++
Astdio/puts.libk.d | 1+
Astdlib/abort.c | 15+++++++++++++++
Astdlib/abort.libk.d | 2++
Astring/memcmp.c | 17+++++++++++++++++
Astring/memcmp.libk.d | 3+++
Astring/memcpy.c | 12++++++++++++
Astring/memcpy.libk.d | 3+++
Astring/memmove.c | 20++++++++++++++++++++
Astring/memmove.libk.d | 3+++
Astring/memset.c | 11+++++++++++
Astring/memset.libk.d | 3+++
Astring/strlen.c | 9+++++++++
Astring/strlen.libk.d | 3+++
27 files changed, 479 insertions(+), 0 deletions(-)

diff --git a/README b/README diff --git a/arch/i386/make.config b/arch/i386/make.config @@ -0,0 +1,8 @@ +ARCH_CFLAGS= +ARCH_CPPFLAGS= +KERNEL_ARCH_CFLAGS= +KERNEL_ARCH_CPPFLAGS= + +ARCH_FREEOBJS=\ + +ARCH_HOSTEDOBJS=\+ \ No newline at end of file diff --git a/include/math.h b/include/math.h @@ -0,0 +1,91 @@ +#ifndef _MATH_H +#define _MATH_H + +#include <sys/types.h> + +#if FLT_EVAL_METHOD == 1 +typedef float_t double; +typedef double_t double; +#elseif FLT_EVAL_METHOD == 2 +typedef float_t long double; +typedef double_t long double; +#else +typedef float_t float; +typedef double_t double; +#endif + +#define M_E 2.718281828 +#define M_PI 3.14159265 +#define PI 3.14159265 + +double acos(double); +float acosf(float); +long double acosl(long double); + +double asin(double); +float asinf(float); +long double asinl(long double); + +double atan(double); +float atanf(float); +long double atanl(long double); + +double atan2(double); +float atan2f(float); +long double atan2l(long double); + +double cos(double); +float cosf(float); +long double cosl(long double); + +double sin(double); +float sinf(float); +long double sinl(long double); + +double tan(double); +float tanf(float); +long double tanl(long double); + +double acosh(double); +float acoshf(float); +long double acoshl(long double); + +double asinh(double); +float asinhf(float); +long double asinhl(long double); + +double atanh(double); +float atanhf(float); +long double atanhl(long double); + +double cosh(double); +float coshf(float); +long double coshl(long double); + +double sinh(double); +float sinhf(float); +long double sinhl(long double); + +double tanh(double); +float tanhf(float); +long double tanhl(long double); + +double exp(double); +float expf(float); +long double expl(double); + +double exp2(double); +float exp2f(float); +long double exp2l(double); + +double expm1(double); +float expm1f(float); +long double expm1l(double); + +double frexp(double, int *); +float frexpf(float, int *); +long double frexpl(double, int *); + + + +#endif diff --git a/include/stdio.h b/include/stdio.h @@ -0,0 +1,14 @@ +#ifndef _STDIO_H +#define _STDIO_H + +#include <sys/cdefs.h> + +#define EOF (-1) + +int printf(const char * __restrict, ...); + +int putchar(int); + +int puts(const char *); + +#endif diff --git a/include/stdlib.h b/include/stdlib.h @@ -0,0 +1,9 @@ +#ifndef _STDLIB_H +#define _STDLIB_H + +#include <sys/cdefs.h> + +__attribute__((__noreturn__)) +void abort(void); + +#endif diff --git a/include/string.h b/include/string.h @@ -0,0 +1,15 @@ +#ifndef _STRING_H +#define _STRING_H + +#include <sys/cdefs.h> + +#include <stddef.h> + +int memcmp(const void *, const void *, size_t); +void * memcpy(void * __restrict, const void * __restrict, size_t); +void * memmove(void *, const void *, size_t); +void * memset(void *, int, size_t); + +size_t strlen(const char *); + +#endif diff --git a/include/sys/cdefs.h b/include/sys/cdefs.h @@ -0,0 +1,6 @@ +#ifndef _SYS_CDEFS_H +#define _SYS_CDEFS_H + +#define __fenix_libc 1 + +#endif diff --git a/include/sys/types.h b/include/sys/types.h @@ -0,0 +1,4 @@ +#ifndef _SYS_TYPES_H +#define _SYS_TYPES_H + +#endif /* not _HEADER */ diff --git a/makefile b/makefile @@ -0,0 +1,92 @@ +DEFAULT_HOST!=../default-host.sh +HOST?=DEFAULT_HOST +HOSTARCH!=../target-triplet-to-arch.sh $(HOST) + +CFLAGS?=-O2 -g +CPPFLAGS?= +LDFLAGS?= +LIBS?= + +DESTDIR?= +PREFIX?=/usr/local +EXEC_PREFIX?=$(PREFIX) +INCLUDEDIR?=$(PREFIX)/include +LIBDIR?=$(EXEC_PREFIX)/lib + +CFLAGS:=$(CFLAGS) -ffreestanding -Wall -Wextra +CPPFLAGS:=$(CPPFLAGS) -D__is_libc -Iinclude +LIBK_CFLAGS:=$(CFLAGS) +LIBK_CPPFLAGS:=$(CPPFLAGS) -D__is_libk + +ARCHDIR=arch/$(HOSTARCH) + +include $(ARCHDIR)/make.config + +CFLAGS:=$(CFLAGS) $(ARCH_CFLAGS) +CPPFLAGS:=$(CPPFLAGS) $(ARCH_CPPFLAGS) +LIBK_CFLAGS:=$(LIBK_CFLAGS) $(KERNEL_ARCH_CFLAGS) +LIBK_CPPFLAGS:=$(LIBK_CPPFLAGS) $(KERNEL_ARCH_CPPFLAGS) + +FREEOBJS=\ +$(ARCH_FREEOBJS) \ +stdio/printf.o \ +stdio/putchar.o \ +stdio/puts.o \ +stdlib/abort.o \ +string/memcmp.o \ +string/memcpy.o \ +string/memmove.o \ +string/memset.o \ +string/strlen.o \ + +HOSTEDOBJS=\ +$(ARCH_HOSTEDOBJS) \ + +OBJS=\ +$(FREEOBJS) \ +$(HOSTEDOBJS) \ + +LIBK_OBJS=$(FREEOBJS:.o=.libk.o) + +BINARIES=libk.a #Add libc.a later + +.PHONY: all clean install install-headers install-libs +.SUFFIXES: .o .libk.o .c .S + +all: $(BINARIES) + +libc.a: $(OBJS) + $(AR) rcs $@ $(OBJS) + +libk.a: $(LIBK_OBJS) + $(AR) rcs $@ $(LIBK_OBJS) + +.c.o: + $(CC) -MD -c $< -o $@ -std=c99 $(CFLAGS) $(CPPFLAGS) + +.c.S: + $(CC) -MD -c $< -o $@ $(CFLAGS) $(CPPFLAGS) + +.c.libk.o: + $(CC) -MD -c $< -o $@ -std=c99 $(LIBK_CFLAGS) $(LIBK_CPPFLAGS) + +.S.libk.o: + $(CC) -MD -c $< -o $@ $(LIBK_CFLAGS) $(LIBK_CPPFLAGS) + +clean: + rm -f $(BINARIES) *.a + rm -f $(OBJS) $(LIBK_OBJS) *.o */*.o */*/*.o + rm -f $(OBJS:.o=.d) $(LIBK_OBJS:.o=.d) *.d */*.d */*/*.d + +install: install-headers install-libs + +install-headers: + mkdir -p $(DESTDIR)$(INCLUDEDIR) + cp -R include/. $(DESTDIR)$(INCLUDEDIR)/. + +install-libs: $(BINARIES) + mkdir -p $(DESTDIR)$(INCLUDEDIR) + cp $(BINARIES) $(DESTDIR)$(LIBDIR) + +-include $(OBJS:.o=.d) +-include $(LIBK_OBJS:.o=.d) diff --git a/stdio/printf.c b/stdio/printf.c @@ -0,0 +1,107 @@ +#include <limits.h> +#include <stdbool.h> +#include <stdarg.h> +#include <stdio.h> +#include <string.h> + +static bool print(const char * str, size_t len) { + const unsigned char * bytes = (const unsigned char *) str; + + for(size_t i = 0; i < len; i++) { + if(putchar(bytes[i]) == EOF) { + return false; + } + } + + return true; +} + +int printf(const char * restrict format, ...) { + va_list parameters; + va_start(parameters, format); + + int written = 0; + + while(*format != '\0') { + size_t maxrem = INT_MAX - written; + + if(format[0] != '%' || format[1] == '%') { + if(format[0] == '%') { + format++; + } + + size_t amount = 1; + + while(format[amount] && format[amount] != '%') { + amount++; + } + + if(maxrem < amount) { + /* TODO: Set errno to EOVERFLOW after implementing errno.h */ + return -1; + } + + if(!print(format, amount)) { + return -1; + } + + format += amount; + written += amount; + continue; + } + + const char * format_begun_at = format++; + + /* TODO: Implement all format specifiers (%g, %o, &c.) */ + if(*format == 'c') { + format++; + char c = (char) va_arg(parameters, int); + + if(!maxrem) { + /* TODO: Set errno to EOVERFLOW after implementing errno.h */ + return -1; + } + + if(!print(&c, sizeof(c))) { + return -1; + } + + written++; + } + else if(*format == 's') { + format++; + const char * str = va_arg(parameters, const char *); + size_t len = strlen(str); + + if(maxrem < len) { + /* TODO: Set errno to EOVERFLOW after implementing errno.h */ + return -1; + } + + if(!print(str, len)) { + return -1; + } + + written += len; + } + else { + format = format_begun_at; + size_t len = strlen(format); + + if(maxrem < len) { + /* TODO: Set errno to EOVERFLOW after implementing errno.h */ + return -1; + } + + if(!print(format, len)) { + return -1; + } + + format += len; + } + } + + va_end(parameters); + + return(written); +} diff --git a/stdio/printf.libk.d b/stdio/printf.libk.d @@ -0,0 +1,6 @@ +stdio/printf.libk.o: stdio/printf.c \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include-fixed/limits.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stdbool.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stdarg.h \ + include/stdio.h include/sys/cdefs.h include/string.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h diff --git a/stdio/putchar.c b/stdio/putchar.c @@ -0,0 +1,16 @@ +#include <stdio.h> + +#ifdef __is_libk +#include <kernel/tty.h> +#endif + +int putchar(int ic) { +#ifdef __is_libk + char c = (char) ic; + term_write(&c, sizeof(c)); +#else + /* TODO: Implement write system call to do this part */ +#endif + + return ic; +} diff --git a/stdio/putchar.libk.d b/stdio/putchar.libk.d @@ -0,0 +1,3 @@ +stdio/putchar.libk.o: stdio/putchar.c include/stdio.h include/sys/cdefs.h \ + /home/helmsulfrinn/Documents/projects/coding/active/fenix/sysroot/usr/include/kernel/tty.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h diff --git a/stdio/puts.c b/stdio/puts.c @@ -0,0 +1,5 @@ +#include <stdio.h> + +int puts(const char * string) { + return printf("%s\n", string); +} diff --git a/stdio/puts.libk.d b/stdio/puts.libk.d @@ -0,0 +1 @@ +stdio/puts.libk.o: stdio/puts.c include/stdio.h include/sys/cdefs.h diff --git a/stdlib/abort.c b/stdlib/abort.c @@ -0,0 +1,15 @@ +#include <stdio.h> +#include <stdlib.h> + +__attribute__((__noreturn__)) +void abort(void) { +#if defined(__is_libk) + /* TODO: Add proper kernel panic */ + printf("kernel: panic: abort()\n"); +#else + /* TODO: Abnormally terminate process like by SIGABRT */ + printf("abort()\n"); +#endif + while(1) {} + __builtin_unreachable(); +} diff --git a/stdlib/abort.libk.d b/stdlib/abort.libk.d @@ -0,0 +1,2 @@ +stdlib/abort.libk.o: stdlib/abort.c include/stdio.h include/sys/cdefs.h \ + include/stdlib.h diff --git a/string/memcmp.c b/string/memcmp.c @@ -0,0 +1,17 @@ +#include <string.h> + +int memcmp(const void * a_ptr, const void * b_ptr, size_t size) { + const unsigned char * a = (const unsigned char *) a_ptr; + const unsigned char * b = (const unsigned char *) b_ptr; + + for(size_t i = 0; i < size; i++) { + if(a[i] < b[i]) { + return -1; + } + else if(a[i] > b[i]) { + return 1; + } + } + + return 0; +} diff --git a/string/memcmp.libk.d b/string/memcmp.libk.d @@ -0,0 +1,3 @@ +string/memcmp.libk.o: string/memcmp.c include/string.h \ + include/sys/cdefs.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h diff --git a/string/memcpy.c b/string/memcpy.c @@ -0,0 +1,12 @@ +#include <string.h> + +void * memcpy(void * restrict dest, const void * restrict src, size_t size) { + unsigned char * to = (unsigned char *) dest; + const unsigned char * from = (const unsigned char *) src; + + for(size_t i = 0; i < size; i++) { + to[i] = from[i]; + } + + return dest; +} diff --git a/string/memcpy.libk.d b/string/memcpy.libk.d @@ -0,0 +1,3 @@ +string/memcpy.libk.o: string/memcpy.c include/string.h \ + include/sys/cdefs.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h diff --git a/string/memmove.c b/string/memmove.c @@ -0,0 +1,20 @@ +#include <string.h> + +void * memmove(void * dest, const void * src, size_t size) { + unsigned char * to = (unsigned char *) dest; + const unsigned char * from = (const unsigned char *) src; + + /* We check this to make sure we don't overwrite memory */ + if(to < from) { + for(size_t i = 0; i < size; i++) { + to[i] = from[i]; + } + } + else { + for(size_t i = size; i != 0; i--) { + to[i - 1] = from[i - 1]; + } + } + + return dest; +} diff --git a/string/memmove.libk.d b/string/memmove.libk.d @@ -0,0 +1,3 @@ +string/memmove.libk.o: string/memmove.c include/string.h \ + include/sys/cdefs.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h diff --git a/string/memset.c b/string/memset.c @@ -0,0 +1,11 @@ +#include <string.h> + +void * memset(void * buffer, int value, size_t size) { + unsigned char * buf = (unsigned char *) buffer; + + for(size_t i = 0; i < size; i++) { + buf[i] = (unsigned char) value; + } + + return buffer; +} diff --git a/string/memset.libk.d b/string/memset.libk.d @@ -0,0 +1,3 @@ +string/memset.libk.o: string/memset.c include/string.h \ + include/sys/cdefs.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h diff --git a/string/strlen.c b/string/strlen.c @@ -0,0 +1,9 @@ +#include <string.h> + +size_t strlen(const char * str) { + size_t len = 0; + while(str[len] != '\0') { + len++; + } + return len; +} diff --git a/string/strlen.libk.d b/string/strlen.libk.d @@ -0,0 +1,3 @@ +string/strlen.libk.o: string/strlen.c include/string.h \ + include/sys/cdefs.h \ + /usr/home/helmsulfrinn/opt/cross/lib/gcc/i686-elf/9.2.0/include/stddef.h