From: Tulio A M Mendes Date: Tue, 17 Feb 2026 06:13:11 +0000 (-0300) Subject: fix: ps shows [kernel] for empty cmdline, ls sorts alphabetically, add qsort to ulibc X-Git-Url: https://projects.tadryanom.me/docs/POSIX_ROADMAP.md?a=commitdiff_plain;h=533ff29a5734325a9141e6841420950014295f24;p=AdrOS.git fix: ps shows [kernel] for empty cmdline, ls sorts alphabetically, add qsort to ulibc --- diff --git a/user/ls.c b/user/ls.c index c613d50..b8cc3ed 100644 --- a/user/ls.c +++ b/user/ls.c @@ -10,6 +10,20 @@ 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) { diff --git a/user/ps.c b/user/ps.c index f9d8d5b..251de0d 100644 --- 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); diff --git a/user/ulibc/include/stdlib.h b/user/ulibc/include/stdlib.h index 688c424..1d662cb 100644 --- a/user/ulibc/include/stdlib.h +++ b/user/ulibc/include/stdlib.h @@ -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)); diff --git a/user/ulibc/src/stdlib.c b/user/ulibc/src/stdlib.c index dddeb03..998dc77 100644 --- a/user/ulibc/src/stdlib.c +++ b/user/ulibc/src/stdlib.c @@ -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; diff --git a/user/ulibc/src/unistd.c b/user/ulibc/src/unistd.c index 7eee36f..f5b1755 100644 --- a/user/ulibc/src/unistd.c +++ b/user/ulibc/src/unistd.c @@ -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)); }