]> 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 b81e67b7616b028d0ec5f4055f2a3d1b0e193345..815fb09d13cec9a3ca6bdcf5e6096f7413dedf39 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) {
@@ -26,6 +40,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) {
@@ -34,27 +49,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 0ddb7d1b7f77a8cbb66bdf1cd3a649a9a82c995a..af3f55e47f406522bc7910afb2f04146b6b640ef 100644 (file)
--- a/user/ps.c
+++ b/user/ps.c
@@ -37,8 +37,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 97ebaae5124e10a7949c44353e216b12e1d225f9..17bbe8173a99c68d1835cb77302d3e1a0c71c371 100644 (file)
@@ -25,6 +25,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 374f85017fcb1d2e76fc871a6b4e49d2fe16ac85..b0545b22e09224b2effa0ffe3584f8d428bf969e 100644 (file)
@@ -185,6 +185,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 52af6091520b2b7cec3782ecf498bfe7b6b556f9..d68ab5db914e14f163df12cf72bde234ffe5f6d5 100644 (file)
@@ -20,7 +20,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));
 }