From c56d59faba0db76bed139f00ce0c83edee8a79e2 Mon Sep 17 00:00:00 2001 From: talksik Date: Fri, 25 Sep 2026 17:19:02 -0700 Subject: [PATCH] implement ls gnu coreutil --- coreutils/ls.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 coreutils/ls.c diff --git a/coreutils/ls.c b/coreutils/ls.c new file mode 100644 index 0000000..9a65b2e --- /dev/null +++ b/coreutils/ls.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +#define MAX_LEN 1000 + +int main(int argc, char *argv[]) { + if (argc != 1) { + printf("Only support one command `ls` without any path param\n"); + return -1; + } + + char path[MAX_LEN]; + getcwd(path, MAX_LEN); + if (!path) { + printf("Unable to get current path\n"); + return -1; + } + + printf("Current path: %s\n", path); + + DIR *dir = opendir(path); + if (!dir) { + printf("Unable to view dir\n"); + return -1; + } + + struct dirent *next_item; + while ((next_item = readdir(dir)) != NULL) { + if (strlen(next_item->d_name) == 0 || + next_item->d_name[0] == '.') { + continue; + } + printf("item: %s\n", next_item->d_name); + } + + closedir(dir); +}