th/backend.c
2025-05-10 22:30:52 +02:00

128 lines
3.6 KiB
C

#define _POSIX_C_SOURCE 200809L
#include <curses.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pthread.h>
#include "defines.h"
#include "sorting.h"
extern unsigned int settings;
extern unsigned int file_modifiers;
extern unsigned int color_count;
extern color *colors;
unsigned long get_dir_size(char *path){
DIR *dir = opendir(path);
unsigned long entry_count = 0;
if (dir) {
struct dirent *entry;
while ((entry=readdir(dir))) {
if (entry->d_name[0] != '.' || (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
entry_count++;
}
}
}
closedir(dir);
return entry_count;
}
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){
struct dirent **entry;
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
scandir(path, &entry, NULL, alphasort);
} else {
scandir(path, &entry, skip_hidden_files, alphasort);
}
unsigned long i = 0;
for (i = 0; i < *dir_file_count; i++ ) {
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(sizeof(entry[i]->d_name));
dir_content[i].file_name = entry[i]->d_name;
struct stat *file;
file = malloc(sizeof(struct stat));
memset(file, ' ', sizeof(struct stat));
lstat(dir_content[i].file_name, file);
if (S_ISDIR(file->st_mode)) {
dir_content[i].file_type = COLOR_DIR;
dir_content[i].color_pair = COLOR_DIR;
} else if (S_ISBLK(file->st_mode)) {
dir_content[i].file_type = COLOR_BLOCK;
dir_content[i].color_pair = COLOR_BLOCK;
/*} else if (S_ISCHR(file->st_mode)) {
dir_content[i].file_type = COLOR_CHARDEV; */
} else if (S_ISLNK(file->st_mode)) {
dir_content[i].file_type = COLOR_SYMLINK;
dir_content[i].color_pair = COLOR_SYMLINK;
} else if (S_ISFIFO(file->st_mode)) {
dir_content[i].file_type = COLOR_FIFO;
dir_content[i].color_pair = COLOR_FIFO;
} else if (S_ISSOCK(file->st_mode)) {
dir_content[i].file_type = COLOR_SOCK;
dir_content[i].color_pair = COLOR_SOCK;
} else if (S_ISREG(file->st_mode)) {
dir_content[i].file_type = COLOR_REGULAR;
dir_content[i].color_pair = COLOR_REGULAR;
unsigned long j = 0;
char *extension = strrchr(entry[i]->d_name, '.');
if (extension) {
for (j = 0; j < color_count; j++) {
if (!strcmp(colors[j].file_extension, extension)) {
dir_content[i].color_pair = colors[j].color_pair;
}
}
} else {
}
} else {
dir_content[i].file_type = COLOR_REGULAR;
dir_content[i].color_pair = COLOR_REGULAR;
unsigned long j = 0;
char *extension = strrchr(entry[i]->d_name, '.');
if (extension) {
for (j = 0; j < color_count; j++) {
if (!strcmp(colors[j].file_extension, extension)) {
dir_content[i].color_pair = colors[j].color_pair;
}
}
} else {
}
}
free(file);
}
}
qsort(dir_content, *dir_file_count, sizeof(file), sort_natural);
for (i = 0; i < *dir_file_count; i++) {
free(entry[i]);
}
free(entry);
}
void print_dir(WINDOW *win, unsigned long *dir_file_count, file *dir_content){
unsigned long i = 0;
for (i = 0; i < *dir_file_count; i++) {
wattron(win, COLOR_PAIR(dir_content[i].color_pair));
if (dir_content[i].status & FILE_STATUS_HOVER) {
wattron(win, A_REVERSE);
}
mvwprintw(win, i, 0, "%d", dir_content[i].color_pair);
mvwaddstr(win, i, 3,dir_content[i].file_name);
if (dir_content[i].status & FILE_STATUS_HOVER) {
wattroff(win, A_REVERSE);
}
wattroff(win, COLOR_PAIR(dir_content[i].color_pair));
}
}