mirror of
https://github.com/tailix/kernel.git
synced 2024-11-20 11:16:10 -05:00
Add "libk.a"
This commit is contained in:
parent
3db7363998
commit
8e8193e7ef
8 changed files with 93 additions and 69 deletions
15
Makefile
15
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
|
||||
|
|
|
@ -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
|
||||
|
|
66
arch/util.h
66
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
|
||||
|
|
1
libk/.gitignore
vendored
Normal file
1
libk/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/libk.a
|
21
libk/Makefile
Normal file
21
libk/Makefile
Normal file
|
@ -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)
|
39
libk/itoa.c
Normal file
39
libk/itoa.c
Normal file
|
@ -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--;
|
||||
}
|
||||
}
|
8
libk/memset.c
Normal file
8
libk/memset.c
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
10
libk/strlen.c
Normal file
10
libk/strlen.c
Normal file
|
@ -0,0 +1,10 @@
|
|||
unsigned int strlen(const char *const s)
|
||||
{
|
||||
unsigned int result = 0;
|
||||
|
||||
while (s[result]) {
|
||||
++result;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
Loading…
Reference in a new issue