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) {
return;
}
+ int count = 0;
char buf[2048];
int rc;
while ((rc = getdents(fd, buf, sizeof(buf))) > 0) {
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) {
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);
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;
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));
}