diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | ls.c | 30 |
3 files changed, 32 insertions, 1 deletions
@@ -2,4 +2,5 @@ cat fallocate false +ls true @@ -1,5 +1,5 @@ CFLAGS+=-std=c11 -Wall -Wextra -Wpedantic -D_POSIX_C_SOURCE=200809L -UTILS=cat fallocate false true +UTILS=cat fallocate false ls true all: ${UTILS} @@ -0,0 +1,30 @@ +#include <dirent.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + + +int ls(const char * path) +{ + DIR * dir; + if (NULL == (dir = opendir(path))) { + perror(path); + return 1; + } + struct dirent * entry; + while (NULL != (entry = readdir(dir))) + printf("%s\n", entry->d_name); + closedir(dir); + return 0; +} + + +int main(int argc, char * argv[]) +{ + int res = 0; + if (argc == 1) + res |= ls("."); + for (int i = 1; i < argc; ++i) + res |= ls(argv[i]); + return res; +} |