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); +}