summary refs log tree commit diff
path: root/progs/ls/ls.c
diff options
context:
space:
mode:
Diffstat (limited to 'progs/ls/ls.c')
-rw-r--r--progs/ls/ls.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/progs/ls/ls.c b/progs/ls/ls.c
new file mode 100644
index 0000000..976e8c9
--- /dev/null
+++ b/progs/ls/ls.c
@@ -0,0 +1,50 @@
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <string.h>
+#include <dirent.h>
+
+void joinPath(char *result, const char *base, const char *rel) {
+    strcpy(result, base);
+    strcat(result, "/");
+    strcat(result, rel);
+}
+
+int main(int argc, char *argv[]) {
+    char dirp[128] = ".";
+    if (argc > 1) {
+        strcpy(dirp, argv[1]);
+    }
+    char newpath[128] = {0};
+    char newresolvedpath[128] = {0};
+    if (dirp[0] == '/') {
+        strcpy(newresolvedpath, dirp);
+    } else {
+        joinPath(newpath, ".", dirp);
+        realpath(newpath, newresolvedpath);
+    }
+    if (!access(newresolvedpath, F_OK)) {
+        struct stat statResult;
+        stat(newresolvedpath, &statResult);
+        if (!S_ISDIR(statResult.st_mode)) {
+            write(1, "uuuuh das not a dir\n", 21);
+            exit(1);
+        }
+    } else {
+        write(1, "uuuuh das not a dir\n", 21);
+        exit(1);
+    }
+    DIR *d;
+    struct dirent *dir;
+    d = opendir(dirp);
+    //TODO - sort
+    if (d) {
+        while ((dir = readdir(d)) != NULL) {
+            printf("%s\n", dir->d_name);
+        }
+        closedir(d);
+    }
+    exit(0);
+}
\ No newline at end of file