fixed some memory leaks
This commit is contained in:
@ -46,7 +46,7 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
|
|||||||
if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||||
} else {
|
} else {
|
||||||
dir_content[i].file_name_width = strlen(entry[i]->d_name);
|
dir_content[i].file_name_width = strlen(entry[i]->d_name);
|
||||||
dir_content[i].file_name = malloc(dir_content[i].file_name_width);
|
dir_content[i].file_name = malloc(dir_content[i].file_name_width + 1);
|
||||||
strcpy(dir_content[i].file_name, entry[i]->d_name);
|
strcpy(dir_content[i].file_name, entry[i]->d_name);
|
||||||
|
|
||||||
struct stat *file;
|
struct stat *file;
|
||||||
@ -127,7 +127,7 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
|
|||||||
void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_count, file *dir_content){
|
void print_dir(WINDOW *win, unsigned long *line_width, unsigned long *dir_file_count, file *dir_content){
|
||||||
|
|
||||||
|
|
||||||
char *hover_bg = malloc(*line_width);
|
char *hover_bg = malloc(*line_width+1);
|
||||||
memset(hover_bg, ' ', *line_width);
|
memset(hover_bg, ' ', *line_width);
|
||||||
hover_bg[*line_width] = '\0';
|
hover_bg[*line_width] = '\0';
|
||||||
unsigned long i = 0;
|
unsigned long i = 0;
|
||||||
|
29
colors.c
29
colors.c
@ -1,6 +1,7 @@
|
|||||||
#define _POSIX_C_SOURCE 200809L
|
#define _POSIX_C_SOURCE 200809L
|
||||||
#include <bits/types/FILE.h>
|
#include <bits/types/FILE.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <curses.h>
|
#include <curses.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
@ -57,70 +58,57 @@ void colors_init() {
|
|||||||
short fg;
|
short fg;
|
||||||
short bg;
|
short bg;
|
||||||
|
|
||||||
char supported_filetype_count = 9;
|
while ((tmp = getline(&line, &size, dircolors)) != -1) {
|
||||||
char initial_pass = 0;
|
|
||||||
while ((tmp = getline(&line, &size, dircolors)) != -1 && initial_pass != supported_filetype_count) {
|
|
||||||
fg = 7;
|
fg = 7;
|
||||||
bg = 0;
|
bg = 0;
|
||||||
token = strtok(line, " ");
|
token = strtok(line, " ");
|
||||||
if (token[0] != '#' && token[0] != '\n') {
|
if (token[0] != '#' && token[0] != '\n') {
|
||||||
if (!strcmp(token, "DIR")) {
|
if (line[0] == '.') {
|
||||||
initial_pass++;
|
color_count++;
|
||||||
|
} else if (!strcmp(token, "DIR")) {
|
||||||
parse_colors(line, &fg, &bg);
|
parse_colors(line, &fg, &bg);
|
||||||
init_pair(1, fg, bg); /* directory */
|
init_pair(1, fg, bg); /* directory */
|
||||||
} else if (!strcmp(token, "EXEC")){
|
} else if (!strcmp(token, "EXEC")){
|
||||||
initial_pass++;
|
|
||||||
parse_colors(line, &fg, &bg);
|
parse_colors(line, &fg, &bg);
|
||||||
init_pair(2, fg, bg); /* exec */
|
init_pair(2, fg, bg); /* exec */
|
||||||
} else if (!strcmp(token, "RESET")){
|
} else if (!strcmp(token, "RESET")){
|
||||||
initial_pass++;
|
|
||||||
parse_colors(line, &fg, &bg);
|
parse_colors(line, &fg, &bg);
|
||||||
init_pair(3, fg, bg); /* regular file */
|
init_pair(3, fg, bg); /* regular file */
|
||||||
} else if (!strcmp(token, "LINK")){
|
} else if (!strcmp(token, "LINK")){
|
||||||
initial_pass++;
|
|
||||||
parse_colors(line, &fg, &bg);
|
parse_colors(line, &fg, &bg);
|
||||||
init_pair(4, fg, bg); /* symlink */
|
init_pair(4, fg, bg); /* symlink */
|
||||||
} else if (!strcmp(token, "BLK")){
|
} else if (!strcmp(token, "BLK")){
|
||||||
initial_pass++;
|
|
||||||
parse_colors(line, &fg, &bg);
|
parse_colors(line, &fg, &bg);
|
||||||
init_pair(5, fg, bg); /* block device */
|
init_pair(5, fg, bg); /* block device */
|
||||||
} else if (!strcmp(token, "CHR")){
|
} else if (!strcmp(token, "CHR")){
|
||||||
initial_pass++;
|
|
||||||
parse_colors(line, &fg, &bg);
|
parse_colors(line, &fg, &bg);
|
||||||
init_pair(6, fg, bg); /* character device */
|
init_pair(6, fg, bg); /* character device */
|
||||||
} else if (!strcmp(token, "SOCK")){
|
} else if (!strcmp(token, "SOCK")){
|
||||||
initial_pass++;
|
|
||||||
parse_colors(line, &fg, &bg);
|
parse_colors(line, &fg, &bg);
|
||||||
init_pair(7, fg, bg); /* socket */
|
init_pair(7, fg, bg); /* socket */
|
||||||
} else if (!strcmp(token, "FIFO")){
|
} else if (!strcmp(token, "FIFO")){
|
||||||
initial_pass++;
|
|
||||||
parse_colors(line, &fg, &bg);
|
parse_colors(line, &fg, &bg);
|
||||||
init_pair(8, fg, bg); /* fifo */
|
init_pair(8, fg, bg); /* fifo */
|
||||||
} else if (!strcmp(token, "ORPHAN")){
|
} else if (!strcmp(token, "ORPHAN")){
|
||||||
initial_pass++;
|
|
||||||
parse_colors(line, &fg, &bg);
|
parse_colors(line, &fg, &bg);
|
||||||
init_pair(9, fg, bg); /* orphan */
|
init_pair(9, fg, bg); /* orphan */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* checks for only extensions (*.[extension], includes the '.') as the filetypes are handled seperately */
|
|
||||||
while ((tmp = getline(&line, &size, dircolors)) != -1) {
|
|
||||||
if (line[0] == '.') {
|
|
||||||
color_count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rewind(dircolors);
|
rewind(dircolors);
|
||||||
|
|
||||||
colors = malloc(sizeof(color) * color_count);
|
colors = malloc(sizeof(color) * color_count);
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
|
unsigned int j = 0;
|
||||||
/* proper pass, reads all defined extensions within /etc/DIR_COLORS */
|
/* proper pass, reads all defined extensions within /etc/DIR_COLORS */
|
||||||
while ((tmp = getline(&line, &size, dircolors)) != -1) {
|
while ((tmp = getline(&line, &size, dircolors)) != -1) {
|
||||||
fg = 7;
|
fg = 7;
|
||||||
bg = 0;
|
bg = 0;
|
||||||
|
printf("%d\n",j);
|
||||||
if (line[0] == '.') {
|
if (line[0] == '.') {
|
||||||
extension = strtok(line, " ");
|
extension = strtok(line, " ");
|
||||||
colors[i].file_extension = malloc(sizeof(extension));
|
colors[i].file_extension = malloc(strlen(extension)+1);
|
||||||
strcpy(colors[i].file_extension, extension);
|
strcpy(colors[i].file_extension, extension);
|
||||||
|
|
||||||
colors[i].color_pair = i+11;
|
colors[i].color_pair = i+11;
|
||||||
@ -129,6 +117,7 @@ void colors_init() {
|
|||||||
i++;
|
i++;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
j++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
init_pair(0, COLOR_WHITE, COLOR_BLACK); /* unknown file */
|
init_pair(0, COLOR_WHITE, COLOR_BLACK); /* unknown file */
|
||||||
|
51
threading.c
51
threading.c
@ -22,6 +22,8 @@ file *mid_content;
|
|||||||
file *lft_content;
|
file *lft_content;
|
||||||
char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used for directory previews */
|
char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used for directory previews */
|
||||||
|
|
||||||
|
file file_current;
|
||||||
|
|
||||||
char *top_content; /* current path */
|
char *top_content; /* current path */
|
||||||
|
|
||||||
unsigned long rgt_file_count;
|
unsigned long rgt_file_count;
|
||||||
@ -70,6 +72,12 @@ void *thread_mid(void *data){
|
|||||||
selected_file_last = selected_file_current;
|
selected_file_last = selected_file_current;
|
||||||
pthread_mutex_unlock(&mutex_selection);
|
pthread_mutex_unlock(&mutex_selection);
|
||||||
|
|
||||||
|
file_current.file_name = malloc(mid_content[selected_file_current].file_name_width + 1);
|
||||||
|
strcpy(file_current.file_name, mid_content[selected_file_current].file_name);
|
||||||
|
file_current.file_name_width = mid_content[selected_file_current].file_name_width;
|
||||||
|
file_current.file_size_bytes = mid_content[selected_file_current].file_size_bytes;
|
||||||
|
file_current.file_type = mid_content[selected_file_current].file_type;
|
||||||
|
|
||||||
}
|
}
|
||||||
free(path);
|
free(path);
|
||||||
pthread_mutex_unlock(&mutex_mid);
|
pthread_mutex_unlock(&mutex_mid);
|
||||||
@ -77,6 +85,7 @@ void *thread_mid(void *data){
|
|||||||
}
|
}
|
||||||
void *thread_lft(void *data){
|
void *thread_lft(void *data){
|
||||||
pthread_mutex_lock(&mutex_lft);
|
pthread_mutex_lock(&mutex_lft);
|
||||||
|
/*{{{*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -102,6 +111,7 @@ void *thread_lft(void *data){
|
|||||||
}
|
}
|
||||||
free(path);
|
free(path);
|
||||||
pthread_mutex_unlock(&mutex_lft);
|
pthread_mutex_unlock(&mutex_lft);
|
||||||
|
/*}}}*/
|
||||||
pthread_exit(NULL);
|
pthread_exit(NULL);
|
||||||
|
|
||||||
|
|
||||||
@ -111,38 +121,34 @@ void *thread_rgt(void *data){
|
|||||||
|
|
||||||
pthread_mutex_lock(&mutex_mid);
|
pthread_mutex_lock(&mutex_mid);
|
||||||
/* TODO(2025-06-13T01:24:43) fix the occasional wrongly coppied path */
|
/* TODO(2025-06-13T01:24:43) fix the occasional wrongly coppied path */
|
||||||
char *path = malloc(mid_content[selected_file_current].file_name_width);
|
free(rgt_content);
|
||||||
strcpy(path, mid_content[selected_file_current].file_name);
|
rgt_content = malloc(sizeof(file));
|
||||||
unsigned long name_len = strlen(mid_content[selected_file_current].file_name);
|
rgt_content[0].file_name = malloc(file_current.file_name_width + 1);
|
||||||
unsigned long file_size = mid_content[selected_file_current].file_size_bytes;
|
strcpy(rgt_content[0].file_name, file_current.file_name);
|
||||||
unsigned char file_type = mid_content[selected_file_current].file_type;
|
rgt_content[0].file_name_width = file_current.file_name_width;
|
||||||
|
rgt_content[0].file_size_bytes = file_current.file_size_bytes;
|
||||||
|
rgt_content[0].file_type = file_current.file_type;
|
||||||
|
|
||||||
pthread_mutex_unlock(&mutex_mid);
|
pthread_mutex_unlock(&mutex_mid);
|
||||||
|
|
||||||
if (file_type == FILE_TYPE_DIR || file_type == FILE_TYPE_SYMLINK) {
|
if (rgt_content[0].file_type == FILE_TYPE_DIR || rgt_content[0].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);
|
free(rgt_content);
|
||||||
rgt_file_count = get_dir_size(path);
|
rgt_file_count = get_dir_size(path);
|
||||||
rgt_content = malloc(rgt_file_count * sizeof(file));
|
rgt_content = malloc(rgt_file_count * sizeof(file));
|
||||||
memset(rgt_content, ' ', rgt_file_count * sizeof(file));
|
memset(rgt_content, ' ', rgt_file_count * sizeof(file));
|
||||||
get_dir_content(path, &rgt_file_count, rgt_content);
|
get_dir_content(path, &rgt_file_count, rgt_content);
|
||||||
rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN;
|
rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN;
|
||||||
} else if (file_type == FILE_TYPE_REGULAR || file_type == FILE_TYPE_EXEC) {
|
free(path);
|
||||||
free(rgt_content);
|
} else if (rgt_content[0].file_type == FILE_TYPE_REGULAR) {
|
||||||
|
|
||||||
rgt_content = malloc(sizeof(file));
|
|
||||||
rgt_content[0].file_name = malloc(name_len);
|
|
||||||
rgt_content[0].file_name_width = name_len;
|
|
||||||
rgt_content[0].file_size_bytes = file_size;
|
|
||||||
/*memcpy(rgt_content[0].file_name, path, sizeof(path)/sizeof(char));*/
|
|
||||||
strcpy(rgt_content[0].file_name, path);
|
|
||||||
rgt_file_count = 1;
|
rgt_file_count = 1;
|
||||||
|
|
||||||
if (file_type != FILE_TYPE_EXEC) {
|
rgt_content[0].file_type = FILE_TYPE_OPEN_FILE;
|
||||||
rgt_content[0].file_type = FILE_TYPE_OPEN_FILE;
|
rgt_content[0].status = FILE_STATUS_HOVER;
|
||||||
rgt_content[0].status = FILE_STATUS_HOVER;
|
free(rgt_buffer);
|
||||||
free(rgt_buffer);
|
rgt_buffer = preview_file(rgt_content[0].file_name, rgt_content[0].file_size_bytes);
|
||||||
rgt_buffer = preview_file(rgt_content[0].file_name, file_size);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,6 +186,11 @@ void threading_init(){
|
|||||||
top_content = malloc(sizeof(char));
|
top_content = malloc(sizeof(char));
|
||||||
rgt_buffer = malloc(sizeof(char));
|
rgt_buffer = malloc(sizeof(char));
|
||||||
|
|
||||||
|
file_current.file_type = 0;
|
||||||
|
file_current.file_size_bytes = 1;
|
||||||
|
file_current.file_name_width = 1;
|
||||||
|
file_current.file_name = "a";
|
||||||
|
|
||||||
pthread_mutex_init(&mutex_top, NULL);
|
pthread_mutex_init(&mutex_top, NULL);
|
||||||
pthread_mutex_init(&mutex_mid, NULL);
|
pthread_mutex_init(&mutex_mid, NULL);
|
||||||
pthread_mutex_init(&mutex_lft, NULL);
|
pthread_mutex_init(&mutex_lft, NULL);
|
||||||
|
Reference in New Issue
Block a user