summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-06-08 23:10:52 +0200
committerAki <please@ignore.pl>2022-06-08 23:10:52 +0200
commit607b468be2faf8e325f2021ee2cbf1718d7ee589 (patch)
treefccfff6b2d406c5523186580801cc116220b78a5
parente8b6d9b2dd1940397137f9e7daca5bcd9767a1c6 (diff)
downloadcoreutils-607b468be2faf8e325f2021ee2cbf1718d7ee589.zip
coreutils-607b468be2faf8e325f2021ee2cbf1718d7ee589.tar.gz
coreutils-607b468be2faf8e325f2021ee2cbf1718d7ee589.tar.bz2
Added optionless ls utility
-rw-r--r--.gitignore1
-rw-r--r--Makefile2
-rw-r--r--ls.c30
3 files changed, 32 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore
index 765f41a..f2bfd53 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,5 @@
cat
fallocate
false
+ls
true
diff --git a/Makefile b/Makefile
index 69667b9..8694215 100644
--- a/Makefile
+++ b/Makefile
@@ -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}
diff --git a/ls.c b/ls.c
new file mode 100644
index 0000000..c507523
--- /dev/null
+++ b/ls.c
@@ -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;
+}