Compare commits

..

79 Commits

Author SHA1 Message Date
nova
47357e039d stability improvenments 2025-07-26 16:46:38 +02:00
nova
ecce6031a4 scrolling limit 2025-07-26 14:12:28 +02:00
nova
e5a2e429f0 added directory scrolling via offset_vertical 2025-07-25 18:04:26 +02:00
nova
1a67c5b4d0 removal of file_size_width 2025-07-25 17:10:44 +02:00
nova
af5a451ec2 size improvements of file struct 2025-07-23 19:49:57 +02:00
nova
a99a519834 general stability improvements 2025-07-23 19:47:37 +02:00
nova
6daeaebb5a added file extension commands 2025-07-18 23:01:49 +02:00
nova
d9ae5c79af improvenments to the rendered output, trunking of longer file names, no more dir sizes and indexes in lft and rgt 2025-07-15 02:45:45 +02:00
nova
2105bb4fe5 improvenments to delete function; need to handle filled dir still 2025-07-14 01:04:36 +02:00
nova
b9c254a0fe now runs with -O2 2025-07-13 21:53:59 +02:00
nova
4092446825 actual decent makefile whoa 2025-07-13 21:50:20 +02:00
nova
bd31ffd17d fixed segfault when quitting 2025-07-13 19:47:01 +02:00
nova
c9c00f4930 added unicode support 2025-07-12 15:00:34 +02:00
nova
d96046ac44 -O2 related refactoring 2025-07-09 01:09:45 +02:00
nova
b6f9633677 handling of empty dirs & refactoring 2025-07-08 20:51:25 +02:00
nova
7fcd148dfe fixed symlink handling 2025-07-08 01:57:58 +02:00
nova
4c958dc10a now parsing file sizes 2025-07-08 01:49:02 +02:00
nova
bc6fb162c5 accounting for files "." and ".." 2025-07-08 00:43:19 +02:00
nova
b77c9a2a29 made configs constant 2025-07-08 00:20:14 +02:00
nova
3f8fdc9e17 improvenments to file previews 2025-07-07 19:28:34 +02:00
nova
b2b100727f fixed some race conditions 2025-07-07 19:11:48 +02:00
nova
bc2989b2d9 added multiple sorting algorithms 2025-07-06 15:59:18 +02:00
nova
47470e4e64 extensionsto key_binding 2025-07-06 13:21:04 +02:00
nova
05ba7c82a0 added alhphasort 2025-07-06 07:04:19 +02:00
nova
322a1495c0 added blackmagic (funcptr/string modifiers on actions) 2025-07-06 07:03:45 +02:00
nova
88beeffafe fixed some compiler warnings 2025-07-06 05:10:14 +02:00
nova
5185e74441 fixed fucked up terminal upon exiting 2025-07-06 05:03:12 +02:00
nova
ab5b466336 handling of unimplemented bindings, added unimplemented bindings 2025-07-06 03:52:25 +02:00
nova
36fd160ccb pressing a key now prints all correlated bindings 2025-07-06 03:36:43 +02:00
nova
142f75d98a improvenments to rendering and start of thread_btm 2025-07-05 22:20:32 +02:00
nova
539af5fd65 creating new files and dirs possile, dedicated update screen key 2025-07-03 01:24:57 +02:00
nova
f320449572 propper input handling 2025-07-03 00:58:31 +02:00
nova
0a6509310d implemented delete and rename 2025-07-02 22:59:47 +02:00
nova
aedfdd1ed5 window sizing now possible in interaction functions 2025-06-26 23:52:04 +02:00
nova
f2eaeee9ea removed the need for a second input pass by making window global 2025-06-26 22:52:05 +02:00
nova
01fe19866c rename of content of struct file 2025-06-26 22:10:04 +02:00
nova
36ff54038e improvenments to threading 2025-06-24 00:18:53 +02:00
nova
1a64b83703 improvenments to the rendering 2025-06-24 00:18:18 +02:00
nova
c9051b2565 removed printf and unused var 2025-06-23 23:24:22 +02:00
nova
1a68fee59c added more default commands 2025-06-23 23:03:54 +02:00
nova
c29870998e improvenments to the string input loop 2025-06-22 02:06:36 +02:00
nova
3eb1c0f93e implemented rename_hovered, custom input string loop 2025-06-22 01:28:59 +02:00
nova
e07ec0b413 implemented open_with 2025-06-21 23:48:56 +02:00
nova
f99035629a opening files using mimetypes defined in config.h 2025-06-18 04:08:02 +02:00
nova
1485d69cad cleanup 2025-06-16 11:11:34 +02:00
nova
e7b45579b1 TODO(2025-06-13T01:26:16) done, untracked due to see previous commit 2025-06-16 11:04:38 +02:00
nova
b1eecad13c forgor to add in commit f42429de89 2025-06-16 11:02:10 +02:00
nova
797f9a7d3b fixed some memory leaks 2025-06-16 11:00:26 +02:00
nova
1f3a55df4b added release target 2025-06-16 10:59:02 +02:00
nova
c1f2f7602a updated config.h, see previous commit 2025-06-15 20:17:11 +02:00
nova
7c9af0d340 user input now handled through function pointers, now defined in config.h 2025-06-15 20:14:53 +02:00
nova
d2bde3a31c added valgrind in makefile 2025-06-15 16:54:05 +02:00
nova
188ecb7f57 finally added .gitignore 2025-06-15 16:48:41 +02:00
nova
dc67be11dc hovered line now fully highlighted 2025-06-15 16:39:55 +02:00
nova
f792399d44 changes to natural sort 2025-06-15 16:06:47 +02:00
nova
f42429de89 file preview infrastructure & implementation of preview of text files 2025-06-15 15:58:49 +02:00
nova
bc2bc8fce0 added config.h 2025-06-13 01:28:16 +02:00
nova
0053c7cb88 added file previev 2025-06-02 22:53:08 +02:00
nova
f7c1d34e05 fixed a bug where files/directory names would occasionally just dissapear 2025-05-31 10:21:25 +02:00
nova
3d3052e436 swapped up and down 2025-05-31 00:40:40 +02:00
nova
0574732a69 very basic and no good file hover implemented 2025-05-31 00:36:05 +02:00
nova
2b52329904 improvenment of path handling 2025-05-30 23:35:37 +02:00
nova
bab921dea2 improvenments to the ui 2025-05-14 02:19:35 +02:00
nova
7f0e65eaf3 improvenment to color managenment 2025-05-10 23:33:27 +02:00
nova
882e6fc85f added colors.h to repo 2025-05-10 22:39:14 +02:00
nova
6617b3fe4e added natural sort 2025-05-10 22:30:52 +02:00
nova
d4a4f5ebb2 parsing of /etc/DIR_COLORS 2025-05-10 21:51:09 +02:00
nova
5a7a4baf9b fixed a bug where trash data may appear 2025-05-08 21:16:03 +02:00
nova
6f7415a913 implementation of file type & extension based coloring 2025-05-08 21:15:14 +02:00
nova
37d5531aa7 once again switched to complex data types, basic file coloring implemented 2025-05-08 16:03:16 +02:00
nova
08913786de removed unused defines in defines.h 2025-04-17 12:02:32 +02:00
nova
38338df254 code base good enough to actually progress 2025-04-17 01:17:13 +02:00
nova
931d7026ea last commit before large rewrite 2025-03-30 15:58:05 +02:00
nova
2a1d273bc0 basic sorting implemented, segfaults in natural sort 2025-03-13 18:45:36 +01:00
nova
d6a827ba74 added more status 2025-03-13 15:37:31 +01:00
nova
3269ced286 init function 2025-03-13 15:36:45 +01:00
nova
6e06b56a23 corrected interactions.h, file_modifiers no longer missing 2025-03-13 14:30:46 +01:00
nova
02d469948e refractoring 2025-03-03 15:45:26 +01:00
nova
abf758cdbf refractoring 2025-03-03 15:44:38 +01:00
20 changed files with 2067 additions and 208 deletions

6
.gitignore vendored Normal file
View File

@@ -0,0 +1,6 @@
*
!.gitignore
!*.c
!*.h
!LICENSE
!Makefile

View File

@@ -1,5 +1,24 @@
CC := gcc
CFLAGS := -Wall -Wextra -O2 -flto=auto
CURSES := -lncursesw -ltinfow #utf8 support
#CURSES := -lncurses -tinfo #no utf8
CFLAGS_DEBUG := $(CFLAGS) -g
GDB := gdb --tui ./th
VALGRIND := valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all --log-fd=9 9>>valgrind.log ./th
HELGRIND := valgrind --tool=helgrind --log-fd=9 9>>helgrind.log ./th
all:
gcc ./main.c -std=c99 -o th -lncurses -ltinfo -Wall
$(CC) ./main.c -o th -std=c89 $(CFLAGS) $(CURSES)
d:
gcc -g -std=c99 ./main.c -o th -lncurses -ltinfo -Wall
$(CC) ./main.c -o th -std=c89 $(CFLAGS_DEBUG) $(CURSES)
$(GDB)
v:
$(CC) ./main.c -o th -std=c89 $(CFLAGS_DEBUG) $(CURSES)
$(VALGRIND)
h:
$(CC) ./main.c -o th -std=c89 $(CFLAGS_DEBUG) $(CURSES)
$(HELGRIND)

325
backend.c
View File

