vxBoot/src/builtin/ls.c

79 lines
1.8 KiB
C

#include "vxBoot/builtin.h"
#include "vxBoot/terminal.h"
#include "vxBoot/fs/smemfs.h"
#include <gint/bfile.h>
#include <string.h>
/* ls_help() : Display the hep message */
static void ls_help(void)
{
terminal_write(
"NAME\n"
" ls - list directory contents\n"
"\n"
"SYNOPSIS\n"
" ls [-h|--help]\n"
"\n"
"DESCRIPTION\n"
" List file information about FILEs (from the root directory only). The"
" display is inspired from the utilitary `tree` in UNIX environment.\n"
"\n"
" -h,--help\n"
" Display this help message\n"
);
}
/* inode_walk() : walk onto the filesystem and display files */
static void inode_walk(struct smemfs_inode *inode, int level, uint32_t bitmap)
{
const char *records;
if (inode == NULL)
return;
/* handle indentation */
for (int i = 0; i < level; ++i) {
records = "\t";
if ((bitmap & (1 << i)) != 0)
records = "|\t";
terminal_write(records);
}
/* handle file name and sibling dependencies */
records = "|-- (%x) %s";
bitmap |= 1 << level;
if (inode->sibling == NULL) {
records = "`-- (%x) %s";
bitmap &= ~(1 << level);
}
terminal_write(records, inode->type, inode->name);
/* handle file type */
if (inode->type == BFile_Type_Directory) {
terminal_write(":\n");
inode_walk(inode->child, level + 1, bitmap);
inode_walk(inode->sibling, level, bitmap);
return;
}
terminal_write("\n");
inode_walk(inode->sibling, level, bitmap);
}
/* ls_main() : entry of the "ls" builtin */
int ls_main(int argc, char **argv)
{
if (smemfs_superblock.fake_root_inode != SMEMFS_FAKE_ROOT_INODE) {
terminal_write("smemfs not mounted !\n");
return (84);
}
if (argc > 1 && strcmp(argv[1], "-help") == 0) {
ls_help();
return (0);
}
terminal_write("/:\n");
inode_walk(smemfs_superblock.root_inode, 0, 0x00000000);
return (0);
}