diff --git a/backend.c b/backend.c index edb03dc..c450679 100644 --- a/backend.c +++ b/backend.c @@ -82,10 +82,15 @@ char* smartstrcasestr(const char *haystack, const char *needle){ char* parse_cmd(const char *cmd, file *f){ + char *out; + if (f == NULL) { + out = malloc(strlen(cmd)+1); + memcpy(out, cmd, strlen(cmd)+1); + return out; + } const char *offset = strstr(cmd, SETTINGS_COMMAND_REPLACE_STR); int count = 0; - char *out; char *pos; unsigned long i = 0; while(f->file_name[i]) { diff --git a/dir.c b/dir.c index 9e078cd..faf4f9a 100644 --- a/dir.c +++ b/dir.c @@ -49,6 +49,12 @@ unsigned long get_dir_size(char *path){ } void get_dir_content(char *path, dir *dir){ + + if (dir->file_count == 0) { + return; + dir = NULL; + } + struct dirent **entry = NULL; if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */ scandir(path, &entry, skip_dot, NULL); diff --git a/interactions.c b/interactions.c index 01fbe93..a00ddbc 100644 --- a/interactions.c +++ b/interactions.c @@ -44,6 +44,11 @@ yank yank_files = { 0 }; void FAIL(char *function, char *str){ printf("ERROR in function %s: %s", function, str); } +#define continue_if_empty_dir \ + if (mid_dir.current_file == NULL) { \ + return; \ + } + void user_interactions() { @@ -165,6 +170,8 @@ void select_all(){ } void move_down(unsigned long passes){ + continue_if_empty_dir; + mid_dir.current_file += passes; if (mid_dir.current_file > mid_dir.file_list + mid_dir.file_count - 1) { mid_dir.current_file = mid_dir.file_list + mid_dir.file_count - 1; @@ -174,6 +181,8 @@ void move_down(unsigned long passes){ } void move_up(unsigned long passes){ + continue_if_empty_dir; + mid_dir.current_file -= passes; if (mid_dir.current_file < mid_dir.file_list) { mid_dir.current_file = mid_dir.file_list; @@ -191,6 +200,8 @@ void move_left(unsigned long passes){ } void move_right(){ + continue_if_empty_dir + if (mid_dir.current_file->file_type & FILE_TYPE_DIR) { change_dir(mid_dir.current_file->file_name); } else if (mid_dir.current_file->file_type & FILE_TYPE_EXEC) { diff --git a/threading.c b/threading.c index c68feb0..b696130 100644 --- a/threading.c +++ b/threading.c @@ -85,6 +85,7 @@ void *thread_mid(){ } else { /* the hovered dir is empty */ mid_dir.current_file = NULL; + mid_dir.file_list = NULL; } unsigned long i; @@ -95,11 +96,13 @@ void *thread_mid(){ } - if (mid_dir.current_file > mid_dir.file_list + mid_dir.file_count - 1) { - mid_dir.current_file = mid_dir.file_list + mid_dir.file_count - 1; - } - if (mid_dir.current_file < mid_dir.file_list) { - mid_dir.current_file = mid_dir.file_list; + if (mid_dir.current_file != NULL) { + if (mid_dir.current_file > mid_dir.file_list + mid_dir.file_count - 1) { + mid_dir.current_file = mid_dir.file_list + mid_dir.file_count - 1; + } + if (mid_dir.current_file < mid_dir.file_list) { + mid_dir.current_file = mid_dir.file_list; + } } pthread_mutex_unlock(&mutex_mid); @@ -204,20 +207,26 @@ void *thread_rgt(){ pthread_mutex_lock(&mutex_mid); - char *file_name = malloc(strlen(mid_dir.current_file->file_name)+1); - file_name = memcpy(file_name, mid_dir.current_file->file_name, strlen(mid_dir.current_file->file_name)+1); + if (mid_dir.current_file != NULL) { + char *file_name = malloc(strlen(mid_dir.current_file->file_name)+1); + file_name = memcpy(file_name, mid_dir.current_file->file_name, strlen(mid_dir.current_file->file_name)+1); - rgt_dir.file_list = malloc(sizeof(file)); - memcpy(rgt_dir.file_list, mid_dir.current_file, sizeof(file)); - rgt_dir.file_list->file_name = file_name; - rgt_dir.current_file = rgt_dir.file_list; - rgt_dir.file_count = 1; + rgt_dir.file_list = malloc(sizeof(file)); + memcpy(rgt_dir.file_list, mid_dir.current_file, sizeof(file)); + rgt_dir.file_list->file_name = file_name; + rgt_dir.current_file = rgt_dir.file_list; + rgt_dir.file_count = 1; + } else { + rgt_dir.current_file = NULL; + rgt_dir.file_list = NULL; + rgt_dir.file_count = 0; + } pthread_mutex_unlock(&mutex_mid); - if (rgt_dir.current_file->permissions & S_IRUSR) { + if (rgt_dir.current_file != NULL && rgt_dir.current_file->permissions & S_IRUSR) { if (rgt_dir.current_file->file_type & FILE_TYPE_DIR) { #if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0 images_clear(); @@ -288,13 +297,18 @@ void *thread_top(){ pthread_mutex_lock(&mutex_mid); - top_buffer = malloc(strlen(global_path)+1 + strlen(mid_dir.current_file->file_name)+1); - memcpy(top_buffer, global_path, strlen(global_path)); - memcpy(top_buffer + strlen(global_path) + 1, mid_dir.current_file->file_name, strlen(mid_dir.current_file->file_name)+1); + if (mid_dir.current_file != NULL) { + top_buffer = malloc(strlen(global_path)+1 + strlen(mid_dir.current_file->file_name)+1); + memcpy(top_buffer, global_path, strlen(global_path)); + memcpy(top_buffer + strlen(global_path) + 1, mid_dir.current_file->file_name, strlen(mid_dir.current_file->file_name)+1); + top_buffer[strlen(global_path)] = '/'; + top_width = strlen(top_buffer); + } else { + top_buffer = malloc(strlen(global_path)+1); + memcpy(top_buffer, global_path, strlen(global_path)+1); + } pthread_mutex_unlock(&mutex_mid); - top_buffer[strlen(global_path)] = '/'; - top_width = strlen(top_buffer); /* rendering */ @@ -404,17 +418,19 @@ void *thread_btm(){ memcpy(btm_buffer + buffer_width - ui_btm_right_block_size, ui_btm_right_block, ui_btm_right_block_size); - btm_buffer[0] = (S_ISLNK(mid_dir.current_file->permissions)) ? 'l': - (S_ISDIR(mid_dir.current_file->permissions) ? 'd': '-'); - btm_buffer[1] = (mid_dir.current_file->permissions & S_IRUSR) ? 'r' : '-'; - btm_buffer[2] = (mid_dir.current_file->permissions & S_IWUSR) ? 'w' : '-'; - btm_buffer[3] = (mid_dir.current_file->permissions & S_IXUSR) ? 'x' : '-'; - btm_buffer[4] = (mid_dir.current_file->permissions & S_IRGRP) ? 'r' : '-'; - btm_buffer[5] = (mid_dir.current_file->permissions & S_IWGRP) ? 'w' : '-'; - btm_buffer[6] = (mid_dir.current_file->permissions & S_IXGRP) ? 'x' : '-'; - btm_buffer[7] = (mid_dir.current_file->permissions & S_IROTH) ? 'r' : '-'; - btm_buffer[8] = (mid_dir.current_file->permissions & S_IWOTH) ? 'w' : '-'; - btm_buffer[9] = (mid_dir.current_file->permissions & S_IXOTH) ? 'x' : '-'; + if (mid_dir.current_file != NULL) { + btm_buffer[0] = (S_ISLNK(mid_dir.current_file->permissions)) ? 'l': + (S_ISDIR(mid_dir.current_file->permissions) ? 'd': '-'); + btm_buffer[1] = (mid_dir.current_file->permissions & S_IRUSR) ? 'r' : '-'; + btm_buffer[2] = (mid_dir.current_file->permissions & S_IWUSR) ? 'w' : '-'; + btm_buffer[3] = (mid_dir.current_file->permissions & S_IXUSR) ? 'x' : '-'; + btm_buffer[4] = (mid_dir.current_file->permissions & S_IRGRP) ? 'r' : '-'; + btm_buffer[5] = (mid_dir.current_file->permissions & S_IWGRP) ? 'w' : '-'; + btm_buffer[6] = (mid_dir.current_file->permissions & S_IXGRP) ? 'x' : '-'; + btm_buffer[7] = (mid_dir.current_file->permissions & S_IROTH) ? 'r' : '-'; + btm_buffer[8] = (mid_dir.current_file->permissions & S_IWOTH) ? 'w' : '-'; + btm_buffer[9] = (mid_dir.current_file->permissions & S_IXOTH) ? 'x' : '-'; + }