fixed some memory leaks

This commit is contained in:
nova
2025-06-16 11:00:26 +02:00
parent 1f3a55df4b
commit 797f9a7d3b
3 changed files with 42 additions and 42 deletions

View File

@ -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)) {
} else {
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);
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){
char *hover_bg = malloc(*line_width);
char *hover_bg = malloc(*line_width+1);
memset(hover_bg, ' ', *line_width);
hover_bg[*line_width] = '\0';
unsigned long i = 0;

View File

@ -1,6 +1,7 @@
#define _POSIX_C_SOURCE 200809L
#include <bits/types/FILE.h>
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <string.h>
#include "defines.h"
@ -57,70 +58,57 @@ void colors_init() {
short fg;
short bg;
char supported_filetype_count = 9;
char initial_pass = 0;
while ((tmp = getline(&line, &size, dircolors)) != -1 && initial_pass != supported_filetype_count) {
while ((tmp = getline(&line, &size, dircolors)) != -1) {
fg = 7;
bg = 0;
token = strtok(line, " ");
if (token[0] != '#' && token[0] != '\n') {
if (!strcmp(token, "DIR")) {
initial_pass++;
if (line[0] == '.') {
color_count++;
} else if (!strcmp(token, "DIR")) {
parse_colors(line, &fg, &bg);
init_pair(1, fg, bg); /* directory */
} else if (!strcmp(token, "EXEC")){
initial_pass++;
parse_colors(line, &fg, &bg);
init_pair(2, fg, bg); /* exec */
} else if (!strcmp(token, "RESET")){
initial_pass++;
parse_colors(line, &fg, &bg);
init_pair(3, fg, bg); /* regular file */
} else if (!strcmp(token, "LINK")){
initial_pass++;
parse_colors(line, &fg, &bg);
init_pair(4, fg, bg); /* symlink */
} else if (!strcmp(token, "BLK")){
initial_pass++;
parse_colors(line, &fg, &bg);
init_pair(5, fg, bg); /* block device */
} else if (!strcmp(token, "CHR")){
initial_pass++;
parse_colors(line, &fg, &bg);
init_pair(6, fg, bg); /* character device */
} else if (!strcmp(token, "SOCK")){
initial_pass++;
parse_colors(line, &fg, &bg);
init_pair(7, fg, bg); /* socket */
} else if (!strcmp(token, "FIFO")){
initial_pass++;
parse_colors(line, &fg, &bg);
init_pair(8, fg, bg); /* fifo */
} else if (!strcmp(token, "ORPHAN")){
initial_pass++;
parse_colors(line, &fg, &bg);
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);
colors = malloc(sizeof(color) * color_count);
unsigned int i = 0;
unsigned int j = 0;
/* proper pass, reads all defined extensions within /etc/DIR_COLORS */
while ((tmp = getline(&line, &size, dircolors)) != -1) {
fg = 7;
bg = 0;
printf("%d\n",j);
if (line[0] == '.') {
extension = strtok(line, " ");
colors[i].file_extension = malloc(sizeof(extension));
colors[i].file_extension = malloc(strlen(extension)+1);
strcpy(colors[i].file_extension, extension);
colors[i].color_pair = i+11;
@ -129,6 +117,7 @@ void colors_init() {
i++;
}
j++;
}
} else {
init_pair(0, COLOR_WHITE, COLOR_BLACK); /* unknown file */

View File

@ -22,6 +22,8 @@ file *mid_content;
file *lft_content;
char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used for directory previews */
file file_current;
char *top_content; /* current path */
unsigned long rgt_file_count;
@ -70,6 +72,12 @@ void *thread_mid(void *data){
selected_file_last = selected_file_current;
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);
pthread_mutex_unlock(&mutex_mid);
@ -77,6 +85,7 @@ void *thread_mid(void *data){
}
void *thread_lft(void *data){
pthread_mutex_lock(&mutex_lft);
/*{{{*/
@ -102,6 +111,7 @@ void *thread_lft(void *data){
}
free(path);
pthread_mutex_unlock(&mutex_lft);
/*}}}*/
pthread_exit(NULL);
@ -111,38 +121,34 @@ void *thread_rgt(void *data){
pthread_mutex_lock(&mutex_mid);
/* TODO(2025-06-13T01:24:43) fix the occasional wrongly coppied path */
char *path = malloc(mid_content[selected_file_current].file_name_width);
strcpy(path, mid_content[selected_file_current].file_name);
unsigned long name_len = strlen(mid_content[selected_file_current].file_name);
unsigned long file_size = mid_content[selected_file_current].file_size_bytes;
unsigned char file_type = mid_content[selected_file_current].file_type;
free(rgt_content);
rgt_content = malloc(sizeof(file));
rgt_content[0].file_name = malloc(file_current.file_name_width + 1);
strcpy(rgt_content[0].file_name, file_current.file_name);
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);
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);
rgt_file_count = get_dir_size(path);
rgt_content = malloc(rgt_file_count * sizeof(file));
memset(rgt_content, ' ', rgt_file_count * sizeof(file));
get_dir_content(path, &rgt_file_count, rgt_content);
rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN;
} else if (file_type == FILE_TYPE_REGULAR || file_type == FILE_TYPE_EXEC) {
free(rgt_content);
free(path);
} 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;
if (file_type != FILE_TYPE_EXEC) {
rgt_content[0].file_type = FILE_TYPE_OPEN_FILE;
rgt_content[0].status = FILE_STATUS_HOVER;
free(rgt_buffer);
rgt_buffer = preview_file(rgt_content[0].file_name, file_size);
}
rgt_content[0].file_type = FILE_TYPE_OPEN_FILE;
rgt_content[0].status = FILE_STATUS_HOVER;
free(rgt_buffer);
rgt_buffer = preview_file(rgt_content[0].file_name, rgt_content[0].file_size_bytes);
}
@ -180,6 +186,11 @@ void threading_init(){
top_content = 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_mid, NULL);
pthread_mutex_init(&mutex_lft, NULL);