From 375104536e4d5e02e3423eb1cc6c6a80526916fc Mon Sep 17 00:00:00 2001 From: Aurel Feer Date: Thu, 2 Jun 2022 16:14:49 +0200 Subject: [PATCH] implement ls, rmdir and mkdir --- errors/errno.fugu | 1 + usr/shelly/shelly_cmd.c | 41 +++++++++++++++++++++++++++++++++-------- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/errors/errno.fugu b/errors/errno.fugu index f3732b8..22e5df4 100755 --- a/errors/errno.fugu +++ b/errors/errno.fugu @@ -1451,4 +1451,5 @@ errors shelly SHELLY_ERR_ { failure WRITE "String write failed", failure SPAWN_RPC "Spawn RPC failed", failure PS_RPC "ps RPC failed", + failure FILE_NOT_FOUND "file not found", }; diff --git a/usr/shelly/shelly_cmd.c b/usr/shelly/shelly_cmd.c index 1687689..0b3435c 100644 --- a/usr/shelly/shelly_cmd.c +++ b/usr/shelly/shelly_cmd.c @@ -1,4 +1,6 @@ #include +#include +#include #include "shelly.h" __attribute__((__used__)) @@ -41,9 +43,36 @@ static errval_t shelly_cmd_help(char * line) { } static errval_t shelly_cmd_ls(char * line) { - // errval_t err; + errval_t err; - return SYS_ERR_OK; + fs_dirhandle_t dh; + err = opendir(line, &dh); + if (err_is_fail(err)) { + return err; + } + + if (dh == NULL) { + return SHELLY_ERR_FILE_NOT_FOUND; + } + + char buf[256]; + + do { + char *name; + err = readdir(dh, &name); + if (err_no(err) == FS_ERR_INDEX_BOUNDS) { + break; + } else if (err_is_fail(err)) { + goto err_out; + } + snprintf(buf, 256, "%s\n\r", name); + err = shelly_write_str(buf); + if (err_is_fail(err)) goto err_out; + } while(err_is_ok(err)); + + return closedir(dh); + err_out: + return err; } static errval_t shelly_cmd_cat(char * line) { @@ -62,15 +91,11 @@ static errval_t shelly_cmd_cat(char * line) { } static errval_t shelly_cmd_mkdir(char * line) { - // errval_t err; - - return SYS_ERR_OK; + return mkdir(line); } static errval_t shelly_cmd_rmdir(char * line) { - // errval_t err; - - return SYS_ERR_OK; + return rmdir(line); } static errval_t shelly_cmd_echo(char * line) {