From 2512acc71678b987198c720ae9a4537854171e60 Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 26 Oct 2022 20:30:22 +0200 Subject: [PATCH] ls: Use command-line arguments --- apps/src/ls.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/src/ls.c b/apps/src/ls.c index 6ecdd29b..aa81fb2f 100644 --- a/apps/src/ls.c +++ b/apps/src/ls.c @@ -1,22 +1,31 @@ #include #include +#include #include #include -int main() +int main(int argc, char** argv) { - DIR* dp = opendir("/bin"); + const char* pathname; + if (argc == 1) pathname = "/"; + else + pathname = argv[1]; + DIR* dp = opendir(pathname); if (!dp) { perror("opendir"); return 1; } + bool first_ent = true; do { struct dirent* ent = readdir(dp); if (!ent) break; - printf("%s\n", ent->d_name); + printf(first_ent ? "%s" : " %s", ent->d_name); + first_ent = false; } while (1); + printf("\n"); + closedir(dp); return 0; } \ No newline at end of file