once again switched to complex data types, basic file coloring implemented

This commit is contained in:
nova
2025-05-08 16:03:16 +02:00
parent 08913786de
commit 37d5531aa7
5 changed files with 100 additions and 103 deletions

View File

@ -15,20 +15,17 @@ pthread_mutex_t mutex_rgt;
/* contains entire directory as 2d array
* may be changed in future to only include parts of the dir (currently includes entire dir) */
char *mid_content;
char *lft_content;
file *rgt_content;
file *mid_content;
file *lft_content;
char *top_content; /* current path */
/* index 0 = file_count
* index 1 = longest_name */
unsigned long *mid_length_width;
unsigned long *lft_length_width;
unsigned long *top_length_width;
/* this array exists so that a file with a 3 char long name wont spend the same amount of time printing as a file with 200 chars
* should both exist in the same directory
* currently unused */
unsigned long *mid_file_name_width;
unsigned long *lft_file_name_width;
unsigned long rgt_file_count;
unsigned long mid_file_count;
unsigned long lft_file_count;
unsigned long top_width;
extern unsigned int status;
@ -36,24 +33,19 @@ void *thread_mid(void *data){
pthread_mutex_lock(&mutex_mid);
free(mid_content);
free(mid_file_name_width);
free(mid_length_width);
mid_length_width = malloc(sizeof(char)*2);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
mid_content = malloc(sizeof("cannot open directory"));
mid_length_width[0] = 1;
mid_length_width[1] = sizeof("cannot open directory");
mid_content = "cannot open directory";
mid_content = malloc(sizeof(file));
mid_content[1].file_name_width = sizeof("cannot open directory");
mid_content[1].file_name = "cannot open directory";
mid_file_count = 1;
} else {
get_dir_size(path, mid_length_width, mid_file_name_width);
mid_file_name_width = malloc(mid_length_width[0] * sizeof(unsigned long));
mid_content = malloc(mid_length_width[0] * mid_length_width[1] * sizeof(char));
memset(mid_content, ' ', mid_length_width[0] * mid_length_width[1] * sizeof(char));
get_dir_content(path, mid_length_width, mid_file_name_width, mid_content);
mid_file_count = (unsigned long)get_dir_size(path);
mid_content = malloc(mid_file_count * sizeof(file));
get_dir_content(path, &mid_file_count, mid_content);
}
free(path);
@ -64,19 +56,15 @@ void *thread_lft(void *data){
pthread_mutex_lock(&mutex_lft);
free(lft_content);
free(lft_file_name_width);
free(lft_length_width);
lft_length_width = malloc(sizeof(char)*2);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
lft_content = malloc(sizeof("cannot open directory"));
lft_length_width[0] = 1;
lft_length_width[1] = sizeof("cannot open directory");
lft_content = "cannot open directory";
lft_content = malloc(sizeof(file));
lft_content[1].file_name_width = sizeof("cannot open directory");
lft_content[1].file_name = "cannot open directory";
lft_file_count = 1;
} else {
char *parent ;
if((parent = malloc(strlen(path)+strlen("/..")+1)) != NULL){
parent[0] = '\0'; /* ensures empty string */
@ -84,16 +72,17 @@ void *thread_lft(void *data){
strcat(parent, "/..");
}
get_dir_size(parent, lft_length_width, lft_file_name_width);
lft_file_name_width = malloc(lft_length_width[0] * sizeof(unsigned long));
lft_content = malloc(lft_length_width[0] * lft_length_width[1] * sizeof(char));
memset(lft_content, ' ', lft_length_width[0] * lft_length_width[1] * sizeof(char));
get_dir_content(parent, lft_length_width, lft_file_name_width, lft_content);
free (parent);
lft_file_count = (unsigned long)get_dir_size(parent);
lft_content = malloc(lft_file_count * sizeof(file));
get_dir_content(parent, &lft_file_count, lft_content);
free(parent);
}
free(path);
pthread_mutex_unlock(&mutex_lft);
pthread_exit(NULL);
}
void *thread_rgt(void *data){
@ -103,19 +92,15 @@ void *thread_rgt(void *data){
void *thread_top(void *data){
pthread_mutex_lock(&mutex_top);
free(top_content);
free(top_length_width);
top_length_width = malloc(sizeof(char)*2);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
top_content = malloc(sizeof("cannot open directory"));
top_length_width[0] = 1;
top_length_width[1] = sizeof("cannot open directory");
top_width = sizeof("cannot open directory");
top_content = "cannot open directory";
} else {
top_content = getcwd(NULL, 0);
top_length_width[0] = 1;
top_length_width[1] = strlen(top_content);
top_width = strlen(top_content);
}
free(path);
@ -128,24 +113,23 @@ void *thread_btm(void *data){
}
void threading_init(){
mid_content = malloc(sizeof(char));
mid_file_name_width = malloc(sizeof(char));
mid_length_width = malloc(sizeof(char));
lft_content = malloc(sizeof(char));
lft_file_name_width = malloc(sizeof(char));
lft_length_width = malloc(sizeof(char));
top_length_width = malloc(sizeof(char));
rgt_content = malloc(sizeof(char));
mid_content = malloc(sizeof(file));
lft_content = malloc(sizeof(file));
top_content = malloc(sizeof(char));
pthread_mutex_init(&mutex_top, NULL);
pthread_mutex_init(&mutex_mid, NULL);
pthread_mutex_init(&mutex_lft, NULL);
}
void threading_free(){
free(rgt_content);
free(mid_content);
free(mid_file_name_width);
free(mid_length_width);
free(lft_content);
free(top_content);
pthread_mutex_destroy(&mutex_top);
pthread_mutex_destroy(&mutex_mid);