]> Projects (at) Tadryanom (dot) Me - AdrOS.git/commitdiff
fix: ps shows [kernel] for empty cmdline, ls sorts alphabetically, add qsort to ulibc
authorTulio A M Mendes <[email protected]>
Tue, 17 Feb 2026 06:13:11 +0000 (03:13 -0300)
committerTulio A M Mendes <[email protected]>
Tue, 17 Feb 2026 06:13:11 +0000 (03:13 -0300)
user/ls.c
user/ps.c
user/ulibc/include/stdlib.h
user/ulibc/src/stdlib.c
user/ulibc/src/unistd.c

index c613d508cee339fc7fa628d672870ccdfaf374c8..b8cc3ed398ee8162f18f91dbe3cf2d15e87d42f1 100644 (file)
--- a/user/ls.c
+++ b/user/ls.c
 static int aflag = 0;   /* -a: show hidden files */
 static int lflag = 0;   /* -l: long format */
 
+#define LS_MAX_ENTRIES 512
+
+struct ls_entry {
+    char name[256];
+    unsigned char type;
+};
+
+static struct ls_entry entries[LS_MAX_ENTRIES];
+
+static int cmp_entry(const void* a, const void* b) {
+    return strcmp(((const struct ls_entry*)a)->name,
+                  ((const struct ls_entry*)b)->name);
+}
+
 static void ls_dir(const char* path) {
     int fd = open(path, O_RDONLY);
     if (fd < 0) {
@@ -17,6 +31,7 @@ static void ls_dir(const char* path) {
         return;
     }
 
+    int count = 0;
     char buf[2048];
     int rc;
     while ((rc = getdents(fd, buf, sizeof(buf))) > 0) {
@@ -25,27 +40,36 @@ static void ls_dir(const char* path) {
             struct dirent* d = (struct dirent*)(buf + off);
             if (d->d_reclen == 0) break;
 
-            /* skip hidden files unless -a */
             if (!aflag && d->d_name[0] == '.') {
                 off += d->d_reclen;
                 continue;
             }
 
-            if (lflag) {
-                char type = '-';
-                if (d->d_type == DT_DIR) type = 'd';
-                else if (d->d_type == DT_CHR) type = 'c';
-                else if (d->d_type == DT_LNK) type = 'l';
-                else if (d->d_type == DT_BLK) type = 'b';
-                printf("%c  %s\n", type, d->d_name);
-            } else {
-                printf("%s\n", d->d_name);
+            if (count < LS_MAX_ENTRIES) {
+                strncpy(entries[count].name, d->d_name, 255);
+                entries[count].name[255] = '\0';
+                entries[count].type = d->d_type;
+                count++;
             }
             off += d->d_reclen;
         }
     }
-
     close(fd);
+
+    qsort(entries, count, sizeof(struct ls_entry), cmp_entry);
+
+    for (int i = 0; i < count; i++) {
+        if (lflag) {
+            char type = '-';
+            if (entries[i].type == DT_DIR) type = 'd';
+            else if (entries[i].type == DT_CHR) type = 'c';
+            else if (entries[i].type == DT_LNK) type = 'l';
+            else if (entries[i].type == DT_BLK) type = 'b';
+            printf("%c  %s\n", type, entries[i].name);
+        } else {
+            printf("%s\n", entries[i].name);
+        }
+    }
 }
 
 int main(int argc, char** argv) {
index f9d8d5ba5bb3483faec9efa3996e4efad9717769..251de0d1584d96674a9d9ef8477df9185ad00e0d 100644 (file)
--- a/user/ps.c
+++ b/user/ps.c
@@ -28,8 +28,13 @@ int main(void) {
                 char cmd[64] = "?";
                 if (cfd >= 0) {
                     int n = read(cfd, cmd, sizeof(cmd) - 1);
-                    if (n > 0) cmd[n] = '\0';
-                    else strcpy(cmd, "?");
+                    if (n > 0) {
+                        cmd[n] = '\0';
+                        while (n > 0 && (cmd[n-1] == '\n' || cmd[n-1] == '\0')) {
+                            cmd[--n] = '\0';
+                        }
+                    }
+                    if (n <= 0) strcpy(cmd, "[kernel]");
                     close(cfd);
                 }
                 printf("%5s %s\n", d->d_name, cmd);
index 688c4240a4689fd7e8cf3bc2538f57593cf1b3e0..1d662cbe1da2a29b6b70fe9254092bf04990725d 100644 (file)
@@ -16,6 +16,9 @@ char*   getenv(const char* name);
 int     abs(int x);
 long    labs(long x);
 
+void    qsort(void* base, size_t nmemb, size_t size,
+              int (*compar)(const void*, const void*));
+
 int     system(const char* cmd);
 void    exit(int status) __attribute__((noreturn));
 
index dddeb03ad8be1a641409ccd64417f54a7e0e640c..998dc77b4c5521ca6e72e9e89afb7d724b857fdb 100644 (file)
@@ -176,6 +176,31 @@ long labs(long x) {
     return x < 0 ? -x : x;
 }
 
+void qsort(void* base, size_t nmemb, size_t size,
+           int (*compar)(const void*, const void*)) {
+    if (nmemb < 2 || !base || !compar) return;
+    char* b = (char*)base;
+    char tmp[256];
+    for (size_t i = 1; i < nmemb; i++) {
+        size_t j = i;
+        while (j > 0 && compar(b + (j - 1) * size, b + j * size) > 0) {
+            /* swap elements — use stack buffer for small, byte-swap for large */
+            char* a1 = b + (j - 1) * size;
+            char* a2 = b + j * size;
+            if (size <= sizeof(tmp)) {
+                memcpy(tmp, a1, size);
+                memcpy(a1, a2, size);
+                memcpy(a2, tmp, size);
+            } else {
+                for (size_t k = 0; k < size; k++) {
+                    char t = a1[k]; a1[k] = a2[k]; a2[k] = t;
+                }
+            }
+            j--;
+        }
+    }
+}
+
 int system(const char* cmd) {
     (void)cmd;
     return -1;
index 7eee36f1ef1e85612317d8e5d5af376e5b9f1139..f5b1755d045697b0ea7653a31d7853ec00f55f6b 100644 (file)
@@ -11,7 +11,8 @@ int write(int fd, const void* buf, size_t count) {
     return __syscall_ret(_syscall3(SYS_WRITE, fd, (int)buf, (int)count));
 }
 
-int open(const char* path, int flags) {
+int open(const char* path, int flags, ...) {
+    (void)flags; /* mode arg unused by kernel currently */
     return __syscall_ret(_syscall2(SYS_OPEN, (int)path, flags));
 }