@@ -1,96 +1,279 @@
#define _POSIX_C_SOURCE 200809L
#include <curses.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <unistd.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 char **content_l;
extern char **content_m;
extern char **content_r;
extern char *path;
extern unsigned int file_modifiers;
extern unsigned int color_count;
extern unsigned int terminal_height;
extern volatile unsigned long selected_file_current;
extern color *colors;
int (*order_func)() = sort_natural;
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name){
char* concat(const char *s1, const char *s2){
const size_t len1 = strlen(s1);
const size_t len2 = strlen(s2);
char *result = malloc(len1 + len2 + 1);
memcpy(result, s1, len1);
memcpy(result + len1, s2, len2 + 1);
return result;
}
unsigned long get_dir_size(char *path){
DIR *dir = opendir(path);
*longest_name = 256; //magic number originates out of readdir(), unless i implement my own name size function, thisll do
unsigned long index = 1; //always makes the array at least 1 big, used for metadata like the amount of files
unsigned long entry_count = 0;
if (dir) {
struct dirent *entry;
while ((entry=readdir(dir))) {
if (entry->d_name[0] != '.' && !(settings & SETTINGS_HIDDEN_FILES)) {
index++;
} else if (settings & SETTINGS_HIDDEN_FILES){
index++;
}
}
}
*file_count = index;
closedir(dir);
}
void get_dir_content(char *path, unsigned long file_count, unsigned long longest_name, char **dir_content){
DIR *dir = opendir(path);
char content[file_count][longest_name];
memset(content,0,sizeof(content));
if (dir) {
int index = 1; //skip index 0 as it is used for metadata like file count
struct dirent *entry;
while ( (entry=readdir(dir)) ) {
if (entry->d_name[0] != '.' && !(settings & SETTINGS_HIDDEN_FILES)) {
strcpy(dir_content[index], entry->d_name);
index++;
} else if (settings & SETTINGS_HIDDEN_FILES) {
strcpy(dir_content[index], entry->d_name);
index++;
if (entry->d_name[0] != '.' || (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
entry_count++;
}
}
}
closedir(dir);
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
/* removes files "." and ".." */
entry_count -= 2;
}
return entry_count;
}
void print_dir(WINDOW *win, char **dir_content){
for (unsigned long i = 1; i < *dir_content[0]; i++ ){ //skip index 0 as it is used for metadata like file count
wprintw(win, "%s",dir_content[i]);
wmove(win, i, 1);
}
}
void *populate_dir(void *which){ // 0=left, 1=main, 2=right
char wh = (char)which;
unsigned long file_count = 0;
unsigned long longest_name = 0;
if (wh) {
if (wh == 1) {
free(content_m);
get_dir_size(path, &file_count, &longest_name);
content_m = calloc(file_count+1, sizeof(*content_m)); //index 0 is used for metadata like file count in dir
for (unsigned long i = 0; i<file_count+1; i++) {
content_m[i] = calloc(longest_name, sizeof(content_m[i]));
}
*content_m[0] = file_count;
get_dir_content(path, file_count, longest_name, content_m);
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, skip_dot, alphasort);
} else {
free(content_r);
get_dir_size(path, &file_count, &longest_name);
content_r = calloc(file_count+1, sizeof(*content_r));
for (unsigned long i = 0; i<file_count+1; i++) {
content_r[i] = calloc(longest_name, sizeof(content_r[i]));
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 = malloc(strlen(entry[i]->d_name)+1);
strcpy(dir_content[i].file_name, entry[i]->d_name);
dir_content[i].file_name[strlen(entry[i]->d_name)] = '\0';
struct stat *file;
file = malloc(sizeof(struct stat));
memset(file, ' ', sizeof(struct stat));
/* using the full path allows using the same function for all windows */
unsigned long path_len = strlen(path);
char *full_path = malloc(strlen(path) + strlen(entry[i]->d_name) + 1 + sizeof("/"));
memcpy(full_path, path, strlen(path));
memcpy(full_path + path_len, "/", sizeof("/"));
memcpy(full_path + path_len + sizeof("/") - 1, entry[i]->d_name, strlen(entry[i]->d_name) + 1);
lstat(full_path, file);
dir_content[i].file_size = file->st_size;
dir_content[i].permissions = 1;
dir_content[i].permissions = file->st_mode;
if (S_ISDIR(file->st_mode)) {
dir_content[i].file_type = FILE_TYPE_DIR;
dir_content[i].color_pair = COLOR_DIR;
dir_content[i].file_size = get_dir_size(full_path);
} else if (S_ISLNK(file->st_mode)) {
dir_content[i].file_type = FILE_TYPE_SYMLINK;
dir_content[i].color_pair = COLOR_SYMLINK;
dir_content[i].file_size = get_dir_size(full_path);
} else if (file->st_mode & S_IXUSR) {
dir_content[i].file_type = FILE_TYPE_EXEC;
dir_content[i].color_pair = COLOR_EXEC;
} else if (S_ISBLK(file->st_mode)) {
dir_content[i].file_type = FILE_TYPE_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_ISFIFO(file->st_mode)) {
dir_content[i].file_type = FILE_TYPE_FIFO;
dir_content[i].color_pair = COLOR_FIFO;
} else if (S_ISSOCK(file->st_mode)) {
dir_content[i].file_type = FILE_TYPE_SOCK;
dir_content[i].color_pair = COLOR_SOCK;
} else if (S_ISREG(file->st_mode)) {
dir_content[i].file_type = FILE_TYPE_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;
}
*content_r[0] = file_count;
get_dir_content(path, file_count, longest_name, content_r);
}
} else {
free(content_l);
get_dir_size(path, &file_count, &longest_name);
content_l = calloc(file_count+1, sizeof(*content_l));
for (unsigned long i = 0; i<file_count+1; i++) {
content_l[i] = calloc(longest_name, sizeof(content_l));
}
*content_l[0] = file_count;
get_dir_content(path, file_count, longest_name, content_l);
} 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(full_path);
free(file);
}
}
return NULL;
qsort(dir_content, *dir_file_count, sizeof(file), order_func);
for (i = 0; i < *dir_file_count; i++) {
free(entry[i]);
}
free(entry);
}
void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content){
/* i am not proud of this function */
unsigned long line_width = getmaxx(win);
char *bg = malloc(line_width+1);
memset(bg, ' ', line_width);
bg[line_width] = '\0';
unsigned long i = 0;
float file_size;
float printed_size = 0;
char size_char = ' ';
char is_selected = 0;
static const char sizes[6] = { 'B', 'K', 'M', 'G', 'T', 'P' };
unsigned long offset_vertical = 0;
unsigned long offset_back = 0;
unsigned long offset_front = 2;
if (print_info) {
if (*dir_file_count > 9) {
offset_front = (snprintf(NULL, 0, "%ld", *dir_file_count)) + 1;
}
if (selected_file_current > (terminal_height/3)*2) {
if (selected_file_current + (terminal_height/3) >= *dir_file_count) {
offset_vertical = *dir_file_count - terminal_height+2;
} else {
offset_vertical = selected_file_current - (terminal_height/3)*2;
}
}
}
for (i = offset_vertical; i < *dir_file_count && i < (terminal_height + offset_vertical); i++) {
if (print_info) {
file_size = dir_content[i].file_size;
char size_index = 0;
while (file_size > 1) {
printed_size=file_size;
file_size /= 1024;
size_index++;
if (size_index >= 6) {
break;
}
}
size_char = sizes[size_index-1];
if (dir_content[i].file_type == FILE_TYPE_DIR || dir_content[i].file_type == FILE_TYPE_SYMLINK) {
offset_back = line_width - (snprintf(NULL,0,"%ld", dir_content[i].file_size) + 1);
} else if (size_char =='B') {
offset_back = line_width - (snprintf(NULL,0,"%0.0lf %c", printed_size, size_char) + 1);
} else {
offset_back = line_width - (snprintf(NULL,0,"%0.2lf %c", printed_size, size_char) + 1);
}
}
if (dir_content[i].status & FILE_STATUS_SELECTED) {
is_selected = 1;
} else {
is_selected = 0;
}
if (dir_content[i].status & FILE_STATUS_SELECTED) {
wattron(win, COLOR_PAIR(8));
} else {
wattron(win, COLOR_PAIR(dir_content[i].color_pair));
}
if (dir_content[i].status & FILE_STATUS_HOVER) {
wattron(win, A_REVERSE);
}
/* shortens the printed file name if it is too long
* example input: aaaaaaaa.txt
* example output: aaa~.txt
* if no extension is found, the name will truncate */
char *file_name;
unsigned long file_name_width = strlen(dir_content[i].file_name);
if ((file_name_width + offset_front + is_selected) > offset_back - 1) {
char *extension = strrchr(dir_content[i].file_name, '.');
if (extension) {
int char_offset = (file_name_width + offset_front + is_selected) - (offset_back - 1) ;
if ((file_name_width - char_offset - strlen(extension) - 1) > 1) {
file_name = malloc(file_name_width - char_offset + 1);
memcpy(file_name, dir_content[i].file_name, file_name_width - char_offset);
memcpy(file_name + (file_name_width - char_offset - strlen(extension)), extension, strlen(extension));
file_name[file_name_width - char_offset - strlen(extension) - 1] = '~';
file_name[file_name_width - char_offset] = '\0';
} else {
file_name = malloc(strlen(extension)+1);
file_name[0] = '~';
memcpy(file_name+1, extension, strlen(extension));
file_name[strlen(extension)] = '\0';
}
} else {
file_name = malloc(file_name_width+1);
memcpy(file_name, dir_content[i].file_name, file_name_width);
file_name[file_name_width] = '\0';
}
} else {
file_name = malloc(file_name_width+1);
memcpy(file_name, dir_content[i].file_name, file_name_width);
file_name[file_name_width] = '\0';
}
mvwaddstr(win, i-offset_vertical, 0, bg);
mvwaddnstr(win, i-offset_vertical, offset_front+is_selected, file_name, line_width-offset_front-is_selected-2);
if(print_info) {
mvwprintw(win, i-offset_vertical, 0, "%ld", i);
free(file_name);
if (dir_content[i].file_type == FILE_TYPE_DIR || dir_content[i].file_type == FILE_TYPE_SYMLINK) {
mvwprintw(win, i-offset_vertical, offset_back, "%ld", dir_content[i].file_size);
}else if (size_char =='B') {
mvwprintw(win, i-offset_vertical, offset_back, "%0.0lf %c", printed_size, size_char);
} else {
mvwprintw(win, i-offset_vertical, offset_back, "%0.2lf %c", printed_size, size_char);
}
} else {
mvwaddnstr(win, i-offset_vertical, 0, file_name, line_width);
free(file_name);
}
if (dir_content[i].status & FILE_STATUS_HOVER) {
wattroff(win, A_REVERSE);
}
if (dir_content[i].status & FILE_STATUS_SELECTED) {
wattroff(win, COLOR_PAIR(8));
} else {
wattroff(win, COLOR_PAIR(dir_content[i].color_pair));
}
}
free(bg);
}

