Compare commits

..
1 Commits
Author SHA1 Message Date
nova 72189ba5c7 Initial commit 2025-03-03 11:52:14 +01:00
21 changed files with 0 additions and 3011 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 -lmagic
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)
-32
View File
@@ -1,34 +1,2 @@
# th
~~~text
/home/toaster/src/desktop/c/th/README.md
th 15 backend.c 3.22 K # th
14 backend.h 253 B a filemanager born from spite
13 colors.c 3.54 K
12 colors.h 41 B qwert* user should adjust
11 config.h 8.07 K bindings in config.h, lol
10 defines.h 2.82 K
9 dir.c 11.05 K build instructions:
8 dir.h 367 B ``make``
7 file_prev~.c 2.68 K
6 file_previ~.h 297 B dependencies:
5 interact~.c 17.79 K libc
4 interactio~.h 900 B a c compiler
3 LICENSE 33.76 K make
2 main.c 5.00 K ncurses
1 Makefile 709 B pthreads
15 README.md 1.39 K libmagic
1 sorting.c 5.62 K ueberzugpp (optional)
2 sorting.h 633 B stuff in config.h
3 string.h 2.39 K
4 threadin~.c 14.11 K note:
5 threading.h 246 B th does as little checking
as possible; it is highly
insecure.
example: if you dont have
ueberzugpp installed,
and hover a picture without
disabling it in config.h,
th just crashes
-rw-r--r-- 176.03K sum of dir, 23.89G total free
-173
View File
@@ -1,173 +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, char *file_name){
char *out;
if (file_name == NULL) {
return NULL;
}
const char *offset = strstr(cmd, SETTINGS_COMMAND_REPLACE_STR);
int count = 0;
char *pos;
unsigned long i = 0;
while(file_name[i]) {
if (file_name[i] == '\'') {
count++;
}
i++;
}
out = malloc(strlen(cmd) + 1 + (strlen(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(file_name[i]) {
if (file_name[i] == '\'') {
*pos++ = '\'';
*pos++ = '\\';
*pos++ = '\'';
}
*pos = 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, char *file_name);
-126
View File
@@ -1,126 +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 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()) {
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();
-163
View File
@@ -1,163 +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 char copy_cmd[] = "cp -r";
static const char cut_cmd[] = "mv";
static const char del_cmd[] = "rm -r";
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", copy_file, COPY_FILE, "copy/yank file/directory" },
{ "dd", copy_file, CUT_FILE, "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, "$TH_START_PATH", "jump to $TH_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 char ui_search_text[] = "/"; /*unlike the other ui, this one doesnt automatically add a space*/
/* {{{ */
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[];
static const char copy_cmd[];
static const char cut_cmd[];
static const char del_cmd[];
static const char ui_search_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_SCREEN_RESIZE 8
#define STATUS_UPDATE_SCREEN_CLEAR 16
#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 COPY_FILE (int*)0x0001
#define CUT_FILE (int*)0x0002
#define BTM_WINDOW_HEIGHT_ON_STR_INTERACTION 5
#define INPUT_BUFFER_SIZE 255
#define ignore_return(in) if (in) { };
#define unused(in) (void)(in);
#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 {
int *status; /*pointer to make blackmagic easier, should probably not deref lol*/
char **list;
unsigned long count;
} yank;
typedef struct Linked_dir {
char *path;
unsigned long index;
struct Linked_dir *next;
} linked_dir;
#endif
-379
View File
@@ -1,379 +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 SETTINGS_LINE_NUMBERS != 0
if (dir->file_count > 9) {
unsigned long dir_file_count_ = dir->file_count;
while(dir_file_count_ > 9) {
offset_front++;
dir_file_count_ /= 10;
}
}
#endif
/* scrolling of the directory if too many files are in a dir */
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 == size_unit[0]) {
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) {
wattron(win, COLOR_PAIR(8)); /* FIFO, as seen in colors.c */
is_selected = 1;
} else {
wattron(win, COLOR_PAIR(dir->file_list[i].color_pair));
is_selected = 0;
}
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, ' ');
}
}
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 == size_unit[0]) {
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);
}
wattrset(win, 0);
/*
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 deselect_all_files() {
unsigned long i;
for (i = 0; i < mid_dir.file_count; i++) {
mid_dir.file_list[i].status &= ~FILE_STATUS_SELECTED;
}
}
void change_dir(char *new_path){
deselect_all_files();
current_linked_dir = list_beginning;
while (current_linked_dir->next != NULL) {
if(strcmp(current_linked_dir->path, global_path) == 0) {
break;
} else {
current_linked_dir = current_linked_dir->next;
}
}
if(strcmp(current_linked_dir->path, global_path) == 0) {
current_linked_dir->index = mid_dir.current_file - mid_dir.file_list;
}
if (chdir(new_path) != 0) {
/* i have made the decission that this is a case that i do not want to handle
* god may help me if this ever fails, cuz damn thatll be hard to debug
* especially in like 2 years when i even forgot about this possibility */
return;
}
free(global_path);
global_path = getcwd(NULL, 0);
current_linked_dir = list_beginning;
while (current_linked_dir->next != NULL) {
if(strcmp(current_linked_dir->path, global_path) == 0) {
break;
} else {
current_linked_dir = current_linked_dir->next;
}
}
if(strcmp(current_linked_dir->path, global_path) != 0) {
current_linked_dir->next = malloc(sizeof(linked_dir));
current_linked_dir = current_linked_dir->next;
current_linked_dir->path = malloc(strlen(global_path)+1);
memcpy(current_linked_dir->path, global_path, strlen(global_path)+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];
}
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;
}
-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 deselect_all_files();
void dir_init();
-121
View File
@@ -1,121 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <magic.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;
extern char *global_path;
char previewd;
magic_t cookie_type;
magic_t cookie_description;
char* text(file *f);
void images_print(char *file_name);
void images_clear();
char* generic(file *f);
char* get_mimetype(file *f){
char *mime = (char*)magic_file(cookie_type, f->file_name); /*freeing this causes th to crash*/
return mime;
}
char* preview_file(file *f){
char *file_buffer;
char *mime = get_mimetype(f);
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
images_clear();
#endif
if (mime != NULL) {
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);
}
} else {
file_buffer = generic(f);
}
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){
char *mime = (char*)magic_file(cookie_description, f->file_name);
char *buffer = malloc(strlen(mime)+1);
memcpy(buffer, mime, strlen(mime)+1);
return buffer;
}
#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){
fprintf(ueberzug, "{\"action\":\"add\", \
\"identifier\":\"preview\", \
\"scaler\":\"fit_contain\", \
\"max_height\":%d, \
\"max_width\":%d, \
\"y\":0, \
\"x\":%d, \
\"path\":\"%s/%s\"}\n", terminal_height, terminal_width/2, terminal_width/2, global_path, file_name);
fflush(ueberzug);
}
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
void file_preview_init(){
cookie_type = magic_open(MAGIC_MIME_TYPE);
cookie_description = magic_open(MAGIC_NONE);
magic_load(cookie_type, NULL);
magic_load(cookie_description, NULL);
}
-14
View File
@@ -1,14 +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 file_preview_init();
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
void ueberzug_init();
void ueberzug_close();
#endif
-741
View File
@@ -1,741 +0,0 @@
#include <curses.h>
#include <pthread.h>
#include <dirent.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 *global_path;
extern char *input;
extern time_t seed;
char search_buffer[INPUT_BUFFER_SIZE];
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);
#define skip_if_empty_dir \
if (mid_dir.current_file == NULL) { \
return; \
}
void user_interactions() {
char ch;
unsigned long i;
unsigned long binding_matches = 0;
static unsigned int input_pass;
ch = getch();
if(ch != (char)ERR) {
input[input_pass] = ch;
input_pass++;
if (ch == 27) { /* esc key */
memset(input, 0, INPUT_BUFFER_SIZE);
input_pass = 0;
}
if (input_pass >= INPUT_BUFFER_SIZE) {
input_pass = INPUT_BUFFER_SIZE - 1;
}
}
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) {
/* strlen is 0 every iteration nothing is pressed
* should this be input in the strncmp, it always succeeds.
* this results in all possible bindings being printed at all times */
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() */
#if SETTINGS_RELOAD_DIR_DELTA != 0
/* workaround for file previews not working on changing file while this is true
* disabling this here does not influence the reload/rerun of the logic */
status &= ~STATUS_DELTA_TIME;
#endif
} 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;
}
}
char read_string_loop(WINDOW *win, char *buffer, char *nullable_return_char, unsigned long *pass, unsigned long y, unsigned long x) {
char ch = wgetch(win_b);
if (nullable_return_char != NULL) {
*nullable_return_char = 0;
}
if (ch == '\n' ) {
buffer[*pass] = 0;
return 1;
} else if ( ch == 27 /* esc key */) {
buffer[*pass] = 0;
return 2;
} else if (ch == '\t') { /* tab */
memcpy(buffer + *pass, mid_dir.current_file->file_name, strlen(mid_dir.current_file->file_name));
mvwaddstr(win_b, 0, x+ *pass, mid_dir.current_file->file_name);
*pass += strlen(mid_dir.current_file->file_name);
} else if (ch == 127) { /* backspace */
if (*pass > 0) {
unsigned long i = x;
while (i < terminal_width) {
mvwaddch(win, y, i, ' ');
i++;
}
*pass -= 1;
buffer[*pass] = 0;
mvwaddstr(win, y, x, buffer);
}
} else if (ch) {
if (nullable_return_char != NULL) {
*nullable_return_char = ch;
}
mvwaddch(win_b, 0, x + *pass, ch);
buffer[*pass] = ch;
*pass += 1;
}
if (*pass >= INPUT_BUFFER_SIZE) {
*pass = INPUT_BUFFER_SIZE - 1;
}
return 0;
}
void quit_program(){
status = STATUS_QUIT_PROGRAM;
}
void update(){
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_CLEAR | 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){
skip_if_empty_dir;
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){
skip_if_empty_dir;
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(){
skip_if_empty_dir
char *cmd = NULL;
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) {
/* if an .exe is executable, this branch will trigger as expected,
* however since executing an .exe without wine causes a exec format error, we should check if the program you tried to access is that
* if an .exe is not executable, it will get to the top else branch, then hit the comparison in the file extension area, and succeed also.
* that is unless the user removed wine out of the file_extension_default_cmd,
* in which case im going to assume wine isnt installed anyways as that would require:
* a. thought on my end in how to catch wine being removed in the config
* b. the user having deliberately changed the default config (good, thats why they are easily configurable)
* c. if wine is not installed on the system, executing an .exe would fail regardless of via direct execution or through wine */
char *extension = mid_dir.current_file->file_name;
while (extension <= mid_dir.current_file->file_name + strlen(mid_dir.current_file->file_name)) {
if (*extension == '.') {
break;
}
extension++;
}
if (strcmp(extension, ".exe") == 0) {
cmd = parse_cmd("wine ./"SETTINGS_COMMAND_REPLACE_STR, mid_dir.current_file->file_name);
} else {
cmd = parse_cmd("./"SETTINGS_COMMAND_REPLACE_STR, mid_dir.current_file->file_name);
}
ignore_return(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->file_name);
pid_t pid = fork();
if (pid == 0 && setsid()) {
ignore_return(system(cmd));
status = STATUS_QUIT_PROGRAM;
exit(1);
}
} else {
cmd = parse_cmd(file_extension_default_cmd[i].command, mid_dir.current_file->file_name);
ignore_return(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->file_name);
pid_t pid = fork();
if (pid == 0 && setsid()) {
ignore_return(system(cmd));
status = STATUS_QUIT_PROGRAM;
exit(1);
}
} else {
cmd = parse_cmd(mimetype_default_cmd[i].command, mid_dir.current_file->file_name);
ignore_return(system(cmd));
}
status |= STATUS_MOVE_RIGHT_MATCH;
update();
break;
}
}
}
status &= ~STATUS_MOVE_RIGHT_MATCH;
}
free(cmd);
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->file_name);
mvwprintw(win_b, 0, 0, parsed_ui_text);
unsigned long pass = 0;
char ret;
while ((ret = read_string_loop(win_b, str, NULL, &pass, 0, strlen(parsed_ui_text)+1)) == 0) {
}
if (ret == 1) {
if (str[0] == SETTINGS_COMMAND_FORK) {
cmd = parse_cmd(str+1, mid_dir.current_file->file_name);
pid_t pid = fork();
if (pid == 0 && setsid()) {
ignore_return(system(cmd));
status = STATUS_QUIT_PROGRAM;
exit(1);
}
} else {
cmd = parse_cmd(str, mid_dir.current_file->file_name);
ignore_return(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);
char *new_file_name = malloc(INPUT_BUFFER_SIZE);
char *parsed_ui_text = parse_cmd(ui_rename_text, mid_dir.current_file->file_name);
mvwprintw(win_b, 0, 0, parsed_ui_text);
unsigned long pass = 0;
char ret;
while ((ret = read_string_loop(win_b, new_file_name, NULL, &pass, 0, strlen(parsed_ui_text)+1)) == 0) {
}
if (ret == 1) {
char *cmd0 = parse_cmd(rename_cmd, mid_dir.current_file->file_name);
char *cmd1 = parse_cmd(cmd0, new_file_name);
ignore_return(system(cmd1));
free(cmd0);
free(cmd1);
}
free(parsed_ui_text);
free(new_file_name);
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void delete(unsigned long passes){
unsigned long i;
unsigned long del_count = 0;
char *cmd;
for (i = 0; i < mid_dir.file_count; i++) {
if (mid_dir.file_list[i].status & FILE_STATUS_SELECTED) {
cmd = parse_cmd(del_cmd, mid_dir.file_list[i].file_name);
ignore_return(system(cmd));
free(cmd);
del_count++;
}
}
if (del_count == 0) {
for (i = 0; i < passes; i++) {
if (&mid_dir.current_file[i] > mid_dir.file_list + mid_dir.file_count) {
break;
}
cmd = parse_cmd(del_cmd, mid_dir.current_file[i].file_name);
ignore_return(system(cmd));
free(cmd);
}
}
deselect_all_files();
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void makedir(){
wclear(win_b);
char *file_name = malloc(INPUT_BUFFER_SIZE);
mvwprintw(win_b, 0, 0, ui_makedir_text);
unsigned long pass = 0;
char ret;
while ((ret = read_string_loop(win_b, file_name, NULL, &pass, 0, strlen(ui_makedir_text)+1)) == 0) {
}
if (ret == 1) {
char *cmd = parse_cmd(makedir_cmd, file_name);
ignore_return(system(cmd));
free(cmd);
}
free(file_name);
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void makefile(){
wclear(win_b);
char *file_name = malloc(INPUT_BUFFER_SIZE);
mvwprintw(win_b, 0, 0, ui_makefile_text);
unsigned long pass = 0;
char ret;
while ((ret = read_string_loop(win_b, file_name, NULL, &pass, 0, strlen(ui_makefile_text)+1)) == 0) {
}
if (ret == 1) {
char *cmd = parse_cmd(makefile_cmd, file_name);
ignore_return(system(cmd));
free(cmd);
}
free(file_name);
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void enter_shell(unsigned long passes, int index){
unused(passes);
endwin();\
echo();\
curs_set(1);\
ignore_return(system(key_binding[index].black_magic));
initscr(); /* start ncurses */
noecho(); /* hide keyboard input */
curs_set(0);
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void jump_to_dir(unsigned long passes, int index){
unused(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_CLEAR | STATUS_RELOAD_DIRECTORY );
}
void order_by(unsigned long passes, int index){
unused(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){
unused(passes);
char *cmd = parse_cmd(key_binding[index].black_magic, mid_dir.current_file->file_name);
ignore_return(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->file_name);
ignore_return(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->file_name);
ignore_return(system(cmd));
free(cmd);
free(tmp->file_name);
free(tmp);
status |= (STATUS_RUN_BACKEND);
}
void copy_file(unsigned long passes, int index){
unused(passes);
yank_files.status = key_binding[index].black_magic;
unsigned long i;
unsigned long j = 0;
if (yank_files.count != 0) {
for (i = 0; i < yank_files.count; i++) {
free(yank_files.list[i]);
}
free(yank_files.list);
yank_files.count = 0;
}
for (i = 0; i < mid_dir.file_count; i++) {
if (mid_dir.file_list[i].status & FILE_STATUS_SELECTED) {
yank_files.count++;
}
}
if (yank_files.count != 0) {
yank_files.list = malloc(yank_files.count * sizeof(char*));
for (i = 0; j < yank_files.count; i++) {
if (mid_dir.file_list[i].status & FILE_STATUS_SELECTED) {
char *str = malloc(strlen(global_path) + 1 + strlen(mid_dir.file_list[i].file_name)+1);
memcpy(str, global_path, strlen(global_path));
memcpy(str+strlen(global_path)+1, mid_dir.file_list[i].file_name, strlen(mid_dir.file_list[i].file_name)+1);
str[strlen(global_path)] = '/';
yank_files.list[j] = str;
j++;
}
}
} else {
/*this condition is achieved if no file is explicitly selected by the user, thus we assume the user wants the currently hovered file*/
yank_files.list = malloc(1 * sizeof(char*));
char *str = malloc(strlen(global_path) + 1 + strlen(mid_dir.current_file->file_name)+1);
memcpy(str, global_path, strlen(global_path));
memcpy(str+strlen(global_path)+1, mid_dir.current_file->file_name, strlen(mid_dir.current_file->file_name)+1);
str[strlen(global_path)] = '/';
*yank_files.list = str;
yank_files.count = 1;
}
status |= (STATUS_RUN_BACKEND);
}
void paste(unsigned long passes){
unsigned long i;
unsigned long j;
file tmp;
char *cmd;
for (j = 0; j < passes; j++) {
for (i = 0; i < yank_files.count; i++) {
char *file_name = strrchr(yank_files.list[i], '/')+1;
char *dest_file_name = malloc(strlen(file_name)+1);
memcpy(dest_file_name, file_name, strlen(file_name)+1);
tmp.file_name = yank_files.list[i];
while(access(dest_file_name, F_OK) == 0) {
unsigned long len = strlen(dest_file_name);
dest_file_name = realloc(dest_file_name, len+2);
dest_file_name[len] = '_';
dest_file_name[len+1] = 0;
}
if (yank_files.status == COPY_FILE) {
cmd = parse_cmd(copy_cmd, tmp.file_name);
} else if (yank_files.status == CUT_FILE) {
cmd = parse_cmd(cut_cmd, tmp.file_name);
} else {
free(dest_file_name);
continue;
}
tmp.file_name = dest_file_name;
cmd = parse_cmd(cmd, tmp.file_name);
ignore_return(system(cmd));
free(cmd);
free(tmp.file_name);
}
}
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
}
void search(){
echo();
unsigned long i;
for (i = 0; i < terminal_width -1; i++) {
mvwaddch(win_b, 0, i, ' ');
}
mvwaddstr(win_b, 0, 0, ui_search_text);
memset(search_buffer, 0, INPUT_BUFFER_SIZE);
curs_set(1);
echo();
timeout(-1); /* negative numbers block until enter is pressed */
unsigned long pass = 0;
char ch;
while (read_string_loop(win_b, search_buffer, &ch, &pass, 0, strlen(ui_search_text)) == 0) {
if (ch) {
unsigned long index = 0;
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);
wrefresh(win_m);
pthread_mutex_unlock(&mutex_render);
break;
}
}
}
}
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(){
char ch;
char err = 0;
char number_buffer[INPUT_BUFFER_SIZE];
char *num = number_buffer;
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 */
wmove(win_b, 0, 1);
while(1) {
ch = wgetch(win_b);
if (ch >= '0' && ch <= '9') {
*num = ch;
num++;
} else if (ch == '\n') {
break;
} else if (ch == 27) { /* esc key */
err = 1;
break;
} else if (ch == 127) { /* backspace */
num--;
if (num < number_buffer) {
num = number_buffer;
}
}
}
*num = 0;
num = number_buffer;
if (err == 0) {
unsigned long parsed_number = 0;
while(*num) {
parsed_number = (parsed_number * 10) + (*num - '0');
num++;
}
mid_dir.current_file = &mid_dir.file_list[parsed_number];
}
noecho();
curs_set(0);
status |= (STATUS_RUN_BACKEND);
}
-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 copy_file();
void paste();
void search();
void search_next();
void search_previous();
void jmp_file_index();
-203
View File
@@ -1,203 +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 "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 file_modifiers = 0;
unsigned int status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_CLEAR | STATUS_UPDATE_SCREEN_RESIZE | STATUS_RELOAD_DIRECTORY);
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);
/* initial render pass to speed first render up,
* yes this only skips SETTINGS_CURSES_TIMEOUT, which by default is 100ms,
* but i started to write th out of spite; because anything else either is too slow and unresponsive
* or i just dont like their ux.
* will i ever make use of the 100ms? will i ever react and do faster inputs? no
* does it feel more responsive? sure does */
pthread_cond_signal(&cond_mid);
render_pass();
while(!(status & STATUS_QUIT_PROGRAM)){
getmaxyx(stdscr, terminal_height, terminal_width);
if (status & STATUS_RUN_BACKEND) {
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_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);
status |= STATUS_UPDATE_SCREEN_CLEAR;
status &= ~STATUS_UPDATE_SCREEN_RESIZE;
}
if (status & STATUS_UPDATE_SCREEN_CLEAR) {
clear();
pthread_mutex_lock(&mutex_render);
refresh();
status &= ~STATUS_UPDATE_SCREEN_CLEAR;
pthread_mutex_unlock(&mutex_render);
}
/*
if (pthread_mutex_trylock(&mutex_render) == 0) {
doupdate();
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
file_preview_init();
ESCDELAY = 10;
global_path = getcwd(NULL, 0);
char *start_path = getcwd(NULL, 0);
setenv("TH_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;
signed 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);
-494
View File
@@ -1,494 +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;
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 previous_dir_state = { 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;
previous_dir_state = 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;
mid_dir.file_list = NULL;
}
unsigned long i;
for(i = 0; i < mid_dir.file_count && i < previous_dir_state.file_count; i++) {
mid_dir.file_list[i].status = previous_dir_state.file_list[i].status;
}
/* youd think that after the above previous_dir_state = mid_dir previous_dir_state.current_file is always within the bounds of the file_list,
* but no, it fucking is not for some reason
* however for some mysterious reason this condition doesnt fail. well it probably does,
* but that doesnt matter as it does what its supposed to do:
* keep the current file selected after a reload even if the dir contents change like a new file being added */
if (previous_dir_state.current_file != NULL && previous_dir_state.current_file < previous_dir_state.file_list + previous_dir_state.file_count) {
for (i = 0; i < mid_dir.file_count; i++) {
if (strcmp(mid_dir.file_list[i].file_name, previous_dir_state.file_list[index].file_name) == 0) {
mid_dir.current_file = &mid_dir.file_list[i];
break;
}
}
}
}
if (mid_dir.current_file != NULL) {
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);*/
wrefresh(win_m);
pthread_mutex_unlock(&mutex_render);
unsigned long i;
for (i = 0; i < previous_dir_state.file_count; i++) {
free(previous_dir_state.file_list[i].file_name);
}
free(previous_dir_state.file_list);
previous_dir_state.current_file = NULL;
previous_dir_state.file_list = NULL;
previous_dir_state.file_count = 0;
free(path);
}
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);*/
wrefresh(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);
if (mid_dir.current_file != NULL) {
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;
} else {
rgt_dir.current_file = NULL;
rgt_dir.file_list = NULL;
rgt_dir.file_count = 0;
}
pthread_mutex_unlock(&mutex_mid);
if (rgt_dir.current_file != NULL && 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);*/
wrefresh(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);*/
wrefresh(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);
pthread_mutex_lock(&mutex_mid);
if(global_path == NULL) {
mvwaddstr(win_t, 0, 0, "cannot open directory");
pthread_mutex_unlock(&mutex_top);
pthread_mutex_unlock(&mutex_mid);
continue;
} else if (mid_dir.current_file == NULL) {
mvwaddstr(win_t, 0, 0, "cannot open file");
pthread_mutex_unlock(&mutex_top);
pthread_mutex_unlock(&mutex_mid);
continue;
}
/* rendering */
pthread_mutex_lock(&mutex_render);
werase(win_t);
if (strlen(global_path) != 1) {
wattron(win_t, COLOR_PAIR(COLOR_PATH));
mvwaddnstr(win_t, 0, 0, global_path, strlen(global_path)+1);
mvwaddch(win_t, 0, strlen(global_path), '/');
wattroff(win_t, COLOR_PAIR(COLOR_PATH));
mvwaddstr(win_t, 0, strlen(global_path)+1, mid_dir.current_file->file_name);
} else { /* dir '/' */
wattron(win_t, COLOR_PAIR(COLOR_PATH));
mvwaddnstr(win_t, 0, 0, global_path, 1);
wattroff(win_t, COLOR_PAIR(COLOR_PATH));
mvwaddstr(win_t, 0, 1, mid_dir.current_file->file_name);
}
/*wnoutrefresh(win_t);*/
wrefresh(win_t);
pthread_mutex_unlock(&mutex_render);
pthread_mutex_unlock(&mutex_mid);
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 (buffer_width != terminal_width) {
buffer_width = (terminal_width > 10) ? terminal_width : 11;
free(btm_buffer);
btm_buffer = malloc(buffer_width+1);
/*in this case STATUS_RUN_BACKEND should be true as defined in the main loop resize check*/
}
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);
}
/*}}}*/
}
memset(btm_buffer, ' ', buffer_width);
if (ui_btm_right_block_size + 9 < buffer_width) {
memcpy(btm_buffer + buffer_width - ui_btm_right_block_size, ui_btm_right_block, ui_btm_right_block_size);
}
if (mid_dir.current_file != NULL) {
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' : '-';
}
btm_buffer[buffer_width] = '\0';
/* rendering */
pthread_mutex_lock(&mutex_render);
werase(win_b);
mvwprintw(win_b, 0, 0, "%s", btm_buffer);
/*wnoutrefresh(win_b);*/
wrefresh(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(){
rgt_buffer = NULL;
btm_buffer = NULL;
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(){
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();