diff --git a/arm9/source/filesys/fsdir.c b/arm9/source/filesys/fsdir.c index aaa208f..c7ed354 100644 --- a/arm9/source/filesys/fsdir.c +++ b/arm9/source/filesys/fsdir.c @@ -5,26 +5,25 @@ void DirEntryCpy(DirEntry* dest, const DirEntry* orig) { dest->name = dest->path + (orig->name - orig->path); } +int compDirEntry(const void* e1, const void* e2) { + const DirEntry* entry1 = (const DirEntry*) e1; + const DirEntry* entry2 = (const DirEntry*) e2; + if (entry1->type == T_DOTDOT) return -1; + if (entry2->type == T_DOTDOT) return 1; + if (entry1->type != entry2->type) + return entry1->type - entry2->type; + return strncasecmp(entry1->path, entry2->path, 256); +} + void SortDirStruct(DirStruct* contents) { - for (u32 s = 0; s < contents->n_entries; s++) { - DirEntry* cmp0 = &(contents->entry[s]); - DirEntry* min0 = cmp0; - if (cmp0->type == T_DOTDOT) continue; - for (u32 c = s + 1; c < contents->n_entries; c++) { - DirEntry* cmp1 = &(contents->entry[c]); - if (min0->type != cmp1->type) { - if (min0->type > cmp1->type) - min0 = cmp1; - continue; - } - if (strncasecmp(min0->name, cmp1->name, 256) > 0) - min0 = cmp1; - } - if (min0 != cmp0) { - DirEntry swap; // swap entries and fix names - DirEntryCpy(&swap, cmp0); - DirEntryCpy(cmp0, min0); - DirEntryCpy(min0, &swap); + qsort(contents->entry, contents->n_entries, sizeof(DirEntry), compDirEntry); + for (int i = 0; i < (int)contents->n_entries; i++) { + DirEntry* entry = &(contents->entry[i]); + if (entry->type == T_DOTDOT) { + entry->name = entry->path + 8; + } else { + char* slash = strrchr(entry->path, '/'); + entry->name = slash ? slash + 1 : entry->path; } } }