th/threading.c

154 lines
4.2 KiB
C

#include <curses.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "defines.h"
#include "backend.h"
pthread_mutex_t mutex_top;
pthread_mutex_t mutex_btm;
pthread_mutex_t mutex_lft;
pthread_mutex_t mutex_mid;
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;
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;
extern unsigned int status;
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";
} 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);
}
free(path);
pthread_mutex_unlock(&mutex_mid);
pthread_exit(NULL);
}
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";
} else {
char *parent ;
if((parent = malloc(strlen(path)+strlen("/..")+1)) != NULL){
parent[0] = '\0'; /* ensures empty string */
strcat(parent, path);
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);
}
free(path);
pthread_mutex_unlock(&mutex_lft);
pthread_exit(NULL);
}
void *thread_rgt(void *data){
pthread_exit(NULL);
}
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_content = "cannot open directory";
} else {
top_content = getcwd(NULL, 0);
top_length_width[0] = 1;
top_length_width[1] = strlen(top_content);
}
free(path);
pthread_mutex_unlock(&mutex_top);
pthread_exit(NULL);
}
void *thread_btm(void *data){
pthread_exit(NULL);
}
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));
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(mid_content);
free(mid_file_name_width);
free(mid_length_width);
pthread_mutex_destroy(&mutex_top);
pthread_mutex_destroy(&mutex_mid);
pthread_mutex_destroy(&mutex_lft);
}