summaryrefslogtreecommitdiff
path: root/kernel/lib/kprintf.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/lib/kprintf.c')
-rw-r--r--kernel/lib/kprintf.c66
1 files changed, 39 insertions, 27 deletions
diff --git a/kernel/lib/kprintf.c b/kernel/lib/kprintf.c
index 7b1ed71..452e45c 100644
--- a/kernel/lib/kprintf.c
+++ b/kernel/lib/kprintf.c
@@ -1,6 +1,7 @@
+#include "lib/kio.h"
#include <lib.h>
+#include <comus/term.h>
#include <comus/drivers/uart.h>
-#include <comus/drivers/term.h>
#define PRINTF_NUMERIC_BUF_LEN 50
@@ -217,6 +218,9 @@ static void get_radix(char spec, options_t *opts)
case 'o':
opts->radix = 8;
break;
+ case 'b':
+ opts->radix = 2;
+ break;
default:
opts->radix = 10;
break;
@@ -262,43 +266,48 @@ static int printf_lltoa(char *buf, options_t *opts, bool is_neg,
}
precision = 0;
- // sign
- if (is_neg) {
- *(buf++) = '-';
- } else if (opts->sign) {
- *(buf++) = '+';
- } else if (opts->space) {
- *(buf++) = ' ';
+ // write number
+ if (num == 0) {
+ *(buf++) = '0';
+ }
+ while (num) {
+ if (opts->precision_set && precision++ >= opts->precision)
+ break;
+ *(buf++) = printf_itoc(opts->is_uppercase, num % opts->radix);
+ num /= opts->radix;
+ }
+
+ // print zeros if needed
+ if (opts->width_set && len < opts->width && opts->zero) {
+ while (len++ < opts->width)
+ *(buf++) = '0';
}
// radix specifier
if (opts->hash) {
- if (opts->radix == 8) {
+ if (opts->radix == 2) {
+ *(buf++) = 'b';
*(buf++) = '0';
+ }
+ if (opts->radix == 8) {
*(buf++) = 'o';
+ *(buf++) = '0';
}
if (opts->radix == 16) {
- *(buf++) = '0';
*(buf++) = 'x';
+ *(buf++) = '0';
}
}
- // print zeros if needed
- if (opts->width_set && len < opts->width && opts->zero) {
- while (len++ < opts->width)
- *(buf++) = '0';
+ // sign
+ if (is_neg) {
+ *(buf++) = '-';
+ } else if (opts->sign) {
+ *(buf++) = '+';
+ } else if (opts->space) {
+ *(buf++) = ' ';
}
- // write number
- if (num == 0) {
- *(buf++) = '0';
- }
- while (num) {
- if (opts->precision_set && precision++ >= opts->precision)
- break;
- *(buf++) = printf_itoc(opts->is_uppercase, num % opts->radix);
- num /= opts->radix;
- }
*(buf++) = '\0';
return buf - start;
@@ -333,8 +342,8 @@ static void handle_int_specifier(context_t *ctx, options_t *const opts,
printf_putc(ctx, opts->zero ? '0' : ' ');
}
// number
- for (int i = 0; i < buf_len; i++)
- printf_putc(ctx, buf[i]);
+ for (int i = 1; i <= buf_len; i++)
+ printf_putc(ctx, buf[buf_len - i]);
// right padding
if (opts->left == 1) {
for (int i = 0; i < padding; i++)
@@ -424,13 +433,15 @@ static void do_printf(context_t *ctx, va_list args)
switch (spec) {
case 'p':
opts.len = PRINTF_LEN_SIZE_T;
- opts.width_set = true;
+ opts.width_set = 1;
+ opts.width = sizeof(void *) * 2;
opts.radix = 16;
opts.hash = true;
opts.zero = true;
case 'd':
case 'i':
case 'u':
+ case 'b':
case 'o':
case 'x':
case 'X':
@@ -478,6 +489,7 @@ static void do_printf(context_t *ctx, va_list args)
// unsigned int
case 'p':
case 'u':
+ case 'b':
case 'o':
case 'x':
case 'X':