View File

@@ -2,7 +2,6 @@
#include "backend.c"
void get_dir_size(char *path, unsigned long *file_count, unsigned long *longest_name);
void get_dir_content(char *path, unsigned long file_count, unsigned long longest_name, char **dir_content);
void print_dir(WINDOW *win, char **dir_content);
void *populate_dir(void *dir);
unsigned long get_dir_size(char *path);
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content);

132
colors.c Normal file
View File

@@ -0,0 +1,132 @@
#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"
unsigned int color_count;
color *colors;
extern file *rgt_content;
extern unsigned long rgt_file_count;
extern unsigned int settings;
extern unsigned int status;
void parse_colors(char *line, short *fg, short *bg){
int tmp;
/* cuts everything before the space, then uses ';' as the token delimeter */
char *token = strtok(strrchr(line, ' '), ";");
while (token != NULL) {
sscanf(token, "%d", &tmp);
/* magic numbers are ansi color codes */
if ( tmp > 29 && tmp < 38) {
*fg = tmp - 30;
} else if ( tmp > 39 && tmp < 48) {
*bg = tmp - 40;
}
token = strtok(NULL, ";");
}
}
void colors_init() {
if (has_colors()) {
settings |= SETTINGS_HAS_COLOR;
start_color();
color_count = 0;
init_pair(0, COLOR_WHITE, COLOR_BLACK); /* unknown file */
if (status & STATUS_USER_ROOT) {
init_pair(10, COLOR_RED, COLOR_BLACK); /* path */
} else {
init_pair(10, COLOR_GREEN, COLOR_BLACK); /* path */
}
FILE *dircolors = fopen("/etc/DIR_COLORS", "r");
if (dircolors) {
char *line = NULL;
char *token;
char *extension;
size_t size = 0;
short fg;
short bg;
while (getline(&line, &size, dircolors) != -1) {
fg = 7;
bg = 0;
token = strtok(line, " ");
if (token[0] != '#' && token[0] != '\n') {
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, "LINK")){
parse_colors(line, &fg, &bg);
init_pair(2, fg, bg); /* symlink */
} else if (!strcmp(token, "EXEC")){
parse_colors(line, &fg, &bg);
init_pair(3, fg, bg); /* exec */
} else if (!strcmp(token, "RESET")){
parse_colors(line, &fg, &bg);
init_pair(4, fg, bg); /* regular file */
} else if (!strcmp(token, "BLK")){
parse_colors(line, &fg, &bg);
init_pair(5, fg, bg); /* block device */
} else if (!strcmp(token, "CHR")){
parse_colors(line, &fg, &bg);
init_pair(6, fg, bg); /* character device */
} else if (!strcmp(token, "SOCK")){
parse_colors(line, &fg, &bg);
init_pair(7, fg, bg); /* socket */
} else if (!strcmp(token, "FIFO")){
parse_colors(line, &fg, &bg);
init_pair(8, fg, bg); /* fifo */
} else if (!strcmp(token, "ORPHAN")){
parse_colors(line, &fg, &bg);
init_pair(9, fg, bg); /* orphan */
}
}
}
rewind(dircolors);
/*is it a leak when its intentional?*/
colors = malloc(sizeof(color) * color_count);
unsigned int i = 0;
/* proper pass, reads all defined extensions within /etc/DIR_COLORS */
while (getline(&line, &size, dircolors) != -1) {
fg = 7;
bg = 0;
if (line[0] == '.') {
extension = strtok(line, " ");
colors[i].file_extension = malloc(strlen(extension)+1);
strcpy(colors[i].file_extension, extension);
colors[i].color_pair = i+11;
parse_colors(line, &fg, &bg);
init_pair(i+11, fg, bg);
i++;
}
}
} else {
init_pair(0, COLOR_WHITE, COLOR_BLACK); /* unknown file */
init_pair(1, COLOR_BLUE, COLOR_BLACK); /* directory */
init_pair(2, COLOR_WHITE, COLOR_BLACK); /* regular file */
init_pair(3, COLOR_YELLOW, COLOR_BLACK); /* symlink */
init_pair(4, COLOR_CYAN, COLOR_BLACK); /* block device */
init_pair(5, COLOR_YELLOW, COLOR_BLACK); /* character device */
init_pair(6, COLOR_MAGENTA, COLOR_BLACK); /* fifo */
init_pair(7, COLOR_YELLOW, COLOR_BLACK); /* socket */
init_pair(8, COLOR_BLACK, COLOR_BLUE); /* reserved */
}
}
}

3
colors.h Normal file
View File

@@ -0,0 +1,3 @@
#include "colors.c"
void colors_init();

81
config.h Normal file
View File

@@ -0,0 +1,81 @@
#include "defines.h"
#include "interactions.h"
#include "sorting.h"
static const mimetype mimetype_default_cmd[] = {
/* mimetype shell command
* ^ substring of "file --mime-type -b ./hovered"
* this does mean that this list is checked for completely linear.
* Example:
* file --mime-type -b ./image.gif
* gives us "image/gif", thusly if we want to open gif in mpv rather than feh,
* we need to define "gif" before "image" */
{ "text", "$EDITOR" },
{ "gif", "mpv --loop-file=\"inf\"" },
{ "image", "feh" },
{ "video", "mpv" },
{ "audio", "mpv" }
};
static const extension file_extension_default_cmd[] = {
/* extension shell command
* similar to mimetype_default_cmd, however it searches for exact string matches
* and is checked before mimetype_default_cmd */
{ ".exe", "wine"},
};
static const binding key_binding[] = {
/*key action blackmagic comment*/
/*you cannot add bindings that include other bindings in its entirety
* possible: mk, mf
* not possible: gg, ggg
* trying to use ggg will always fail as it will execute gg first instead, resetting the input buffer, thus never reaching ggg */
/* blackmagic holds a modifier of an action, either as string or as function pointer depending on the action */
{ "q", quit_program, NULL, "quit" },
{ " ", toggle_selection, NULL, "toggle file selection" }, /* on hovered file/directory */
{ "e", update, NULL, "rerun all backend" }, /* executes the entire backend and redrawing of the screen */
{ "/", not_implemented, NULL, "search" },
{ "h", move_left, NULL, "move left" }, /* moves one dir up */
{ "t", move_down, NULL, "move down" },
{ "n", move_up, NULL, "move up" },
{ "s", move_right, NULL, "move right" }, /* if a dir is hovered, cd into it, if a file is selected, see mimetype_default_cmd */
{ "\n", open_with, NULL, "open \"open with\" dialog" }, /* opens the hovered file with an arbitrary command */
{ "r", rename_hovered, NULL, "rename hovered file" }, /* renames currently hovered file/directory */
{ "d", delete, NULL, "delete file" }, /* deletes currently hovered OR selected file/directory
* this means that it does not delete the hovered files if files are already selected */
{ "G", jump_bottom, NULL, "jump to last file in dir" },
{ "gg", jump_top, NULL, "jump to first file in dir" },
{ "gh", jump_to_dir, "$HOME", "jump to $HOME" },
{ "gs", jump_to_dir, "$START_PATH", "jump to $START_PATH" },
{ "gd", jump_to_dir, "/dev", "jump to /dev" },
{ "ge", jump_to_dir, "/etc", "jump to /etc" },
{ "gm", jump_to_dir, "/mnt", "jump to /mnt" },
{ "go", jump_to_dir, "/opt", "jump to /opt" },
{ "gt", jump_to_dir, "/tmp", "jump to /tmp" },
{ "gv", jump_to_dir, "/var", "jump to /var" },
{ "u7", cmd_on_selected, "7z x", "unzip 7z" },
{ "ub", cmd_on_selected, "tar -xvf", "unzip bz2" },
{ "ur", cmd_on_selected, "unrar x", "unzip rar" },
{ "ut", cmd_on_selected, "tar -xvf", "unzip tar" },
{ "ut", cmd_on_selected, "gzip -d", "unzip gzip" },
{ "uz", cmd_on_selected, "unzip ", "unzip zip" },
{ "on", order_by, sort_natural, "order natural" },
{ "or", not_implemented, "", "order reverse" },
{ "oe", not_implemented, "", "order extension" },
{ "os", order_by, sort_size, "order size" },
{ "ot", order_by, sort_type, "order type" },
{ "oz", order_by, sort_random, "order random" },
{ "oa", order_by, sort_alpha, "order alphabetically" },
{ "mk", makedir, NULL, "create directory" },
{ "mf", makefile, NULL, "create file" },
{ "a", toggle_hidden_files, NULL, "toggle hidden files" },
};
static const unsigned long binding_count = sizeof(key_binding) / sizeof(binding);
static const unsigned long mimetype_default_count = sizeof(mimetype_default_cmd) / sizeof(mimetype);
static const unsigned long file_extension_default_count = sizeof(file_extension_default_cmd) / sizeof(extension);

