| git.druid.rocks | index | druid520 | kaboom | src/ | user/ | info.nsc |
src/user/info.nsc
include "syscalls.nsh";
include "stdio.nsh";
void
print_stat(ptr label, u64 val)
{
puts(label);
put_udec(val);
putchar((u8)10);
}
/* prints a virtual file's content straight through -- /int/mem and
* /int/cpu (virtfs.nsc, kernel-side) already format themselves as
* plain readable text, so "info" showing memory/cpu sections is
* nothing more than opening and reading them, the same way ps.nsc is
* just a hardcoded cat of /proc/ps. */
void
cat_virtfile(ptr path, u64 path_len)
{
i8 buf[256];
i32 fd;
i64 n;
fd = sys_open(path, path_len);
if(fd < 0)
{
return;
}
n = sys_read(fd, &buf[0], (u64)256);
if(n > (i64)0)
{
sys_write(1, &buf[0], (u64)n);
}
sys_close(fd);
}
/* dumps kfs stats (blocks/inodes), then memory and cpu info by way of
* /int/mem and /int/cpu -- no options, matching the rest of this
* kernel's coreutils; extend the info(buf) syscall (idt.nsc's number
* 14) if kfs ever needs its own new numbers here instead. */
global i32
main(i32 argc, ptr argv)
{
u64 stats[4];
sys_info((ptr)&stats[0]);
print_stat("blocks total: ", stats[0]);
print_stat("blocks used: ", stats[1]);
print_stat("inodes total: ", stats[2]);
print_stat("inodes used: ", stats[3]);
stdio_flush();
cat_virtfile("/int/mem", (u64)8);
cat_virtfile("/int/cpu", (u64)8);
return 0;
}