2024-11-21 12:54:55 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ftw.h>
|
2024-03-09 18:37:04 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2024-11-01 00:19:40 +01:00
|
|
|
#include <stdarg.h>
|
2024-03-09 18:37:04 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2024-11-01 00:19:40 +01:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/ioctl.h>
|
2024-11-21 12:54:55 +01:00
|
|
|
#include <termios.h>
|
|
|
|
#include <time.h>
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-03-30 04:39:02 +01:00
|
|
|
#include "icons.h"
|
2024-11-21 12:54:55 +01:00
|
|
|
#include "file.h"
|
2024-03-09 18:37:04 +01:00
|
|
|
#include "util.h"
|
2024-03-09 19:24:38 +01:00
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
#define LEN(x) (sizeof(x) / sizeof(*(x)))
|
|
|
|
|
2024-11-17 15:49:24 +01:00
|
|
|
/* Keybindings */
|
|
|
|
enum keys {
|
|
|
|
CTRLD = 0x04,
|
|
|
|
ENTER = 0xD,
|
|
|
|
CTRLU = 0x15,
|
|
|
|
SPACE = 0x20,
|
|
|
|
TILDE = 0x7E,
|
|
|
|
BACKSPACE = 127,
|
|
|
|
ARROW_LEFT = 1000,
|
|
|
|
ARROW_RIGHT,
|
|
|
|
ARROW_UP,
|
|
|
|
ARROW_DOWN,
|
|
|
|
DEL_KEY,
|
|
|
|
HOME_KEY,
|
|
|
|
END_KEY,
|
|
|
|
PAGE_UP,
|
|
|
|
PAGE_DOWN,
|
|
|
|
};
|
2024-11-17 15:53:45 +01:00
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
enum action {
|
|
|
|
ACT_QUIT = 1,
|
|
|
|
ACT_RELOAD,
|
|
|
|
ACT_BACK,
|
|
|
|
ACT_ENTER,
|
|
|
|
ACT_JUMP_UP,
|
|
|
|
ACT_JUMP_DOWN,
|
|
|
|
ACT_UP,
|
|
|
|
ACT_DOWN,
|
|
|
|
ACT_BOTTOM,
|
|
|
|
ACT_TOP,
|
|
|
|
ACT_HOME,
|
|
|
|
ACT_TRASH_DIR,
|
|
|
|
ACT_SORT,
|
|
|
|
ACT_SHOW_DIR_SIZE,
|
|
|
|
ACT_PREV_DIR,
|
|
|
|
ACT_SHOW_HELP,
|
|
|
|
ACT_HIDDEN_FILES,
|
|
|
|
ACT_FILE_DETAILS,
|
|
|
|
ACT_SHOW_ICONS,
|
|
|
|
ACT_CREATE_FILE,
|
|
|
|
ACT_CREATE_DIR,
|
|
|
|
ACT_RENAME_FILE,
|
|
|
|
ACT_GOTO_DIR,
|
|
|
|
ACT_TOGGLE_EXE,
|
|
|
|
ACT_START_SHELL,
|
|
|
|
ACT_COPY_FILENAME,
|
|
|
|
ACT_OPEN_FILE,
|
|
|
|
ACT_OPEN_FILE_DETACHED,
|
|
|
|
ACT_VIEW_FILE_ATTR,
|
|
|
|
ACT_SHOW_HIST,
|
|
|
|
ACT_FAV1,
|
|
|
|
ACT_FAV2,
|
|
|
|
ACT_FAV3,
|
|
|
|
ACT_FAV4,
|
|
|
|
ACT_FAV5,
|
|
|
|
ACT_FAV6,
|
|
|
|
ACT_FAV7,
|
|
|
|
ACT_FAV8,
|
|
|
|
ACT_FAV9,
|
|
|
|
ACT_MARK_FILE,
|
|
|
|
ACT_MARK_ALL,
|
|
|
|
ACT_DELETE,
|
|
|
|
ACT_MOVE,
|
|
|
|
ACT_COPY,
|
|
|
|
ACT_SYM_LINK,
|
|
|
|
ACT_BULK_RENAME,
|
|
|
|
};
|
|
|
|
|
2024-11-17 15:49:24 +01:00
|
|
|
typedef struct {
|
|
|
|
int key;
|
2024-11-18 23:37:25 +01:00
|
|
|
enum action act;
|
2024-11-21 12:54:55 +01:00
|
|
|
} Key;
|
2024-11-17 15:53:45 +01:00
|
|
|
|
2024-11-17 15:49:24 +01:00
|
|
|
#define PATH_MAX 4096 /* Max length of path */
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
int getsel(void);
|
2024-11-05 00:02:35 +01:00
|
|
|
void handle_sigwinch(int ignore);
|
|
|
|
void cleanup(void);
|
|
|
|
void show_help(void);
|
|
|
|
char *check_trash_dir(void);
|
2024-03-20 23:43:34 +01:00
|
|
|
void change_dir(const char *buf, int selection, int ftype);
|
2024-03-29 23:34:26 +01:00
|
|
|
void mkdir_p(const char *destdir);
|
2024-11-01 00:19:40 +01:00
|
|
|
void populate_files(const char *path, int ftype, ArrayList **list);
|
2024-03-14 00:51:05 +01:00
|
|
|
int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf);
|
2024-03-31 22:44:05 +02:00
|
|
|
void add_file_stat(char *filename, char *path, int ftype);
|
2024-11-05 00:02:35 +01:00
|
|
|
void list_files(void);
|
|
|
|
void show_file_content(void);
|
|
|
|
void edit_file(void);
|
|
|
|
void toggle_executable(void);
|
2024-11-13 01:08:32 +01:00
|
|
|
void replace_home(char *str);
|
2024-11-05 00:02:35 +01:00
|
|
|
int write_last_d(void);
|
2024-11-01 00:19:40 +01:00
|
|
|
int sort_compare(const void *a, const void *b);
|
2024-11-05 00:02:35 +01:00
|
|
|
void sort_files(void);
|
2024-04-10 14:51:39 +02:00
|
|
|
char *get_panel_string(char *prompt);
|
2024-11-05 00:02:35 +01:00
|
|
|
void rename_file(void);
|
|
|
|
void goto_dir(void);
|
|
|
|
void create_dir(void);
|
|
|
|
void create_file(void);
|
|
|
|
void delete_files(void);
|
2024-11-17 16:17:41 +01:00
|
|
|
void start_shell(void);
|
2024-11-17 19:10:26 +01:00
|
|
|
void yank_clipboard(void);
|
2024-11-17 20:04:15 +01:00
|
|
|
void view_file_attr(void);
|
2024-11-17 23:53:06 +01:00
|
|
|
void show_history(void);
|
2024-11-18 01:21:09 +01:00
|
|
|
void open_with(void);
|
|
|
|
void open_detached(void);
|
2024-03-29 15:05:01 +01:00
|
|
|
void wpprintw(const char *fmt, ...);
|
2024-11-01 00:19:40 +01:00
|
|
|
void move_cursor(int row, int col);
|
2024-11-17 19:10:26 +01:00
|
|
|
int readch(void);
|
2024-11-01 00:19:40 +01:00
|
|
|
int get_window_size(int *row, int *col);
|
|
|
|
void bprintf(const char *fmt, ...);
|
2024-03-09 18:37:04 +01:00
|
|
|
|
2024-03-09 19:24:38 +01:00
|
|
|
/* global variables */
|
2024-11-01 00:19:40 +01:00
|
|
|
long sel_file = 0;
|
2024-11-18 23:37:25 +01:00
|
|
|
int file_picker = 0;
|
2024-11-17 15:49:24 +01:00
|
|
|
int to_open_file = 0;
|
2024-11-17 20:30:14 +01:00
|
|
|
char argv_cp[PATH_MAX];
|
|
|
|
char cwd[PATH_MAX];
|
|
|
|
char p_cwd[PATH_MAX]; /* previous cwd */
|
2024-03-09 19:39:29 +01:00
|
|
|
int half_width;
|
2024-03-20 21:29:04 +01:00
|
|
|
ArrayList *files;
|
|
|
|
ArrayList *marked;
|
2024-11-01 00:19:40 +01:00
|
|
|
ArrayList *tmp1; /* temp store of dirs */
|
|
|
|
ArrayList *tmp2; /* tmp store of files */
|
|
|
|
int rows, cols;
|
|
|
|
struct termios oldt, newt;
|
2024-03-09 18:37:04 +01:00
|
|
|
|
2024-11-01 00:19:40 +01:00
|
|
|
unsigned long total_dir_size = 0;
|
2024-03-14 00:51:05 +01:00
|
|
|
|
2024-11-01 00:19:40 +01:00
|
|
|
int main(int argc, char **argv)
|
2024-03-09 18:37:04 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
if (argc == 3) {
|
|
|
|
if (strncmp(argv[2], "-p", 2) == 0)
|
2024-11-17 15:49:24 +01:00
|
|
|
file_picker = 1;
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
if (argc <= 3 && argc != 1) {
|
|
|
|
if (strncmp(argv[1], "-h", 2) == 0)
|
|
|
|
die("Usage: ccc filename");
|
|
|
|
|
|
|
|
struct stat st;
|
|
|
|
if (lstat(argv[1], &st)) {
|
|
|
|
perror("ccc");
|
|
|
|
die("Error from lstat");
|
|
|
|
}
|
|
|
|
/* chdir to directory from argument */
|
|
|
|
if (S_ISDIR(st.st_mode) && chdir(argv[1])) {
|
|
|
|
perror("ccc");
|
|
|
|
die("Error from chdir");
|
|
|
|
} else if (S_ISREG(st.st_mode)) {
|
2024-11-17 20:30:14 +01:00
|
|
|
strcpy(argv_cp, argv[1]);
|
2024-11-01 00:19:40 +01:00
|
|
|
char *last_slash = strrchr(argv_cp, '/');
|
2024-11-17 15:49:24 +01:00
|
|
|
if (last_slash) {
|
|
|
|
*last_slash = '\0';
|
|
|
|
if (chdir(argv[1])) {
|
|
|
|
perror("ccc");
|
|
|
|
die("Error from chdir");
|
|
|
|
}
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
2024-11-17 15:49:24 +01:00
|
|
|
to_open_file = 1;
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check if it is interactive shell */
|
|
|
|
if (!isatty(STDIN_FILENO))
|
|
|
|
die("ccc: No tty detected. ccc requires an interactive shell to run.\n");
|
|
|
|
|
2024-11-01 23:42:11 +01:00
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_handler = handle_sigwinch;
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
sigemptyset(&sa.sa_mask);
|
|
|
|
|
|
|
|
if (sigaction(SIGWINCH, &sa, NULL) == -1) {
|
|
|
|
perror("sigaction");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2024-11-01 00:19:40 +01:00
|
|
|
/* initialize screen, don't print special chars,
|
|
|
|
* make ctrl + c work, don't show cursor
|
|
|
|
* enable arrow keys */
|
|
|
|
bprintf("\033[?1049h\033[2J\033[?25l");
|
|
|
|
tcgetattr(STDIN_FILENO, &oldt);
|
|
|
|
newt = oldt;
|
|
|
|
/* Disable canonical mode and echo */
|
|
|
|
newt.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
|
|
|
|
/* newt.c_oflag &= ~(OPOST); */
|
|
|
|
newt.c_cflag |= (CS8);
|
|
|
|
newt.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
|
|
|
|
newt.c_cc[VMIN] = 0;
|
|
|
|
newt.c_cc[VTIME] = 1;
|
|
|
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &newt);
|
|
|
|
|
|
|
|
/* init files and marked arrays */
|
|
|
|
marked = arraylist_init(100);
|
|
|
|
hashtable_init();
|
|
|
|
|
|
|
|
getcwd(cwd, PATH_MAX);
|
|
|
|
populate_files(cwd, 0, &files);
|
2024-11-05 00:02:35 +01:00
|
|
|
handle_sigwinch(-1);
|
2024-11-01 00:19:40 +01:00
|
|
|
|
|
|
|
if (to_open_file) {
|
2024-11-17 15:49:24 +01:00
|
|
|
sel_file = arraylist_search(files, argv_cp, 1);
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
int run = 1;
|
2024-11-01 00:19:40 +01:00
|
|
|
while (run) {
|
2024-11-17 19:10:26 +01:00
|
|
|
list_files();
|
2024-11-18 23:37:25 +01:00
|
|
|
int sel = getsel();
|
|
|
|
switch (sel) {
|
2024-11-01 00:19:40 +01:00
|
|
|
/* quit */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_QUIT:
|
2024-11-01 00:19:40 +01:00
|
|
|
if (write_last_d() == -1) {
|
|
|
|
/* prompt user so error message can be shown to user */
|
2024-11-17 19:10:26 +01:00
|
|
|
readch();
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
cleanup();
|
|
|
|
run = 0;
|
|
|
|
break;
|
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* reload */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_RELOAD:
|
2024-11-01 00:19:40 +01:00
|
|
|
change_dir(cwd, 0, 0);
|
|
|
|
break;
|
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* go back */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_BACK:;
|
2024-11-17 20:27:42 +01:00
|
|
|
char dir[PATH_MAX];
|
|
|
|
strcpy(dir, cwd);
|
2024-11-05 21:00:50 +01:00
|
|
|
/* get parent directory */
|
2024-11-17 20:27:42 +01:00
|
|
|
char *last_slash = strrchr(dir, '/');
|
2024-11-17 19:56:58 +01:00
|
|
|
if (last_slash) {
|
2024-11-17 20:27:42 +01:00
|
|
|
if (!strcmp(last_slash, dir)) {
|
2024-11-05 21:00:50 +01:00
|
|
|
change_dir("/", 0, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*last_slash = '\0';
|
2024-11-17 20:27:42 +01:00
|
|
|
change_dir(dir, 0, 0);
|
2024-11-05 21:00:50 +01:00
|
|
|
}
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* enter directory/open a file */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_ENTER:;
|
2024-11-06 16:11:32 +01:00
|
|
|
file c_file = files->items[sel_file];
|
|
|
|
|
|
|
|
/* Check if it is directory or a regular file */
|
|
|
|
if (c_file.type == DRY) {
|
|
|
|
/* Change cwd to directory */
|
|
|
|
change_dir(c_file.path, 0, 0);
|
|
|
|
} else if (c_file.type == REG) {
|
|
|
|
/* Write opened file to a file for file pickers */
|
|
|
|
if (file_picker) {
|
2024-11-13 01:08:32 +01:00
|
|
|
char opened_file_path[PATH_MAX];
|
2024-11-06 16:11:32 +01:00
|
|
|
strcpy(opened_file_path, "~/.cache/ccc/opened_file");
|
2024-11-13 01:08:32 +01:00
|
|
|
replace_home(opened_file_path);
|
2024-11-06 16:11:32 +01:00
|
|
|
FILE *opened_file = fopen(opened_file_path, "w+");
|
|
|
|
fprintf(opened_file, "%s\n", c_file.path);
|
|
|
|
fclose(opened_file);
|
|
|
|
cleanup();
|
|
|
|
run = 0;
|
|
|
|
} else {
|
|
|
|
edit_file();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* jump up */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_JUMP_UP:
|
2024-11-13 01:15:56 +01:00
|
|
|
if ((sel_file - jump_num) > 0)
|
|
|
|
sel_file -= jump_num;
|
2024-11-05 21:00:50 +01:00
|
|
|
else
|
|
|
|
sel_file = 0;
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* jump down */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_JUMP_DOWN:
|
2024-11-13 01:15:56 +01:00
|
|
|
if ((sel_file + jump_num) < (files->length - 1))
|
|
|
|
sel_file += jump_num;
|
2024-11-05 21:00:50 +01:00
|
|
|
else
|
|
|
|
sel_file = (files->length - 1);
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
/* go up */
|
|
|
|
case ACT_UP:
|
|
|
|
if (sel_file > 0)
|
|
|
|
sel_file--;
|
|
|
|
break;
|
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* go down */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_DOWN:
|
2024-11-05 21:00:50 +01:00
|
|
|
if (sel_file < (files->length - 1))
|
|
|
|
sel_file++;
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* jump to the bottom */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_BOTTOM:
|
2024-11-06 16:11:32 +01:00
|
|
|
sel_file = (files->length - 1);
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* jump to the top */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_TOP:
|
2024-11-17 16:22:55 +01:00
|
|
|
sel_file = 0;
|
2024-11-05 21:00:50 +01:00
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* '~' go to $HOME */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_HOME:;
|
2024-11-05 21:00:50 +01:00
|
|
|
char *home = getenv("HOME");
|
2024-11-17 19:56:58 +01:00
|
|
|
if (!home) {
|
|
|
|
wpprintw("$HOME not defined (Press any key to continue)");
|
|
|
|
readch();
|
2024-11-05 21:00:50 +01:00
|
|
|
} else {
|
|
|
|
change_dir(home, 0, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* go to the trash dir */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_TRASH_DIR:;
|
2024-11-05 21:00:50 +01:00
|
|
|
char *trash_dir = check_trash_dir();
|
2024-11-17 19:56:58 +01:00
|
|
|
if (trash_dir) {
|
2024-11-05 21:00:50 +01:00
|
|
|
change_dir(trash_dir, 0, 0);
|
2024-11-17 20:39:46 +01:00
|
|
|
free(trash_dir);
|
2024-11-05 21:00:50 +01:00
|
|
|
}
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* sort files */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_SORT:
|
2024-11-05 21:00:50 +01:00
|
|
|
sort_files();
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* show directories' sizes */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_SHOW_DIR_SIZE:
|
2024-11-05 21:00:50 +01:00
|
|
|
dirs_size = !dirs_size;
|
|
|
|
change_dir(cwd, 0, 0);
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* go to previous dir */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_PREV_DIR:
|
2024-11-05 21:00:50 +01:00
|
|
|
if (strlen(p_cwd) != 0)
|
|
|
|
change_dir(p_cwd, 0, 0);
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* show help */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_SHOW_HELP:
|
2024-11-05 21:00:50 +01:00
|
|
|
show_help();
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* toggle hidden files */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_HIDDEN_FILES:
|
2024-11-05 21:00:50 +01:00
|
|
|
show_hidden = !show_hidden;
|
|
|
|
change_dir(cwd, 0, 0);
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* toggle file details */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_FILE_DETAILS:
|
2024-11-13 01:15:56 +01:00
|
|
|
show_details = !show_details;
|
2024-11-05 21:00:50 +01:00
|
|
|
change_dir(cwd, 0, 0);
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_SHOW_ICONS:
|
2024-11-05 21:00:50 +01:00
|
|
|
show_icons = !show_icons;
|
|
|
|
change_dir(cwd, 0, 0);
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_CREATE_FILE:
|
2024-11-05 21:00:50 +01:00
|
|
|
create_file();
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_CREATE_DIR:
|
2024-11-05 21:00:50 +01:00
|
|
|
create_dir();
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_RENAME_FILE:
|
2024-11-05 21:00:50 +01:00
|
|
|
rename_file();
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_GOTO_DIR:
|
2024-11-05 21:00:50 +01:00
|
|
|
goto_dir();
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_TOGGLE_EXE:
|
2024-11-05 21:00:50 +01:00
|
|
|
toggle_executable();
|
2024-11-17 15:53:45 +01:00
|
|
|
change_dir(cwd, 0, 0);
|
|
|
|
break;
|
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_START_SHELL:
|
2024-11-17 16:17:41 +01:00
|
|
|
start_shell();
|
2024-11-05 21:00:50 +01:00
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_COPY_FILENAME:
|
2024-11-17 19:10:26 +01:00
|
|
|
yank_clipboard();
|
|
|
|
break;
|
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_OPEN_FILE:
|
2024-11-18 01:21:09 +01:00
|
|
|
open_with();
|
|
|
|
break;
|
2024-11-18 23:37:25 +01:00
|
|
|
|
|
|
|
case ACT_OPEN_FILE_DETACHED:
|
2024-11-18 01:21:09 +01:00
|
|
|
open_detached();
|
|
|
|
break;
|
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_VIEW_FILE_ATTR:
|
2024-11-17 20:04:15 +01:00
|
|
|
view_file_attr();
|
|
|
|
break;
|
2024-11-17 19:37:53 +01:00
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_SHOW_HIST:
|
2024-11-17 23:53:06 +01:00
|
|
|
show_history();
|
|
|
|
break;
|
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_FAV1: case ACT_FAV2: case ACT_FAV3: case ACT_FAV4: case ACT_FAV5:
|
|
|
|
case ACT_FAV6: case ACT_FAV7: case ACT_FAV8: case ACT_FAV9:;
|
2024-11-18 00:51:19 +01:00
|
|
|
char envname[9];
|
2024-11-18 23:37:25 +01:00
|
|
|
snprintf(envname, 9, "CCC_FAV%d", sel - ACT_FAV1 + 1);
|
2024-11-18 00:51:19 +01:00
|
|
|
char *fav = getenv(envname);
|
|
|
|
if (fav && !strcmp(fav, "")) {
|
|
|
|
char dir[PATH_MAX];
|
|
|
|
strcpy(dir, fav);
|
|
|
|
change_dir(dir, 0, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* mark one file */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_MARK_FILE:
|
2024-11-05 21:00:50 +01:00
|
|
|
add_file_stat(files->items[sel_file].name, files->items[sel_file].path, 1);
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* mark all files in directory */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_MARK_ALL:
|
2024-11-05 21:00:50 +01:00
|
|
|
change_dir(cwd, sel_file, 2); /* reload current dir */
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* mark actions: */
|
|
|
|
/* delete */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_DELETE:
|
2024-11-05 21:00:50 +01:00
|
|
|
delete_files();
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* move */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_MOVE:
|
2024-11-06 16:11:32 +01:00
|
|
|
if (marked->length) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* copy */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_COPY:
|
2024-11-06 16:11:32 +01:00
|
|
|
if (marked->length) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* symbolic link */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_SYM_LINK:
|
2024-11-06 16:11:32 +01:00
|
|
|
if (marked->length) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-05 21:00:50 +01:00
|
|
|
/* bulk rename */
|
2024-11-18 23:37:25 +01:00
|
|
|
case ACT_BULK_RENAME:
|
2024-11-06 16:11:32 +01:00
|
|
|
if (marked->length) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
|
|
|
default:
|
2024-11-06 16:11:32 +01:00
|
|
|
break;
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
|
|
|
|
2024-11-18 23:37:25 +01:00
|
|
|
int getsel(void)
|
|
|
|
{
|
|
|
|
int c = readch();
|
|
|
|
for (int i = 0; i < LEN(keybindings); i++) {
|
|
|
|
if (c == keybindings[i].key) {
|
|
|
|
return keybindings[i].act;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-11-05 00:02:35 +01:00
|
|
|
void handle_sigwinch(int ignore)
|
2024-11-01 23:42:11 +01:00
|
|
|
{
|
|
|
|
get_window_size(&rows, &cols);
|
2024-11-13 01:15:56 +01:00
|
|
|
half_width = cols / 2 + window_offset;
|
2024-11-01 23:42:11 +01:00
|
|
|
list_files();
|
|
|
|
}
|
|
|
|
|
2024-11-05 00:02:35 +01:00
|
|
|
void cleanup(void)
|
2024-03-20 21:29:04 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
if (files->length != 0) {
|
|
|
|
arraylist_free(files);
|
|
|
|
}
|
2024-11-18 01:25:09 +01:00
|
|
|
free(marked->items);
|
|
|
|
free(marked);
|
2024-11-01 00:19:40 +01:00
|
|
|
/* Restore old terminal settings */
|
|
|
|
tcsetattr(STDIN_FILENO, TCSAFLUSH, &oldt);
|
|
|
|
bprintf("\033[2J\033[?1049l\033[?25h");
|
2024-03-20 17:03:54 +01:00
|
|
|
}
|
|
|
|
|
2024-11-05 00:02:35 +01:00
|
|
|
void show_help(void)
|
2024-03-20 13:28:08 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
bprintf("\033[2J");
|
|
|
|
move_cursor(1, 1);
|
|
|
|
bprintf("h: go to parent dir\nj: scroll down\nk: scroll up\n"
|
|
|
|
"l: go to child dir\n\nleft: go to parent dir\ndown: scroll down\n"
|
|
|
|
"up: scroll up\nright: go to child dir\n\n"
|
|
|
|
"enter: go to child dir/open file\nbackspace: go to parent dir\n\n"
|
|
|
|
"gg: go to top\nG: go to bottom\n\nctrl+u: jump up\nctrl+d: jump down\n\n"
|
|
|
|
"t: go to trash dir\n~: go to home dir\n-: go to previous dir\n"
|
|
|
|
"z: refresh current dir\n:: go to a directory by typing\nu: sort files\n\n"
|
|
|
|
".: toggle hidden files\ni: toggle file details\nX: toggle executable\n\n"
|
|
|
|
"A: show directory disk usage/block size\n\nf: new file\nn: new dir\n"
|
|
|
|
"r: rename\n\nspace: mark file\na: mark all files in directory\nd: trash"
|
|
|
|
"\n\n?: show help\nq: exit with last dir written to file\n"
|
|
|
|
"ctrl+c exit without writing last dir"
|
2024-11-17 19:10:26 +01:00
|
|
|
"\nPress any key to continue"
|
2024-11-17 20:36:27 +01:00
|
|
|
);
|
2024-11-01 00:19:40 +01:00
|
|
|
wpprintw("Visit https://github.com/night0721/ccc or use 'man ccc' for help");
|
2024-11-17 19:10:26 +01:00
|
|
|
readch();
|
2024-03-20 13:28:08 +01:00
|
|
|
}
|
|
|
|
|
2024-03-31 22:44:05 +02:00
|
|
|
/*
|
|
|
|
* Checks if the trash directory is set and returns it
|
|
|
|
*/
|
2024-11-05 00:02:35 +01:00
|
|
|
char *check_trash_dir(void)
|
2024-03-29 23:34:26 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
char *path = memalloc(PATH_MAX);
|
|
|
|
|
|
|
|
/* check if there is trash_dir */
|
2024-11-13 01:08:32 +01:00
|
|
|
if (!strcmp(trash_dir, "")) {
|
2024-11-01 00:19:40 +01:00
|
|
|
wpprintw("Trash directory not defined");
|
|
|
|
return NULL;
|
|
|
|
} else {
|
2024-11-13 01:08:32 +01:00
|
|
|
strcpy(path, trash_dir);
|
2024-11-01 00:19:40 +01:00
|
|
|
/* if trash_dir has ~ then make it $HOME */
|
|
|
|
/* use path as trash_dir */
|
2024-11-13 01:08:32 +01:00
|
|
|
if (path[0] == '~')
|
|
|
|
replace_home(path);
|
2024-11-01 00:19:40 +01:00
|
|
|
|
|
|
|
/* if has access to trash_dir */
|
|
|
|
if (access(path, F_OK) != 0) {
|
|
|
|
/* create the directory with 755 permissions if it doesn't exist */
|
|
|
|
mkdir_p(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path;
|
2024-03-29 23:34:26 +01:00
|
|
|
}
|
|
|
|
|
2024-03-14 13:46:38 +01:00
|
|
|
/*
|
2024-03-16 22:46:21 +01:00
|
|
|
* Change directory in window with selection
|
2024-03-14 13:46:38 +01:00
|
|
|
*/
|
2024-03-20 23:43:34 +01:00
|
|
|
void change_dir(const char *buf, int selection, int ftype)
|
2024-03-14 13:46:38 +01:00
|
|
|
{
|
2024-11-17 20:27:42 +01:00
|
|
|
if (strcmp(cwd, buf) != 0) {
|
|
|
|
char tmp[PATH_MAX];
|
|
|
|
strcpy(tmp, buf);
|
|
|
|
strcpy(p_cwd, cwd);
|
|
|
|
strcpy(cwd, tmp);
|
2024-11-17 23:53:06 +01:00
|
|
|
char history_path[PATH_MAX];
|
|
|
|
strcpy(history_path, "~/.cache/ccc/history");
|
|
|
|
replace_home(history_path);
|
|
|
|
FILE *history_file = fopen(history_path, "a");
|
|
|
|
fprintf(history_file, "%s\n", cwd);
|
|
|
|
fclose(history_file);
|
2024-11-17 20:27:42 +01:00
|
|
|
}
|
2024-11-01 00:19:40 +01:00
|
|
|
if (ftype == 0)
|
|
|
|
arraylist_free(files);
|
|
|
|
chdir(cwd);
|
|
|
|
sel_file = selection;
|
|
|
|
populate_files(cwd, ftype, &files);
|
2024-03-14 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
2024-03-14 15:46:06 +01:00
|
|
|
/*
|
|
|
|
* Recursively create directory by creating each subdirectory
|
2024-03-19 23:07:05 +01:00
|
|
|
* like mkdir -p
|
2024-03-14 15:46:06 +01:00
|
|
|
*/
|
2024-03-29 23:34:26 +01:00
|
|
|
void mkdir_p(const char *destdir)
|
2024-03-14 15:46:06 +01:00
|
|
|
{
|
2024-11-17 20:36:27 +01:00
|
|
|
char path[PATH_MAX], dir_path[PATH_MAX];
|
2024-11-01 00:19:40 +01:00
|
|
|
|
|
|
|
if (destdir[0] == '~') {
|
|
|
|
char *home = getenv("HOME");
|
2024-11-17 19:56:58 +01:00
|
|
|
if (!home) {
|
2024-11-01 00:19:40 +01:00
|
|
|
wpprintw("$HOME not defined");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* replace ~ with home */
|
|
|
|
snprintf(path, PATH_MAX, "%s%s", home, destdir + 1);
|
|
|
|
} else {
|
|
|
|
strcpy(path, destdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* fix first / not appearing in the string */
|
|
|
|
if (path[0] == '/')
|
|
|
|
dir_path[0] = '/';
|
|
|
|
|
|
|
|
char *token = strtok(path, "/");
|
2024-11-17 19:56:58 +01:00
|
|
|
while (token) {
|
2024-11-01 00:19:40 +01:00
|
|
|
strcat(dir_path, token);
|
|
|
|
strcat(dir_path, "/");
|
|
|
|
|
|
|
|
if (mkdir(dir_path, 0755) == -1) {
|
|
|
|
struct stat st;
|
|
|
|
if (stat(dir_path, &st) == 0 && S_ISDIR(st.st_mode)) {
|
|
|
|
/* Directory already exists, continue to the next dir */
|
|
|
|
token = strtok(NULL, "/");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
wpprintw("mkdir failed: %s", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
token = strtok(NULL, "/");
|
|
|
|
}
|
|
|
|
return;
|
2024-03-14 15:46:06 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 20:14:26 +01:00
|
|
|
/*
|
2024-04-06 20:58:31 +02:00
|
|
|
* Read the provided directory and add all files in directory to an Arraylist
|
2024-03-18 00:21:58 +01:00
|
|
|
* ftype: normal files = 0, marked = 1, marking ALL = 2
|
2024-03-09 20:14:26 +01:00
|
|
|
*/
|
2024-11-01 00:19:40 +01:00
|
|
|
void populate_files(const char *path, int ftype, ArrayList **list)
|
2024-03-09 18:37:04 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
DIR *dp;
|
|
|
|
struct dirent *ep;
|
|
|
|
|
2024-11-17 19:56:58 +01:00
|
|
|
if ((dp = opendir(path))) {
|
2024-11-01 00:19:40 +01:00
|
|
|
if (ftype == 0) {
|
|
|
|
tmp1 = arraylist_init(10);
|
|
|
|
tmp2 = arraylist_init(10);
|
|
|
|
}
|
|
|
|
|
2024-11-17 19:56:58 +01:00
|
|
|
while ((ep = readdir(dp))) {
|
2024-11-01 00:19:40 +01:00
|
|
|
char *filename = estrdup(ep->d_name);
|
|
|
|
|
|
|
|
/* Filter out dotfiles */
|
|
|
|
if ((!show_hidden && strncmp(filename, ".", 1) && strncmp(filename, "..", 2))
|
|
|
|
|| (show_hidden && strcmp(filename, ".") && strcmp(filename, ".."))) {
|
|
|
|
/* Construct full file path */
|
|
|
|
int fpath_len = strlen(path) + strlen(filename) + 2;
|
|
|
|
char *fpath = memalloc(fpath_len);
|
|
|
|
snprintf(fpath, fpath_len, "%s/%s", path, filename);
|
|
|
|
add_file_stat(filename, fpath, ftype);
|
|
|
|
}
|
|
|
|
else free(filename);
|
|
|
|
}
|
|
|
|
if (ftype == 0) {
|
|
|
|
*list = arraylist_init(tmp1->length + tmp2->length);
|
|
|
|
(*list)->length = tmp1->length + tmp2->length;
|
2024-11-17 19:37:53 +01:00
|
|
|
/* Need to see how to sort by date */
|
2024-11-01 00:19:40 +01:00
|
|
|
qsort(tmp1->items, tmp1->length, sizeof(file), sort_compare);
|
|
|
|
qsort(tmp2->items, tmp2->length, sizeof(file), sort_compare);
|
|
|
|
memcpy((*list)->items, tmp1->items, tmp1->length * sizeof(file));
|
|
|
|
memcpy((*list)->items + tmp1->length, tmp2->items, tmp2->length * sizeof(file));
|
|
|
|
free(tmp1->items);
|
|
|
|
free(tmp2->items);
|
|
|
|
free(tmp1);
|
|
|
|
free(tmp2);
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
} else {
|
|
|
|
wpprintw("stat failed: %s", strerror(errno));
|
|
|
|
}
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
|
|
|
|
2024-03-14 00:51:05 +01:00
|
|
|
int get_directory_size(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
|
2024-03-13 11:18:35 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
total_dir_size += sb->st_size;
|
|
|
|
return 0;
|
2024-03-13 11:18:35 +01:00
|
|
|
}
|
|
|
|
|
2024-03-11 22:54:14 +01:00
|
|
|
/*
|
2024-03-13 11:18:35 +01:00
|
|
|
* Get file's last modified time, size, type
|
|
|
|
* Add that file into list
|
2024-03-29 23:34:26 +01:00
|
|
|
* ftype: normal file = 0, normal marked = 1, marked ALL = 2
|
2024-03-11 22:54:14 +01:00
|
|
|
*/
|
2024-03-31 22:44:05 +02:00
|
|
|
void add_file_stat(char *filename, char *path, int ftype)
|
2024-03-12 21:59:36 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
struct stat file_stat;
|
|
|
|
if (stat(path, &file_stat) == -1) {
|
|
|
|
/* can't be triggered? */
|
|
|
|
if (errno == EACCES)
|
2024-11-17 15:49:24 +01:00
|
|
|
arraylist_add(files, filename, path, NULL, REG, NULL, DEF_COLOR, 0, 0);
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int type;
|
2024-11-17 23:24:31 +01:00
|
|
|
char icon_str[5];
|
2024-11-01 00:19:40 +01:00
|
|
|
|
|
|
|
filename[strlen(filename)] = '\0';
|
|
|
|
/* handle file without extension
|
|
|
|
* ext is the extension if . exist in filename
|
|
|
|
* otherwise is nothing and handled through tenery operator */
|
|
|
|
char *ext = strrchr(filename, '.');
|
2024-11-17 19:56:58 +01:00
|
|
|
if (ext) {
|
2024-11-01 00:19:40 +01:00
|
|
|
ext += 1;
|
|
|
|
}
|
|
|
|
/* add file extension */
|
2024-11-17 19:56:58 +01:00
|
|
|
icon *ext_icon = hashtable_search(ext ? ext : filename);
|
|
|
|
if (!ext_icon)
|
2024-11-01 00:19:40 +01:00
|
|
|
memcpy(icon_str, "", 4);
|
|
|
|
else
|
2024-11-17 23:24:31 +01:00
|
|
|
memcpy(icon_str, ext_icon->icon, 4);
|
2024-11-01 00:19:40 +01:00
|
|
|
|
2024-11-13 01:28:17 +01:00
|
|
|
int color = DEF_COLOR;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
|
|
|
if (S_ISDIR(file_stat.st_mode)) {
|
|
|
|
type = DRY; /* dir */
|
2024-11-13 01:28:17 +01:00
|
|
|
color = DIR_COLOR;
|
2024-11-17 23:24:31 +01:00
|
|
|
memcpy(icon_str, "", 4);
|
2024-11-01 00:19:40 +01:00
|
|
|
} else if (S_ISREG(file_stat.st_mode)) {
|
|
|
|
type = REG; /* regular file */
|
2024-11-13 01:28:17 +01:00
|
|
|
color = REG_COLOR;
|
2024-11-01 00:19:40 +01:00
|
|
|
} else if (S_ISLNK(file_stat.st_mode)) {
|
|
|
|
type = LNK; /* symbolic link */
|
2024-11-13 01:28:17 +01:00
|
|
|
color = LNK_COLOR;
|
2024-11-01 00:19:40 +01:00
|
|
|
} else if (S_ISCHR(file_stat.st_mode)) {
|
|
|
|
type = CHR; /* character device */
|
2024-11-13 01:28:17 +01:00
|
|
|
color = CHR_COLOR;
|
2024-11-01 00:19:40 +01:00
|
|
|
} else if (S_ISSOCK(file_stat.st_mode)) {
|
|
|
|
type = SOC; /* socket */
|
2024-11-13 01:28:17 +01:00
|
|
|
color = SOC_COLOR;
|
2024-11-01 00:19:40 +01:00
|
|
|
} else if (S_ISBLK(file_stat.st_mode)) {
|
|
|
|
type = BLK; /* block device */
|
2024-11-13 01:28:17 +01:00
|
|
|
color = BLK_COLOR;
|
2024-11-01 00:19:40 +01:00
|
|
|
} else if (S_ISFIFO(file_stat.st_mode)) {
|
|
|
|
type = FIF; /* FIFO */
|
2024-11-13 01:28:17 +01:00
|
|
|
color = FIF_COLOR;
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If file is to be marked */
|
|
|
|
if (ftype == 1 || ftype == 2) {
|
|
|
|
/* Force if user is marking all files */
|
2024-11-17 15:49:24 +01:00
|
|
|
int force = ftype == 2 ? 1 : 0;
|
|
|
|
arraylist_add(marked, filename, path, NULL, type, icon_str, DEF_COLOR, 1,
|
2024-11-13 01:28:17 +01:00
|
|
|
force);
|
2024-11-01 00:19:40 +01:00
|
|
|
/* free type and return without allocating more stuff */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get last modified time */
|
|
|
|
size_t time_size = 17;
|
2024-11-17 20:36:27 +01:00
|
|
|
char time[time_size];
|
2024-11-01 00:19:40 +01:00
|
|
|
/* Format last modified time to a string */
|
|
|
|
strftime(time, time_size, "%Y-%m-%d %H:%M", localtime(&file_stat.st_mtime));
|
|
|
|
|
|
|
|
/* get file size */
|
|
|
|
double bytes = file_stat.st_size;
|
|
|
|
|
|
|
|
if (dirs_size) {
|
2024-11-17 15:49:24 +01:00
|
|
|
/* dirs_size is 1, so calculate disk usage */
|
2024-11-01 00:19:40 +01:00
|
|
|
if (S_ISDIR(file_stat.st_mode)) {
|
|
|
|
/* at most 15 fd opened */
|
|
|
|
total_dir_size = 0;
|
|
|
|
nftw(path, &get_directory_size, 15, FTW_PHYS);
|
|
|
|
bytes = total_dir_size;
|
|
|
|
}
|
|
|
|
}
|
2024-11-13 01:15:56 +01:00
|
|
|
/* 4 before decimal + 1 dot + decimal_place (after decimal) +
|
2024-11-17 20:36:27 +01:00
|
|
|
* unit length (1 for K, 3 for KiB, taking units[1] as B never changes) + 1 space + 1 null */
|
2024-11-13 00:45:53 +01:00
|
|
|
static const char* units[] = {"B", "K", "M", "G", "T", "P"};
|
2024-11-13 01:15:56 +01:00
|
|
|
int size_size = 4 + 1 + decimal_place + strlen(units[1]) + 1 + 1;
|
2024-11-17 20:36:27 +01:00
|
|
|
char size[size_size];
|
2024-11-01 00:19:40 +01:00
|
|
|
int unit = 0;
|
|
|
|
while (bytes > 1024) {
|
|
|
|
bytes /= 1024;
|
|
|
|
unit++;
|
|
|
|
}
|
|
|
|
/* display sizes and check if there are decimal places */
|
|
|
|
if (bytes == (unsigned int) bytes) {
|
|
|
|
sprintf(size, "%d%s", (unsigned int) bytes, units[unit]);
|
|
|
|
} else {
|
2024-11-13 01:15:56 +01:00
|
|
|
sprintf(size, "%.*f%s", decimal_place, bytes, units[unit]);
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
/* get file mode string */
|
2024-11-17 20:36:27 +01:00
|
|
|
char mode_str[11];
|
|
|
|
mode_str[0] = S_ISDIR(file_stat.st_mode) ? 'd' : '-';
|
|
|
|
mode_str[1] = (file_stat.st_mode & S_IRUSR) ? 'r' : '-';
|
|
|
|
mode_str[2] = (file_stat.st_mode & S_IWUSR) ? 'w' : '-';
|
|
|
|
mode_str[3] = (file_stat.st_mode & S_IXUSR) ? 'x' : '-';
|
|
|
|
mode_str[4] = (file_stat.st_mode & S_IRGRP) ? 'r' : '-';
|
|
|
|
mode_str[5] = (file_stat.st_mode & S_IWGRP) ? 'w' : '-';
|
|
|
|
mode_str[6] = (file_stat.st_mode & S_IXGRP) ? 'x' : '-';
|
|
|
|
mode_str[7] = (file_stat.st_mode & S_IROTH) ? 'r' : '-';
|
|
|
|
mode_str[8] = (file_stat.st_mode & S_IWOTH) ? 'w' : '-';
|
|
|
|
mode_str[9] = (file_stat.st_mode & S_IXOTH) ? 'x' : '-';
|
2024-11-17 23:24:31 +01:00
|
|
|
mode_str[10] = 0;
|
2024-11-17 20:36:27 +01:00
|
|
|
|
2024-11-01 00:19:40 +01:00
|
|
|
if (mode_str[0] == '-' && (mode_str[3] == 'x' || mode_str[6] == 'x' || mode_str[9] == 'x')) {
|
2024-11-13 01:28:17 +01:00
|
|
|
color = EXE_COLOR;
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
|
2024-11-17 23:24:31 +01:00
|
|
|
/* mode_str + time(17) + size_size + 2 spaces + 1 null */
|
|
|
|
size_t stat_size = 11 + 17 + size_size + 3;
|
2024-11-01 00:19:40 +01:00
|
|
|
char *total_stat = memalloc(stat_size);
|
2024-11-17 23:24:31 +01:00
|
|
|
sprintf(total_stat, "%s %s %-*s", mode_str, time, size_size, size);
|
2024-11-01 00:19:40 +01:00
|
|
|
|
|
|
|
/* DIR if color is blue */
|
2024-11-12 17:53:50 +01:00
|
|
|
if (color == 34)
|
2024-11-17 15:49:24 +01:00
|
|
|
arraylist_add(tmp1, filename, path, total_stat, type, icon_str, color, 0, 0);
|
2024-11-01 00:19:40 +01:00
|
|
|
else
|
2024-11-17 15:49:24 +01:00
|
|
|
arraylist_add(tmp2, filename, path, total_stat, type, icon_str, color, 0, 0);
|
2024-03-29 15:05:01 +01:00
|
|
|
}
|
2024-03-14 16:29:03 +01:00
|
|
|
|
2024-03-09 20:14:26 +01:00
|
|
|
/*
|
2024-11-01 00:19:40 +01:00
|
|
|
* Print files in directory window
|
2024-03-09 20:14:26 +01:00
|
|
|
*/
|
2024-11-05 00:02:35 +01:00
|
|
|
void list_files(void)
|
2024-03-09 18:37:04 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
/* calculate range of files to show */
|
|
|
|
long range = files->length;
|
|
|
|
/* not highlight if no files in directory */
|
|
|
|
if (range == 0 && errno == 0) {
|
2024-11-21 15:28:15 +01:00
|
|
|
for (int i = 0; i < rows - 1; i++) {
|
2024-11-13 00:45:53 +01:00
|
|
|
move_cursor(i + 1, 1);
|
|
|
|
bprintf("\033[K");
|
|
|
|
}
|
2024-11-01 00:19:40 +01:00
|
|
|
move_cursor(1, half_width);
|
2024-11-13 00:45:53 +01:00
|
|
|
bprintf("empty directory");
|
2024-11-01 00:19:40 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-21 15:28:15 +01:00
|
|
|
long overflow = 0;
|
|
|
|
if (sel_file > rows - 2) {
|
|
|
|
/* overflown */
|
|
|
|
overflow = sel_file - (rows - 2);
|
|
|
|
}
|
|
|
|
if (range > rows - 1) {
|
2024-11-01 00:19:40 +01:00
|
|
|
/* if there are more files than rows available to display
|
|
|
|
* shrink range to avaiable rows to display with
|
|
|
|
* overflow to keep the number of iterations to be constant */
|
2024-11-21 15:28:15 +01:00
|
|
|
range = rows - 1 + overflow;
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
move_cursor(1, 1);
|
|
|
|
bprintf("\033[2J");
|
|
|
|
|
2024-11-21 15:28:15 +01:00
|
|
|
int max_flen = 0;
|
2024-11-01 00:19:40 +01:00
|
|
|
for (long i = overflow; i < range; i++) {
|
|
|
|
int is_selected = 0;
|
|
|
|
if ((overflow == 0 && i == sel_file) ||
|
|
|
|
(overflow != 0 && i == sel_file)) {
|
|
|
|
is_selected = 1;
|
|
|
|
/* check for marked files */
|
|
|
|
long num_marked = marked->length;
|
|
|
|
if (num_marked > 0) {
|
|
|
|
/* Determine length of formatted string */
|
|
|
|
int m_len = snprintf(NULL, 0, "[%ld] selected", num_marked);
|
2024-11-17 20:39:46 +01:00
|
|
|
char selected[m_len + 1];
|
2024-11-01 00:19:40 +01:00
|
|
|
|
|
|
|
snprintf(selected, m_len + 1, "[%ld] selected", num_marked);
|
|
|
|
wpprintw("(%ld/%ld) %s %s", sel_file + 1, files->length, selected, cwd);
|
2024-11-17 20:36:27 +01:00
|
|
|
} else {
|
2024-11-01 00:19:40 +01:00
|
|
|
wpprintw("(%ld/%ld) %s", sel_file + 1, files->length, cwd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* print the actual filename and stats */
|
2024-11-13 01:15:56 +01:00
|
|
|
char *line = get_line(files, i, show_details, show_icons);
|
2024-11-12 17:53:50 +01:00
|
|
|
int color = files->items[i].color;
|
2024-11-21 15:28:15 +01:00
|
|
|
int width = strlen(line);
|
|
|
|
if (width > max_flen) {
|
|
|
|
max_flen = width;
|
|
|
|
}
|
2024-11-01 00:19:40 +01:00
|
|
|
/* check is file marked for action */
|
2024-11-17 15:49:24 +01:00
|
|
|
int is_marked = arraylist_search(marked, files->items[i].path, 0) != -1;
|
2024-11-21 15:28:15 +01:00
|
|
|
move_cursor(i - overflow + 1, 1);
|
2024-11-17 23:24:31 +01:00
|
|
|
if (is_marked) color = MAR_COLOR;
|
|
|
|
bprintf("\033[30m\033[%dm%s\033[m\n",
|
2024-11-12 17:53:50 +01:00
|
|
|
is_selected ? color + 10 : color, line);
|
2024-11-01 00:19:40 +01:00
|
|
|
|
|
|
|
free(line);
|
|
|
|
}
|
2024-11-21 15:28:15 +01:00
|
|
|
half_width = max_flen;
|
2024-11-01 00:19:40 +01:00
|
|
|
|
|
|
|
/* show file content every time cursor changes */
|
|
|
|
show_file_content();
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 20:14:26 +01:00
|
|
|
/*
|
2024-03-10 21:23:15 +01:00
|
|
|
* Get file content into buffer and show it to preview window
|
2024-03-09 20:14:26 +01:00
|
|
|
*/
|
2024-11-05 00:02:35 +01:00
|
|
|
void show_file_content(void)
|
2024-03-09 18:37:04 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
file current_file = files->items[sel_file];
|
|
|
|
|
|
|
|
move_cursor(1, half_width);
|
|
|
|
if (current_file.type == DRY) {
|
|
|
|
ArrayList *files_visit;
|
|
|
|
populate_files(current_file.name, 0, &files_visit);
|
2024-11-21 15:28:15 +01:00
|
|
|
for (long i = 0; i < files_visit->length && i < rows - 1; i++) {
|
2024-11-17 15:49:24 +01:00
|
|
|
char *line = get_line(files_visit, i, 0, show_icons);
|
2024-11-12 17:53:50 +01:00
|
|
|
int color = files_visit->items[i].color;
|
2024-11-01 00:19:40 +01:00
|
|
|
move_cursor(i + 1, half_width);
|
2024-11-12 17:53:50 +01:00
|
|
|
bprintf("\033[K\033[%dm%s\033[m\n", color, line);
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
arraylist_free(files_visit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FILE *file = fopen(current_file.path, "r");
|
2024-11-17 19:56:58 +01:00
|
|
|
if (!file) {
|
2024-11-01 00:19:40 +01:00
|
|
|
bprintf("Unable to read %s", current_file.name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-13 10:48:13 +01:00
|
|
|
int c;
|
|
|
|
/* Check if its binary */
|
|
|
|
while ((c = fgetc(file)) != EOF) {
|
|
|
|
if (c == '\0') {
|
|
|
|
bprintf("binary");
|
|
|
|
return;
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
int pipe_fd[2];
|
|
|
|
if (pipe(pipe_fd) == -1) {
|
|
|
|
perror("pipe");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
/* Child */
|
2024-11-13 10:48:13 +01:00
|
|
|
close(pipe_fd[0]);
|
|
|
|
dup2(pipe_fd[1], STDOUT_FILENO);
|
|
|
|
dup2(pipe_fd[1], STDERR_FILENO);
|
|
|
|
close(pipe_fd[1]);
|
|
|
|
execlp("vip", "vip", "-c", current_file.name, NULL);
|
2024-11-01 00:19:40 +01:00
|
|
|
_exit(1);
|
|
|
|
} else if (pid > 0) {
|
|
|
|
/* Parent */
|
2024-11-13 10:48:13 +01:00
|
|
|
close(pipe_fd[1]);
|
|
|
|
char buffer[4096];
|
|
|
|
int row = 1;
|
|
|
|
FILE *stream = fdopen(pipe_fd[0], "r");
|
2024-11-17 19:56:58 +01:00
|
|
|
while (fgets(buffer, sizeof(buffer), stream) && row <= rows - 1) {
|
2024-11-13 10:48:13 +01:00
|
|
|
buffer[strcspn(buffer, "\n")] = 0;
|
2024-11-21 12:54:55 +01:00
|
|
|
size_t buflen = strlen(buffer);
|
|
|
|
if (buffer[0] == '\0' || strspn(buffer, " \t") == buflen) {
|
2024-11-13 10:48:13 +01:00
|
|
|
move_cursor(row++, half_width);
|
2024-11-21 15:28:15 +01:00
|
|
|
bprintf("\n");
|
2024-11-13 10:48:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
2024-11-21 12:54:55 +01:00
|
|
|
move_cursor(row++, half_width);
|
|
|
|
/* Visible length */
|
|
|
|
size_t len = 0;
|
|
|
|
for (size_t i = 0; i < buflen; i++) {
|
|
|
|
if (buffer[i] == '\033' && buffer[i + 1] == '[') {
|
|
|
|
char *end = strpbrk(buffer + i, "ABCDEFGHJKfhlmus");
|
|
|
|
if (end) {
|
|
|
|
char ansiseq[128];
|
|
|
|
int start = i;
|
|
|
|
/* Find the last 'm' after the escape character */
|
|
|
|
int length = end - (buffer + start) + 1;
|
|
|
|
/* Ensure it fits within the buffer */
|
|
|
|
if (length < sizeof(ansiseq)) {
|
|
|
|
strncpy(ansiseq, buffer + start, length);
|
|
|
|
ansiseq[length] = '\0';
|
|
|
|
bprintf("%s", ansiseq);
|
|
|
|
/* Update the index to the character after the sequence */
|
|
|
|
i += length - 1;
|
|
|
|
continue;
|
|
|
|
}
|
2024-11-21 15:28:15 +01:00
|
|
|
} else {
|
|
|
|
bprintf("%c", buffer[i]);
|
2024-11-13 10:48:13 +01:00
|
|
|
}
|
2024-11-21 12:54:55 +01:00
|
|
|
} else {
|
|
|
|
bprintf("%c", buffer[i]);
|
|
|
|
len++;
|
2024-11-13 10:48:13 +01:00
|
|
|
}
|
2024-11-21 15:28:15 +01:00
|
|
|
if (len && (len) % (cols - half_width + 1) == 0) {
|
|
|
|
if (row + 1 < rows) {
|
|
|
|
move_cursor(row++, half_width);
|
|
|
|
}
|
|
|
|
}
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
}
|
2024-11-13 10:48:13 +01:00
|
|
|
fclose(stream);
|
2024-11-01 00:19:40 +01:00
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
} else {
|
|
|
|
wpprintw("fork failed: %s", strerror(errno));
|
|
|
|
}
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
|
|
|
|
2024-03-11 16:57:41 +01:00
|
|
|
/*
|
|
|
|
* Opens $EDITOR to edit the file
|
|
|
|
*/
|
2024-11-05 00:02:35 +01:00
|
|
|
void edit_file(void)
|
2024-03-12 21:59:36 +01:00
|
|
|
{
|
2024-11-17 19:56:58 +01:00
|
|
|
if (!strcmp(editor, "")) {
|
2024-11-13 01:08:32 +01:00
|
|
|
editor = getenv("EDITOR");
|
2024-11-17 19:56:58 +01:00
|
|
|
if (!editor) {
|
2024-11-13 01:08:32 +01:00
|
|
|
wpprintw("$EDITOR not defined");
|
|
|
|
return;
|
|
|
|
}
|
2024-11-01 00:19:40 +01:00
|
|
|
} else {
|
|
|
|
char *filename = files->items[sel_file].path;
|
|
|
|
|
2024-11-06 16:11:32 +01:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
/* Child process */
|
|
|
|
execlp(editor, editor, filename, NULL);
|
|
|
|
_exit(1); /* Exit if exec fails */
|
|
|
|
} else if (pid > 0) {
|
|
|
|
/* Parent process */
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
} else {
|
|
|
|
/* Fork failed */
|
|
|
|
wpprintw("fork failed: %s", strerror(errno));
|
|
|
|
}
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
2024-03-11 16:57:41 +01:00
|
|
|
}
|
|
|
|
|
2024-11-05 00:02:35 +01:00
|
|
|
void toggle_executable(void)
|
2024-03-29 15:05:01 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
file f = files->items[sel_file];
|
|
|
|
struct stat st;
|
|
|
|
if (stat(f.path, &st) == -1) {
|
2024-11-17 19:37:53 +01:00
|
|
|
wpprintw("stat failed: %s (Press any key to continue)", strerror(errno));
|
2024-11-17 19:38:22 +01:00
|
|
|
readch();
|
2024-11-17 19:37:53 +01:00
|
|
|
return;
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
if (f.type == DRY)
|
|
|
|
return;
|
|
|
|
/* chmod by xor executable bits */
|
|
|
|
if (chmod(f.path, st.st_mode ^ (S_IXUSR | S_IXGRP | S_IXOTH)) == -1) {
|
2024-11-17 19:37:53 +01:00
|
|
|
wpprintw("Error toggling executable: %s (Press any key to continue)", strerror(errno));
|
|
|
|
readch();
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
2024-03-29 15:05:01 +01:00
|
|
|
}
|
|
|
|
|
2024-11-13 01:08:32 +01:00
|
|
|
void replace_home(char *str)
|
2024-04-06 22:41:36 +02:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
char *home = getenv("HOME");
|
2024-11-17 19:56:58 +01:00
|
|
|
if (!home) {
|
2024-11-01 00:19:40 +01:00
|
|
|
wpprintw("$HOME not defined");
|
2024-11-13 01:08:32 +01:00
|
|
|
return;
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
2024-11-13 01:08:32 +01:00
|
|
|
int len = strlen(str) + strlen(home) + 1;
|
|
|
|
char newstr[len];
|
2024-11-01 00:19:40 +01:00
|
|
|
/* replace ~ with home */
|
2024-11-13 01:08:32 +01:00
|
|
|
snprintf(newstr, len, "%s%s", home, str + 1);
|
|
|
|
strcpy(str, newstr);
|
2024-04-06 22:41:36 +02:00
|
|
|
}
|
|
|
|
|
2024-11-05 00:02:35 +01:00
|
|
|
int write_last_d(void)
|
2024-03-20 21:29:04 +01:00
|
|
|
{
|
2024-11-13 01:08:32 +01:00
|
|
|
if (!strcmp(last_d, "")) {
|
|
|
|
strcpy(last_d, getenv("CCC_LAST_D"));
|
|
|
|
if (!strcmp(last_d, "")) {
|
2024-11-17 19:10:26 +01:00
|
|
|
wpprintw("$CCC_LAST_D not defined (Press any key to continue)");
|
2024-11-13 01:08:32 +01:00
|
|
|
return -1;
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
2024-11-13 01:08:32 +01:00
|
|
|
} else {
|
|
|
|
if (last_d[0] == '~')
|
|
|
|
replace_home(last_d);
|
|
|
|
|
|
|
|
char last_ddup[PATH_MAX];
|
|
|
|
strcpy(last_ddup, last_d);
|
2024-11-01 00:19:40 +01:00
|
|
|
|
|
|
|
char *last_d_dir = strrchr(last_ddup, '/');
|
2024-11-17 19:56:58 +01:00
|
|
|
if (last_d_dir) {
|
2024-11-01 00:19:40 +01:00
|
|
|
*last_d_dir = '\0'; /* truncate string */
|
|
|
|
}
|
|
|
|
mkdir_p(last_ddup);
|
|
|
|
FILE *last_d_file = fopen(last_d, "w");
|
2024-11-17 19:56:58 +01:00
|
|
|
if (!last_d_file) {
|
2024-11-17 19:10:26 +01:00
|
|
|
wpprintw("Cannot open last directory file (Press any key to continue)");
|
2024-11-01 00:19:40 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fwrite(cwd, strlen(cwd), sizeof(char), last_d_file);
|
|
|
|
fclose(last_d_file);
|
|
|
|
}
|
|
|
|
return 0;
|
2024-03-19 23:07:05 +01:00
|
|
|
}
|
|
|
|
|
2024-11-01 00:19:40 +01:00
|
|
|
int sort_compare(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return strcmp(((file *) a)->name, ((file *) b)->name);
|
2024-04-07 23:25:58 +02:00
|
|
|
}
|
|
|
|
|
2024-11-05 00:02:35 +01:00
|
|
|
void sort_files(void)
|
2024-04-07 23:25:58 +02:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
qsort(files->items, files->length, sizeof(file), sort_compare);
|
2024-04-07 23:25:58 +02:00
|
|
|
}
|
|
|
|
|
2024-04-10 14:51:39 +02:00
|
|
|
char *get_panel_string(char *prompt)
|
2024-04-02 03:30:28 +02:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
size_t bufsize = 128;
|
|
|
|
char *buf = memalloc(bufsize);
|
|
|
|
size_t buflen = 0;
|
|
|
|
buf[0] = '\0';
|
2024-11-17 19:10:26 +01:00
|
|
|
bprintf("\033[?25h");
|
2024-11-01 00:19:40 +01:00
|
|
|
while (1) {
|
2024-11-17 19:10:26 +01:00
|
|
|
wpprintw("%s%s", prompt, buf);
|
|
|
|
int c = readch();
|
2024-11-01 00:19:40 +01:00
|
|
|
if (c == BACKSPACE) {
|
|
|
|
if (buflen != 0) {
|
|
|
|
buf[--buflen] = '\0';
|
|
|
|
}
|
|
|
|
} else if (c == '\033') {
|
|
|
|
wpprintw("");
|
|
|
|
free(buf);
|
2024-11-17 19:10:26 +01:00
|
|
|
bprintf("\033[?25l");
|
2024-11-01 00:19:40 +01:00
|
|
|
return NULL;
|
|
|
|
} else if (c == '\r') {
|
|
|
|
wpprintw("");
|
|
|
|
if (buflen != 0) {
|
2024-11-17 19:10:26 +01:00
|
|
|
bprintf("\033[?25l");
|
2024-11-01 00:19:40 +01:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
} else if (!iscntrl(c) && c < 128) {
|
|
|
|
if (buflen == bufsize - 1) {
|
|
|
|
bufsize *= 2;
|
|
|
|
buf = realloc(buf, bufsize);
|
|
|
|
}
|
|
|
|
buf[buflen++] = c;
|
|
|
|
buf[buflen] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-13 01:08:32 +01:00
|
|
|
if (buf[0] == '~')
|
|
|
|
replace_home(buf);
|
|
|
|
|
2024-11-17 19:10:26 +01:00
|
|
|
bprintf("\033[?25l");
|
2024-11-13 01:08:32 +01:00
|
|
|
return buf;
|
2024-04-10 14:51:39 +02:00
|
|
|
}
|
|
|
|
|
2024-11-05 00:02:35 +01:00
|
|
|
void rename_file(void)
|
2024-04-10 14:51:39 +02:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
char *filename = files->items[sel_file].path;
|
|
|
|
char *input = get_panel_string("Rename file: ");
|
2024-11-17 19:10:26 +01:00
|
|
|
if (!input) {
|
|
|
|
return;
|
|
|
|
}
|
2024-11-17 19:56:58 +01:00
|
|
|
char newfilename[PATH_MAX];
|
|
|
|
strcpy(newfilename, filename);
|
2024-11-01 00:19:40 +01:00
|
|
|
/* remove basename of newfilename */
|
|
|
|
char *last_slash = strrchr(newfilename, '/');
|
|
|
|
*last_slash = '\0';
|
|
|
|
/* add the slash back to newfilename */
|
|
|
|
strcat(newfilename, "/");
|
|
|
|
strcat(newfilename, input);
|
|
|
|
if (rename(filename, newfilename)) {
|
2024-11-17 19:10:26 +01:00
|
|
|
wpprintw("rename failed: %s (Press any key to continue)", strerror(errno));
|
|
|
|
readch();
|
2024-11-01 00:19:40 +01:00
|
|
|
} else {
|
|
|
|
change_dir(cwd, 0, 0);
|
|
|
|
wpprintw("Renamed %s to %s", filename, newfilename);
|
|
|
|
}
|
|
|
|
free(input);
|
2024-04-10 14:51:39 +02:00
|
|
|
}
|
|
|
|
|
2024-11-05 00:02:35 +01:00
|
|
|
void goto_dir(void)
|
2024-04-10 14:51:39 +02:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
char *input = get_panel_string("Goto dir: ");
|
2024-11-17 19:56:58 +01:00
|
|
|
if (!input) {
|
|
|
|
return;
|
|
|
|
}
|
2024-11-01 00:19:40 +01:00
|
|
|
struct stat st;
|
|
|
|
if (lstat(input, &st)) {
|
2024-11-17 19:10:26 +01:00
|
|
|
wpprintw("lstat failed: %s (Press any key to continue)", strerror(errno));
|
|
|
|
readch();
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
|
|
|
/* chdir to directory from argument */
|
|
|
|
if (S_ISDIR(st.st_mode) && chdir(input)) {
|
2024-11-17 19:10:26 +01:00
|
|
|
wpprintw("chdir failed: %s (Press any key to continue)", strerror(errno));
|
|
|
|
readch();
|
2024-11-01 00:19:40 +01:00
|
|
|
}
|
2024-11-17 20:27:42 +01:00
|
|
|
char new_cwd[PATH_MAX];
|
|
|
|
getcwd(new_cwd, PATH_MAX);
|
|
|
|
change_dir(new_cwd, 0, 0);
|
2024-11-01 00:19:40 +01:00
|
|
|
free(input);
|
2024-04-10 14:51:39 +02:00
|
|
|
}
|
|
|
|
|
2024-11-05 00:02:35 +01:00
|
|
|
void create_dir(void)
|
2024-04-10 14:51:39 +02:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
char *input = get_panel_string("New dir: ");
|
2024-11-17 19:56:58 +01:00
|
|
|
if (!input) {
|
|
|
|
return;
|
|
|
|
}
|
2024-11-17 20:39:46 +01:00
|
|
|
char newfilename[PATH_MAX];
|
2024-11-01 00:19:40 +01:00
|
|
|
snprintf(newfilename, PATH_MAX, "%s/%s", cwd, input);
|
2024-11-17 20:39:46 +01:00
|
|
|
|
2024-11-01 00:19:40 +01:00
|
|
|
if (access(newfilename, F_OK) != 0) {
|
|
|
|
mkdir_p(newfilename);
|
|
|
|
change_dir(cwd, 0, 0);
|
|
|
|
wpprintw("Created %s", input);
|
|
|
|
} else {
|
|
|
|
wpprintw("Directory already exist");
|
|
|
|
}
|
|
|
|
free(input);
|
2024-04-10 14:51:39 +02:00
|
|
|
}
|
|
|
|
|
2024-11-05 00:02:35 +01:00
|
|
|
void create_file(void)
|
2024-04-10 14:51:39 +02:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
char *input = get_panel_string("New file: ");
|
2024-11-17 19:56:58 +01:00
|
|
|
if (!input) {
|
|
|
|
return;
|
|
|
|
}
|
2024-11-01 00:19:40 +01:00
|
|
|
FILE *f = fopen(input, "w+");
|
|
|
|
fclose(f);
|
|
|
|
change_dir(cwd, 0, 0);
|
|
|
|
wpprintw("Created %s", input);
|
|
|
|
free(input);
|
2024-04-02 03:30:28 +02:00
|
|
|
}
|
|
|
|
|
2024-11-05 00:02:35 +01:00
|
|
|
void delete_files(void)
|
2024-04-02 03:30:28 +02:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
if (marked->length) {
|
|
|
|
char *trash_dir = check_trash_dir();
|
2024-11-17 19:56:58 +01:00
|
|
|
if (trash_dir) {
|
2024-11-01 00:19:40 +01:00
|
|
|
for (int i = 0; i < marked->length; i++) {
|
2024-11-17 20:39:46 +01:00
|
|
|
char new_path[PATH_MAX];
|
2024-11-01 00:19:40 +01:00
|
|
|
snprintf(new_path, PATH_MAX, "%s/%s", trash_dir, marked->items[i].name);
|
|
|
|
if (rename(marked->items[i].path, new_path)) {
|
|
|
|
wpprintw("delete failed: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
2024-11-17 20:45:32 +01:00
|
|
|
change_dir(cwd, 0, 0);
|
2024-10-11 21:03:31 +02:00
|
|
|
for (int i = 0; i < marked->length; i++) {
|
|
|
|
arraylist_remove(marked, 0);
|
|
|
|
}
|
2024-11-01 00:19:40 +01:00
|
|
|
} else {
|
|
|
|
wpprintw("TODO: implement hard delete");
|
|
|
|
}
|
|
|
|
}
|
2024-04-02 03:30:28 +02:00
|
|
|
}
|
|
|
|
|
2024-11-17 16:17:41 +01:00
|
|
|
void start_shell(void)
|
|
|
|
{
|
|
|
|
bprintf("\033[2J\033[?25h");
|
|
|
|
move_cursor(1, 1);
|
|
|
|
char shell[PATH_MAX];
|
2024-11-19 00:12:25 +01:00
|
|
|
char *shellenv = getenv("SHELL");
|
|
|
|
if (!shellenv) {
|
2024-11-17 16:17:41 +01:00
|
|
|
strcpy(shell, "sh");
|
|
|
|
} else {
|
2024-11-19 00:12:25 +01:00
|
|
|
strcpy(shell, shellenv);
|
|
|
|
}
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
/* Child process */
|
|
|
|
execlp(shell, shell, NULL);
|
|
|
|
_exit(1); /* Exit if exec fails */
|
|
|
|
} else if (pid > 0) {
|
|
|
|
/* Parent process */
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
bprintf("\033[?25l");
|
|
|
|
} else {
|
|
|
|
/* Fork failed */
|
|
|
|
wpprintw("fork failed: %s", strerror(errno));
|
2024-11-17 16:17:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 19:10:26 +01:00
|
|
|
void yank_clipboard(void)
|
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
/* Child process */
|
|
|
|
execlp(clipboard, clipboard, files->items[sel_file].name, NULL);
|
|
|
|
_exit(1); /* Exit if exec fails */
|
|
|
|
} else if (pid > 0) {
|
|
|
|
/* Parent process */
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
bprintf("\033[?25l");
|
|
|
|
} else {
|
|
|
|
/* Fork failed */
|
|
|
|
wpprintw("fork failed: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 20:04:15 +01:00
|
|
|
void view_file_attr(void)
|
|
|
|
{
|
|
|
|
bprintf("\033[2J");
|
|
|
|
move_cursor(1, 1);
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
/* Child process */
|
|
|
|
execlp("stat", "stat", files->items[sel_file].name, NULL);
|
|
|
|
_exit(1); /* Exit if exec fails */
|
|
|
|
} else if (pid > 0) {
|
|
|
|
/* Parent process */
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
} else {
|
|
|
|
/* Fork failed */
|
|
|
|
wpprintw("fork failed: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
readch();
|
|
|
|
}
|
|
|
|
|
2024-11-17 23:53:06 +01:00
|
|
|
void show_history(void)
|
|
|
|
{
|
|
|
|
bprintf("\033[2J");
|
|
|
|
move_cursor(1, 1);
|
|
|
|
char history_path[PATH_MAX];
|
|
|
|
strcpy(history_path, "~/.cache/ccc/history");
|
|
|
|
replace_home(history_path);
|
|
|
|
FILE *history_file = fopen(history_path, "r");
|
|
|
|
char buffer[PATH_MAX];
|
|
|
|
int row = 1;
|
|
|
|
while (fgets(buffer, sizeof(buffer), history_file) && row <= rows - 1) {
|
|
|
|
move_cursor(row++, 1);
|
|
|
|
bprintf(buffer);
|
|
|
|
|
|
|
|
}
|
|
|
|
fclose(history_file);
|
|
|
|
readch();
|
|
|
|
}
|
|
|
|
|
2024-11-18 01:21:09 +01:00
|
|
|
void open_with(void)
|
|
|
|
{
|
|
|
|
char *input = get_panel_string("open with: ");
|
|
|
|
if (!input) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
/* Child process */
|
|
|
|
if (marked->length > 0) {
|
|
|
|
char *args[marked->length + 2];
|
|
|
|
args[0] = input;
|
|
|
|
for (int i = 0; i < marked->length; i++) {
|
|
|
|
args[i + 1] = marked->items[i].name;
|
|
|
|
}
|
|
|
|
args[marked->length + 1] = NULL;
|
|
|
|
execvp(input, args);
|
|
|
|
} else {
|
|
|
|
execlp(input, input, files->items[sel_file].name, NULL);
|
|
|
|
}
|
|
|
|
_exit(1); /* Exit if exec fails */
|
|
|
|
} else if (pid > 0) {
|
|
|
|
/* Parent process */
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
} else {
|
|
|
|
/* Fork failed */
|
|
|
|
wpprintw("fork failed: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void open_detached(void)
|
|
|
|
{
|
|
|
|
char *input = get_panel_string("open with (detached): ");
|
|
|
|
if (!input) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
/* Child process */
|
|
|
|
if (marked->length > 0) {
|
|
|
|
char *args[marked->length + 3];
|
|
|
|
args[0] = "nohup";
|
|
|
|
args[1] = input;
|
|
|
|
for (int i = 0; i < marked->length; i++) {
|
|
|
|
args[i + 2] = marked->items[i].name;
|
|
|
|
}
|
|
|
|
args[marked->length + 1] = NULL;
|
|
|
|
execvp("nohup", args);
|
|
|
|
} else {
|
|
|
|
execlp("nohup", "nohup", input, files->items[sel_file].name, NULL);
|
|
|
|
}
|
|
|
|
_exit(1); /* Exit if exec fails */
|
|
|
|
} else if (pid > 0) {
|
|
|
|
/* Parent process */
|
|
|
|
waitpid(pid, NULL, 0);
|
|
|
|
} else {
|
|
|
|
/* Fork failed */
|
|
|
|
wpprintw("fork failed: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 15:46:06 +01:00
|
|
|
/*
|
2024-03-17 11:09:22 +01:00
|
|
|
* Print line to the panel
|
2024-03-14 15:46:06 +01:00
|
|
|
*/
|
2024-03-29 15:05:01 +01:00
|
|
|
void wpprintw(const char *fmt, ...)
|
2024-03-14 15:46:06 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
char buffer[256];
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buffer, sizeof(buffer), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
move_cursor(rows, 1);
|
|
|
|
/* Clear line and print formatted string */
|
|
|
|
bprintf("\033[K%s", buffer);
|
2024-03-14 15:46:06 +01:00
|
|
|
}
|
|
|
|
|
2024-11-01 00:19:40 +01:00
|
|
|
void move_cursor(int row, int col)
|
2024-03-09 18:37:04 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
bprintf("\033[%d;%dH", row, col);
|
|
|
|
}
|
|
|
|
|
2024-11-17 19:10:26 +01:00
|
|
|
int readch(void)
|
2024-11-01 00:19:40 +01:00
|
|
|
{
|
|
|
|
int nread;
|
|
|
|
char c;
|
|
|
|
while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
|
|
|
|
if (nread == -1 && errno != EAGAIN) {
|
|
|
|
die("read");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == '\033') {
|
|
|
|
char seq[3];
|
|
|
|
|
|
|
|
if (read(STDIN_FILENO, &seq[0], 1) != 1)
|
|
|
|
return '\033';
|
|
|
|
if (read(STDIN_FILENO, &seq[1], 1) != 1)
|
|
|
|
return '\033';
|
|
|
|
|
|
|
|
if (seq[0] == '[') {
|
|
|
|
if (seq[1] >= '0' && seq[1] <= '9') {
|
|
|
|
if (read(STDIN_FILENO, &seq[2], 1) != 1)
|
|
|
|
return '\033';
|
|
|
|
if (seq[2] == '~') {
|
|
|
|
switch (seq[1]) {
|
|
|
|
case '1': return HOME_KEY;
|
|
|
|
case '3': return DEL_KEY;
|
|
|
|
case '4': return END_KEY;
|
|
|
|
case '5': return PAGE_UP;
|
|
|
|
case '6': return PAGE_DOWN;
|
|
|
|
case '7': return HOME_KEY;
|
|
|
|
case '8': return END_KEY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (seq[1]) {
|
|
|
|
case 'A': return ARROW_UP;
|
|
|
|
case 'B': return ARROW_DOWN;
|
|
|
|
case 'C': return ARROW_RIGHT;
|
|
|
|
case 'D': return ARROW_LEFT;
|
|
|
|
case 'F': return END_KEY;
|
|
|
|
case 'H': return HOME_KEY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (seq[0] == 'O') {
|
|
|
|
switch (seq[1]) {
|
|
|
|
case 'F': return END_KEY;
|
|
|
|
case 'H': return HOME_KEY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return '\033';
|
|
|
|
} else {
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_cursor_position(int *rows, int *cols)
|
|
|
|
{
|
|
|
|
char buf[32];
|
|
|
|
unsigned int i = 0;
|
|
|
|
bprintf("\033[6n");
|
|
|
|
while (i < sizeof(buf) - 1) {
|
|
|
|
if (read(STDIN_FILENO, &buf[i], 1) != 1) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (buf[i] == 'R') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
buf[i] = '\0';
|
|
|
|
if (buf[0] != '\033' || buf[1] != '[') {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (sscanf(&buf[2], "%d;%d", rows, cols) != 2) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_window_size(int *row, int *col)
|
|
|
|
{
|
|
|
|
struct winsize ws;
|
|
|
|
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
|
|
|
|
/* Can't get window size */
|
|
|
|
bprintf("\033[999C\033[999B");
|
|
|
|
return get_cursor_position(row, col);
|
|
|
|
} else {
|
|
|
|
*col = ws.ws_col;
|
|
|
|
*row = ws.ws_row;
|
|
|
|
return 0;
|
|
|
|
}
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2024-11-01 00:19:40 +01:00
|
|
|
* printf, but write to STDOUT_FILENO
|
2024-03-09 18:37:04 +01:00
|
|
|
*/
|
2024-11-01 00:19:40 +01:00
|
|
|
void bprintf(const char *fmt, ...)
|
2024-03-09 18:37:04 +01:00
|
|
|
{
|
2024-11-01 00:19:40 +01:00
|
|
|
char buffer[512];
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
vsnprintf(buffer, sizeof(buffer), fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
write(STDOUT_FILENO, buffer, strlen(buffer));
|
2024-03-09 18:37:04 +01:00
|
|
|
}
|