85
defines.h Normal file
View File

@@ -0,0 +1,85 @@
#include <sys/types.h>
#define STATUS_QUIT_PROGRAM 1
#define STATUS_RUN_BACKEND 2
#define STATUS_RELOAD_DIRECTORY 4
#define STATUS_UPDATE_SCREEN_MASK 24 /* 11000 */
#define STATUS_UPDATE_SCREEN_0 8
#define STATUS_UPDATE_SCREEN_RESIZE 16
#define STATUS_UPDATE_SCREEN_RELOAD_FULL 32
#define STATUS_USER_ROOT 64
#define SETTINGS_HAS_COLOR 1
#define FILE_MODIFIERS_HIDDEN_FILES 1
#define FILE_MODIFIERS_SORT_BITMASK 126 /* 00000000000000000000000001111110*/
#define FILE_MODIFIERS_SORT_ALPHABETIC 2
#define FILE_MODIFIERS_SORT_TYPE 4
#define FILE_MODIFIERS_SORT_EXTENSION 8
#define FILE_MODIFIERS_SORT_SIZE 16
#define FILE_MODIFIERS_SORT_RANDOM 32
#define FILE_MODIFIERS_SORT_REVERSE 64
/*FILE_MODIFIERS_SORT_NATURAL is when bitmask is 0*/
#define FILE_STATUS_HOVER 1
#define FILE_STATUS_SELECTED 2
#define FILE_STATUS_IS_REGULAR_FILE 4
#define FILE_STATUS_DIR_EMPTY 64 /* if a directory is empty */
#define FILE_STATUS_FILE_OPEN 128 /* only used for file previews */
#define COLOR_UNKNOWN 0
#define COLOR_DIR 1
#define COLOR_SYMLINK 2
#define COLOR_EXEC 3 /* not really a filetype, moreso if it is executable */
#define COLOR_REGULAR 4
#define COLOR_BLOCK 5
#define COLOR_CHARDEV 6
#define COLOR_SOCK 7
#define COLOR_FIFO 8
#define COLOR_ORPHAN 9
#define COLOR_PATH 10
#define FILE_TYPE_UNKNOWN COLOR_UNKNOWN
#define FILE_TYPE_DIR COLOR_DIR
#define FILE_TYPE_EXEC COLOR_EXEC
#define FILE_TYPE_REGULAR COLOR_REGULAR
#define FILE_TYPE_SYMLINK COLOR_SYMLINK
#define FILE_TYPE_BLOCK COLOR_BLOCK
#define FILE_TYPE_CHARDEV COLOR_CHARDEV
#define FILE_TYPE_SOCK COLOR_SOCK
#define FILE_TYPE_FIFO COLOR_FIFO
#define FILE_TYPE_ORPHAN COLOR_ORPHAN
#define FILE_TYPE_OPEN_FILE 128 /* this is only used in rgt_content to print a file preview, not the dir */
#ifndef STRUCT_GUARD
#define STRUCT_GUARD
/* complex types are good actually */
typedef struct File {
char status;
unsigned char file_type;
unsigned short color_pair;
unsigned short permissions;
unsigned long file_size; /*if its a file, its in bytes, if its a dir, its the count of files within that dir */
char *file_name;
} file;
typedef struct Color {
char *file_extension;
short color_pair;
} color;
typedef struct Mimetype {
char *mimetype;
char *command;
} mimetype;
typedef struct Extension {
char *file_extension;
char *command;
} extension;
typedef struct Binding {
char* key;
void (*func)();
void* black_magic;
char* comment;
} binding;
#endif

75
file_previews.c Normal file
View File

@@ -0,0 +1,75 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defines.h"
char* text(char *path, unsigned long *file_size);
char* generic(char *path);
char* get_mimetype(char *path){
static char *cmd_str = "file --mime-type -b ./\"";
unsigned long cmd_len = strlen(cmd_str);
unsigned int path_len = strlen(path);
char *cmd = malloc((cmd_len + path_len) + 2);
memset(cmd, ' ', cmd_len + path_len);
memcpy(cmd, cmd_str, cmd_len);
memcpy(cmd + cmd_len, path, path_len);
cmd[cmd_len + path_len] = '\"';
cmd[cmd_len + path_len + 1] = '\0';
FILE *cmd_open = popen(cmd, "r");
char *line;
size_t size = 0;
if (getline(&line, &size, cmd_open) != -1){
pclose(cmd_open);
return line;
} else {
pclose(cmd_open);
return "unknown";
}
}
char* preview_file(file *file_current){
/* this calls "file" on path */
char *file_buffer;
char *mime = get_mimetype(file_current->file_name);
if (strstr(mime, "text")) {
file_buffer = text(file_current->file_name, &file_current->file_size);
} else {
file_buffer = generic(file_current->file_name);
}
free(mime);
return file_buffer;
}
char* text(char *path, unsigned long *file_size){
char *file_buffer = malloc(*file_size + 1);
FILE *fp = fopen(path, "r");
if (fread(file_buffer, *file_size, 1, fp) != 0) {
file_buffer[*file_size] = '\0';
return file_buffer;
} else {
return "failed reading file";
}
}
char* generic(char *path){
char *cmd = concat("file ./\"", path);
cmd = concat(cmd, "\"");
FILE *cmd_open = popen(cmd, "r");
char *line;
size_t size = 0;
if (getline(&line, &size, cmd_open) != -1) {
pclose(cmd_open);
return line;
} else {
pclose(cmd_open);
return "failed executing shell command \"file\"";
}
}

5
file_previews.h Normal file
View File

@@ -0,0 +1,5 @@
#include "file_previews.c"
#include "defines.h"
char* preview_file(file *file_current);
char* get_mimetype(char *path);

View File

