From 9979966610bb3afea12f6363475c2c78fab139a8 Mon Sep 17 00:00:00 2001 From: Piotr Marendowski Date: Fri, 29 Mar 2024 22:43:28 +0100 Subject: [PATCH] Lay the groundwork for deleting files --- ccc.c | 73 ++++++++++++++++++++++++++++++++++++++++---------------- config.h | 6 ++++- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/ccc.c b/ccc.c index 09b746a..a50c190 100644 --- a/ccc.c +++ b/ccc.c @@ -18,6 +18,7 @@ /* functions' definitions */ void show_help(); void start_ccc(); +char *check_trash_dir(); void change_dir(const char *buf, int selection, int ftype); int mkdir_p(const char *destdir); void populate_files(const char *path, int ftype); @@ -39,8 +40,9 @@ long current_selection = 0; bool dirs_size = DIRS_SIZE; bool show_hidden = SHOW_HIDDEN; bool file_details = SHOW_DETAILS; +char *trash_dir; char *cwd; -char *p_cwd; /* previous cwd */ +char *p_cwd; /* previous cwd */ int half_width; ArrayList *files; ArrayList *marked; @@ -234,25 +236,9 @@ int main(int argc, char** argv) /* go to the trash dir */ case 't':; - #ifdef TRASH_DIR - char *trash_dir = TRASH_DIR; - #else - char *trash_dir = getenv("CCC_TRASH"); - #endif - if (trash_dir == NULL) { - wpprintw("$CCC_TRASH is not defined"); - } else { - if (access(trash_dir, F_OK) != 0) { - /* create the directory with 755 perm if it doesn't exit */ - if (mkdir_p(trash_dir) == -1) { - switch (errno) { - case EACCES: - wpprintw("Parent directory does not allow write permission or one of directories does not allow search access"); - } - } - } + char *trash_dir = check_trash_dir(); + if (trash_dir != NULL) change_dir(trash_dir, 0, 0); - } break; /* show directories' sizes */ @@ -302,7 +288,10 @@ int main(int argc, char** argv) /* delete */ case 'd':; if (marked->length) { - ; + char *trash_dir = check_trash_dir(); + if (trash_dir != NULL) + /* do your magic here */ + change_dir(trash_dir, 0, 0); } break; @@ -374,6 +363,50 @@ void start_ccc() init_windows(); } +/* Checks if trash directory is set and returns it */ +char *check_trash_dir() +{ + char *path = memalloc(PATH_MAX * sizeof(char)); + + #ifdef TRASH_DIR + trash_dir = TRASH_DIR; + #endif + + /* check if there is trash_dir */ + if (trash_dir == NULL) { + wpprintw("No trash directory defined"); + return NULL; + } else { + /* if trash_dir has ~ then make it $HOME */ + /* use path as trash_dir */ + if (trash_dir[0] == '~') { + char *home = getenv("HOME"); + if (home == NULL) { + wpprintw("$HOME is not defined, can't read the trash directory"); + return NULL; + } + /* replace ~ with home */ + snprintf(path, PATH_MAX, "%s%s", home, trash_dir + 1); + } + else { + strcpy(path, trash_dir); + } + + /* if has access to trash_dir */ + if (access(path, F_OK) != 0) { + /* create the directory with 755 perm if it doesn't exit */ + if (mkdir_p(path) == -1) { + switch (errno) { + case EACCES: + wpprintw("Parent directory does not allow write permission or one of directories does not allow search access"); + return NULL; + } + } + } + } + return path; +} + /* * Change directory in window with selection */ diff --git a/config.h b/config.h index f8abf36..d8ca159 100644 --- a/config.h +++ b/config.h @@ -8,8 +8,12 @@ #define DRAW_BORDERS true /* Draw borders around windows? */ #define DRAW_PREVIEW true /* Draw file preview? */ + #define SHOW_HIDDEN true /* show hidden files/dotfiles in preview */ -#define SHOW_DETAILS true /* show file details */ +#define SHOW_DETAILS true /* show file details */ + +/* will create this directory if doesn't exist! */ +#define TRASH_DIR "~/.local/ccc/trash/" /* set width offset for windows: +-------------%-------------+