From 8e8193e7efed1c0c23206a5f76fd6855cfb4c59d Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Sat, 4 Nov 2017 06:04:04 +0000 Subject: [PATCH] Add "libk.a" --- Makefile | 15 +++++++---- arch/Makefile | 2 +- arch/util.h | 66 +++---------------------------------------------- libk/.gitignore | 1 + libk/Makefile | 21 ++++++++++++++++ libk/itoa.c | 39 +++++++++++++++++++++++++++++ libk/memset.c | 8 ++++++ libk/strlen.c | 10 ++++++++ 8 files changed, 93 insertions(+), 69 deletions(-) create mode 100644 libk/.gitignore create mode 100644 libk/Makefile create mode 100644 libk/itoa.c create mode 100644 libk/memset.c create mode 100644 libk/strlen.c diff --git a/Makefile b/Makefile index 977701f..793ce53 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,10 @@ run: run-iso -all: all-kernel all-iso +all: all-kernel all-iso all-libk +clean: clean-kernel clean-iso clean-libk -clean: clean-iso clean-kernel - -all-kernel: - make all -C arch +all-kernel: all-libk + make all -C arch LIBK=$(shell pwd)/libk/libk.a clean-kernel: make clean -C arch @@ -18,3 +17,9 @@ all-iso: all-kernel clean-iso: make clean -C iso + +all-libk: + make all -C libk + +clean-libk: + make clean -C libk diff --git a/arch/Makefile b/arch/Makefile index 0add25d..4371eed 100644 --- a/arch/Makefile +++ b/arch/Makefile @@ -28,7 +28,7 @@ clean: rm -f kernel $(OBJS) kernel: $(OBJS) - $(CC) -T linker.ld -o $@ -ffreestanding -nostdlib -lgcc $(OBJS) + $(CC) -T linker.ld -o $@ -ffreestanding -nostdlib -lgcc $(OBJS) $(LIBK) grub-file --is-x86-multiboot2 $@ %.c.o: %.c diff --git a/arch/util.h b/arch/util.h index 8db205f..3cd3c92 100644 --- a/arch/util.h +++ b/arch/util.h @@ -1,68 +1,8 @@ #ifndef KERNELMQ_INCLUDED_UTIL #define KERNELMQ_INCLUDED_UTIL 1 -static inline void memset(void *buffer, unsigned char value, unsigned int size); -static inline unsigned int strlen(const char *s); -static inline void itoa(char *buf, int base, int d); - -void memset(void *buffer, unsigned char value, unsigned int size) -{ - const unsigned char *end = buffer + size; - - for (unsigned char *p = buffer; p < end; ++p) { - *p = value; - } -} - -unsigned int strlen(const char *const s) -{ - unsigned int result = 0; - - while (s[result]) { - ++result; - } - - return result; -} - -void itoa(char *buf, int base, int d) -{ - char *p = buf; - char *p1, *p2; - unsigned long ud = d; - int divisor = 10; - - // If %d is specified and D is minus, put '-' in the head. - if (base == 'd' && d < 0) { - *p++ = '-'; - buf++; - ud = -d; - } - else if (base == 'x') { - divisor = 16; - } - - // Divide UD by DIVISOR until UD == 0. - do { - int remainder = ud % divisor; - *p++ = (remainder < 10) ? remainder + '0' : remainder + 'A' - 10; - } - while (ud /= divisor); - - // Terminate BUF. - *p = 0; - - // Reverse BUF. - p1 = buf; - p2 = p - 1; - - while (p1 < p2) { - char tmp = *p1; - *p1 = *p2; - *p2 = tmp; - p1++; - p2--; - } -} +void memset(void *buffer, unsigned char value, unsigned int size); +unsigned int strlen(const char *s); +void itoa(char *buf, int base, int d); #endif diff --git a/libk/.gitignore b/libk/.gitignore new file mode 100644 index 0000000..9f644e5 --- /dev/null +++ b/libk/.gitignore @@ -0,0 +1 @@ +/libk.a diff --git a/libk/Makefile b/libk/Makefile new file mode 100644 index 0000000..4f841f8 --- /dev/null +++ b/libk/Makefile @@ -0,0 +1,21 @@ +CCPREFIX = i686-elf- + +AR = $(CCPREFIX)ar +CC = $(CCPREFIX)gcc + +CFLAGS = -std=gnu99 -ffreestanding -nostdinc -fno-builtin -fno-stack-protector -Wall -Wextra + +OUTPUT = libk.a + +OBJS = memset.o strlen.o itoa.o + +all: $(OUTPUT) + +clean: + rm -f $(OUTPUT) $(OBJS) + +$(OUTPUT): $(OBJS) + $(AR) -rcs $@ $(OBJS) + +%.o: %.c + $(CC) -c $< -o $@ $(CFLAGS) diff --git a/libk/itoa.c b/libk/itoa.c new file mode 100644 index 0000000..729b3f8 --- /dev/null +++ b/libk/itoa.c @@ -0,0 +1,39 @@ +void itoa(char *buf, int base, int d) +{ + char *p = buf; + char *p1, *p2; + unsigned long ud = d; + int divisor = 10; + + // If %d is specified and D is minus, put '-' in the head. + if (base == 'd' && d < 0) { + *p++ = '-'; + buf++; + ud = -d; + } + else if (base == 'x') { + divisor = 16; + } + + // Divide UD by DIVISOR until UD == 0. + do { + int remainder = ud % divisor; + *p++ = (remainder < 10) ? remainder + '0' : remainder + 'A' - 10; + } + while (ud /= divisor); + + // Terminate BUF. + *p = 0; + + // Reverse BUF. + p1 = buf; + p2 = p - 1; + + while (p1 < p2) { + char tmp = *p1; + *p1 = *p2; + *p2 = tmp; + p1++; + p2--; + } +} diff --git a/libk/memset.c b/libk/memset.c new file mode 100644 index 0000000..c79c884 --- /dev/null +++ b/libk/memset.c @@ -0,0 +1,8 @@ +void memset(void *const buffer, const unsigned char value, const unsigned int size) +{ + const unsigned char *end = buffer + size; + + for (unsigned char *p = buffer; p < end; ++p) { + *p = value; + } +} diff --git a/libk/strlen.c b/libk/strlen.c new file mode 100644 index 0000000..40f47b5 --- /dev/null +++ b/libk/strlen.c @@ -0,0 +1,10 @@ +unsigned int strlen(const char *const s) +{ + unsigned int result = 0; + + while (s[result]) { + ++result; + } + + return result; +}