@@ -1,17 +1,604 @@
#include <curses.h>
#include <pthread.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <signal.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include "defines.h"
#include "config.h"
void user_interactions(char *input, unsigned int *status, unsigned int *settings) {
if (*input == 'q') {
*status ^= STATUS_QUIT_PROGRAM;
} else if (*input == *"KEY_BACKSPACE") {
*settings ^= SETTINGS_HIDDEN_FILES;
} else if (*input == 'a') {
*settings ^= SETTINGS_HIDDEN_FILES;
extern volatile unsigned long selected_file_current;
extern volatile unsigned long selected_file_last;
extern unsigned int file_modifiers;
extern pthread_mutex_t mutex_selection;
extern pthread_mutex_t mutex_rgt;
extern pthread_mutex_t mutex_mid;
extern file *mid_content;
extern file *lft_content;
extern file *rgt_content;
extern volatile file *file_current;
extern unsigned int terminal_height;
extern unsigned int terminal_width;
extern WINDOW *win_b;
extern char *rgt_buffer;
extern char *btm_buffer;
extern unsigned long mid_file_count;
extern unsigned int status;
unsigned int timeout_time = 0;
extern char *input;
unsigned int input_pass;
int parsed_input_number;
extern char *start_path;
int read_string(WINDOW *win, int y, int x, char *str);
int strcmp_offset(char *in0, char *in1, char offset);
extern void render_pass();
extern int (*order_func)();
void FAIL(char *function, char *str){
noraw();
endwin();
curs_set(1);
echo();
printf("ERROR in function %s: %s", function, str);
kill(getpid(),9);
}
else {
void user_interactions() {
char ch;
unsigned long i;
unsigned long binding_matches = 0;
static char binding_pass = 0;
ch = getch();
if(ch != ERR) {
timeout(1); /* blocking timeout of getch() */
input[input_pass] = ch;
mvaddstr(terminal_height-1, (terminal_width/3)*2, input);
input_pass++;
if (ch == 27) { /* esc key */
memset(input, 0, 255);
input_pass = 0;
timeout(100); /* blocking timeout of getch() */
status |= STATUS_UPDATE_SCREEN_0;
}
binding_pass = 0;
status |= STATUS_UPDATE_SCREEN_0;
}
void (*func_ptr)(int, int);
int number_length = 0;
if (!binding_pass) {
parsed_input_number = 0;
while((*input >= '0') && (*input <= '9')) {
parsed_input_number = (parsed_input_number * 10) + (*input - '0');
input++;
number_length++;
mvaddch(terminal_height-5, 5, number_length+48);
}
input -= number_length;
binding_pass = 1;
char cmp_len = strlen(input);
if(strlen(input) < 1) {
cmp_len++;
}
for (i = 0; i < binding_count; i++) {
if (strcmp(input + number_length, key_binding[i].key) == 0) {
func_ptr = key_binding[i].func;
func_ptr(parsed_input_number, i);
memset(input, 0, 255);
input_pass = 0;
binding_pass = 0;
number_length = 0;
timeout(100); /* blocking timeout of getch() */
} else if (strncmp(input+number_length, key_binding[i].key, cmp_len) == 0) {
binding_matches++;
attron(A_UNDERLINE);
mvaddstr(terminal_height-binding_matches-2, 0, "input");
mvaddstr(terminal_height-binding_matches-2, sizeof("input"), "\t\t\t");
mvaddstr(terminal_height-binding_matches-2, sizeof("input") + sizeof("\t\t\t"), "command");
attroff(A_UNDERLINE);
mvaddstr(terminal_height-binding_matches-1, 0, key_binding[i].key);
mvaddstr(terminal_height-binding_matches-1, 0, key_binding[i].key);
mvaddstr(terminal_height-binding_matches-1, sizeof(key_binding[i].key), "\t");
mvaddstr(terminal_height-binding_matches-1, sizeof(key_binding[i].key) + sizeof("\t"), key_binding[i].comment);
}
}
}
}
int read_string(WINDOW *win, int y, int x, char *str){
curs_set(1);
timeout(-1); /* negative numbers block until enter is pressed */
unsigned int pass = 0;
char ch;
char err = 0;
wmove(win, y, x);
while(1) {
/*ch = mvwgetch(win, y, x + pass);*/
ch = wgetch(win);
if (ch == '\n') {
err = 0;
break;
} else if (ch == 27) { /* esc key */
err = 1;
break;
} else if (ch == 127) { /* backspace */
if (pass > 0) {
pass--;
mvwdelch(win, y, pass);
}
} else {
mvwaddch(win, y, x +pass, ch);
str[pass] = ch;
pass++;
}
}
str[pass] = '\0';
timeout(10);
curs_set(0);
return err;
}
int strcmp_offset(char *in0, char *in1, char offset){
int i = 0;
while (in0[i] != '\0' && in1[i] != '\0') {
if (in0[i+offset] != in1[i]) {
return 1;
}
i++;
in1++;
}
return 0;
}
void quit_program(){
status = STATUS_QUIT_PROGRAM;
}
void toggle_selection(){
pthread_mutex_lock(&mutex_selection);
pthread_mutex_lock(&mutex_mid);
mid_content[selected_file_current].status ^= FILE_STATUS_SELECTED;
file_current->status ^= FILE_STATUS_SELECTED;
status |= (STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_mid);
pthread_mutex_unlock(&mutex_selection);
}
void move_down(int passes){
pthread_mutex_lock(&mutex_selection);
if (passes == 0) {
passes++;
}
/*capping the maximum file is done inside thread_mid */
selected_file_current += passes;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection);
}
void move_up(int passes){
pthread_mutex_lock(&mutex_selection);
if (passes == 0) {
passes++;
}
int i;
for (i = 0; i < passes; i++) {
if (selected_file_current != 0) {
selected_file_current--;
}
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection);
}
void move_left(int passes){
if (passes == 0) {
passes++;
}
int i;
for (i = 0; i < passes; i++) {
if (chdir("..") != 0) {
/* TODO(2025-07-09T00:30:05) fix */
FAIL("move_left", "unhandled error of chdir");
}
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void move_right(){
if (file_current->file_type == FILE_TYPE_DIR || file_current->file_type == FILE_TYPE_SYMLINK) {
if (chdir(file_current->file_name) != 0) {
FAIL("move_right", "unhandled error of chdir");
}
} else {
unsigned long i = 0;
char match = 0;
char *mime = get_mimetype(file_current->file_name);
char *extension = strrchr(file_current->file_name, '.');
for (i = 0; i < file_extension_default_count; i++) {
if (strstr(extension, file_extension_default_cmd[i].file_extension)) {
char *cmd = concat(file_extension_default_cmd[i].command, " ./\"");
cmd = concat(cmd, file_current->file_name);
cmd = concat(cmd, "\"");
if (system(cmd) == -1) {
/*do nothing*/
}
match = 1;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
break;
}
}
if (match == 0) {
for (i = 0; i < mimetype_default_count; i++) {
if (strstr(mime, mimetype_default_cmd[i].mimetype)) {
char *cmd = concat(mimetype_default_cmd[i].command, " ./\"");
cmd = concat(cmd, file_current->file_name);
cmd = concat(cmd, "\"");
btm_buffer = malloc(strlen(cmd));
strcpy(btm_buffer, cmd-1);
if (system(cmd) == -1) {
/*do nothing*/
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
break;
}
}
}
free(mime);
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void toggle_hidden_files(){
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void jump_bottom(){
pthread_mutex_lock(&mutex_selection);
selected_file_current = 0 - 1;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection);
}
void jump_top(){
pthread_mutex_lock(&mutex_selection);
selected_file_current = 0;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection);
}
void open_with(){
btm_buffer = concat("open \"", file_current->file_name);
btm_buffer = concat(btm_buffer, "\" with:");
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
unsigned long local_height;
local_height = getmaxy(win_b);
/* TODO(2025-06-22T01:24:36) fix fixed buffer size */
char *str = malloc(255);
memset(str, ' ', 255);
int err = read_string(win_b, local_height - 1, 0 , str);
if (!err) {
char *cmd = concat(str, " ./\"");
cmd = concat(cmd, file_current->file_name);
cmd = concat(cmd, "\"");
if (system(cmd) == -1) {
FAIL("open_with", "creating subcommand failed unhandled");
}
free(btm_buffer);
btm_buffer = cmd;
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
free(str);
}
void rename_hovered(){
btm_buffer = concat("rename \"", file_current->file_name);
btm_buffer = concat(btm_buffer, "\" to:");
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
unsigned long local_height;
local_height = getmaxy(win_b);
/* TODO(2025-06-22T01:24:30) fix fixed buffer size */
char *str = malloc(255);
memset(str, ' ', 255);
int err = read_string(win_b, local_height - 1, 0, str);
if (!err) {
char *cmd = concat("mv ./\"", file_current->file_name);
cmd = concat(cmd, "\" ./\"");
cmd = concat(cmd, str);
cmd = concat(cmd, "\"");
if (system(cmd) == -1) {
FAIL("rename_hovered", "mv or creating subcommand failed");
};
btm_buffer = cmd;
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
free(str);
}
void delete(){
unsigned int i = 0;
unsigned int hits = 0;
char *file_str = " ";
for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
file_str = concat(file_str, "\"");
file_str = concat(file_str, mid_content[i].file_name);
file_str = concat(file_str, "\" ");
hits++;
}
}
if (hits) {
btm_buffer = concat("delete:", file_str);
} else {
btm_buffer = concat("delete: \"", file_current->file_name);
btm_buffer = concat(btm_buffer, "\"");
}
btm_buffer = concat(btm_buffer, "?");
btm_buffer = concat(btm_buffer, "\n\n");
btm_buffer = concat(btm_buffer, "(y/N)");
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
timeout(-1); /* negative numbers block until enter is pressed */
/* TODO(2025-06-22T01:24:30) fix fixed buffer size */
char ch = wgetch(win_b);
if (ch == 'y' || ch == 'Y') {
/* TODO(2025-06-30T02:27:06) IMPORTANT: this really fucks up when the file has a quotation mark in its name */
int error;
if (hits) {
int j = 2;
for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
error = remove(mid_content[i].file_name);
if (error != 0) {
mvaddstr(terminal_height-j, 0, "could not delete: " );
mvaddstr(terminal_height-j, strlen("could not delete: "), mid_content[i].file_name);
j++;
}
}
}
free(btm_buffer);
btm_buffer = concat("deleted: ", file_str);
} else {
free(btm_buffer);
error = remove(file_current->file_name);
if (error != 0) {
mvaddstr(terminal_height-2, 0, "could not delete: " );
mvaddstr(terminal_height-2, strlen("could not delete: "), mid_content[i].file_name);
btm_buffer = " ";
} else {
btm_buffer = concat("deleted: \"", file_current->file_name);
btm_buffer = concat(btm_buffer, "\"");
}
}
/*system(cmd);*/
} else {
free(btm_buffer);
btm_buffer = "cancled deletion";
}
timeout(10);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
if (hits) {
free(file_str);
}
}
void makedir(){
btm_buffer = "create dir: ";
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
unsigned long local_height;
local_height = getmaxy(win_b);
/* TODO(2025-07-03T01:19:55) fix fixed buffer size */
char *str = malloc(255);
memset(str, ' ', 255);
int err = read_string(win_b, local_height - 1, 0, str);
if (!err) {
btm_buffer = concat(btm_buffer, str);
mkdir(str, 755); /*magic number from default permissions as created by mkdir*/
}
free(str);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
}
void makefile(){
btm_buffer = "create file: ";
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
unsigned long local_height;
local_height = getmaxy(win_b);
/* TODO(2025-07-03T01:19:49) fix fixed buffer size */
char *str = malloc(255);
memset(str, ' ', 255);
int err = read_string(win_b, local_height - 1, 0, str);
if (!err) {
btm_buffer = concat(btm_buffer, str);
FILE *fp;
fp = fopen(str, "w");
fclose(fp);
}
free(str);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
}
void update(){
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
}
void not_implemented(int passes, int index){
mvaddstr(terminal_height-1, 0, key_binding[index].comment);
mvaddstr(terminal_height-1, strlen(key_binding[index].comment), "\t");
mvaddstr(terminal_height-1, strlen(key_binding[index].comment) + strlen("\t"), "is not yet implemented");
}
void jump_to_dir(int passes, int index){
if ((char*)key_binding[index].black_magic) {
if (chdir(getenv((char*)key_binding[index].black_magic+1)) != 0) {
FAIL("jump_to_dir", "jumping to black_magic in config.h failed");
}
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void order_by(int passes, int index){
order_func = key_binding[index].black_magic;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void cmd_on_selected(int passes, int index){
unsigned int i = 0;
unsigned int hits = 0;
char *file_str = " ";
for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
file_str = concat(file_str, "\"");
file_str = concat(file_str, mid_content[i].file_name);
file_str = concat(file_str, "\" ");
hits++;
}
}
if (hits) {
btm_buffer = concat(key_binding[index].black_magic, file_str);
} else {
btm_buffer = concat(key_binding[index].black_magic, "\"");
btm_buffer = concat(btm_buffer, file_current->file_name);
btm_buffer = concat(btm_buffer, "\"");
}
btm_buffer = concat(btm_buffer, "?");
btm_buffer = concat(btm_buffer, "\n\n");
btm_buffer = concat(btm_buffer, "(y/N)");
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
timeout(-1); /* negative numbers block until enter is pressed */
/* TODO(2025-07-06T07:22:49) fix fixed buffer size */
char ch = wgetch(win_b);
if (ch == 'y' || ch == 'Y') {
/* the second loop is used to add "./", wich is not being printed" */
char *cmd = malloc(sizeof(char));
/* TODO(2025-07-06T07:23:05) IMPORTANT: this really fucks up when the file has a quotation mark in its name */
if (hits) {
for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
free(cmd);
cmd = concat((char*)key_binding[index].black_magic, " \"");
cmd = concat(cmd, mid_content[i].file_name);
cmd = concat(cmd, "\"");
if (system(cmd) != 0) {
/*do nothing*/
}
}
}
free(btm_buffer);
btm_buffer = concat("completed: ", key_binding[index].black_magic);
} else {
free(btm_buffer);
free(cmd);
cmd = concat((char*)key_binding[index].black_magic, " \"");
cmd = concat(cmd, file_current->file_name);
cmd = concat(cmd, "\"");
if (system(cmd) != 0) {
/*do nothing*/
}
mvaddstr(10,10, cmd);
}
/*system(cmd);*/
free(cmd);
} else {
free(btm_buffer);
btm_buffer = "cancled deletion";
}
timeout(10);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
if (hits) {
free(file_str);
}
*status ^= STATUS_RUN_BACKEND;
}

View File

@@ -1,5 +1,28 @@
#include <curses.h>
#include <pthread.h>
#ifndef INTERACTIONS_GUARD
#define INTERACTIONS_GUARD
#include "interactions.c"
#endif
void user_interactions(char *input, unsigned int *status, unsigned int *settings);
void user_interactions();
void quit_program();
void toggle_selection();
void move_right();
void move_up(int passes);
void move_down(int passes);
void move_left(int passes);
void jump_bottom();
void jump_top();
void toggle_hidden_files();
void open_with();
void rename_hovered();
void delete();
void makedir();
void makefile();
void update();
void not_implemented(int passes, int index);
void jump_to_dir(int passes, int index);
void order_by(int passes, int index);
void cmd_on_selected(int passes, int index);

227
main.c
View File

@@ -1,80 +1,197 @@
#define _POSIX_C_SOURCE 200809L
#include <curses.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/sysinfo.h>
#include <locale.h>
#include "threading.h"
#include "window.h"
#include "interactions.h"
#include "defines.h"
#include "colors.h"
#include "interactions.h"
unsigned int terminal_height;
unsigned int terminal_width;
unsigned int temp_heigth = 0; //used for screen refresh
unsigned int temp_heigth = 0; /*used for screen refresh*/
unsigned int temp_width = 0;
unsigned int settings;
unsigned int status; //bit 0 = enable
char **content_l; //content of parent dir, used in left window
char **content_m; //content of current dir, used in main window
char **content_r; //content of child dir, used in right window
char *path = ".";
unsigned int file_modifiers;
unsigned int status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
char *start_path;
WINDOW *win_t;
WINDOW *win_b;
WINDOW *win_l;
WINDOW *win_m;
WINDOW *win_r;
char *input; /*used in user_interactions*/
char *terminal_width_empty_line; /* used in user_interactions */
void render_pass();
void init();
int main(){
initscr(); //start ncurses
timeout(50); //blocking timeout of getch()
keypad(stdscr, TRUE);
init();
getmaxyx(stdscr, terminal_height, terminal_width);
WINDOW *winl = newwin(terminal_height, terminal_width/3, terminal_height, (terminal_width/3));
WINDOW *winm = newwin(terminal_height, terminal_width/3, 0, 0);
WINDOW *winr = newwin(terminal_height, terminal_width/3, terminal_height, ((terminal_width/3)*2));
settings ^= SETTINGS_HIDDEN_FILES;
status ^= STATUS_RUN_BACKEND;
char input = 0;
WINDOW *wint = newwin(1, terminal_width, 0, 0);
WINDOW *winb = newwin(1, terminal_width, terminal_height-1, 0);
WINDOW *winl = newwin(terminal_height-2, terminal_width/8, 1, 0);
WINDOW *winm = newwin(terminal_height-2, terminal_width/3, 1, (terminal_width/8));
WINDOW *winr = newwin(terminal_height-2, terminal_width/3, 1, ((terminal_width/2)));
win_t = wint;
win_b = winb;
win_r = winr;
win_m = winm;
win_l = winl;
pthread_t thread_b;
pthread_t thread_t;
pthread_t thread_l;
pthread_t thread_m;
pthread_t thread_r;
char threading = 0;
terminal_width_empty_line = malloc(terminal_width);
content_l = calloc(1, sizeof(*content_l)); //allocation in order to allow a more streamlined backend
content_m = calloc(1, sizeof(*content_m));
content_r = calloc(1, sizeof(*content_r));
while(!(status & STATUS_QUIT_PROGRAM)){
getmaxyx(stdscr, temp_heigth, temp_width);
if (status & STATUS_RUN_BACKEND) {
pthread_t populate_l;
pthread_t populate_m;
pthread_t populate_r;
pthread_create(&populate_l, NULL, populate_dir, (void*)0); //parent_content slash win_l
pthread_create(&populate_m, NULL, populate_dir, (void*)1); //current_content slash win_m
pthread_create(&populate_r, NULL, populate_dir, (void*)2); //child_content slash win_r
pthread_join(populate_l, NULL);
pthread_join(populate_m, NULL);
pthread_join(populate_r, NULL);
}
getmaxyx(stdscr, terminal_height, terminal_width);
temp_heigth -= terminal_height;
temp_width -= terminal_width;
if (!temp_heigth || !temp_width || (status & STATUS_RUN_BACKEND)) { //updates screen
window_left(winl, 0, 0, content_l);
window_main(winm, 0, terminal_width/3, content_m);
window_right(winr, 0, (terminal_width/3)*2, content_r);
wmove(stdscr,0,0);
status ^= STATUS_RUN_BACKEND;
if (!(terminal_height == temp_heigth) || !(terminal_width == temp_width)) {
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_RESIZE);
temp_width = terminal_width;
temp_heigth = terminal_height;
}
if (status & STATUS_RUN_BACKEND && threading) {
pthread_cancel(thread_b);
pthread_cancel(thread_r);
pthread_cancel(thread_m);
pthread_cancel(thread_l);
pthread_cancel(thread_t);
}
if (threading) {
status &= ~(STATUS_RELOAD_DIRECTORY);
pthread_join(thread_t, NULL);
pthread_join(thread_l, NULL);
pthread_join(thread_m, NULL);
pthread_join(thread_b, NULL);
pthread_join(thread_r, NULL);
threading = 0;
}
if (status & STATUS_RUN_BACKEND) {
pthread_create(&thread_t, NULL, thread_top, &status); /*top bar*/
pthread_create(&thread_l, NULL, thread_lft, &status); /*parent_content slash win_l*/
pthread_create(&thread_m, NULL, thread_mid, &status); /*current_content slash win_m*/
pthread_create(&thread_b, NULL, thread_btm, &status); /*bottom bar*/
pthread_create(&thread_r, NULL, thread_rgt, &status); /*child_content slash win_r*/
status &= ~(STATUS_RUN_BACKEND);
status |= STATUS_UPDATE_SCREEN_0;
threading = 1;
}
user_interactions();
render_pass();
}
threading_free();
free(start_path);
if (threading) {
pthread_join(thread_l, NULL);
pthread_join(thread_r, NULL);
pthread_join(thread_m, NULL);
pthread_join(thread_t, NULL);
pthread_join(thread_b, NULL);
}
if ((input = getch())) {
user_interactions(&input, &status, &settings);
}
}
free(content_l);
free(content_m);
free(content_r);
delwin(win_l);
delwin(win_m);
delwin(win_r);
delwin(win_t);
delwin(win_b);
noraw();
endwin();
curs_set(1);
echo();
return 0;
}
void render_pass(){
if (status & STATUS_UPDATE_SCREEN_RESIZE) {
if (status & STATUS_UPDATE_SCREEN_RELOAD_FULL) {
clear();
status &= ~STATUS_UPDATE_SCREEN_RELOAD_FULL;
}
/*TODO: check if deallocation of window and reallocation is faster than this or not */
wclear(win_t);
wclear(win_b);
wclear(win_l);
wclear(win_m);
wclear(win_r);
wresize(win_t, 1, terminal_width);
wresize(win_l, terminal_height-2, terminal_width/8);
wresize(win_m, terminal_height-2, (terminal_width/2)-(terminal_width/8));
wresize(win_r, terminal_height-2, terminal_width/2);
wresize(win_b, 1, terminal_width);
mvwin(win_t, 0, 0);
mvwin(win_l, 1, 0);
mvwin(win_m, 1, (terminal_width/8));
mvwin(win_r, 1, ((terminal_width/2)));
mvwin(win_b, terminal_height-1, 0);
status |= STATUS_UPDATE_SCREEN_0;
}
if (status & STATUS_UPDATE_SCREEN_MASK) {
status &= ~(STATUS_UPDATE_SCREEN_MASK);
window_top(win_t);
window_lft(win_l);
window_mid(win_m);
window_rgt(win_r);
window_btm(win_b);
wrefresh(win_t);
wrefresh(win_l);
wrefresh(win_m);
wrefresh(win_r);
wrefresh(win_b);
}
}
/*this function exists for things done at startup (initialization, reading config, etc)*/
void init() {
setlocale(LC_ALL, "");
initscr(); /* start ncurses */
noecho(); /* hide keyboard input */
timeout(0); /* blocking timeout of getch() */
curs_set(0);
/*file_modifiers = (FILE_MODIFIERS_HIDDEN_FILES | FILE_MODIFIERS_SORT_BITMASK);*/
input = malloc(sizeof(char)*255); /* size of input buffer, out of bounds access will not be accounted for */
memset(input, 0, 255);
status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
if (getuid() == 0) {
status |= STATUS_USER_ROOT;
}
threading_init(); /* found in threading.c */
colors_init();
ESCDELAY = 10;
char *start_path = getcwd(NULL, 0);
setenv("START_PATH", start_path, 0);
free(start_path);
}

157
sorting.c Normal file
View File

@@ -0,0 +1,157 @@
#include <curses.h>
#include <dirent.h>
#include <strings.h>
#include <stdlib.h>
#include <string.h>
#include "defines.h"
extern unsigned int settings;
extern unsigned int file_modifiers;
int skip_hidden_files(const struct dirent *entry){
if (entry->d_name[0] == '.') {
return 0;
}
return 1;
}
int skip_dot(const struct dirent *entry){
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
return 0;
}
return 1;
}
int sort_natural(const void *file0, const void *file1){
unsigned char file_type0 = ((file*)file0)->file_type;
unsigned char file_type1 = ((file*)file1)->file_type;
char weight = 0;
if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) {
weight |= 1;
}
if (file_type1 == FILE_TYPE_DIR || file_type1 == FILE_TYPE_SYMLINK) {
weight |= 2;
}
if (weight == 0 || weight == 3) {
char *file_name0 = ((file*)file0)->file_name;
char *file_name1 = ((file*)file1)->file_name;
return strcasecmp(file_name0, file_name1);
} else {
if (file_type0 > file_type1) {
return 1;
} else if (file_type0 < file_type1) {
return -1;
} else {
char *file_name0 = ((file*)file0)->file_name;
char *file_name1 = ((file*)file1)->file_name;
return strcasecmp(file_name0, file_name1);
}
}
}
int sort_alpha(const void *file0, const void *file1){
char *file_name0 = ((file*)file0)->file_name;
char *file_name1 = ((file*)file1)->file_name;
return strcmp(file_name0, file_name1);
}
int sort_random(const void *file0, const void *file1){
unsigned char file_type0 = ((file*)file0)->file_type;
unsigned char file_type1 = ((file*)file1)->file_type;
static int seed = 0;
static int random = 0;
if (seed == 0) {
seed = rand();
}
if (random == 0) {
random = seed;
}
char weight = 0;
if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) {
weight |= 1;
}
if (file_type1 == FILE_TYPE_DIR || file_type1 == FILE_TYPE_SYMLINK) {
weight |= 2;
}
if (weight == 0 || weight == 3) {
random = random > 1;
if ((random & 2) == 2) {
return -1;
} else {
if (random & 1){
return 1;
} else {
return 0;
}
}
} else {
if (file_type0 > file_type1) {
return 1;
} else if (file_type0 < file_type1) {
return -1;
} else {
random = random > 1;
if ((random & 2) == 2) {
return -1;
} else {
if (random & 1){
return 1;
} else {
return 0;
}
}
}
}
return 0;
}
int sort_type(const void *file0, const void *file1){
unsigned char file_type0 = ((file*)file0)->file_type;
unsigned char file_type1 = ((file*)file1)->file_type;
char *file_name0 = ((file*)file0)->file_name;
char *file_name1 = ((file*)file1)->file_name;
if (file_type0 == file_type1) {
return strcasecmp(file_name0, file_name1);
} else if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) {
return -1;
} else if (file_type1 == FILE_TYPE_DIR || file_type1 == FILE_TYPE_SYMLINK) {
return 1;
} else {
if (file_type0 > file_type1) {
return -1;
} else if (file_type0 < file_type1) {
return 1;
} else {
return strcasecmp(file_name0, file_name1);
}
}
}
int sort_size(const void *file0, const void *file1){
unsigned char file_type0 = ((file*)file0)->file_type;
unsigned char file_type1 = ((file*)file1)->file_type;
unsigned long file_size0 = ((file*)file0)->file_size;
unsigned long file_size1 = ((file*)file1)->file_size;
char *file_name0 = ((file*)file0)->file_name;
char *file_name1 = ((file*)file1)->file_name;
if (file_type0 == file_type1) {
if (file_size0 > file_size1) {
return -1;
} else if (file_size0 < file_size1) {
return 1;
} else {
return strcasecmp(file_name0, file_name1);
}
} else {
if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) {
return -1;
} else if (file_type1 == FILE_TYPE_DIR || file_type1 == FILE_TYPE_SYMLINK) {
return 1;
} else {
if (file_size0 > file_size1) {
return -1;
} else if (file_size0 < file_size1) {
return 1;
} else {
return strcasecmp(file_name0, file_name1);
}
}
}
}

