Improved DIR formatting

This commit is contained in:
Peter Weingartner 2022-04-24 13:48:51 -04:00
parent 7c2573e9d6
commit 03de9599a9
5 changed files with 2186 additions and 2067 deletions

View file

@ -1,6 +1,7 @@
#include <ctype.h> #include <ctype.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "syscalls.h" #include "syscalls.h"
@ -288,10 +289,64 @@ short cmd_rename(short screen, int argc, const char * argv[]) {
return 0; return 0;
} }
/**
* Structure to hold file and directory information for sorting
*/
typedef struct s_dir_entry {
t_file_info info;
struct s_dir_entry *next, *prev;
} t_dir_entry, *p_dir_entry;
/**
* Add a directory entry to a list of directory entries using simple insertion sort
*
* @param list_pointer a pointer to a pointer to a list of directory entries
* @param entry a pointer to a directory entry to add
*/
void dir_entry_insert(p_dir_entry * list_pointer, p_dir_entry entry) {
p_dir_entry list = *list_pointer;
if (list == 0) {
*list_pointer = entry;
entry->next = 0;
entry->prev = 0;
} else {
p_dir_entry x = list;
while (x != 0) {
if (strcmp(x->info.name, entry->info.name) >= 0) {
if (x->prev == 0) {
*list_pointer = entry;
entry->next = x;
entry->prev = 0;
x->prev = entry;
} else {
entry->prev = x->prev;
entry->next = x;
x->prev->next = entry;
x->prev = entry;
}
return;
} else {
if (x->next == 0) {
x->next = entry;
entry->prev = x;
entry->next = 0;
return;
} else {
x = x->next;
}
}
}
}
}
short cmd_dir(short screen, int argc, const char * argv[]) { short cmd_dir(short screen, int argc, const char * argv[]) {
short result; short result;
char buffer[80]; char buffer[80];
t_file_info my_file; t_file_info my_file;
p_dir_entry directories = 0, files = 0, entry = 0, prev = 0;
char * path = ""; char * path = "";
char label[40]; char label[40];
@ -313,18 +368,24 @@ short cmd_dir(short screen, int argc, const char * argv[]) {
if ((my_file.attributes & AM_HID) == 0) { if ((my_file.attributes & AM_HID) == 0) {
if (my_file.attributes & AM_DIR) { if (my_file.attributes & AM_DIR) {
sprintf(buffer, "%s/\n", my_file.name); entry = (p_dir_entry)malloc(sizeof(t_dir_entry));
chan_write(screen, buffer, strlen(buffer)); if (entry) {
memcpy(&entry->info, &my_file, sizeof(t_file_info));
dir_entry_insert(&directories, entry);
} else {
print(screen, "Unable to display directory... out of memory.\n");
return -1;
}
} else { } else {
if (my_file.size < 1024) { entry = (p_dir_entry)malloc(sizeof(t_dir_entry));
sprintf(buffer, "%-20.20s %d\n", my_file.name, (int)my_file.size); if (entry) {
} else if (my_file.size < 1024*1024) { memcpy(&entry->info, &my_file, sizeof(t_file_info));
sprintf(buffer, "%-20.20s %d KB\n", my_file.name, (int)my_file.size / 1024); dir_entry_insert(&files, entry);
} else { } else {
sprintf(buffer, "%-29.20s %d MB\n", my_file.name, (int)my_file.size / (1024*1024)); print(screen, "Unable to display directory... out of memory.\n");
return -1;
} }
chan_write(screen, buffer, strlen(buffer));
} }
} }
} else { } else {
@ -333,6 +394,33 @@ short cmd_dir(short screen, int argc, const char * argv[]) {
} }
fsys_closedir(dir); fsys_closedir(dir);
// Print the directories
entry = directories;
while (entry != 0) {
sprintf(buffer, "%s/\n", entry->info.name);
print(screen, buffer);
prev = entry;
entry = entry->next;
free(prev);
}
// Print the files
entry = files;
while (entry != 0) {
if (entry->info.size < 1024) {
sprintf(buffer, "%-20.20s %d B\n", entry->info.name, (int)entry->info.size);
} else if (my_file.size < 1024*1024) {
sprintf(buffer, "%-20.20s %d KB\n", entry->info.name, (int)entry->info.size / 1024);
} else {
sprintf(buffer, "%-29.20s %d MB\n", entry->info.name, (int)entry->info.size / (1024*1024));
}
print(screen, buffer);
prev = entry;
entry = entry->next;
free(prev);
}
} else { } else {
err_print(screen, "Unable to open directory", dir); err_print(screen, "Unable to open directory", dir);
return dir; return dir;

View file

@ -311,7 +311,7 @@ short cli_mem_test(short channel, int argc, const char * argv[]) {
volatile unsigned char * memory = 0x00000000; volatile unsigned char * memory = 0x00000000;
t_sys_info sys_info; t_sys_info sys_info;
unsigned long mem_start = 0x00010000; unsigned long mem_start = 0x00010000;
unsigned long mem_end = mem_get_ramtop(); unsigned long mem_end = sys_mem_get_ramtop();
char message[80]; char message[80];
unsigned long i; unsigned long i;
@ -326,7 +326,7 @@ short cli_mem_test(short channel, int argc, const char * argv[]) {
} }
#endif #endif
sprintf(message, "\x1B[H\x1B[2JTesting memory from 0x%08X to 0x%08X\n", mem_start, mem_end); sprintf(message, "\x1B[H\x1B[2JTesting memory from 0x%08X to 0x%08X\n", (unsigned long)mem_start, (unsigned long)mem_end);
print(channel, message); print(channel, message);
for (i = mem_start; i < mem_end; i++) { for (i = mem_start; i < mem_end; i++) {

File diff suppressed because it is too large Load diff

View file

@ -7,6 +7,6 @@
#define VER_MAJOR 0 #define VER_MAJOR 0
#define VER_MINOR 52 #define VER_MINOR 52
#define VER_BUILD 19 #define VER_BUILD 23
#endif #endif