瀏覽代碼

Support "ls <path>" rather than just "ls" :^)

Andreas Kling 6 年之前
父節點
當前提交
e440c3fa87
共有 1 個文件被更改,包括 13 次插入6 次删除
  1. 13 6
      Userland/ls.cpp

+ 13 - 6
Userland/ls.cpp

@@ -4,20 +4,27 @@
 #include <LibC/errno.h>
 #include <LibC/errno.h>
 #include <LibC/string.h>
 #include <LibC/string.h>
 
 
+static int do_dir(const char* path);
+
 int main(int argc, char** argv)
 int main(int argc, char** argv)
 {
 {
-    (void) argc;
-    (void) argv;
-    bool colorize = true;
+    if (argc == 2) {
+        return do_dir(argv[1]);
+    }
+    return do_dir(".");
+}
 
 
-    DIR* dirp = opendir(".");
+int do_dir(const char* path)
+{
+    DIR* dirp = opendir(path);
     if (!dirp) {
     if (!dirp) {
-        perror("opendir failed");
+        perror("opendir");
         return 1;
         return 1;
     }
     }
+    bool colorize = true;
     char pathbuf[256];
     char pathbuf[256];
     while (auto* de = readdir(dirp)) {
     while (auto* de = readdir(dirp)) {
-        sprintf(pathbuf, "%s", de->d_name);
+        sprintf(pathbuf, "%s/%s", path, de->d_name);
 
 
         struct stat st;
         struct stat st;
         int rc = lstat(pathbuf, &st);
         int rc = lstat(pathbuf, &st);