18
sorting.h Normal file
View File

@@ -0,0 +1,18 @@
#include <curses.h>
#include <dirent.h>
#include "defines.h"
#ifndef SORTING_GUARD
#define SORTING_GUARD
#include "sorting.c"
#endif
int skip_hidden_files(const struct dirent *entry);
int skip_dot(const struct dirent *entry);
void sort_dir(unsigned long *dir_length_width, char *dir_content);
void sort_dir(unsigned long *dir_length_width, char *dir_content);
int sort_natural(const void *file0, const void *file1);
int sort_alpha(const void *file0, const void *file1);
int sort_random(const void *file0, const void *file1);
int sort_type(const void *file0, const void *file1);
int sort_size(const void *file0, const void *file1);

BIN
th

Binary file not shown.

308
threading.c Normal file
View File

@@ -0,0 +1,308 @@
#include <curses.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include "defines.h"
#include "backend.h"
#include "file_previews.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;
pthread_mutex_t mutex_selection;
pthread_mutex_t mutex_wait;
pthread_cond_t cond_wait;
volatile char wait_count; /* this is used to determine how many threads are waiting for cont_wait */
file *rgt_content;
file *mid_content;
file *lft_content;
char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used for directory previews */
char *btm_buffer;
char *top_buffer; /* current path */
volatile file *file_current;
unsigned long rgt_file_count;
unsigned long mid_file_count;
unsigned long lft_file_count;
unsigned long top_width;
volatile unsigned long selected_file_current = 0;
volatile unsigned long selected_file_last = 0;
extern unsigned int terminal_width;
void *thread_mid(void *data){
unsigned int status = *(unsigned int*)data;
pthread_mutex_lock(&mutex_mid);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
mid_content = malloc(sizeof(file));
mid_content->file_name = "cannot open directory";
mid_file_count = 1;
} else {
if (status & STATUS_RELOAD_DIRECTORY) {
free(mid_content);
mid_file_count = get_dir_size(path);
if (mid_file_count > 0) {
mid_content = malloc(mid_file_count * sizeof(file));
memset(mid_content, '\0', mid_file_count * sizeof(file));
get_dir_content(path, &mid_file_count, mid_content);
} else {
selected_file_current = 0;
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 = "empty dir";
file_current->file_name = mid_content->file_name;
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);
}
}
pthread_mutex_lock(&mutex_selection);
if (selected_file_current >= mid_file_count) {
selected_file_current = mid_file_count-1;
selected_file_current = mid_file_count-1;
}
if (selected_file_current != selected_file_last) {
mid_content[selected_file_last].status &= ~FILE_STATUS_HOVER;
}
selected_file_last = selected_file_current;
free(file_current->file_name);
file_current->file_name = malloc(strlen(mid_content[selected_file_current].file_name));
strcpy(file_current->file_name, mid_content[selected_file_current].file_name);
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;
mid_content[selected_file_current].status |= FILE_STATUS_HOVER;
file_current->status = mid_content[selected_file_current].status;
pthread_mutex_unlock(&mutex_selection);
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);
}
void *thread_lft(void *data){
unsigned int status = *(unsigned int*)data;
pthread_mutex_lock(&mutex_lft);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
lft_content = malloc(sizeof(file));
lft_content[0].file_name = "cannot open directory";
lft_file_count = 1;
} else {
path[strrchr(path, '/')-path] = '\0';
path[0] = '/';
if (status & STATUS_RELOAD_DIRECTORY) {
lft_file_count = get_dir_size(path);
free(lft_content);
lft_content = malloc(lft_file_count * sizeof(file));
memset(lft_content, '\0', lft_file_count * sizeof(file));
get_dir_content(path, &lft_file_count, lft_content);
}
}
free(path);
pthread_mutex_unlock(&mutex_lft);
pthread_exit(0);
}
void *thread_rgt(void *data){
unsigned int status = *(unsigned int*)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_selection);
free(rgt_content);
rgt_content = malloc(sizeof(file));
rgt_content->file_name = malloc(strlen(file_current->file_name));
strcpy(rgt_content->file_name, file_current->file_name);
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_selection);
if (rgt_content->file_type == FILE_TYPE_DIR || rgt_content->file_type == FILE_TYPE_SYMLINK) {
char *path = malloc(strlen(rgt_content[0].file_name) + 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;
free(path);
free(rgt_buffer);
rgt_buffer = malloc(sizeof(char));
rgt_buffer[0] = '\0';
} else {
rgt_file_count = 0;
free(rgt_buffer);
if (rgt_content->status & FILE_STATUS_DIR_EMPTY) {
rgt_buffer = "empty dir";
} else {
rgt_content->file_type = FILE_TYPE_OPEN_FILE;
rgt_content->status = FILE_STATUS_HOVER;
rgt_buffer = preview_file(rgt_content);
}
}
pthread_mutex_unlock(&mutex_rgt);
pthread_exit(0);
}
void *thread_top(void *data){
unsigned int status = *(unsigned int*)data;
pthread_mutex_lock(&mutex_top);
free(top_buffer);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
top_buffer = malloc(sizeof("cannot open directory"));
top_width = sizeof("cannot open directory");
top_buffer = "cannot open directory";
} else {
top_buffer = getcwd(NULL, 0);
top_width = strlen(top_buffer);
}
free(path);
pthread_mutex_unlock(&mutex_top);
pthread_exit(0);
}
void *thread_btm(void *data){
unsigned int status = *(unsigned int*)data;
pthread_mutex_lock(&mutex_btm);
pthread_mutex_lock(&mutex_wait);
wait_count++;
pthread_cond_wait(&cond_wait, &mutex_wait);
wait_count--;
pthread_mutex_unlock(&mutex_wait);
free(btm_buffer);
int buffer_width = terminal_width;
btm_buffer = malloc(buffer_width);
memset(btm_buffer, 0, buffer_width);
btm_buffer[0] = (S_ISDIR(file_current->permissions)) ? 'd' : '-';
btm_buffer[1] = (file_current->permissions & S_IRUSR) ? 'r' : '-';
btm_buffer[2] = (file_current->permissions & S_IWUSR) ? 'w' : '-';
btm_buffer[3] = (file_current->permissions & S_IXUSR) ? 'x' : '-';
btm_buffer[4] = (file_current->permissions & S_IRGRP) ? 'r' : '-';
btm_buffer[5] = (file_current->permissions & S_IWGRP) ? 'w' : '-';
btm_buffer[6] = (file_current->permissions & S_IXGRP) ? 'x' : '-';
btm_buffer[7] = (file_current->permissions & S_IROTH) ? 'r' : '-';
btm_buffer[8] = (file_current->permissions & S_IWOTH) ? 'w' : '-';
btm_buffer[9] = (file_current->permissions & S_IXOTH) ? 'x' : '-';
pthread_mutex_unlock(&mutex_btm);
pthread_exit(0);
}
void threading_init(){
rgt_content = malloc(sizeof(file));
mid_content = malloc(sizeof(file));
lft_content = malloc(sizeof(file));
top_buffer = malloc(sizeof(char));
rgt_buffer = malloc(sizeof(char));
btm_buffer = malloc(sizeof(char));
memset(top_buffer, '\0', sizeof(char));
memset(rgt_buffer, '\0', sizeof(char));
memset(btm_buffer, '\0', sizeof(char));
mid_content->file_type = 0;
mid_content->file_size = 0;
mid_content->file_name = malloc(sizeof(char));
mid_content->file_name[0] = '\0';
rgt_content->file_type = 0;
rgt_content->file_size = 0;
rgt_content->file_name = malloc(sizeof(char));
rgt_content->file_name[0] = '\0';
file_current = malloc(sizeof(file));
file_current->file_type = 0;
file_current->file_size = 0;
file_current->file_name = malloc(sizeof(char));
file_current->file_name[0] = '\0';
volatile char vol; /* needed to make sure higher optimization steps dont move these around */
vol = pthread_mutex_init(&mutex_top, NULL);
vol = pthread_mutex_init(&mutex_mid, NULL);
vol = pthread_mutex_init(&mutex_lft, NULL);
vol = pthread_mutex_init(&mutex_btm, NULL);
vol = pthread_mutex_init(&mutex_rgt, NULL);
vol = pthread_mutex_init(&mutex_selection, NULL);
vol = pthread_mutex_init(&mutex_wait, NULL);
vol = pthread_cond_init(&cond_wait, NULL);
vol;
selected_file_current = 0;
selected_file_last = 0;
}
void threading_free(){
free(rgt_content);
free(mid_content);
free(lft_content);
free(top_buffer);
pthread_mutex_destroy(&mutex_top);
pthread_mutex_destroy(&mutex_mid);
pthread_mutex_destroy(&mutex_lft);
}

