From c50af43b7bedb3256d58fe122b4fa9276406faaa Mon Sep 17 00:00:00 2001 From: night0721 Date: Mon, 18 Nov 2024 00:21:09 +0000 Subject: [PATCH] Add open with w/ detached feature --- ccc.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- file.c | 4 ++-- 2 files changed, 71 insertions(+), 3 deletions(-) diff --git a/ccc.c b/ccc.c index 9330009..289fd85 100644 --- a/ccc.c +++ b/ccc.c @@ -71,6 +71,8 @@ void start_shell(void); void yank_clipboard(void); void view_file_attr(void); void show_history(void); +void open_with(void); +void open_detached(void); void wpprintw(const char *fmt, ...); void move_cursor(int row, int col); int readch(void); @@ -363,7 +365,12 @@ int main(int argc, char **argv) break; case 'o': + open_with(); + break; case 'O': + open_detached(); + break; + case 'x': view_file_attr(); break; @@ -373,7 +380,7 @@ int main(int argc, char **argv) break; case '1': case '2': case '3': case '4': case '5': case '6': case '7': - case '8': case '9': + case '8': case '9':; char envname[9]; snprintf(envname, 9, "CCC_FAV%d", ch - '0'); char *fav = getenv(envname); @@ -1314,6 +1321,67 @@ void show_history(void) readch(); } +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)); + } +} + /* * Print line to the panel */ diff --git a/file.c b/file.c index ee5e638..07601a6 100644 --- a/file.c +++ b/file.c @@ -16,6 +16,7 @@ ArrayList *arraylist_init(size_t capacity) void arraylist_free(ArrayList *list) { + /* for (size_t i = 0; i < list->length; i++) { if (list->items[i].name != NULL) free(list->items[i].name); @@ -24,7 +25,7 @@ void arraylist_free(ArrayList *list) if (list->items[i].stats != NULL) free(list->items[i].stats); } - + */ free(list->items); free(list); } @@ -57,7 +58,6 @@ void arraylist_remove(ArrayList *list, long index) free(list->items[index].name); free(list->items[index].path); free(list->items[index].stats); - free(list->items[index].icon); */ for (long i = index; i < list->length - 1; i++) list->items[i] = list->items[i + 1];