From b6f9633677bfdbde540d21d09bf6f068f7727512 Mon Sep 17 00:00:00 2001 From: nova Date: Tue, 8 Jul 2025 20:51:25 +0200 Subject: [PATCH] handling of empty dirs & refactoring --- colors.c | 7 ++--- defines.h | 1 + interactions.c | 1 - main.c | 30 +++++++++--------- threading.c | 83 ++++++++++++++++++++++++++++++++++++++------------ window.c | 17 +++++++++-- 6 files changed, 98 insertions(+), 41 deletions(-) diff --git a/colors.c b/colors.c index cf378bb..d55eba1 100644 --- a/colors.c +++ b/colors.c @@ -50,15 +50,14 @@ void colors_init() { FILE *dircolors = fopen("/etc/DIR_COLORS", "r"); if (dircolors) { - char *line; + char *line = NULL; char *token; char *extension; - int tmp = 0; size_t size = 0; short fg; short bg; - while ((tmp = getline(&line, &size, dircolors)) != -1) { + while (getline(&line, &size, dircolors) != -1) { fg = 7; bg = 0; token = strtok(line, " "); @@ -101,7 +100,7 @@ void colors_init() { colors = malloc(sizeof(color) * color_count); unsigned int i = 0; /* proper pass, reads all defined extensions within /etc/DIR_COLORS */ - while ((tmp = getline(&line, &size, dircolors)) != -1) { + while (getline(&line, &size, dircolors) != -1) { fg = 7; bg = 0; if (line[0] == '.') { diff --git a/defines.h b/defines.h index ceea496..e77e7b0 100644 --- a/defines.h +++ b/defines.h @@ -24,6 +24,7 @@ #define FILE_STATUS_HOVER 1 #define FILE_STATUS_SELECTED 2 #define FILE_STATUS_IS_REGULAR_FILE 4 +#define FILE_STATUS_DIR_EMPTY 64 /* if a directory is empty */ #define FILE_STATUS_FILE_OPEN 128 /* only used for file previews */ #define COLOR_UNKNOWN 0 diff --git a/interactions.c b/interactions.c index cf2feb0..4b6e744 100644 --- a/interactions.c +++ b/interactions.c @@ -59,7 +59,6 @@ void user_interactions() { terminal_width_empty_line = malloc(terminal_width); memset(terminal_width_empty_line, ' ', terminal_width); - move(terminal_height, terminal_width); ch = getch(); if(ch != ERR) { timeout(1); /* blocking timeout of getch() */ diff --git a/main.c b/main.c index 54f558f..d1255f9 100644 --- a/main.c +++ b/main.c @@ -66,28 +66,30 @@ int main(){ temp_width = terminal_width; temp_heigth = terminal_height; } + if (status & STATUS_RUN_BACKEND && threading) { + pthread_cancel(thread_b); + pthread_cancel(thread_r); + pthread_cancel(thread_m); + pthread_cancel(thread_l); + pthread_cancel(thread_t); + } + if (threading) { + pthread_join(thread_b, NULL); + pthread_join(thread_r, NULL); + pthread_join(thread_m, NULL); + pthread_join(thread_l, NULL); + pthread_join(thread_t, NULL); + threading = 0; + } if (status & STATUS_RUN_BACKEND) { - if (threading) { - pthread_cancel(thread_t); - pthread_cancel(thread_l); - pthread_cancel(thread_m); - pthread_cancel(thread_r); - pthread_cancel(thread_b); - pthread_join(thread_b, NULL); - pthread_join(thread_r, NULL); - pthread_join(thread_m, NULL); - pthread_join(thread_l, NULL); - pthread_join(thread_t, NULL); - threading = 0; - } pthread_create(&thread_t, NULL, thread_top, win_t); /*top bar*/ pthread_create(&thread_l, NULL, thread_lft, win_l); /*current_content slash win_m*/ pthread_create(&thread_m, NULL, thread_mid, win_m); /*parent_content slash win_l*/ pthread_create(&thread_r, NULL, thread_rgt, win_r); /*child_content slash win_r*/ pthread_create(&thread_b, NULL, thread_btm, win_b); /*bottom bar*/ - threading = 1; status &= ~(STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY); status |= STATUS_UPDATE_SCREEN_0; + threading = 1; } user_interactions(); diff --git a/threading.c b/threading.c index 0c3bf50..f162e89 100644 --- a/threading.c +++ b/threading.c @@ -51,8 +51,8 @@ void *thread_mid(void *data){ char *path; if((path=getcwd(NULL, 0)) == NULL) { mid_content = malloc(sizeof(file)); - mid_content[0].file_name_width = sizeof("cannot open directory"); - mid_content[0].file_name = "cannot open directory"; + mid_content->file_name_width = sizeof("cannot open directory"); + mid_content->file_name = "cannot open directory"; mid_file_count = 1; } else { @@ -60,9 +60,40 @@ void *thread_mid(void *data){ if (status & STATUS_RELOAD_DIRECTORY) { free(mid_content); mid_file_count = (unsigned long)get_dir_size(path); - mid_content = malloc(mid_file_count * sizeof(file)); - memset(mid_content, ' ', mid_file_count * sizeof(file)); - get_dir_content(path, &mid_file_count, mid_content); + if (mid_file_count > 0) { + mid_content = malloc(mid_file_count * sizeof(file)); + memset(mid_content, ' ', mid_file_count * sizeof(file)); + get_dir_content(path, &mid_file_count, mid_content); + } else { + mid_content = malloc(sizeof(file)); + mid_content->status = FILE_STATUS_DIR_EMPTY; + mid_content->file_type = 0; + mid_content->file_size = 0; + mid_content->permissions = 0; + mid_content->color_pair = 0; + mid_content->file_name_width = sizeof("empty dir"); + mid_content->file_name = "empty dir"; + + file_current->file_name = mid_content->file_name; + file_current->file_name_width = mid_content->file_name_width; + file_current->file_size = mid_content->file_size; + file_current->file_type = mid_content->file_type; + file_current->color_pair = mid_content->color_pair; + file_current->permissions = mid_content->permissions; + file_current->status = mid_content->status; + mid_file_count = 0; + + while(wait_count < 2){ + /*wait for thread_rgt and thread_btm to lock*/ + } + pthread_mutex_lock(&mutex_wait); + pthread_cond_broadcast(&cond_wait); + pthread_mutex_unlock(&mutex_wait); + + free(path); + pthread_mutex_unlock(&mutex_mid); + pthread_exit(0); + } } @@ -81,7 +112,9 @@ void *thread_mid(void *data){ file_current->file_name_width = mid_content[selected_file_current].file_name_width; file_current->file_size = mid_content[selected_file_current].file_size; file_current->file_type = mid_content[selected_file_current].file_type; + file_current->color_pair = mid_content[selected_file_current].color_pair; file_current->permissions = mid_content[selected_file_current].permissions; + file_current->status = mid_content[selected_file_current].status; while(wait_count < 2){ /*wait for thread_rgt and thread_btm to lock*/ @@ -97,7 +130,7 @@ void *thread_mid(void *data){ } free(path); pthread_mutex_unlock(&mutex_mid); - pthread_exit(NULL); + pthread_exit(0); } void *thread_lft(void *data){ pthread_mutex_lock(&mutex_lft); @@ -124,19 +157,20 @@ void *thread_lft(void *data){ } free(path); pthread_mutex_unlock(&mutex_lft); - pthread_exit(NULL); + pthread_exit(0); } void *thread_rgt(void *data){ + pthread_mutex_lock(&mutex_rgt); pthread_mutex_lock(&mutex_wait); wait_count++; pthread_cond_wait(&cond_wait, &mutex_wait); wait_count--; pthread_mutex_unlock(&mutex_wait); - pthread_mutex_lock(&mutex_rgt); + pthread_mutex_lock(&mutex_mid); free(rgt_content); rgt_content = malloc(sizeof(file)); rgt_content->file_name = malloc(file_current->file_name_width + 1); @@ -144,10 +178,10 @@ void *thread_rgt(void *data){ rgt_content->file_name_width = file_current->file_name_width; rgt_content->file_size = file_current->file_size; rgt_content->file_type = file_current->file_type; - + rgt_content->status = file_current->status; pthread_mutex_unlock(&mutex_mid); - if (rgt_content[0].file_type == FILE_TYPE_DIR || rgt_content[0].file_type == FILE_TYPE_SYMLINK) { + if (rgt_content->file_type == FILE_TYPE_DIR || rgt_content->file_type == FILE_TYPE_SYMLINK) { char *path = malloc(rgt_content[0].file_name_width + 1); strcpy(path, rgt_content[0].file_name); free(rgt_content); @@ -159,17 +193,21 @@ void *thread_rgt(void *data){ free(path); } else { - rgt_file_count = 1; - - rgt_content->file_type = FILE_TYPE_OPEN_FILE; - rgt_content->status = FILE_STATUS_HOVER; - free(rgt_buffer); - rgt_buffer = preview_file(rgt_content); + rgt_file_count = 0; + if (rgt_content->status & FILE_STATUS_DIR_EMPTY) { + free(rgt_buffer); + rgt_buffer = "empty dir"; + } else { + rgt_content->file_type = FILE_TYPE_OPEN_FILE; + rgt_content->status = FILE_STATUS_HOVER; + free(rgt_buffer); + rgt_buffer = preview_file(rgt_content); + } } pthread_mutex_unlock(&mutex_rgt); - pthread_exit(NULL); + pthread_exit(0); } void *thread_top(void *data){ pthread_mutex_lock(&mutex_top); @@ -187,7 +225,7 @@ void *thread_top(void *data){ free(path); pthread_mutex_unlock(&mutex_top); - pthread_exit(NULL); + pthread_exit(0); } void *thread_btm(void *data){ pthread_mutex_lock(&mutex_wait); @@ -214,7 +252,7 @@ void *thread_btm(void *data){ pthread_mutex_unlock(&mutex_btm); - pthread_exit(NULL); + pthread_exit(0); } void threading_init(){ @@ -226,6 +264,13 @@ void threading_init(){ rgt_buffer = malloc(sizeof(char)); btm_buffer = malloc(sizeof(char)); + + rgt_content[0].file_type = 0; + rgt_content[0].file_size = 1; + rgt_content[0].file_name_width = 1; + rgt_content[0].file_name = malloc(sizeof("a")); + strcpy(rgt_content[0].file_name, "a"); + file_current = malloc(sizeof(file)); file_current->file_type = 0; file_current->file_size = 1; diff --git a/window.c b/window.c index 25ac579..3d4cd6b 100644 --- a/window.c +++ b/window.c @@ -92,7 +92,11 @@ void window_mid(WINDOW *win){ pthread_mutex_unlock(&mutex_rgt); } else { - print_dir(win, &local_width, &mid_file_count, mid_content); + if (mid_file_count == 0) { + mvwprintw(win, 0, 0, "empty"); + } else { + print_dir(win, &local_width, &mid_file_count, mid_content); + } pthread_mutex_unlock(&mutex_mid); } } @@ -109,8 +113,15 @@ void window_rgt(WINDOW *win){ status |= STATUS_UPDATE_SCREEN_0; pthread_mutex_unlock(&mutex_rgt); } else { - if (rgt_content[0].file_type == FILE_TYPE_OPEN_FILE) { - mvwprintw(win, 0, 0, "%s", rgt_buffer); + + if (rgt_file_count == 0) { + if (rgt_content[0].file_type == FILE_TYPE_OPEN_FILE) { + mvwprintw(win, 0, 0, "%s", rgt_buffer); + } else { + mvwprintw(win, 0, 0, "empty"); + } + + } else { print_dir(win, &local_width, &rgt_file_count, rgt_content); }