mirror of
https://github.com/cowmonk/cowos.git
synced 2025-12-26 11:16:50 +00:00
cowos uses it's own cowlibc! big W
This commit is contained in:
parent
435b2bf2a0
commit
b08b0728ae
18 changed files with 188 additions and 3050 deletions
100
kernel/klibc/stdio.c
Normal file
100
kernel/klibc/stdio.c
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
#include <limits.h>
|
||||
#include <stdarg.h>
|
||||
/* #include <tty.h> */
|
||||
#include <klibc/stdio.h>
|
||||
#include <klibc/string.h>
|
||||
|
||||
/* returns 1 for true, adhering to suckless */
|
||||
static int
|
||||
print(const char* data, size_t length)
|
||||
{
|
||||
const unsigned char* bytes = (const unsigned char*) data;
|
||||
for (size_t i = 0; i < length; i++) {
|
||||
if (putchar(bytes[i]) == EOF) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* todo: set errno to eoverflow for some cases in which errno should not be used */
|
||||
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) {
|
||||
return -1;
|
||||
}
|
||||
if (!print(format, amount))
|
||||
return -1;
|
||||
format += amount;
|
||||
written += amount;
|
||||
continue;
|
||||
}
|
||||
|
||||
const char* format_begun_at = format++;
|
||||
|
||||
if (*format == 'c') {
|
||||
format++;
|
||||
char c = (char) va_arg(parameters, int /* char promotes to int */);
|
||||
if (!maxrem) {
|
||||
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) {
|
||||
return -1;
|
||||
}
|
||||
if (!print(str, len)) {
|
||||
return -1;
|
||||
}
|
||||
written += len;
|
||||
} else {
|
||||
format = format_begun_at;
|
||||
size_t len = strlen(format);
|
||||
if (maxrem < len) {
|
||||
return -1;
|
||||
}
|
||||
if (!print(format, len)) {
|
||||
return -1;
|
||||
}
|
||||
written += len;
|
||||
format += len;
|
||||
}
|
||||
}
|
||||
|
||||
va_end(parameters);
|
||||
return written;
|
||||
}
|
||||
|
||||
/* todo: Implement stdio and the write system call. */
|
||||
int
|
||||
putchar(int ic)
|
||||
{
|
||||
return ic;
|
||||
}
|
||||
|
||||
int
|
||||
puts(const char* string)
|
||||
{
|
||||
return printf("%s\n", string);
|
||||
}
|
||||
62
kernel/klibc/string.c
Normal file
62
kernel/klibc/string.c
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#include <string.h>
|
||||
|
||||
size_t
|
||||
strlen(const char* str)
|
||||
{
|
||||
size_t len = 0;
|
||||
while (str[len]) {
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
void*
|
||||
memset(void* bufptr, int value, size_t size)
|
||||
{
|
||||
unsigned char* buf = (unsigned char*) bufptr;
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
buf[i] = (unsigned char) value;
|
||||
}
|
||||
return bufptr;
|
||||
}
|
||||
|
||||
void*
|
||||
memmove(void* dstptr, const void* srcptr, size_t size)
|
||||
{
|
||||
unsigned char* dst = (unsigned char*) dstptr;
|
||||
const unsigned char* src = (const unsigned char*) srcptr;
|
||||
if (dst < src) {
|
||||
for (size_t i = 0; i < size; i++)
|
||||
dst[i] = src[i];
|
||||
} else {
|
||||
for (size_t i = size; i != 0; i--)
|
||||
dst[i-1] = src[i-1];
|
||||
}
|
||||
return dstptr;
|
||||
}
|
||||
|
||||
void*
|
||||
memcpy(void* restrict dstptr, const void* restrict srcptr, size_t size)
|
||||
{
|
||||
unsigned char* dst = (unsigned char*) dstptr;
|
||||
const unsigned char* src = (const unsigned char*) srcptr;
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
dst[i] = src[i];
|
||||
}
|
||||
return dstptr;
|
||||
}
|
||||
|
||||
int
|
||||
memcmp(const void* aptr, const void* bptr, size_t size)
|
||||
{
|
||||
const unsigned char* a = (const unsigned char*) aptr;
|
||||
const unsigned char* b = (const unsigned char*) bptr;
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
if (a[i] < b[i]) {
|
||||
return -1;
|
||||
} else if (b[i] < a[i]) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue