Revert "Revert "Initial commit - work done up through 11 Feb 2020""
This reverts commit 56f77cf071
.
oops
This commit is contained in:
parent
56f77cf071
commit
36cb297b57
27 changed files with 478 additions and 0 deletions
107
stdio/printf.c
Executable file
107
stdio/printf.c
Executable file
|
@ -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);
|
||||
}
|
6
stdio/printf.libk.d
Executable file
6
stdio/printf.libk.d
Executable file
|
@ -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
|
16
stdio/putchar.c
Executable file
16
stdio/putchar.c
Executable file
|
@ -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;
|
||||
}
|
3
stdio/putchar.libk.d
Executable file
3
stdio/putchar.libk.d
Executable file
|
@ -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
|
5
stdio/puts.c
Executable file
5
stdio/puts.c
Executable file
|
@ -0,0 +1,5 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int puts(const char * string) {
|
||||
return printf("%s\n", string);
|
||||
}
|
1
stdio/puts.libk.d
Executable file
1
stdio/puts.libk.d
Executable file
|
@ -0,0 +1 @@
|
|||
stdio/puts.libk.o: stdio/puts.c include/stdio.h include/sys/cdefs.h
|
Loading…
Add table
Add a link
Reference in a new issue