From 32db3667811b4d89181db87e09513334a0c1625f Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 23 Oct 2022 14:06:11 +0200 Subject: [PATCH] apps: Add a little ls utility that lists the files in /bin --- apps/Makefile | 2 +- apps/src/ls.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 apps/src/ls.c diff --git a/apps/Makefile b/apps/Makefile index 601b773a..68f40ffe 100644 --- a/apps/Makefile +++ b/apps/Makefile @@ -1,4 +1,4 @@ -APPS := init sym sh crash uname uptime hello ps +APPS := init sym sh crash uname uptime hello ps ls APPS_DIR := $(LUNA_ROOT)/apps APPS_SRC := $(APPS_DIR)/src diff --git a/apps/src/ls.c b/apps/src/ls.c new file mode 100644 index 00000000..83ebaabb --- /dev/null +++ b/apps/src/ls.c @@ -0,0 +1,28 @@ +#include +#include +#include +#include + +int main() +{ + int dirfd = open("/bin", O_RDONLY); + if (dirfd < 0) + { + perror("open"); + return 1; + } + struct luna_dirent dirent[5]; + ssize_t nread; + do { + nread = getdents(dirfd, dirent, 5); + if (nread < 0) + { + perror("getdents"); + return 1; + } + for (int i = 0; i < nread; i++) { printf("%s\n", dirent[i].name); } + } while (nread == 5); + + close(dirfd); + return 0; +} \ No newline at end of file