summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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;
+}