diff options
author | Aki <please@ignore.pl> | 2022-06-08 23:10:52 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-06-08 23:10:52 +0200 |
commit | 607b468be2faf8e325f2021ee2cbf1718d7ee589 (patch) | |
tree | fccfff6b2d406c5523186580801cc116220b78a5 | |
parent | e8b6d9b2dd1940397137f9e7daca5bcd9767a1c6 (diff) | |
download | coreutils-607b468be2faf8e325f2021ee2cbf1718d7ee589.zip coreutils-607b468be2faf8e325f2021ee2cbf1718d7ee589.tar.gz coreutils-607b468be2faf8e325f2021ee2cbf1718d7ee589.tar.bz2 |
Added optionless ls utility
-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; +} |