diff options
Diffstat (limited to 'src/kernel/lib')
-rw-r--r-- | src/kernel/lib/math/pow.c | 13 | ||||
-rw-r--r-- | src/kernel/lib/memory.c | 66 | ||||
-rw-r--r-- | src/kernel/lib/stdio/debug.c | 93 | ||||
-rw-r--r-- | src/kernel/lib/stdio/getch.c | 10 | ||||
-rw-r--r-- | src/kernel/lib/stdio/printf.c | 8 | ||||
-rw-r--r-- | src/kernel/lib/stdio/readline.c | 7 | ||||
-rw-r--r-- | src/kernel/lib/stdio/vprintf.c | 73 | ||||
-rw-r--r-- | src/kernel/lib/stdio/writec.c | 2 | ||||
-rw-r--r-- | src/kernel/lib/stdlib/atoi.c | 28 | ||||
-rw-r--r-- | src/kernel/lib/stdlib/htoa.c | 33 | ||||
-rw-r--r-- | src/kernel/lib/stdlib/htoi.c | 26 | ||||
-rw-r--r-- | src/kernel/lib/stdlib/itoa.c | 70 | ||||
-rw-r--r-- | src/kernel/lib/string/strcat.c | 9 | ||||
-rw-r--r-- | src/kernel/lib/string/strcati.c | 7 | ||||
-rw-r--r-- | src/kernel/lib/string/strcmp.c | 9 | ||||
-rw-r--r-- | src/kernel/lib/string/strcpy.c | 7 | ||||
-rw-r--r-- | src/kernel/lib/string/strdisp.c | 8 | ||||
-rw-r--r-- | src/kernel/lib/string/strdup.c | 8 | ||||
-rw-r--r-- | src/kernel/lib/string/strinv.c | 14 | ||||
-rw-r--r-- | src/kernel/lib/string/strlen.c | 7 |
20 files changed, 261 insertions, 237 deletions
diff --git a/src/kernel/lib/math/pow.c b/src/kernel/lib/math/pow.c index 24670a0..5cdcfa5 100644 --- a/src/kernel/lib/math/pow.c +++ b/src/kernel/lib/math/pow.c @@ -1,10 +1,13 @@ int pow(int base, int exp) { - if (exp < 0) return 0; + if (exp < 0) + return 0; - if (!exp) return 1; + if (!exp) + return 1; - int ret = base; - for (int i = 1; i < exp; i++) ret *= base; - return ret; + int ret = base; + for (int i = 1; i < exp; i++) + ret *= base; + return ret; }
\ No newline at end of file diff --git a/src/kernel/lib/memory.c b/src/kernel/lib/memory.c index eb7f610..f9e0054 100644 --- a/src/kernel/lib/memory.c +++ b/src/kernel/lib/memory.c @@ -3,65 +3,67 @@ void *memcpy(void *dest, const void *src, size_t count) { - const char *sp = (const char *) src; - char *dp = (char *) dest; - for (; count != 0; count--) *dp++ = *sp++; - return dest; + const char *sp = (const char *)src; + char *dp = (char *)dest; + for (; count != 0; count--) + *dp++ = *sp++; + return dest; } void *memset(void *dest, char val, size_t count) { - char *temp = (char *) dest; - for (; count != 0; count--) *temp++ = val; - return dest; + char *temp = (char *)dest; + for (; count != 0; count--) + *temp++ = val; + return dest; } 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 (b[i] < a[i]) - return 1; - } - return 0; + 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 (b[i] < a[i]) + return 1; + } + return 0; } uint32_t total_memory; struct memory_entry { - uint64_t base; - uint64_t length; - uint32_t type; + uint64_t base; + uint64_t length; + uint32_t type; } __attribute__((packed)); struct memory_entry *memory_get_entries() { - return (struct memory_entry *) 0xA000; + return (struct memory_entry *)0xA000; } void memory_init() { - uint64_t max_base = 0; - uint64_t max_length = 0; - for (struct memory_entry *i = memory_get_entries(); i->type; i++) { - if (i->type == 1 && i->base > max_base) { - max_base = i->base; - max_length = i->length; - } - } - total_memory = (uint32_t) (max_base + max_length); - total_memory /= 1024; + uint64_t max_base = 0; + uint64_t max_length = 0; + for (struct memory_entry *i = memory_get_entries(); i->type; i++) { + if (i->type == 1 && i->base > max_base) { + max_base = i->base; + max_length = i->length; + } + } + total_memory = (uint32_t)(max_base + max_length); + total_memory /= 1024; } uint32_t memory_get_free() { - return total_memory; // - paging_get_used_pages() * 4; + return total_memory; // - paging_get_used_pages() * 4; } uint32_t memory_get_all() { - return total_memory; + return total_memory; }
\ No newline at end of file diff --git a/src/kernel/lib/stdio/debug.c b/src/kernel/lib/stdio/debug.c index cd042f4..b5e5094 100644 --- a/src/kernel/lib/stdio/debug.c +++ b/src/kernel/lib/stdio/debug.c @@ -7,54 +7,55 @@ void _write_serial(const char *data) { - for (size_t i = 0; i < strlen(data); i++) serial_put(data[i]); + for (size_t i = 0; i < strlen(data); i++) + serial_put(data[i]); } void serial_printf(const char *fmt, ...) { - va_list args; - va_start(args, fmt); - - uint8_t readyToFormat = 0; - - char buff = 0; - - for (; *fmt; fmt++) { - if (readyToFormat) { - if (*fmt == '%') { - serial_put('%'); - readyToFormat = 0; - continue; - } - - buff = *fmt; - if (buff == 's') { - const char *str = va_arg(args, const char*); - _write_serial(str); - readyToFormat = 0; - } else if (buff == 'x') { - char *p = htoa((uint32_t) va_arg(args, int)); - _write_serial(p); - kfree(p); - readyToFormat = 0; - } else if (buff == 'd') { - char *p = itoa(va_arg(args, int)); - _write_serial(p); - kfree(p); - readyToFormat = 0; - } else if (buff == 'c') { - serial_put((char) va_arg(args, int)); - readyToFormat = 0; - } - } else { - if (*fmt == '%') - readyToFormat = 1; - else - serial_put(*fmt); - } - } - - serial_put('\n'); - - va_end(args); + va_list args; + va_start(args, fmt); + + uint8_t readyToFormat = 0; + + char buff = 0; + + for (; *fmt; fmt++) { + if (readyToFormat) { + if (*fmt == '%') { + serial_put('%'); + readyToFormat = 0; + continue; + } + + buff = *fmt; + if (buff == 's') { + const char *str = va_arg(args, const char *); + _write_serial(str); + readyToFormat = 0; + } else if (buff == 'x') { + char *p = htoa((uint32_t)va_arg(args, int)); + _write_serial(p); + kfree(p); + readyToFormat = 0; + } else if (buff == 'd') { + char *p = itoa(va_arg(args, int)); + _write_serial(p); + kfree(p); + readyToFormat = 0; + } else if (buff == 'c') { + serial_put((char)va_arg(args, int)); + readyToFormat = 0; + } + } else { + if (*fmt == '%') + readyToFormat = 1; + else + serial_put(*fmt); + } + } + + serial_put('\n'); + + va_end(args); }
\ No newline at end of file diff --git a/src/kernel/lib/stdio/getch.c b/src/kernel/lib/stdio/getch.c index f3ccbd9..e806013 100644 --- a/src/kernel/lib/stdio/getch.c +++ b/src/kernel/lib/stdio/getch.c @@ -3,9 +3,9 @@ char getch() { - keyboard_char_buffer = 0; - while (keyboard_char_buffer == 0) { - timer_wait(1); // IDK why! - } - return keyboard_char_buffer; + keyboard_char_buffer = 0; + while (keyboard_char_buffer == 0) { + timer_wait(1); // IDK why! + } + return keyboard_char_buffer; }
\ No newline at end of file diff --git a/src/kernel/lib/stdio/printf.c b/src/kernel/lib/stdio/printf.c index accb97d..5c3eb8e 100644 --- a/src/kernel/lib/stdio/printf.c +++ b/src/kernel/lib/stdio/printf.c @@ -3,8 +3,8 @@ void printf(const char *fmt, ...) { - va_list args; - va_start(args, fmt); - vprintf(fmt, args); - va_end(args); + va_list args; + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); }
\ No newline at end of file diff --git a/src/kernel/lib/stdio/readline.c b/src/kernel/lib/stdio/readline.c index c65882d..1bda252 100644 --- a/src/kernel/lib/stdio/readline.c +++ b/src/kernel/lib/stdio/readline.c @@ -3,7 +3,8 @@ char *readline() { - keyboard_clear_buffer(); - while (keyboard_buffer[strlen(keyboard_buffer) - 1] != '\n') {} - return keyboard_buffer; + keyboard_clear_buffer(); + while (keyboard_buffer[strlen(keyboard_buffer) - 1] != '\n') { + } + return keyboard_buffer; }
\ No newline at end of file diff --git a/src/kernel/lib/stdio/vprintf.c b/src/kernel/lib/stdio/vprintf.c index af8e47f..b9188c6 100644 --- a/src/kernel/lib/stdio/vprintf.c +++ b/src/kernel/lib/stdio/vprintf.c @@ -7,47 +7,48 @@ void _writes(const char *data) { - for (size_t i = 0; i < strlen(data); i++) writec(data[i]); + for (size_t i = 0; i < strlen(data); i++) + writec(data[i]); } void vprintf(const char *fmt, va_list args) { - uint8_t readyToFormat = 0; + uint8_t readyToFormat = 0; - char buff = 0; + char buff = 0; - for (; *fmt; fmt++) { - if (readyToFormat) { - if (*fmt == '%') { - writec('%'); - readyToFormat = 0; - continue; - } + for (; *fmt; fmt++) { + if (readyToFormat) { + if (*fmt == '%') { + writec('%'); + readyToFormat = 0; + continue; + } - buff = *fmt; - if (buff == 's') { - const char *str = va_arg(args, const char*); - _writes(str); - readyToFormat = 0; - } else if (buff == 'x') { - char *p = htoa((uint32_t) va_arg(args, int)); - _writes(p); - kfree(p); - readyToFormat = 0; - } else if (buff == 'd') { - char *p = itoa(va_arg(args, int)); - _writes(p); - kfree(p); - readyToFormat = 0; - } else if (buff == 'c') { - writec((char) va_arg(args, int)); - readyToFormat = 0; - } - } else { - if (*fmt == '%') - readyToFormat = 1; - else - writec(*fmt); - } - } + buff = *fmt; + if (buff == 's') { + const char *str = va_arg(args, const char *); + _writes(str); + readyToFormat = 0; + } else if (buff == 'x') { + char *p = htoa((uint32_t)va_arg(args, int)); + _writes(p); + kfree(p); + readyToFormat = 0; + } else if (buff == 'd') { + char *p = itoa(va_arg(args, int)); + _writes(p); + kfree(p); + readyToFormat = 0; + } else if (buff == 'c') { + writec((char)va_arg(args, int)); + readyToFormat = 0; + } + } else { + if (*fmt == '%') + readyToFormat = 1; + else + writec(*fmt); + } + } }
\ No newline at end of file diff --git a/src/kernel/lib/stdio/writec.c b/src/kernel/lib/stdio/writec.c index 94d4fed..83b05de 100644 --- a/src/kernel/lib/stdio/writec.c +++ b/src/kernel/lib/stdio/writec.c @@ -2,5 +2,5 @@ void writec(char c) { - vesa_draw_char(c); + vesa_draw_char(c); }
\ No newline at end of file diff --git a/src/kernel/lib/stdlib/atoi.c b/src/kernel/lib/stdlib/atoi.c index bcdd395..2d58d84 100644 --- a/src/kernel/lib/stdlib/atoi.c +++ b/src/kernel/lib/stdlib/atoi.c @@ -5,20 +5,24 @@ int atoi(char *str) { - size_t s_str = strlen(str); - if (!s_str) return 0; + size_t s_str = strlen(str); + if (!s_str) + return 0; - uint8_t negative = 0; - if (str[0] == '-') negative = 1; + uint8_t negative = 0; + if (str[0] == '-') + negative = 1; - size_t i = 0; - if (negative) i++; + size_t i = 0; + if (negative) + i++; - int ret = 0; - for (; i < s_str; i++) { - ret += (str[i] - '0') * pow(10, (int) ((s_str - i) - 1)); - } + int ret = 0; + for (; i < s_str; i++) { + ret += (str[i] - '0') * pow(10, (int)((s_str - i) - 1)); + } - if (negative) ret *= -1; - return ret; + if (negative) + ret *= -1; + return ret; }
\ No newline at end of file diff --git a/src/kernel/lib/stdlib/htoa.c b/src/kernel/lib/stdlib/htoa.c index 49ca703..ee639ec 100644 --- a/src/kernel/lib/stdlib/htoa.c +++ b/src/kernel/lib/stdlib/htoa.c @@ -6,25 +6,26 @@ static const char HTOA_TABLE[] = "0123456789ABCDEF"; char *htoa(uint32_t n) { - char *ret = (char *) kmalloc(10); + char *ret = (char *)kmalloc(10); - int i = 0; - while (n) { - ret[i++] = HTOA_TABLE[n & 0xF]; - n >>= 4; - } + int i = 0; + while (n) { + ret[i++] = HTOA_TABLE[n & 0xF]; + n >>= 4; + } - if (!i) { - ret[0] = '0'; - i++; - } + if (!i) { + ret[0] = '0'; + i++; + } - for (; i <= 9; i++) ret[i] = 0; + for (; i <= 9; i++) + ret[i] = 0; - char *aux = strdup(ret); - kfree(ret); - ret = aux; + char *aux = strdup(ret); + kfree(ret); + ret = aux; - strinv(ret); - return ret; + strinv(ret); + return ret; }
\ No newline at end of file diff --git a/src/kernel/lib/stdlib/htoi.c b/src/kernel/lib/stdlib/htoi.c index cc77e16..dcc4b63 100644 --- a/src/kernel/lib/stdlib/htoi.c +++ b/src/kernel/lib/stdlib/htoi.c @@ -4,20 +4,20 @@ int htoi(char *str) { - size_t s_str = strlen(str); + size_t s_str = strlen(str); - size_t i = 0; - int ret = 0; - for (; i < s_str; i++) { - char c = str[i]; - int aux = 0; - if (c >= '0' && c <= '9') - aux = c - '0'; - else if (c >= 'A' && c <= 'F') - aux = (c - 'A') + 10; + size_t i = 0; + int ret = 0; + for (; i < s_str; i++) { + char c = str[i]; + int aux = 0; + if (c >= '0' && c <= '9') + aux = c - '0'; + else if (c >= 'A' && c <= 'F') + aux = (c - 'A') + 10; - ret += aux * pow(16, (int) ((s_str - i) - 1)); - } + ret += aux * pow(16, (int)((s_str - i) - 1)); + } - return ret; + return ret; }
\ No newline at end of file diff --git a/src/kernel/lib/stdlib/itoa.c b/src/kernel/lib/stdlib/itoa.c index dec75bf..94c3351 100644 --- a/src/kernel/lib/stdlib/itoa.c +++ b/src/kernel/lib/stdlib/itoa.c @@ -8,38 +8,40 @@ static const char ITOA_TABLE[] = "0123456789"; char *itoa(int n) { - if (paging_enabled == 0) - return "0"; // kmalloc isn't available - - if (!n) { - char *ret = (char *) kmalloc(2); - ret[0] = '0'; - ret[1] = 0; - return ret; - } - uint8_t negative = (uint8_t) (n < 0); - if (negative) n *= -1; - - int sz; - for (sz = 0; n % pow(10, sz) != n; sz++) {} - - char *ret = (char *) kmalloc((uint32_t) (sz + 1)); - - for (int i = 0; i < sz; i++) { - int digit = (n % pow(10, i + 1)) / pow(10, i); - ret[i] = ITOA_TABLE[digit]; - } - ret[sz] = 0; - - if (negative) { - char *aux = (char *) kmalloc((uint32_t) (sz + 2)); - strcpy(aux, ret); - aux[sz] = '-'; - aux[sz + 1] = 0; - kfree(ret); - ret = aux; - } - - strinv(ret); - return ret; + if (paging_enabled == 0) + return "0"; // kmalloc isn't available + + if (!n) { + char *ret = (char *)kmalloc(2); + ret[0] = '0'; + ret[1] = 0; + return ret; + } + uint8_t negative = (uint8_t)(n < 0); + if (negative) + n *= -1; + + int sz; + for (sz = 0; n % pow(10, sz) != n; sz++) { + } + + char *ret = (char *)kmalloc((uint32_t)(sz + 1)); + + for (int i = 0; i < sz; i++) { + int digit = (n % pow(10, i + 1)) / pow(10, i); + ret[i] = ITOA_TABLE[digit]; + } + ret[sz] = 0; + + if (negative) { + char *aux = (char *)kmalloc((uint32_t)(sz + 2)); + strcpy(aux, ret); + aux[sz] = '-'; + aux[sz + 1] = 0; + kfree(ret); + ret = aux; + } + + strinv(ret); + return ret; }
\ No newline at end of file diff --git a/src/kernel/lib/string/strcat.c b/src/kernel/lib/string/strcat.c index e0d6885..4328f87 100644 --- a/src/kernel/lib/string/strcat.c +++ b/src/kernel/lib/string/strcat.c @@ -2,9 +2,10 @@ void strcat(char *dest, const char *orig) { - size_t s_dest = strlen(dest); - size_t s_orig = strlen(orig); + size_t s_dest = strlen(dest); + size_t s_orig = strlen(orig); - for (size_t i = 0; i < s_orig; i++) dest[s_dest + i] = orig[i]; - dest[s_dest + s_orig] = 0; + for (size_t i = 0; i < s_orig; i++) + dest[s_dest + i] = orig[i]; + dest[s_dest + s_orig] = 0; }
\ No newline at end of file diff --git a/src/kernel/lib/string/strcati.c b/src/kernel/lib/string/strcati.c index 9ee3a43..5dab283 100644 --- a/src/kernel/lib/string/strcati.c +++ b/src/kernel/lib/string/strcati.c @@ -2,7 +2,8 @@ void strcati(char *dest, const char *orig) { - size_t s_orig = strlen(orig); - strdisp(dest, (int) s_orig); - for (size_t i = 0; i < s_orig; i++) dest[i] = orig[i]; + size_t s_orig = strlen(orig); + strdisp(dest, (int)s_orig); + for (size_t i = 0; i < s_orig; i++) + dest[i] = orig[i]; }
\ No newline at end of file diff --git a/src/kernel/lib/string/strcmp.c b/src/kernel/lib/string/strcmp.c index b8ac2a7..89f77a0 100644 --- a/src/kernel/lib/string/strcmp.c +++ b/src/kernel/lib/string/strcmp.c @@ -2,9 +2,12 @@ char strcmp(const char *a, const char *b) { - if (strlen(a) != strlen(b)) return 1; + if (strlen(a) != strlen(b)) + return 1; - for (size_t i = 0; i < strlen(a); i++) if (a[i] != b[i]) return 1; + for (size_t i = 0; i < strlen(a); i++) + if (a[i] != b[i]) + return 1; - return 0; + return 0; }
\ No newline at end of file diff --git a/src/kernel/lib/string/strcpy.c b/src/kernel/lib/string/strcpy.c index bab8535..6117e7c 100644 --- a/src/kernel/lib/string/strcpy.c +++ b/src/kernel/lib/string/strcpy.c @@ -2,8 +2,9 @@ void strcpy(char *dest, const char *orig) { - size_t s_orig = strlen(orig); + size_t s_orig = strlen(orig); - for (size_t i = 0; i < s_orig; i++) dest[i] = orig[i]; - dest[s_orig] = 0; + for (size_t i = 0; i < s_orig; i++) + dest[i] = orig[i]; + dest[s_orig] = 0; }
\ No newline at end of file diff --git a/src/kernel/lib/string/strdisp.c b/src/kernel/lib/string/strdisp.c index 36c03c3..e63f038 100644 --- a/src/kernel/lib/string/strdisp.c +++ b/src/kernel/lib/string/strdisp.c @@ -2,11 +2,13 @@ void strdisponce(char *str) { - for (size_t i = sizeof(str) + 2; i > 0; i--) str[i] = str[i - 1]; - str[0] = 0; + for (size_t i = sizeof(str) + 2; i > 0; i--) + str[i] = str[i - 1]; + str[0] = 0; } void strdisp(char *str, int n) { - for (int i = 0; i < n; i++) strdisponce(str); + for (int i = 0; i < n; i++) + strdisponce(str); }
\ No newline at end of file diff --git a/src/kernel/lib/string/strdup.c b/src/kernel/lib/string/strdup.c index e59dff4..82d14ef 100644 --- a/src/kernel/lib/string/strdup.c +++ b/src/kernel/lib/string/strdup.c @@ -3,8 +3,8 @@ char *strdup(const char *orig) { - size_t s_orig = strlen(orig); - char *ret = (char *) kmalloc(s_orig + 1); - strcpy(ret, orig); - return ret; + size_t s_orig = strlen(orig); + char *ret = (char *)kmalloc(s_orig + 1); + strcpy(ret, orig); + return ret; }
\ No newline at end of file diff --git a/src/kernel/lib/string/strinv.c b/src/kernel/lib/string/strinv.c index b4b4212..071a99d 100644 --- a/src/kernel/lib/string/strinv.c +++ b/src/kernel/lib/string/strinv.c @@ -2,12 +2,12 @@ void strinv(char *str) { - size_t s_str = strlen(str); + size_t s_str = strlen(str); - int iterations = (int) s_str / 2; - for (int i = 0; i < iterations; i++) { - char aux = str[i]; - str[i] = str[(s_str - i) - 1]; - str[(s_str - i) - 1] = aux; - } + int iterations = (int)s_str / 2; + for (int i = 0; i < iterations; i++) { + char aux = str[i]; + str[i] = str[(s_str - i) - 1]; + str[(s_str - i) - 1] = aux; + } }
\ No newline at end of file diff --git a/src/kernel/lib/string/strlen.c b/src/kernel/lib/string/strlen.c index 77e3c00..9e7e448 100644 --- a/src/kernel/lib/string/strlen.c +++ b/src/kernel/lib/string/strlen.c @@ -2,7 +2,8 @@ size_t strlen(const char *str) { - size_t len = 0; - while (str[len]) len++; - return len; + size_t len = 0; + while (str[len]) + len++; + return len; }
\ No newline at end of file |