handling of empty dirs & refactoring
This commit is contained in:
83
threading.c
83
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;
|
||||
|
Reference in New Issue
Block a user