Files
my-os-project2/user/fs/tree.c

60 lines
1.3 KiB
C

#include <stddef.h>
#include <stdint.h>
#include <ulib.h>
int showtree(char *root, int indent) {
#define INDENT() for (size_t i = 0; i < (size_t)indent; i++) uprintf(" ")
FsStat statbuf; ZERO(&statbuf);
if (fs_stat(root, &statbuf) != E_OK) {
uprintf("fs: could not stat %s\n", root);
return -1;
}
const char *bn = path_basename(root);
if (string_len(bn) > 0) {
INDENT();
uprintf("%s\n", bn);
}
if (statbuf.type == FSSTAT_FILE) {
return 0;
}
if (statbuf.type == FSSTAT_DIR) {
FsDirent *dirents = (FsDirent *)umalloc(statbuf.size * sizeof(*dirents));
for (size_t i = 0; i < statbuf.size; i++) {
fs_fetchdirent(root, &dirents[i], i);
FsDirent *dirent = &dirents[i];
if (string_strcmp(dirent->name, ".") == 0 || string_strcmp(dirent->name, "..") == 0) {
continue;
}
char tmpbuf[1024]; ZERO(tmpbuf);
string_strcpy(tmpbuf, root);
if (root[string_len(root) - 1] != '/') {
string_combine(tmpbuf, "/");
}
string_combine(tmpbuf, dirent->name);
showtree(tmpbuf, indent+1);
}
ufree(dirents);
}
return 0;
}
void fs_tree(void) {
if (argslen() < 2) {
uprintf("fs: Not enough arguments\n");
return;
}
char *path = *(args()+1);
showtree(path, 0);
}