diff options
-rw-r--r-- | src/init/shell.c | 34 | ||||
-rw-r--r-- | src/init/stdlib.c | 15 |
2 files changed, 49 insertions, 0 deletions
diff --git a/src/init/shell.c b/src/init/shell.c index 5612f35..39db99f 100644 --- a/src/init/shell.c +++ b/src/init/shell.c @@ -78,6 +78,38 @@ static void cmd_cat_ls(const char *args, bool ls) { _syscall_close(fd); } +static void cmd_hexdump(const char *args) { + static uint8_t buf[513]; + int fd, len; + + fd = _syscall_open(args, strlen(args)); + if (fd < 0) { + printf("couldn't open.\n"); + return; + } + + len = _syscall_read(fd, buf, sizeof buf, 0); + for (int i = 0; i < len; i += 16) { + printf("%08x ", i); + + for (int j = i; j < i + 8 && j < len; j++) + printf("%02x ", buf[j]); + printf(" "); + for (int j = i + 8; j < i + 16 && j < len; j++) + printf("%02x ", buf[j]); + printf(" |"); + + for (int j = i; j < i + 16 && j < len; j++) { + char c = '.'; + if (0x20 <= buf[j] && buf[j] < 0x7f) c = buf[j]; + printf("%c", c); + } + printf("|\n"); + } + + _syscall_close(fd); +} + void shell_loop(void) { static char cmd[256]; int level = 0; @@ -93,6 +125,8 @@ void shell_loop(void) { cmd_cat_ls(args, false); } else if (!strcmp(cmd, "ls")) { cmd_cat_ls(args, true); + } else if (!strcmp(cmd, "hexdump")) { + cmd_hexdump(args); } else if (!strcmp(cmd, "catall")) { const char *files[] = { "/init/fake.txt", diff --git a/src/init/stdlib.c b/src/init/stdlib.c index 18da3da..e8b3f97 100644 --- a/src/init/stdlib.c +++ b/src/init/stdlib.c @@ -21,8 +21,20 @@ int printf(const char *fmt, ...) { case '%': _syscall_write(0, seg, fmt - seg - 1, 0); total += fmt - seg - 1; + size_t pad_len = 0; +parse_fmt: c = *fmt++; switch (c) { + case '0': + pad_len = *fmt++ - '0'; // can skip over the null byte, idc + goto parse_fmt; + + case 'c': + char c = va_arg(argp, int); + _syscall_write(0, &c, 1, 0); + total += 1; + break; + case 's': const char *s = va_arg(argp, char*); if (s) { @@ -37,6 +49,9 @@ int printf(const char *fmt, ...) { while (n >> i && i < (sizeof(int) * 8)) i += 4; + if (i < pad_len * 4) + i = pad_len * 4; + while (i > 0) { i -= 4; char h = '0' + ((n >> i) & 0xf); |