Add open with w/ detached feature

This commit is contained in:
Night Kaly 2024-11-18 00:21:09 +00:00
parent 5da3b3b6be
commit c50af43b7b
Signed by: night0721
SSH key fingerprint: SHA256:B/hgVwUoBpx5vdNsXl9w8XwZljA9766uk6T4ubZp5HM
2 changed files with 71 additions and 3 deletions

70
ccc.c
View file

@ -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
*/

4
file.c
View file

@ -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];