From 7b0b3dabc4e1859fde1b171b777af5ec4411807a Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 29 Mar 2023 01:07:58 +0200 Subject: [PATCH] apps: Add ls --- apps/CMakeLists.txt | 1 + apps/ls.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 apps/ls.c diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 7e009718..e86ebf70 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -11,3 +11,4 @@ luna_app(cat.c cat) luna_app(edit.c edit) luna_app(sh.c sh) luna_app(date.c date) +luna_app(ls.c ls) diff --git a/apps/ls.c b/apps/ls.c new file mode 100644 index 00000000..f8a0a9ba --- /dev/null +++ b/apps/ls.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + const char* pathname; + if (argc == 1) pathname = "/"; + else + pathname = argv[1]; + + DIR* dp = opendir(pathname); + if (!dp) + { + perror("opendir"); + return 1; + } + + int first_ent = 1; + do { + struct dirent* ent = readdir(dp); + if (!ent) break; + printf(first_ent ? "%s" : " %s", ent->d_name); + first_ent = 0; + } while (1); + + putchar('\n'); + + closedir(dp); + return 0; +}