summaryrefslogtreecommitdiff
path: root/src/init/shell.c
diff options
context:
space:
mode:
authordzwdz2022-03-26 18:10:42 +0100
committerdzwdz2022-03-26 18:10:42 +0100
commit8ec5fee44bb69d092d6c74ec33a3334d2a34bf0d (patch)
tree8d4181eabf4d823000c495b9a32bb30ba16bb5ec /src/init/shell.c
parent1b1cda7368fe521faa1b0cb6461426c5e90e2686 (diff)
init/shell: minor usability tweaks to `ls`
Diffstat (limited to 'src/init/shell.c')
-rw-r--r--src/init/shell.c20
1 files changed, 16 insertions, 4 deletions
diff --git a/src/init/shell.c b/src/init/shell.c
index 9992cd5..0113866 100644
--- a/src/init/shell.c
+++ b/src/init/shell.c
@@ -47,17 +47,29 @@ static int readline(char *buf, size_t max) {
static void cmd_cat_ls(const char *args, bool ls) {
int fd;
static char buf[256];
- int len = 256;
+ int len; // first used for strlen(args), then length of buffer
- if (!args) return; // no argument
+ if (!args) args = "/";
+ len = strlen(args);
+ memcpy(buf, args, len + 1); // no overflow check - the shell is just a PoC
- fd = _syscall_open(args, strlen(args));
+ if (ls) { // paths to directories always have a trailing slash
+ char *p = buf;
+ while (*p) p++;
+ if (p[-1] != '/') {
+ p[0] = '/';
+ p[1] = '\0';
+ len++;
+ }
+ }
+
+ fd = _syscall_open(buf, len);
if (fd < 0) {
printf("couldn't open.\n");
return;
}
- len = _syscall_read(fd, buf, len, 0);
+ len = _syscall_read(fd, buf, sizeof buf, 0);
if (ls)
for (int i = 0; i < len; i++)
if (buf[i] == '\0') buf[i] = '\n';