10
threading.h Normal file
View File

@@ -0,0 +1,10 @@
#include <curses.h>
#include "threading.c"
void *thread_lft(void *data);
void *thread_mid(void *data);
void *thread_rgt(void *data);
void *thread_top(void *data);
void *thread_btm(void *data);
void threading_init();
void threading_free();

175
window.c
View File

@@ -1,72 +1,123 @@
#include <curses.h>
#include <dirent.h>
#include "backend.h"
#include <pthread.h>
#include <unistd.h>
#include "defines.h"
extern unsigned int terminal_height;
extern unsigned int terminal_width;
extern unsigned int status;
extern char *input;
extern unsigned int timeout_time;
extern unsigned int color_count;
extern color *colors;
extern file *mid_content;
extern file *lft_content;
extern file *rgt_content;
extern char *top_buffer;
extern char *rgt_buffer;
extern char *btm_buffer;
extern unsigned long lft_file_count;
extern unsigned long mid_file_count;
extern unsigned long rgt_file_count;
extern unsigned long top_width;
extern pthread_mutex_t mutex_top;
extern pthread_mutex_t mutex_btm;
extern pthread_mutex_t mutex_lft;
extern pthread_mutex_t mutex_mid;
extern pthread_mutex_t mutex_rgt;
void window_top(WINDOW *win){
werase(win);
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content){
//WINDOW *win = (window_data)window_data.win;
unsigned int local_width;
unsigned int local_height;
//{{{ size & positioning
wresize(win, terminal_height, terminal_width/3);
getmaxyx(win, local_height, local_width);
mvwin(win, start_y, start_x);
wclear(win);
//}}}
wmove(win, 1, 1);
print_dir(win, dir_content);
box(win,0,0);
wrefresh(win);
if (pthread_mutex_trylock(&mutex_top) == 0) {
wattron(win, COLOR_PAIR(COLOR_PATH));
if (*top_buffer != ' ') { /*printing ' ' (standard initialized value, see threading_init) makes valgrind throw a fuss*/
mvwaddstr(win, 0, 0, top_buffer);
}
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content){
unsigned int local_width;
unsigned int local_height;
//{{{ size & positioning
wresize(win, terminal_height, terminal_width/3);
getmaxyx(win, local_height, local_width);
mvwin(win, start_y, start_x);
wclear(win);
//}}}
wmove(win, 1, 1);
print_dir(win, dir_content);
box(win,0,0);
wrefresh(win);
wattroff(win, COLOR_PAIR(COLOR_PATH));
pthread_mutex_unlock(&mutex_top);
} else {
wprintw(win,"loading");
status |= STATUS_UPDATE_SCREEN_0;
}
}
void window_btm(WINDOW *win){
werase(win);
unsigned long local_width;
unsigned long local_height;
getmaxyx(win, local_height, local_width);
if (pthread_mutex_trylock(&mutex_btm) == 0) {
if (*top_buffer != ' ') { /*printing ' ' (standard initialized value, see threading_init) makes valgrind throw a fuss*/
mvwprintw(win, 0, 0, "%s", btm_buffer);
}
pthread_mutex_unlock(&mutex_btm);
/*the printing of the input char is done in user_interactions*/
/*the printing of all possible inputs are done in user_interactions */
} else {
mvwprintw(win, local_height/2, local_width/2, "LOADING");
status |= STATUS_UPDATE_SCREEN_0;
}
}
void window_lft(WINDOW *win){
werase(win);
box(win, 0, 0);
unsigned long local_width;
unsigned long local_height;
getmaxyx(win, local_height, local_width);
if (pthread_mutex_trylock(&mutex_lft) == 0) {
print_dir(win, 0, &lft_file_count, lft_content);
pthread_mutex_unlock(&mutex_lft);
} else {
mvwprintw(win, local_height/2, local_width/2, "LOADING");
status |= STATUS_UPDATE_SCREEN_0;
}
}
void window_mid(WINDOW *win){
werase(win);
box(win, 0, 0);
unsigned long local_width;
unsigned long local_height;
getmaxyx(win, local_height, local_width);
if (pthread_mutex_trylock(&mutex_mid) == 0) {
if (mid_file_count == 0) {
mvwprintw(win, 0, 0, "empty");
} else {
print_dir(win, 1, &mid_file_count, mid_content);
}
pthread_mutex_unlock(&mutex_mid);
} else {
mvwprintw(win, local_height/2, local_width/2, "LOADING");
status |= STATUS_UPDATE_SCREEN_0;
}
}
void window_rgt(WINDOW *win){
werase(win);
box(win, 0, 0);
unsigned long local_width;
unsigned long local_height;
getmaxyx(win, local_height, local_width);
if (pthread_mutex_trylock(&mutex_rgt) == 0) {
if (rgt_file_count == 0) {
if (rgt_content[0].file_type == FILE_TYPE_OPEN_FILE) {
mvwprintw(win, 0, 0, "%s", rgt_buffer);
} else {
mvwprintw(win, 0, 0, "empty");
}
} else {
print_dir(win, 0, &rgt_file_count, rgt_content);
}
pthread_mutex_unlock(&mutex_rgt);
} else {
mvwprintw(win, local_height/2, local_width/2, "LOADING");
status |= STATUS_UPDATE_SCREEN_0;
}
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content){
wmove(win, 0, 0);
unsigned int local_width;
unsigned int local_height;
//{{{ size & positioning
wresize(win, terminal_height, terminal_width/3);
getmaxyx(win, local_height, local_width);
mvwin(win, start_y, start_x);
wclear(win);
//}}}
wmove(win, local_height/2, local_width/2);
wprintw(win, "%d,%d", local_height, local_width);
box(win,0,0);
wrefresh(win);
}

View File

@@ -1,7 +1,7 @@
#include <curses.h>
#include "window.c"
void window_left(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content);
void window_main(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content);
void window_right(WINDOW *win, unsigned int start_y, unsigned int start_x, char **dir_content);
void window_top(WINDOW *win);
void window_btm(WINDOW *win);
void window_lft(WINDOW *win);
void window_mid(WINDOW *win);
void window_rgt(WINDOW *win);