Compare commits

..

1 Commits

Author SHA1 Message Date
nova 72189ba5c7 Initial commit 2025-03-03 11:52:14 +01:00
22 changed files with 0 additions and 2902 deletions
-6
View File
@@ -1,6 +0,0 @@
*
!.gitignore
!*.c
!*.h
!LICENSE
!Makefile
-27
View File
@@ -1,27 +0,0 @@
CC := gcc
CFLAGS := -Wall -Wextra -O3 -flto=auto
CURSES := $(shell pkg-config --libs ncursesw)
CFLAGS_DEBUG := $(CFLAGS) -ggdb
CFLAGS_PROFILE := $(CFLAGS) -pg
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:
$(CC) ./main.c -o th -std=c89 $(CFLAGS) $(CURSES)
d:
$(CC) ./main.c -o th -std=c89 $(CFLAGS_DEBUG) $(CURSES)
$(GDB)
p:
$(CC) ./main.c -o th -std=c89 $(CFLAGS_PROFILE) $(CURSES)
v:
$(CC) ./main.c -o th -std=c89 $(CFLAGS_DEBUG) $(CURSES)
$(VALGRIND)
h:
$(CC) ./main.c -o th -std=c89 $(CFLAGS_DEBUG) $(CURSES)
$(HELGRIND)
-170
View File
@@ -1,170 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#define concat(out, s1, s2, _free) { \
concat## _free(out, s1, s2); \
}
#define concat0(out, s1, s2) \
char *result = malloc(strlen(s1) + strlen(s2) + 1); \
memcpy(result, s1, strlen(s1)); \
memcpy(result + strlen(s1), s2, strlen(s2) + 1); \
out = result;
#define concat1(out, s1, s2) \
char *result = malloc(strlen(s1) + strlen(s2) + 1); \
memcpy(result, s1, strlen(s1)); \
memcpy(result + strlen(s1), s2, strlen(s2) + 1); \
free(s1); \
out = result;
#define concat2(out, s1, s2) \
char *result = malloc(strlen(s1) + strlen(s2) + 1); \
memcpy(result, s1, strlen(s1)); \
memcpy(result + strlen(s1), s2, strlen(s2) + 1); \
free(s2); \
out = result;
#define concat3(out, s1, s2) \
char *result = malloc(strlen(s1) + strlen(s2) + 1); \
memcpy(result, s1, strlen(s1)); \
memcpy(result + strlen(s1), s2, strlen(s2) + 1); \
free(s1); \
free(s2); \
out = result;
char* smartstrcasestr(const char *haystack, const char *needle){
char smart = 0;
char *ret;
char passes = 0;
while (*needle) {
if (*needle >= 'A' && *needle <= 'Z') {
smart = 1;
break;
}
passes++;
needle++;
}
needle -= passes;
if (smart == 0) {
char *needle_case = malloc(strlen(needle)+1);
memcpy(needle_case, needle, strlen(needle)+1);
passes = 0;
while (*needle_case) {
*needle_case = *needle_case | ' ';
needle_case++;
passes++;
}
needle_case -= passes;
char *haystack_case = malloc(strlen(haystack)+1);
memcpy(haystack_case, haystack, strlen(haystack)+1);
passes = 0;
while (*haystack_case) {
*haystack_case = *haystack_case | ' ';
haystack_case++;
passes++;
}
haystack_case -= passes;
ret = strstr(haystack_case, needle_case);
free(needle_case);
free(haystack_case);
} else {
ret = strstr(haystack, needle);
}
return ret;
}
char* parse_cmd(const char *cmd, file *f){
const char *offset = strstr(cmd, SETTINGS_COMMAND_REPLACE_STR);
int count = 0;
char *out;
char *pos;
unsigned long i = 0;
while(f->file_name[i]) {
if (f->file_name[i] == '\'') {
count++;
}
i++;
}
out = malloc(strlen(cmd) + 1 + (strlen(f->file_name)+(count*3)) + 3);
pos = out;
if (offset) {
memcpy(pos, cmd, offset - cmd);
pos += offset - cmd;
} else {
memcpy(pos, cmd, strlen(cmd));
pos += strlen(cmd) + 1;
pos[-1] = ' ';
}
*pos = '\'';
pos++;
i = 0;
while(f->file_name[i]) {
if (f->file_name[i] == '\'') {
*pos++ = '\'';
*pos++ = '\\';
*pos++ = '\'';
}
*pos = f->file_name[i];
pos++;
i++;
}
*pos = '\'';
if (offset) {
pos[1]= ' ';
memcpy(pos + 1, offset+1, strlen(offset)+1);
pos[strlen(offset)+1] = '\0';
} else {
pos[1] = '\0';
}
return out;
}
char* parse_path(char *path){
int count = 0;
char *out;
char *pos;
unsigned long i = 0;
while(path[i]) {
if (path[i] == '\'') {
count++;
}
i++;
}
out = malloc((strlen(path)+(count*3)) + 4);
pos = out;
pos++;
out[0] = '\'';
i = 0;
while(path[i]) {
if (path[i] == '\'') {
*pos++ = '\'';
*pos++ = '\\';
*pos++ = '\'';
}
*pos = path[i];
pos++;
i++;
}
pos[0] = '\'';
pos[1]= ' ';
pos[2] = '\0';
return out;
}
-11
View File
@@ -1,11 +0,0 @@
#include <curses.h>
#ifndef BACKEND_GUARD
#define BACKEND_GUARD
#include "backend.c"
#endif
/*char* concat(const char *s1, const char *s2);*/
char* smartstrcasestr(const char *haystack, const char *needle);
char* parse_cmd(const char *cmd, file *f);
-128
View File
@@ -1,128 +0,0 @@
#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 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, " ")) != NULL) {
colors[i].file_extension = malloc(strlen(extension)+1);
memcpy(colors[i].file_extension, extension, strlen(extension)+1);
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
View File
@@ -1,3 +0,0 @@
#include "colors.c"
void colors_init();
-154
View File
@@ -1,154 +0,0 @@
#define SETTINGS_LINE_NUMBERS 2 /* 0 is disabled, 1 is enabled, 2 is relative */
#define SETTINGS_UEBERZUG_IMAGE_PREVIEW 1 /* 0 is disabled, 1 is enabled, 2 is with caching; depends on ueberzug */
#define SETTINGS_RELOAD_DIR_DELTA 10 /* 0 is disabled, time in seconds between automatic refresh of dir contents */
#define SETTINGS_CURSES_TIMEOUT 100 /* read: inopts(3NCURSES), blocking time between user inputs */
#define SETTINGS_COMMAND_REPLACE_STR "^" /* if a command in any cmd inside this fle contains this string, it will be replaced with the hovered/selected file name
* as of right now, this character cannot be escaped; i.e ``command\^ --arg`` will parse into ``command\path_of_hovered_file --arg``*/
#define SETTINGS_COMMAND_FORK '@' /* commands starting with this character will be forked into a new non blokcing detached process */
/* {{{ */
#ifndef CONFIG_GUARD
#define CONFIG_GUARD
#include "defines.h"
#include "sorting.h"
#include "interactions.h"
/* }}} */
static const char clipboard_cmd[] = "echo ^ | xsel -ib --trim"; /*used in yank_text*/
static const char rename_cmd[] = "mv"; /* as used in rename_hovered, makedir and makefile */
static const char makefile_cmd[] = "touch"; /*as of now both only do ``rename_cmd new_name``*/
static const char makedir_cmd[] = "mkdir";
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" },
{ "avif", "@ mpv" },
{ "gif", "@ mpv --loop-file=\"inf\"" },
{ "image", "@ feh" },
{ "video", "@ mpv" },
{ "audio", "mpv" },
{ "pdf", "@ zathura" },
};
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"},
{ ".cbz", "@ mcomix"},
{ ".cbr", "@ mcomix"},
{ ".json", "$EDITOR"},
{ ".csv", "$EDITOR"},
{ ".blend", "blender-bin-5.0.0"}, /* version number in bin in main repo? yea idc */
};
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 */
{ "v", select_all, NULL, "select all files in dir" },
{ "e", update, NULL, "rerun all backend" }, /* executes the entire backend and redrawing of the screen */
{ "B", enter_shell, "$SHELL", "enter $SHELL shell" },
{ "/", search, NULL, "search" },
{ ":", jmp_file_index, NULL, "jump to file on input number" },
{ "l", search_next, NULL, "search next" },
{ "L", search_previous, NULL, "search previous" },
{ "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" }, /* execute shell cmd on file, accounts for SETTINGS_COMMAND_FORK */
{ "r", rename_hovered, NULL, "rename hovered file" }, /* renames currently hovered file/directory */
{ "dD", 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 */
{ "yn", yank_file_name, NULL, "yank filename of hovered file" },
{ "yp", yank_file_path, NULL, "yank path of hovered file" },
{ "yy", yank_file, "copy", "copy/yank file/directory" },
{ "dd", yank_file, "cut", "cut file/directory" },
{ "pp", paste, NULL, "paste" },
{ "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" }, /* the path you started th in */
{ "gD", jump_to_dir, "$HOME/Downloads", "jump to $HOME/Downloads" },
{ "gC", jump_to_dir, "$HOME/Documents", "jump to $HOME/Documents" },
{ "gP", jump_to_dir, "$HOME/Pictures", "jump to $HOME/Pictures" },
{ "gM", jump_to_dir, "$HOME/Music", "jump to $HOME/Music" },
{ "gd", jump_to_dir, "/dev", "jump to /dev" },
{ "ge", jump_to_dir, "/etc", "jump to /etc" },
{ "gp", jump_to_dir, "/etc/portage", "jump to /etc/portage" },
{ "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" },
{ "gu", jump_to_dir, "/usr", "jump to /usr" },
{ "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" },
{ "uj", cmd_on_selected, "unzip -O shift-jis", "unzip zip shift-jis" },
{ "on", order_by, sort_natural, "order natural" },
{ "oe", order_by, sort_extension, "order extension" },
{ "os", order_by, sort_size, "order size" },
{ "ot", order_by, sort_type, "order type" },
{ "or", 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" },
{ "\x7F", toggle_hidden_files, NULL, "toggle hidden files" }, /* backspace key */
};
static const char size_unit[] = { 'B', 'K', 'M', 'G', 'T', 'P' }; /* this defines the maximum size unit, deleting everything except B results in all sizes being displayed in byte */
static const char ui_btm_text_storage_left[] = "total free";
static const char ui_btm_current_dir_size[] = "sum of dir,";
static const char ui_open_with_text[] = "open "SETTINGS_COMMAND_REPLACE_STR" with:";
static const char ui_rename_text[] = "rename "SETTINGS_COMMAND_REPLACE_STR" to:";
static const char ui_makefile_text[] = "makedir:";
static const char ui_makedir_text[] = "makefile:";
static const char ui_delete_text[] = "delete:";
/* {{{ */
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);
static const char size_unit_count = (sizeof(size_unit) / sizeof(size_unit[0])) - 1;
#else
static const char clipboard_cmd[];
static const char rename_cmd[];
static const char makefile_cmd[];
static const char makedir_cmd[];
static const mimetype mimetype_default_cmd[];
static const extension file_extension_default_cmd[];
static const binding key_binding[];
static const unsigned long binding_count;
static const unsigned long mimetype_default_count;
static const unsigned long file_extension_default_count;
static const char size_unit[];
static const char size_unit_count;
static const char ui_open_with_text[];
static const char ui_makedir_text[];
static const char ui_makefile_text[];
static const char ui_rename_text[];
static const char ui_delete_text[];
#endif
/* }}} */
-115
View File
@@ -1,115 +0,0 @@
#include <sys/types.h>
#define STATUS_QUIT_PROGRAM 1
#define STATUS_RUN_BACKEND 2
#define STATUS_RELOAD_DIRECTORY 4
#define STATUS_UPDATE_ASYNC_REFRESH 8
#define STATUS_UPDATE_SCREEN_RESIZE 16
#define STATUS_UPDATE_SCREEN_CLEAR 32
#define STATUS_UPDATE_SCREEN_MASK (STATUS_UPDATE_ASYNC_REFRESH | STATUS_UPDATE_SCREEN_RESIZE | STATUS_UPDATE_SCREEN_CLEAR)
#define STATUS_USER_ROOT 128
#define STATUS_INPUT_MATCH 256
#define STATUS_DELTA_TIME 512
#define STATUS_MOVE_RIGHT_MATCH 1024
#define SETTINGS_HAS_COLOR 1
#define RENDER_QUEUE_TOP 0
#define RENDER_QUEUE_LFT 1
#define RENDER_QUEUE_MID 2
#define RENDER_QUEUE_RGT 3
#define RENDER_QUEUE_BTM 4
#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_FILE_OPEN 64 /* 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 0
#define FILE_TYPE_EXEC 1
#define FILE_TYPE_REGULAR 2
#define FILE_TYPE_BLOCK 3
#define FILE_TYPE_CHARDEV 4
#define FILE_TYPE_SOCK 5
#define FILE_TYPE_FIFO 6
#define FILE_TYPE_ORPHAN 7
#define FILE_TYPE_DIR 32
#define FILE_TYPE_SYMLINK 64
#define FILE_TYPE_OPEN_FILE 128 /* this is only used in rgt_content to print a file preview, not the dir */
#define YANK_IS_USED 1
#define YANK_CUT 2
#define YANK_COPY 4
#define BTM_WINDOW_HEIGHT_ON_STR_INTERACTION 5
#define INPUT_BUFFER_SIZE 255
#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 Dir {
file *current_file;
file *file_list;
unsigned long file_count;
} dir;
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;
typedef struct Yank {
char status;
char *path;
char **list;
unsigned long count;
} yank;
typedef struct Linked_dir {
char *path;
unsigned long index;
struct Linked_dir *next;
} linked_dir;
#endif
-410
View File
@@ -1,410 +0,0 @@
#include <curses.h>
#include <pthread.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "sorting.h"
#include "defines.h"
#include "config.h"
extern dir mid_dir;
extern unsigned int settings;
extern unsigned int file_modifiers;
extern unsigned int color_count;
extern unsigned int terminal_height;
extern char *global_path;
extern color *colors;
int (*order_func)() = sort_natural;
linked_dir *list_beginning;
linked_dir *current_linked_dir;
unsigned long get_dir_size(char *path){
DIR *dir = opendir(path);
unsigned long entry_count = 0;
struct dirent *entry;
if (dir) {
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
while ((entry=readdir(dir))) {
entry_count++;
}
/* removes files "." and ".." */
entry_count -= 2;
} else {
while ((entry=readdir(dir))) {
if (entry->d_name[0] != '.') {
entry_count++;
}
}
}
closedir(dir);
}
return entry_count;
}
void get_dir_content(char *path, dir *dir){
struct dirent **entry = NULL;
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
scandir(path, &entry, skip_dot, NULL);
} else {
scandir(path, &entry, skip_hidden_files, NULL);
}
char *full_path = NULL;
unsigned long i = 0;
for (i = 0; i < dir->file_count; i++ ) {
dir->file_list[i].file_name = NULL;
if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
} else {
dir->file_list[i].file_name = malloc(strlen(entry[i]->d_name)+1);
memcpy(dir->file_list[i].file_name, entry[i]->d_name, strlen(entry[i]->d_name) + 1);
dir->file_list[i].status = 0;
struct stat *file;
file = malloc(sizeof(struct stat));
/* using the full path allows using the same function for all windows */
full_path = malloc(strlen(path) + strlen(entry[i]->d_name) + 1 + sizeof("/"));
memcpy(full_path, path, strlen(path));
memcpy(full_path + strlen(path) + sizeof("/") - 1, entry[i]->d_name, strlen(entry[i]->d_name) + 1);
full_path[strlen(path)] = '/';
lstat(full_path, file);
dir->file_list[i].file_size = file->st_size;
dir->file_list[i].permissions = file->st_mode;
if (S_ISLNK(file->st_mode)) {
stat(full_path, file);
if (S_ISDIR(file->st_mode)) {
dir->file_list[i].file_type = FILE_TYPE_DIR | FILE_TYPE_SYMLINK;
dir->file_list[i].color_pair = COLOR_SYMLINK;
dir->file_list[i].file_size = get_dir_size(full_path);
} else {
dir->file_list[i].file_type = FILE_TYPE_REGULAR | FILE_TYPE_SYMLINK;
dir->file_list[i].color_pair = COLOR_SYMLINK;
}
} else if (S_ISDIR(file->st_mode)) {
dir->file_list[i].file_type = FILE_TYPE_DIR;
dir->file_list[i].color_pair = COLOR_DIR;
dir->file_list[i].file_size = get_dir_size(full_path);
} else if (file->st_mode & S_IXUSR) {
dir->file_list[i].file_type = FILE_TYPE_EXEC;
dir->file_list[i].color_pair = COLOR_EXEC;
} else if (S_ISREG(file->st_mode)) {
dir->file_list[i].file_type = FILE_TYPE_REGULAR;
dir->file_list[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->file_list[i].color_pair = colors[j].color_pair;
}
}
}
} else if (S_ISBLK(file->st_mode)) {
dir->file_list[i].file_type = FILE_TYPE_BLOCK;
dir->file_list[i].color_pair = COLOR_BLOCK;
} else if (S_ISCHR(file->st_mode)) {
dir->file_list[i].file_type = COLOR_CHARDEV;
} else if (S_ISFIFO(file->st_mode)) {
dir->file_list[i].file_type = FILE_TYPE_FIFO;
dir->file_list[i].color_pair = COLOR_FIFO;
} else if (S_ISSOCK(file->st_mode)) {
dir->file_list[i].file_type = FILE_TYPE_SOCK;
dir->file_list[i].color_pair = COLOR_SOCK;
} else {
dir->file_list[i].file_type = COLOR_REGULAR;
dir->file_list[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->file_list[i].color_pair = colors[j].color_pair;
}
}
} else {
}
}
free(full_path);
free(file);
free(entry[i]);
}
}
qsort(dir->file_list, dir->file_count, sizeof(file), order_func);
free(entry);
}
void print_dir(WINDOW *win, char print_info, dir *dir){
/* i am not proud of this function */
unsigned long line_width = getmaxx(win);
unsigned long i = 0;
float file_size;
float printed_size = 0;
char size_char = ' ';
char is_selected = 0;
unsigned long offset_vertical = 0;
unsigned long offset_back = 0;
#if SETTINGS_LINE_NUMBERS == 0
unsigned long offset_front = 0;
#else
long offset_front = 2;
#endif
unsigned long selected_file_current = dir->current_file - dir->file_list;
long offset_index = 2; /* only used for the index of the file itself */
if (print_info) {
if (dir->file_count > 9) {
#if SETTINGS_LINE_NUMBERS != 0
unsigned long dir_file_count_ = dir->file_count;
while(dir_file_count_ > 9) {
offset_front++;
dir_file_count_ /= 10;
}
#endif
}
if (selected_file_current > (terminal_height/3)*2 && dir->file_count > terminal_height - 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;
}
}
} else {
offset_front = 0;
}
for (i = offset_vertical; i < dir->file_count && i < (terminal_height + offset_vertical); i++) {
if (print_info) {
file_size = dir->file_list[i].file_size;
char size_index = -1;
do {
printed_size=file_size;
file_size /= 1024;
size_index++;
} while (file_size > 1 && size_index < size_unit_count);
size_char = size_unit[(unsigned)size_index];
if (dir->file_list[i].file_type & FILE_TYPE_DIR) {
offset_back = line_width - (snprintf(NULL,0,"%ld", dir->file_list[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);
}
} else {
offset_back = line_width;
}
if (dir->file_list[i].status & FILE_STATUS_SELECTED) {
is_selected = 1;
} else {
is_selected = 0;
}
if (dir->file_list[i].status & FILE_STATUS_SELECTED) {
wattron(win, COLOR_PAIR(8));
} else {
wattron(win, COLOR_PAIR(dir->file_list[i].color_pair));
}
if (&dir->file_list[i] == dir->current_file) {
wattron(win, A_REVERSE);
unsigned long bg = 0;
for (bg = 0; bg < line_width; bg++) {
mvwaddch(win, i-offset_vertical, bg, ' ');
}
} else {
wattroff(win, A_REVERSE);
}
if(print_info) {
#if SETTINGS_LINE_NUMBERS == 2
long i_ = (selected_file_current) - i;
offset_index = 0;
while(i_) {
offset_index++;
i_ /= 10;
}
long relative_index = selected_file_current - i;
if (relative_index < 0) {
relative_index = relative_index * -1;
} else if (relative_index == 0) {
i_ = (selected_file_current != 0) ? (selected_file_current) : 1;
while(i_) {
offset_index++;
i_ /= 10;
}
relative_index = i;
}
mvwprintw(win, i-offset_vertical, offset_front-offset_index-1, "%ld", relative_index);
#elif SETTINGS_LINE_NUMBERS == 1
unsigned long i_ = i;
offset_index = 2;
while(i_ > 9) {
offset_index++;
i_ /= 10;
}
mvwprintw(win, i-offset_vertical, offset_front-offset_index, "%ld", i);
#endif
if (dir->file_list[i].file_type & FILE_TYPE_DIR) {
mvwprintw(win, i-offset_vertical, offset_back, "%ld", dir->file_list[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);
}
}
char *extension = strrchr(dir->file_list[i].file_name, '.');
unsigned long printable_size = offset_back-offset_front-is_selected-2;
mvwaddnstr(win, i-offset_vertical, offset_front+is_selected, dir->file_list[i].file_name, printable_size);
if (extension && printable_size <= strlen(dir->file_list[i].file_name)) {
mvwaddnstr(win, i-offset_vertical, offset_back-strlen(extension)-1, extension, strlen(extension));
mvwaddnstr(win, i-offset_vertical, offset_back-strlen(extension)-2, "~", 1);
}
if (dir->file_list[i].status & FILE_STATUS_SELECTED) {
wattroff(win, COLOR_PAIR(8));
} else {
wattroff(win, COLOR_PAIR(dir->file_list[i].color_pair));
}
}
}
void dir_changed(){
char *path = getcwd(NULL, 0);
current_linked_dir = list_beginning;
while (current_linked_dir->next != NULL) {
if(strcmp(current_linked_dir->path, path) == 0) {
break;
} else {
current_linked_dir = current_linked_dir->next;
}
}
if(strcmp(current_linked_dir->path, path) == 0) {
mid_dir.current_file = &mid_dir.file_list[current_linked_dir->index];
} else {
/**/
current_linked_dir->next = malloc(sizeof(linked_dir));
current_linked_dir->next->path = malloc(strlen(global_path)+1);
memcpy(current_linked_dir->next->path, global_path, strlen(global_path)+1);
current_linked_dir->next->next = NULL;
current_linked_dir->next->index = 0;
mid_dir.current_file = mid_dir.file_list;
}
}
void change_dir(char *new_path){
char *old_path = getcwd(NULL, 0);
current_linked_dir = list_beginning;
while (current_linked_dir->next != NULL) {
if(strcmp(current_linked_dir->path, old_path) == 0) {
break;
} else {
current_linked_dir = current_linked_dir->next;
}
}
if(strcmp(current_linked_dir->path, old_path) == 0) {
current_linked_dir->index = mid_dir.current_file - mid_dir.file_list;
}
chdir(new_path);
char *new_path_real = getcwd(NULL, 0);
current_linked_dir = list_beginning;
while (current_linked_dir->next != NULL) {
if(strcmp(current_linked_dir->path, new_path_real) == 0) {
break;
} else {
current_linked_dir = current_linked_dir->next;
}
}
if(strcmp(current_linked_dir->path, new_path_real) != 0) {
current_linked_dir->next = malloc(sizeof(linked_dir));
current_linked_dir = current_linked_dir->next;
current_linked_dir->path = malloc(strlen(new_path_real)+1);
memcpy(current_linked_dir->path, new_path_real, strlen(new_path_real)+1);
current_linked_dir->next = NULL;
current_linked_dir->index = 0;
/*TODO(2026-05-25T22:49:30)
*handle if new_path == "..", should this case be true, focus the index on which the old_path falls on*/
}
mid_dir.current_file = &mid_dir.file_list[current_linked_dir->index];
free(old_path);
free(new_path_real);
}
void dir_init(){
list_beginning = malloc(sizeof(linked_dir));
list_beginning->path = getcwd(NULL, 0);
list_beginning->index = 0;
list_beginning->next = NULL;
current_linked_dir = list_beginning;
}
void recursive_delete(file current_file){
/*
if (S_ISLNK(current_file.permissions)) {
remove(current_file.file_name);
} else if (current_file.file_type & FILE_TYPE_DIR ) {
unsigned int file_modifiers_tmp = file_modifiers;
file_modifiers |= FILE_MODIFIERS_HIDDEN_FILES;
unsigned long current_file_count = get_dir_size(current_file.file_name);
if (current_file_count != 0) {
file *current_dir = malloc(current_file_count * sizeof(file));
memset(current_dir, '\0', current_file_count * sizeof(file));
get_dir_content(current_file.file_name, &current_file_count, current_dir);
if (chdir(current_file.file_name) != 0) {
return;
}
unsigned long i;
for (i = 0; i < current_file_count; i++) {
recursive_delete(current_dir[i]);
free(current_dir[i].file_name);
}
free(current_dir);
if (chdir("..") != 0) {
return;
}
}
remove(current_file.file_name);
file_modifiers = file_modifiers_tmp;
} else {
remove(current_file.file_name);
}
*/
}
-13
View File
@@ -1,13 +0,0 @@
#ifndef DIR_GUARD
#define DIR_GUARD
#include "dir.c"
#endif
unsigned long get_dir_size(char *path);
void get_dir_content(char *path, dir *dir);
void print_dir(WINDOW *win, char print_info, dir *dir);
char update_selected_file();
void dir_set_selected_file_current(unsigned long selected_file_current);
unsigned long dir_get_selected_file_current();
void dir_init();
void recursive_delete(file current_file);
-133
View File
@@ -1,133 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "backend.h"
#include "defines.h"
#include "config.h"
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
static FILE *ueberzug = NULL;
#endif
extern unsigned int terminal_height;
extern unsigned int terminal_width;
char previewd;
char* text(file *f);
void images_print(char *file_name);
void images_clear();
char* generic(file *f);
char* get_mimetype(file *f){
static const char *cmd_str = "file --mime-type -b";
char *cmd = parse_cmd(cmd_str, f);
FILE *cmd_open = popen(cmd, "r");
char *line = NULL;
size_t size = 0;
free(cmd);
if (getline(&line, &size, cmd_open) != -1){
pclose(cmd_open);
return line;
} else {
pclose(cmd_open);
return "unknown";
}
}
char* preview_file(file *f){
/* this calls "file" on path */
char *file_buffer;
char *mime = get_mimetype(f);
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
images_clear();
#endif
if (strstr(mime, "text")) {
file_buffer = text(f);
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
} else if (strstr(mime, "image")) {
file_buffer = generic(f);
images_print(f->file_name);
previewd = 1;
#endif
} else {
file_buffer = generic(f);
}
free(mime);
return file_buffer;
}
char* text(file *f){
unsigned long size = (terminal_width/2) * terminal_height;
if (size > f->file_size) {
size = f->file_size;
}
char *file_buffer = malloc(size + 1);
FILE *fp = fopen(f->file_name, "r");
if (fread(file_buffer, size, 1, fp) != 0) {
fclose(fp);
file_buffer[size] = '\0';
return file_buffer;
} else {
fclose(fp);
return "failed reading file";
}
}
char* generic(file *f){
static const char *cmd_str = "file";
char *cmd = parse_cmd(cmd_str, f);
FILE *cmd_open = popen(cmd, "r");
char *line = NULL;
size_t size = 0;
free(cmd);
if (getline(&line, &size, cmd_open) != -1) {
pclose(cmd_open);
return line;
} else {
pclose(cmd_open);
return "failed executing shell command \"file\"";
}
}
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
void images_clear(){
if (previewd == 1) {
fprintf(ueberzug, "{\"action\": \"remove\", \
\"identifier\": \"preview\"}\n");
fflush(ueberzug);
previewd = 0;
}
}
void images_print(char *file_name){
char *path=getcwd(NULL, 0);
fprintf(ueberzug, "{\"action\":\"add\", \
\"identifier\":\"preview\", \
\"max_height\":%d, \
\"max_width\":%d, \
\"y\":0, \
\"x\":%d, \
\"path\":\"%s/%s\"}\n", terminal_height, terminal_width/2, terminal_width/2, path, file_name);
fflush(ueberzug);
free(path);
}
void ueberzug_init(){
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW == 2
ueberzug = popen("ueberzug layer -s ", "w");
#elif SETTINGS_UEBERZUG_IMAGE_PREVIEW == 1
ueberzug = popen("ueberzug layer -s --no-cache ", "w");
#endif
}
void ueberzug_close(){
images_clear();
pclose(ueberzug);
}
#endif
-13
View File
@@ -1,13 +0,0 @@
#ifndef PREVIEW_GUARD
#define PREVIEW_GUARD
#include "file_previews.c"
#include "config.h"
#endif
char* preview_file(file *f);
char* get_mimetype(file *f);
void images_clear();
void ueberzug_init();
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
void ueberzug_close();
#endif
-545
View File
@@ -1,545 +0,0 @@
#include <curses.h>
#include <pthread.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "file_previews.h"
#include "backend.h"
#include "defines.h"
#include "config.h"
#include "dir.h"
extern pthread_mutex_t mutex_mid;
extern pthread_mutex_t mutex_render;
extern WINDOW *win_b;
extern WINDOW *win_m;
extern dir mid_dir;
extern unsigned int terminal_height;
extern unsigned int terminal_width;
extern unsigned int status;
extern char *start_path;
extern char *global_path;
extern char *input;
extern time_t seed;
char search_buffer[INPUT_BUFFER_SIZE];
unsigned int input_pass;
unsigned long parsed_input_number;
yank yank_files = { 0 };
#define TODO noraw(); \
endwin();\
curs_set(1);\
echo();\
printf("TODO: %s at %d in %s \n", __func__, __LINE__, __FILE__);\
exit(1);
void FAIL(char *function, char *str){
printf("ERROR in function %s: %s", function, str);
}
void user_interactions() {
char ch;
unsigned long i;
unsigned long binding_matches = 0;
ch = getch();
if(ch != ERR) {
input[input_pass] = ch;
input_pass++;
if (ch == 27) { /* esc key */
memset(input, 0, INPUT_BUFFER_SIZE);
input_pass = 0;
}
}
void (*func_ptr)(int, int);
unsigned long number_length = 0;
parsed_input_number = 0;
while((*input >= '0') && (*input <= '9')) {
parsed_input_number = (parsed_input_number * 10) + (*input - '0');
input++;
number_length++;
}
if (parsed_input_number == 0) {
parsed_input_number = 1;
}
input -= number_length;
char cmp_len = strlen(input);
if(strlen(input) < 1) {
cmp_len = 1;
}
for (i = 0; i < binding_count; i++) {
if (strncmp(input + number_length, key_binding[i].key, cmp_len) == 0) {
if (strcmp(input + number_length, key_binding[i].key) == 0) {
pthread_mutex_lock(&mutex_mid);
func_ptr = key_binding[i].func;
func_ptr(parsed_input_number, i);
pthread_mutex_unlock(&mutex_mid);
timeout(SETTINGS_CURSES_TIMEOUT); /* blocking timeout of getch() */
} else {
binding_matches++;
mvwprintw(stdscr, terminal_height-binding_matches-1, 0, "\t\t\t");
mvwprintw(stdscr, terminal_height-binding_matches-1, 0, "%s\t%s", key_binding[i].key, key_binding[i].comment);
status |= STATUS_INPUT_MATCH;
}
}
}
if (status & STATUS_INPUT_MATCH) {
attron(A_UNDERLINE);
mvwprintw(stdscr, terminal_height-binding_matches-2, 0, "input\tcommand\t\t");
attroff(A_UNDERLINE);
status &= ~STATUS_INPUT_MATCH;
} else if (number_length != strlen(input)) {
memset(input, 0, INPUT_BUFFER_SIZE);
input_pass = 0;
number_length = 0;
binding_matches = 0;
}
}
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 == '\t') { /* tab */
memcpy(str + pass, mid_dir.current_file->file_name, strlen(mid_dir.current_file->file_name));
mvwaddstr(win, y, x + pass, mid_dir.current_file->file_name);
pass += strlen(mid_dir.current_file->file_name);
} else if (ch == 127) { /* backspace */
if (pass > 0) {
pass--;
mvwdelch(win, y, x + pass);
}
} else if (ch == 27) { /* esc key */
err = 1;
break;
} else {
mvwaddch(win, y, x + pass, ch);
str[pass] = ch;
pass++;
}
}
str[pass] = '\0';
curs_set(0);
return err;
}
void quit_program(){
status = STATUS_QUIT_PROGRAM;
}
void update(){
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY );
}
void select_all(){
unsigned long i;
for (i = 0; i < mid_dir.file_count; i++) {
mid_dir.file_list[i].status ^= FILE_STATUS_SELECTED;
}
status |= (STATUS_RUN_BACKEND);
}
void move_down(unsigned long passes){
mid_dir.current_file += passes;
if (mid_dir.current_file > mid_dir.file_list + mid_dir.file_count - 1) {
mid_dir.current_file = mid_dir.file_list + mid_dir.file_count - 1;
}
status |= (STATUS_RUN_BACKEND);
}
void move_up(unsigned long passes){
mid_dir.current_file -= passes;
if (mid_dir.current_file < mid_dir.file_list) {
mid_dir.current_file = mid_dir.file_list;
}
status |= (STATUS_RUN_BACKEND);
}
void move_left(unsigned long passes){
unsigned long i;
for (i = 0; i < passes; i++) {
change_dir("..");
}
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void move_right(){
if (mid_dir.current_file->file_type & FILE_TYPE_DIR) {
change_dir(mid_dir.current_file->file_name);
} else if (mid_dir.current_file->file_type & FILE_TYPE_EXEC) {
char *cmd = parse_cmd("./"SETTINGS_COMMAND_REPLACE_STR,mid_dir.current_file);
if (system(cmd)) {
}
} else {
char *mime = get_mimetype(mid_dir.current_file);
char *extension = mid_dir.current_file->file_name;
char *cmd = NULL;
while (extension <= mid_dir.current_file->file_name + strlen(mid_dir.current_file->file_name)) {
if (*extension == '.') {
break;
}
extension++;
}
unsigned long i;
if (extension <= mid_dir.current_file->file_name + strlen(mid_dir.current_file->file_name)) {
for (i = 0; i < file_extension_default_count; i++) {
if (strcmp(extension, file_extension_default_cmd[i].file_extension) == 0) {
if (file_extension_default_cmd[i].command[0] == SETTINGS_COMMAND_FORK) {
cmd = parse_cmd(file_extension_default_cmd[i].command + 1, mid_dir.current_file);
pid_t pid = fork();
if (pid == 0 && setsid()) {
system(cmd);
status = STATUS_QUIT_PROGRAM;
exit(1);
}
} else {
cmd = parse_cmd(file_extension_default_cmd[i].command, mid_dir.current_file);
if (system(cmd)) {
}
}
status |= STATUS_MOVE_RIGHT_MATCH;
update();
break;
}
}
}
if (!(status & STATUS_MOVE_RIGHT_MATCH)) {
for (i = 0; i < mimetype_default_count; i++) {
if (strstr(mime, mimetype_default_cmd[i].mimetype)) {
if (mimetype_default_cmd[i].command[0] == SETTINGS_COMMAND_FORK) {
cmd = parse_cmd(mimetype_default_cmd[i].command + 1, mid_dir.current_file);
pid_t pid = fork();
if (pid == 0 && setsid()) {
system(cmd);
status = STATUS_QUIT_PROGRAM;
exit(1);
}
} else {
cmd = parse_cmd(mimetype_default_cmd[i].command, mid_dir.current_file);
if (system(cmd)) {
}
}
status |= STATUS_MOVE_RIGHT_MATCH;
update();
break;
}
}
}
status &= ~STATUS_MOVE_RIGHT_MATCH;
free(cmd);
free(mime);
}
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void toggle_hidden_files(){
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void toggle_selection(unsigned long passes){
unsigned long i;
for (i = 0; i < passes; i++) {
mid_dir.current_file->status ^= FILE_STATUS_SELECTED;
move_down(1);
}
status |= (STATUS_RUN_BACKEND);
}
void jump_bottom(){
mid_dir.current_file = mid_dir.file_list + mid_dir.file_count - 1;
status |= (STATUS_RUN_BACKEND);
}
void jump_top(){
mid_dir.current_file = mid_dir.file_list;
status |= (STATUS_RUN_BACKEND);
}
void open_with(){
wclear(win_b);
char *cmd;
char *str = malloc(INPUT_BUFFER_SIZE);
char *parsed_ui_text = parse_cmd(ui_open_with_text, mid_dir.current_file);
mvwprintw(win_b, 0, 0, parsed_ui_text);
if (read_string(win_b, 0, strlen(parsed_ui_text)+1, str) == 0) {
if (str[0] == SETTINGS_COMMAND_FORK) {
cmd = parse_cmd(str+1, mid_dir.current_file);
pid_t pid = fork();
if (pid == 0 && setsid()) {
system(cmd);
status = STATUS_QUIT_PROGRAM;
exit(1);
}
} else {
cmd = parse_cmd(str, mid_dir.current_file);
if (system(cmd)) {
}
}
free(cmd);
}
free(parsed_ui_text);
free(str);
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_CLEAR);
}
void rename_hovered(){
wclear(win_b);
file tmp;
tmp.file_name = malloc(INPUT_BUFFER_SIZE);
char *parsed_ui_text = parse_cmd(ui_rename_text, mid_dir.current_file);
mvwprintw(win_b, 0, 0, parsed_ui_text);
if (read_string(win_b, 0, strlen(parsed_ui_text)+1, tmp.file_name) == 0) {
char *cmd0 = parse_cmd(rename_cmd, mid_dir.current_file);
char *cmd1 = parse_cmd(cmd0, &tmp);
system(cmd1);
free(cmd0);
free(cmd1);
}
free(parsed_ui_text);
free(tmp.file_name);
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void delete(){
TODO;
}
void makedir(){
wclear(win_b);
file tmp;
tmp.file_name = malloc(INPUT_BUFFER_SIZE);
mvwprintw(win_b, 0, 0, ui_makedir_text);
if (read_string(win_b, 0, strlen(ui_makedir_text)+1, tmp.file_name) == 0) {
char *cmd = parse_cmd(makedir_cmd, &tmp);
system(cmd);
free(cmd);
}
free(tmp.file_name);
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void makefile(){
wclear(win_b);
file tmp;
tmp.file_name = malloc(INPUT_BUFFER_SIZE);
mvwprintw(win_b, 0, 0, ui_makefile_text);
if (read_string(win_b, 0, strlen(ui_makefile_text)+1, tmp.file_name) == 0) {
char *cmd = parse_cmd(makefile_cmd, &tmp);
system(cmd);
free(cmd);
}
free(tmp.file_name);
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void enter_shell(unsigned long passes, int index){
(void)passes; /*remove compiler warning*/
endwin();\
echo();\
curs_set(1);\
if (system(key_binding[index].black_magic)) {
}
initscr(); /* start ncurses */
noecho(); /* hide keyboard input */
curs_set(0);
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void not_implemented(unsigned long passes, int index){
(void)passes;
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");
TODO;
}
void jump_to_dir(unsigned long passes, int index){
(void)passes;
unsigned long len;
char *c = strchr(key_binding[index].black_magic, '/');
if (c) {
len = c - (char*)key_binding[index].black_magic;
} else {
len = strlen(key_binding[index].black_magic);
}
char *to_env = malloc(len + 1);
memcpy(to_env, key_binding[index].black_magic, len);
to_env[len] = '\0';
char *env = getenv(to_env + 1); /*+1 to remove the '$' prefix, freeing env always segfaults*/
if (env) {
char *path = malloc(strlen(key_binding[index].black_magic) + strlen(env) + 1);
memcpy(path, env, strlen(env));
memcpy(path + strlen(env), key_binding[index].black_magic + len, strlen(key_binding[index].black_magic)+1);
change_dir(path);
free(path);
} else {
change_dir(key_binding[index].black_magic);
}
free(to_env);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY );
}
void order_by(unsigned long passes, int index){
(void)passes;
seed = time(NULL);
order_func = key_binding[index].black_magic;
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void cmd_on_selected(unsigned long passes, int index){
(void)passes;
char *cmd = parse_cmd(key_binding[index].black_magic, mid_dir.current_file);
system(cmd);
free(cmd);
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_CLEAR);
}
void yank_file_name(){
char *cmd = parse_cmd(clipboard_cmd, mid_dir.current_file);
system(cmd);
free(cmd);
status |= (STATUS_RUN_BACKEND);
}
void yank_file_path(){
file *tmp = malloc(sizeof(file));
tmp->file_name = malloc(strlen(global_path)+1+strlen(mid_dir.current_file->file_name)+1);
memcpy(tmp->file_name, global_path, strlen(global_path));
memcpy(tmp->file_name+strlen(global_path)+1, mid_dir.current_file->file_name, strlen(mid_dir.current_file->file_name)+1);
tmp->file_name[strlen(global_path)] = '/'; /*no +1 is needed*/
char *cmd = parse_cmd(clipboard_cmd, tmp);
system(cmd);
free(cmd);
free(tmp->file_name);
free(tmp);
status |= (STATUS_RUN_BACKEND);
}
void yank_file(unsigned long passes, int index){
TODO;
}
void paste(){
TODO;
}
void search(){
echo();
unsigned long i;
for (i = 0; i < terminal_width -1; i++) {
mvwaddch(win_b, 0, i, ' ');
}
mvwaddch(win_b, 0, 0, '/');
memset(search_buffer, 0, INPUT_BUFFER_SIZE);
curs_set(1);
echo();
timeout(-1); /* negative numbers block until enter is pressed */
unsigned int pass = 0;
char ch;
char err = 0;
wmove(win_b, 0, 1);
while(1) {
ch = wgetch(win_b);
if (ch == '\n') {
err = 0;
break;
} else if (ch == '\t') { /* tab */
memcpy(search_buffer + pass, mid_dir.current_file->file_name, strlen(mid_dir.current_file->file_name));
mvwaddstr(win_b, 0, pass+1, mid_dir.current_file->file_name);
pass += strlen(mid_dir.current_file->file_name);
} else if (ch == 127) { /* backspace */
if (pass > 0) {
pass--;
mvwdelch(win_b, 0, pass+1);
}
} else if (ch == 27) { /* esc key */
err = 1;
break;
}
if (ch) {
mvwaddch(win_b, 0, pass+1, ch);
search_buffer[pass] = ch;
pass++;
unsigned long index = (mid_dir.current_file - mid_dir.file_list);
unsigned long x = getmaxx(win_b);
for (; &mid_dir.file_list[index] < mid_dir.file_list + mid_dir.file_count; index++) {
if (smartstrcasestr(mid_dir.file_list[index].file_name, search_buffer)) {
mid_dir.current_file = &mid_dir.file_list[index];
/* re-render current dir */
pthread_mutex_lock(&mutex_render);
werase(win_m);
print_dir(win_m, 1, &mid_dir);
wnoutrefresh(win_m);
pthread_mutex_unlock(&mutex_render);
break;
}
}
}
}
search_buffer[pass] = '\0';
noecho();
curs_set(0);
status |= (STATUS_RUN_BACKEND);
}
void search_next(){
long index = (mid_dir.current_file - mid_dir.file_list) + 1;
for (; &mid_dir.file_list[index] < mid_dir.file_list + mid_dir.file_count; index++) {
if (smartstrcasestr(mid_dir.file_list[index].file_name, search_buffer)) {
mid_dir.current_file = &mid_dir.file_list[index];
break;
}
}
status |= (STATUS_RUN_BACKEND);
}
void search_previous(){
long index = (mid_dir.current_file - mid_dir.file_list) - 1;
for (; &mid_dir.file_list[index] >= mid_dir.file_list; index--) {
if (smartstrcasestr(mid_dir.file_list[index].file_name, search_buffer)) {
mid_dir.current_file = &mid_dir.file_list[index];
break;
}
}
status |= (STATUS_RUN_BACKEND);
}
void jmp_file_index(){
TODO;
}
-36
View File
@@ -1,36 +0,0 @@
#ifndef INTERACTIONS_GUARD
#define INTERACTIONS_GUARD
#include "interactions.c"
#endif
void user_interactions();
void quit_program();
void toggle_selection();
void select_all();
void move_right();
void move_up(unsigned long passes);
void move_down(unsigned long passes);
void move_left(unsigned long 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 enter_shell(unsigned long passes, int index);
void not_implemented(unsigned long passes, int index);
void jump_to_dir(unsigned long passes, int index);
void order_by(unsigned long passes, int index);
void cmd_on_selected(unsigned long passes, int index);
void yank_file_name();
void yank_file_path();
void yank_file(unsigned long passes, int index);
void paste();
void search();
void search_next();
void search_previous();
void jmp_file_index();
-192
View File
@@ -1,192 +0,0 @@
#define _POSIX_C_SOURCE 200809L
#include <curses.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <time.h>
#include "defines.h"
#include "config.h"
#include "threading.h"
#include "window.c"
#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_width = 0;
unsigned int settings;
unsigned int file_modifiers = 0;
unsigned int status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY) & ~STATUS_UPDATE_SCREEN_RESIZE;
char *global_path;
time_t seed;
WINDOW *win_t;
WINDOW *win_b;
WINDOW *win_l;
WINDOW *win_m;
WINDOW *win_r;
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;
char *input; /*used in user_interactions*/
void render_pass();
void init();
int main(){
init();
getmaxyx(stdscr, terminal_height, terminal_width);
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;
#if SETTINGS_RELOAD_DIR_DELTA != 0
time_t t;
time_t dt;
time(&t);
#endif
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_r, NULL, thread_rgt, &status); /*child_content slash win_r*/
pthread_create(&thread_b, NULL, thread_btm, &status); /*bottom bar*/
timeout(SETTINGS_CURSES_TIMEOUT);
while(!(status & STATUS_QUIT_PROGRAM)){
getmaxyx(stdscr, terminal_height, terminal_width);
if (status & STATUS_RUN_BACKEND) {
free(global_path);
global_path = getcwd(NULL, 0);
pthread_cond_signal(&cond_mid);
pthread_cond_signal(&cond_lft);
status &= ~(STATUS_RUN_BACKEND);
} else {
status &= ~(STATUS_RELOAD_DIRECTORY | STATUS_DELTA_TIME);
}
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;
}
user_interactions();
render_pass();
#if SETTINGS_RELOAD_DIR_DELTA != 0
time(&dt);
if (dt - t >= SETTINGS_RELOAD_DIR_DELTA) {
time(&t);
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY | STATUS_DELTA_TIME);
}
#endif
}
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
ueberzug_close();
#endif
threading_free();
delwin(win_l);
delwin(win_m);
delwin(win_r);
delwin(win_t);
delwin(win_b);
noraw();
echo();
curs_set(1);
endwin();
return 0;
}
void render_pass(){
if (status & STATUS_UPDATE_SCREEN_CLEAR) {
clear();
status &= ~STATUS_UPDATE_SCREEN_CLEAR;
}
if (status & STATUS_UPDATE_SCREEN_RESIZE) {
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);
}
if (pthread_mutex_lock(&mutex_render) == 0 || status & STATUS_UPDATE_SCREEN_MASK) {
doupdate();
status &= ~STATUS_UPDATE_SCREEN_MASK;
pthread_mutex_unlock(&mutex_render);
}
}
/*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(10); /* blocking timeout of getch(), using 10 rather than SETTINGS_CURSES_TIMEOUT to cause a faster first render
regardless on SETTINGS_CURSES_TIMEOUT, 10 was taken arbitrary.
if the blocking is too low, the first render happens delayed, however even this delay causes a quicker than
(compute time of threads) + timeout */
curs_set(0);
/*file_modifiers = (FILE_MODIFIERS_HIDDEN_FILES | FILE_MODIFIERS_SORT_BITMASK);*/
input = malloc(INPUT_BUFFER_SIZE); /* size of input buffer, out of bounds access will not be accounted for */
memset(input, 0, INPUT_BUFFER_SIZE);
if (getuid() == 0) {
status |= STATUS_USER_ROOT;
}
threading_init(); /* found in threading.c */
colors_init(); /* in colors.c */
dir_init(); /*in dir.c */
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
ueberzug_init(); /* in file_previews.c */
#endif
ESCDELAY = 10;
global_path = getcwd(NULL, 0);
char *start_path = getcwd(NULL, 0);
setenv("START_PATH", start_path, 0);
free(start_path);
seed = time(NULL);
}
-203
View File
@@ -1,203 +0,0 @@
#include <curses.h>
#include <dirent.h>
#include <strings.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include "defines.h"
extern time_t seed;
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){
if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
return -1;
}
if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
return 1;
}
const unsigned char *a = (unsigned char*)((file*)file0)->file_name;
const unsigned char *b = (unsigned char*)((file*)file1)->file_name;
long parsed_number0 = 0;
long parsed_number1 = 0;
char is_num = 0;
char result = 0;
do {
if ((*a <= '9') && (*a >= '0')) {
parsed_number0 = 0;
do {
parsed_number0 = (parsed_number0 * 10) + (*a - '0');
a++;
} while((*a <= '9') && (*a >= '0'));
is_num |= 1;
}
if ((*b <= '9') && (*b >= '0')) {
parsed_number1 = 0;
do {
parsed_number1 = (parsed_number1 * 10) + (*b - '0');
b++;
} while((*b <= '9') && (*b >= '0'));
is_num |= 2;
}
if (is_num) {
if (is_num == 1) {
if (*b > '9') {
result = -1;
} else {
result = 1;
}
break;
} else if (is_num == 2) {
if (*a > '9') {
result = 1;
} else {
result = -1;
}
break;
} else {
if (parsed_number0 > parsed_number1) {
result = 1;
break;
} else if (parsed_number0 < parsed_number1) {
result = -1;
break;
}
}
/* those breaks are not set here, due to the possibillity that both numbers are equal
* in which case the comparison should continue */
is_num = 0;
}
unsigned char aa = ((*a >= 'A') && (*a <= 'Z')) ? (*a | ' ') : *a;
unsigned char bb = ((*b >= 'A') && (*b <= 'Z')) ? (*b | ' ') : *b;
/*using a simple aa - bb would occasionaly cause underflows with wide chars*/
result = ((aa == bb) ? 0 : ((aa > bb) ? 1 : -1 ));
a++;
b++;
} while (result == 0);
return result;
}
int sort_alpha(const void *file0, const void *file1){
if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
return -1;
}
if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
return 1;
}
return strcmp(((file*)file0)->file_name, ((file*)file1)->file_name);
}
int sort_random(const void *file0, const void *file1){
if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
return -1;
}
if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
return 1;
}
/* seed is the only value that actually changes whenever this funcion is called.
* seed only updates when the order_by binding is executed.
* this means that the random output only changes on the command of the user, and not SETTINGS_RELOAD_DIR_DELTA, this is intentional.
* well the other stuff like file sizes and file count in a dir may also change, but this needs user intervention too.
* that is unless the user decides to sort random in /var/log or something, which begs the question: why would you do that?
*/
time_t num = (((time_t)&sort_random) + (time_t)seed) ^ (((file*)file0)->file_size + ((file*)file1)->file_size);
int i;
for (i = 1; i < (((time_t)&getpid)%3); i++) {
num ^= num << ((((time_t)&file0)%27)+i);
num ^= num >> ((((time_t)&file1)%18)+i);
num ^= ((time_t)&getpid) ^ (((time_t)&getpid) >> ((time_t)getpid())%10);
}
return (((num & (3 << seed%16)) >> seed%16) - 1);
/*return ((num%3) - 1);*/
}
int sort_type(const void *file0, const void *file1){
if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
return -1;
}
if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
return 1;
}
if (((file*)file0)->file_type > ((file*)file1)->file_type) {
return -1;
} else if (((file*)file0)->file_type < ((file*)file1)->file_type) {
return 1;
} else {
return sort_natural(file0, file1);
}
}
int sort_size(const void *file0, const void *file1){
if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
return -1;
}
if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
return 1;
}
if (((file*)file0)->file_size > ((file*)file1)->file_size) {
return -1;
} else if (((file*)file0)->file_size < ((file*)file1)->file_size) {
return 1;
} else {
return sort_natural(file0, file1);
}
}
int sort_extension(const void *file0, const void *file1){
if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
return -1;
}
if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
return 1;
}
char *extension0 = strrchr(((file*)file0)->file_name, '.');
char *extension1 = strrchr(((file*)file1)->file_name, '.');
if (extension0 && extension1) {
if ((strcmp(extension0, extension1)) == 0) {
return sort_natural(file0, file1);
} else {
file f0;
file f1;
memcpy(&f0, file0, sizeof(file));
memcpy(&f1, file1, sizeof(file));
f0.file_name = extension0;
f1.file_name = extension1;
return sort_natural(&f0, &f1);
}
} else if (extension0 != NULL && extension1 == NULL) {
return 1;
} else if (extension0 == NULL && extension1 != NULL) {
return -1;
} else {
return sort_natural(file0, file1);
}
}
-18
View File
@@ -1,18 +0,0 @@
#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);
-119
View File
@@ -1,119 +0,0 @@
#include <curses.h>
#ifndef BACKEND_GUARD
#define BACKEND_GUARD
#include "defines.h"
#define SETTINGS_COMMAND_REPLACE_STR "%"
typedef struct String {
char *data;
size_t size;
} string;
#define s(str) { \
return (string) { \
.data = str; \
.size = strlen(str); \
} \
}
#define concat(out, s1, s2, _free) { \
concat## _free(out, s1, s2); \
}
#define concat0(out, s1, s2) \
char *result = malloc(strlen(s1) + strlen(s2) + 1); \
memcpy(result, s1, strlen(s1)); \
memcpy(result + strlen(s1), s2, strlen(s2) + 1); \
out = result;
#define concat1(out, s1, s2) \
char *result = malloc(strlen(s1) + strlen(s2) + 1); \
memcpy(result, s1, strlen(s1)); \
memcpy(result + strlen(s1), s2, strlen(s2) + 1); \
free(s1); \
out = result;
#define concat2(out, s1, s2) \
char *result = malloc(strlen(s1) + strlen(s2) + 1); \
memcpy(result, s1, strlen(s1)); \
memcpy(result + strlen(s1), s2, strlen(s2) + 1); \
free(s2); \
out = result;
#define concat3(out, s1, s2) \
char *result = malloc(strlen(s1) + strlen(s2) + 1); \
memcpy(result, s1, strlen(s1)); \
memcpy(result + strlen(s1), s2, strlen(s2) + 1); \
free(s1); \
free(s2); \
out = result;
/*
int s_strcmp(const char *s0, char string *s1) {
size_t size = s1->size - s0->size;
if (size) {
return size;
}
size = s0.size;
while (*s0->data == *s0->data && size) {
s0->data += 1;
s1->data += 1;
size--;
}
return size;
}
*/
char* smartstrcasestr(const char *haystack, const char *needle){
char smart = 0;
char *ret;
char passes = 0;
while (*needle) {
if (*needle >= 'A' && *needle <= 'Z') {
smart = 1;
break;
}
passes++;
needle++;
}
needle -= passes;
if (smart == 0) {
char *needle_case = malloc(strlen(needle)+1);
memcpy(needle_case, needle, strlen(needle)+1);
passes = 0;
while (*needle_case) {
*needle_case = *needle_case | ' ';
needle_case++;
passes++;
}
needle_case -= passes;
char *haystack_case = malloc(strlen(haystack)+1);
memcpy(haystack_case, haystack, strlen(haystack)+1);
passes = 0;
while (*haystack_case) {
*haystack_case = *haystack_case | ' ';
haystack_case++;
passes++;
}
haystack_case -= passes;
ret = strstr(haystack_case, needle_case);
free(needle_case);
free(haystack_case);
} else {
ret = strstr(haystack, needle);
}
return ret;
}
#endif
/*char* concat(const char *s1, const char *s2);*/
char* smartstrcasestr(const char *haystack, const char *needle);
-467
View File
@@ -1,467 +0,0 @@
#include <curses.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include "config.h"
#include "dir.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_render;
pthread_cond_t cond_mid;
pthread_cond_t cond_rgt;
pthread_cond_t cond_lft;
pthread_cond_t cond_top;
pthread_cond_t cond_btm;
dir rgt_dir;
dir mid_dir;
dir lft_dir;
char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used for directory previews */
char *btm_buffer;
char *top_buffer; /* current path */
extern WINDOW *win_t;
extern WINDOW *win_b;
extern WINDOW *win_l;
extern WINDOW *win_m;
extern WINDOW *win_r;
unsigned long top_width;
extern unsigned int terminal_width;
extern unsigned int status;
extern char *global_path;
unsigned int btm_status;
void *thread_mid(){
dir tmp;
tmp.current_file = NULL;
tmp.file_list = NULL;
tmp.file_count = 0;
while(!(status & STATUS_QUIT_PROGRAM)){
pthread_mutex_lock(&mutex_mid);
pthread_cond_wait(&cond_mid, &mutex_mid);
if (global_path == NULL) {
mid_dir.current_file = NULL;
mid_dir.file_count = 0;
pthread_mutex_unlock(&mutex_mid);
continue;
}
char *path = malloc(strlen(global_path)+1);
memcpy(path, global_path, strlen(global_path)+1);
if (status & STATUS_RELOAD_DIRECTORY) {
long index = mid_dir.current_file - mid_dir.file_list;
tmp = mid_dir;
mid_dir.file_count = get_dir_size(path);
mid_dir.file_list = malloc(mid_dir.file_count * sizeof(file));
if (mid_dir.file_count) { /* fails if dir empty */
get_dir_content(path, &mid_dir);
mid_dir.current_file = &mid_dir.file_list[index];
} else { /* the hovered dir is empty */
mid_dir.current_file = NULL;
}
unsigned long i;
for(i = 0; i < mid_dir.file_count && i < tmp.file_count; i++) {
mid_dir.file_list[i].status = tmp.file_list[i].status;
}
}
if (mid_dir.current_file > mid_dir.file_list + mid_dir.file_count - 1) {
mid_dir.current_file = mid_dir.file_list + mid_dir.file_count - 1;
}
if (mid_dir.current_file < mid_dir.file_list) {
mid_dir.current_file = mid_dir.file_list;
}
pthread_mutex_unlock(&mutex_mid);
pthread_cond_signal(&cond_top);
pthread_cond_signal(&cond_btm);
pthread_cond_signal(&cond_rgt);
/* rendering */
pthread_mutex_lock(&mutex_render);
werase(win_m);
if (mid_dir.file_count == 0) {
mvwaddstr(win_m, 0, 0, "empty");
} else {
print_dir(win_m, 1, &mid_dir);
}
wnoutrefresh(win_m);
pthread_mutex_unlock(&mutex_render);
unsigned long i;
for (i = 0; i < tmp.file_count; i++) {
free(tmp.file_list[i].file_name);
}
free(tmp.file_list);
tmp.current_file = NULL;
tmp.file_list = NULL;
tmp.file_count = 0;
}
pthread_exit(0);
}
void *thread_lft(){
while(!(status & STATUS_QUIT_PROGRAM)){
pthread_mutex_lock(&mutex_lft);
pthread_cond_wait(&cond_lft, &mutex_lft);
if (global_path == NULL) {
lft_dir.current_file = NULL;
lft_dir.file_count = 0;
pthread_mutex_unlock(&mutex_lft);
continue;
}
char *path = malloc(strlen(global_path)+1);
memcpy(path, global_path, strlen(global_path)+1);
if (strcmp(path, "/") != 0) {
path[strrchr(path, '/')-path+1] = '\0';
path[strrchr(path, '/')-path] = '\0';
path[0] = '/';
if (status & STATUS_RELOAD_DIRECTORY) {
unsigned long i = 0;
for (i = 0; i < lft_dir.file_count; i++) {
free(lft_dir.file_list[i].file_name);
}
free(lft_dir.file_list);
lft_dir.file_count = get_dir_size(path);
if (lft_dir.file_count != 0) {
lft_dir.file_list = malloc(lft_dir.file_count * sizeof(file));
get_dir_content(path, &lft_dir);
} else {
lft_dir.current_file = NULL;
lft_dir.file_count = 0;
}
}
} else {
lft_dir.file_count = 0;
}
/* rendering */
pthread_mutex_lock(&mutex_render);
werase(win_l);
print_dir(win_l, 0, &lft_dir);
wnoutrefresh(win_l);
pthread_mutex_unlock(&mutex_render);
pthread_mutex_unlock(&mutex_lft);
free(path);
}
pthread_exit(0);
}
void *thread_rgt(){
dir tmp;
tmp.current_file = NULL;
tmp.file_list = NULL;
tmp.file_count = 0;
while(!(status & STATUS_QUIT_PROGRAM)){
pthread_mutex_lock(&mutex_rgt);
pthread_cond_wait(&cond_rgt, &mutex_rgt);
pthread_mutex_lock(&mutex_mid);
char *file_name = malloc(strlen(mid_dir.current_file->file_name)+1);
file_name = memcpy(file_name, mid_dir.current_file->file_name, strlen(mid_dir.current_file->file_name)+1);
rgt_dir.file_list = malloc(sizeof(file));
memcpy(rgt_dir.file_list, mid_dir.current_file, sizeof(file));
rgt_dir.file_list->file_name = file_name;
rgt_dir.current_file = rgt_dir.file_list;
rgt_dir.file_count = 1;
pthread_mutex_unlock(&mutex_mid);
if (rgt_dir.current_file->permissions & S_IRUSR) {
if (rgt_dir.current_file->file_type & FILE_TYPE_DIR) {
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
images_clear();
#endif
tmp = rgt_dir;
rgt_dir.file_count = get_dir_size(rgt_dir.current_file->file_name);
rgt_dir.file_list = malloc(rgt_dir.file_count * sizeof(file));
if (rgt_dir.file_count) { /* fails if dir empty */
get_dir_content(tmp.current_file->file_name, &rgt_dir);
} else { /* the hovered dir is empty */
rgt_dir.current_file = NULL;
}
pthread_mutex_lock(&mutex_render);
werase(win_r);
print_dir(win_r, 0, &rgt_dir);
wnoutrefresh(win_r);
pthread_mutex_unlock(&mutex_render);
} else if ((status & STATUS_DELTA_TIME) != STATUS_DELTA_TIME && rgt_dir.file_count > 0) {
free(rgt_buffer);
rgt_buffer = preview_file(rgt_dir.current_file);
rgt_dir.current_file->file_type |= FILE_TYPE_OPEN_FILE;
pthread_mutex_lock(&mutex_render);
werase(win_r);
if (rgt_dir.current_file->file_type & FILE_TYPE_OPEN_FILE) {
mvwaddnstr(win_r, 0, 0, rgt_buffer, (terminal_width/2) * terminal_width);
} else if ((rgt_dir.current_file->permissions & S_IRUSR) == 0) {
mvwaddstr(win_r, 0, 0, "not accessible");
}
wnoutrefresh(win_r);
pthread_mutex_unlock(&mutex_render);
}
}
pthread_mutex_unlock(&mutex_rgt);
unsigned long i;
for (i = 0; i < tmp.file_count; i++) {
free(tmp.file_list[i].file_name);
}
free(tmp.file_list);
tmp.current_file = NULL;
tmp.file_list = NULL;
tmp.file_count = 0;
}
pthread_exit(0);
}
void *thread_top(){
while(!(status & STATUS_QUIT_PROGRAM)){
pthread_mutex_lock(&mutex_top);
pthread_cond_wait(&cond_top, &mutex_top);
free(top_buffer);
if(global_path == NULL) {
top_buffer = malloc(sizeof("cannot open directory"));
top_width = sizeof("cannot open directory");
top_buffer = "cannot open directory";
pthread_mutex_unlock(&mutex_top);
continue;
}
pthread_mutex_lock(&mutex_mid);
top_buffer = malloc(strlen(global_path)+1 + strlen(mid_dir.current_file->file_name)+1);
memcpy(top_buffer, global_path, strlen(global_path));
memcpy(top_buffer + strlen(global_path) + 1, mid_dir.current_file->file_name, strlen(mid_dir.current_file->file_name)+1);
pthread_mutex_unlock(&mutex_mid);
top_buffer[strlen(global_path)] = '/';
top_width = strlen(top_buffer);
/* rendering */
pthread_mutex_lock(&mutex_render);
werase(win_t);
wattron(win_t, COLOR_PAIR(COLOR_PATH));
mvwaddnstr(win_t, 0, 0, top_buffer, strlen(global_path)+1);
wattroff(win_t, COLOR_PAIR(COLOR_PATH));
mvwaddstr(win_t, 0, strlen(global_path)+1, top_buffer+strlen(global_path)+1);
wnoutrefresh(win_t);
pthread_mutex_unlock(&mutex_render);
pthread_mutex_unlock(&mutex_top);
}
pthread_exit(0);
}
void *thread_btm(){
char *path = NULL;
char *ui_btm_right_block = malloc(sizeof(char));
unsigned int ui_btm_right_block_size = 0;
unsigned int buffer_width = 0;
while(!(status & STATUS_QUIT_PROGRAM)){
pthread_mutex_lock(&mutex_btm);
pthread_cond_wait(&cond_btm, &mutex_btm);
if (status & (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY)) {
/*{{{ parse storage info; the fold of shame*/
pthread_mutex_lock(&mutex_mid);
unsigned long i;
float total_dir_size = 0;
for(i = 0; i < mid_dir.file_count; i++) {
if ((mid_dir.file_list[i].file_type & (FILE_TYPE_DIR)) != FILE_TYPE_DIR) {
total_dir_size += mid_dir.file_list[i].file_size;
}
}
pthread_mutex_unlock(&mutex_mid);
free(ui_btm_right_block);
if(path) {
free(path);
}
path = getcwd(NULL, 0);
struct statvfs fs;
statvfs(path, &fs);
float disk_size_free = fs.f_bsize * fs.f_bavail;
float parsed_number[2] = { 0 };
char size_index[2] = { 0 };
if (total_dir_size > 1) {
size_index[0] = -1;
while (total_dir_size > 1 && size_index[0] < size_unit_count) {
parsed_number[0]=total_dir_size;
size_index[0]++;
total_dir_size /= 1024;
}
} else {
size_index[0] = 0;
parsed_number[0] = 0;
}
if (disk_size_free > 1) {
size_index[1] = -1;
while (disk_size_free > 1 && size_index[1] < size_unit_count) {
parsed_number[1]=disk_size_free;
size_index[1]++;
disk_size_free /= 1024;
}
} else {
size_index[1] = 0;
}
if (size_index[0] > 0 && size_index[1] > 0) {
ui_btm_right_block_size = snprintf(NULL, 0, "%0.2lf%c %s %0.2lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
ui_btm_right_block = malloc(ui_btm_right_block_size);
sprintf(ui_btm_right_block, "%0.2lf%c %s %0.2lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
} else if (size_index[0] <= 0 && size_index[1] > 0) {
ui_btm_right_block_size = snprintf(NULL, 0, "%0.0lf%c %s %0.2lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
ui_btm_right_block = malloc(ui_btm_right_block_size);
sprintf(ui_btm_right_block, "%0.0lf%c %s %0.2lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
} else if (size_index[0] > 0 && size_index[1] <= 0) {
ui_btm_right_block_size = snprintf(NULL, 0, "%0.2lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
ui_btm_right_block = malloc(ui_btm_right_block_size);
sprintf(ui_btm_right_block, "%0.2lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
} else {
ui_btm_right_block_size = snprintf(NULL, 0, "%0.0lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
ui_btm_right_block = malloc(ui_btm_right_block_size);
sprintf(ui_btm_right_block, "%0.0lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
}
/*}}}*/
}
if (buffer_width != terminal_width) {
buffer_width = terminal_width;
free(btm_buffer);
btm_buffer = malloc(buffer_width+1);
}
memset(btm_buffer, ' ', buffer_width);
btm_buffer[buffer_width] = '\0';
memcpy(btm_buffer + buffer_width - ui_btm_right_block_size, ui_btm_right_block, ui_btm_right_block_size);
btm_buffer[0] = (S_ISLNK(mid_dir.current_file->permissions)) ? 'l':
(S_ISDIR(mid_dir.current_file->permissions) ? 'd': '-');
btm_buffer[1] = (mid_dir.current_file->permissions & S_IRUSR) ? 'r' : '-';
btm_buffer[2] = (mid_dir.current_file->permissions & S_IWUSR) ? 'w' : '-';
btm_buffer[3] = (mid_dir.current_file->permissions & S_IXUSR) ? 'x' : '-';
btm_buffer[4] = (mid_dir.current_file->permissions & S_IRGRP) ? 'r' : '-';
btm_buffer[5] = (mid_dir.current_file->permissions & S_IWGRP) ? 'w' : '-';
btm_buffer[6] = (mid_dir.current_file->permissions & S_IXGRP) ? 'x' : '-';
btm_buffer[7] = (mid_dir.current_file->permissions & S_IROTH) ? 'r' : '-';
btm_buffer[8] = (mid_dir.current_file->permissions & S_IWOTH) ? 'w' : '-';
btm_buffer[9] = (mid_dir.current_file->permissions & S_IXOTH) ? 'x' : '-';
/* rendering */
pthread_mutex_lock(&mutex_render);
werase(win_b);
if (*top_buffer != ' ') { /*printing ' ' (standard initialized value, see threading_init) makes valgrind throw a fuss*/
mvwprintw(win_b, 0, 0, "%s", btm_buffer);
}
wnoutrefresh(win_b);
pthread_mutex_unlock(&mutex_render);
pthread_mutex_unlock(&mutex_btm);
/*the printing of all possible inputs are done in user_interactions */
pthread_mutex_unlock(&mutex_btm);
}
pthread_exit(0);
}
void threading_init(){
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));
pthread_mutex_init(&mutex_top, NULL);
pthread_mutex_init(&mutex_mid, NULL);
pthread_mutex_init(&mutex_lft, NULL);
pthread_mutex_init(&mutex_btm, NULL);
pthread_mutex_init(&mutex_rgt, NULL);
pthread_mutex_init(&mutex_render, NULL);
pthread_cond_init(&cond_rgt, NULL);
pthread_cond_init(&cond_lft, NULL);
pthread_cond_init(&cond_mid, NULL);
pthread_cond_init(&cond_top, NULL);
pthread_cond_init(&cond_btm, NULL);
}
void threading_free(){
free(top_buffer);
pthread_mutex_destroy(&mutex_top);
pthread_mutex_destroy(&mutex_mid);
pthread_mutex_destroy(&mutex_lft);
}
-14
View File
@@ -1,14 +0,0 @@
#include <curses.h>
#ifndef THREADING_GUARD
#define THREADING_GUARD
#include "threading.c"
#endif
void *thread_lft();
void *thread_mid();
void *thread_rgt();
void *thread_top();
void *thread_btm();
void threading_init();
void threading_free();
-118
View File
@@ -1,118 +0,0 @@
#include <curses.h>
#include <pthread.h>
#include <unistd.h>
#include "defines.h"
#include "dir.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 dir rgt_dir;
extern dir mid_dir;
extern dir lft_dir;
extern char *top_buffer;
extern char *rgt_buffer;
extern char *btm_buffer;
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){
if (pthread_mutex_trylock(&mutex_top) == 0) {
if (*top_buffer != ' ') { /*printing ' ' (standard initialized value, see threading_init) makes valgrind throw a fuss*/
wattron(win, COLOR_PAIR(COLOR_PATH));
mvwaddstr(win, 0, 0, top_buffer);
mvwaddch(win, 0, strlen(top_buffer), '/');
wattroff(win, COLOR_PAIR(COLOR_PATH));
if (mid_dir.file_count != 0) {
mvwaddstr(win, 0, strlen(top_buffer)+1, mid_dir.current_file->file_name);
}
}
pthread_mutex_unlock(&mutex_top);
} else {
mvwaddstr(win, 0, terminal_width/2, "LOADING");
/*
status |= STATUS_UPDATE_SCREEN_GENERIC;
*/
}
}
void window_btm(WINDOW *win){
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 {
mvwaddstr(win, 0, terminal_width/2, "LOADING");
/*
status |= STATUS_UPDATE_SCREEN_GENERIC;
*/
}
}
void window_lft(WINDOW *win){
if (pthread_mutex_trylock(&mutex_lft) == 0) {
print_dir(win, 0, &lft_dir);
pthread_mutex_unlock(&mutex_lft);
} else {
mvwaddstr(win, terminal_height/2, terminal_width/8, "LOADING");
/*
status |= STATUS_UPDATE_SCREEN_GENERIC;
*/
}
}
void window_mid(WINDOW *win){
if (pthread_mutex_trylock(&mutex_mid) == 0) {
if (mid_dir.file_count == 0) {
mvwaddstr(win, 0, 0, "empty");
} else {
print_dir(win, 1, &mid_dir);
}
pthread_mutex_unlock(&mutex_mid);
} else {
mvwaddstr(win, terminal_height/2, terminal_width/4, "LOADING");
/*
status |= STATUS_UPDATE_SCREEN_GENERIC;
*/
}
}
void window_rgt(WINDOW *win){
if (pthread_mutex_trylock(&mutex_rgt) == 0) {
if (!rgt_dir.current_file) {
mvwaddstr(win, 0, 0, "not accessible");
}else if (rgt_dir.current_file->file_type == FILE_TYPE_OPEN_FILE) {
mvwaddnstr(win, 0, 0, rgt_buffer, (terminal_width/2) * terminal_width);
} else if ((rgt_dir.current_file->permissions & S_IRUSR) == 0) {
mvwaddstr(win, 0, 0, "not accessible");
} else {
print_dir(win, 0, &rgt_dir);
}
pthread_mutex_unlock(&mutex_rgt);
} else {
mvwaddstr(win, terminal_height/2, terminal_width/4, "LOADING");
/*
status |= STATUS_UPDATE_SCREEN_GENERIC;
*/
}
}
-7
View File
@@ -1,7 +0,0 @@
#include "window.c"
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);