How to redirect a command for a specific application

For example, the feh program can delete the currently viewed image, presumably using the rm command at some point. Let’s say that instead of deleting the file, we want to run a trash script, say trash.sh. Is there anyway of running feh but tricking it into running trash.sh every time it calls rm?

Asked By: kleinbottle4

||

You need to modify the sources of feh and run an arbitrary script instead of unlink() system call in C language, then compile new code:

git clone https://github.com/derf/feh.git
cd feh
cat<<EOF > trashbin.patch
--- src/filelist.c  2023-09-18 23:19:53.444892742 +0200
+++ src/filelist_new.c  2023-09-18 23:19:11.293556737 +0200
@@ -112,7 +112,11 @@
 
 gib_list *feh_file_rm_and_free(gib_list * list, gib_list * l)
 {
-   unlink(FEH_FILE(l->data)->filename);
+   //unlink(FEH_FILE(l->data)->filename);
+    char command[255];
+    sprintf(command, "/usr/local/bin/trashbin.sh %s", FEH_FILE(l->data)->filename);
+    system(command);
+    
    return(feh_file_remove_from_list(list, l));
 }

EOF
patch src/filelist.c < trashbin.patch
make
sudo make install

Then, create:

/usr/local/bin/trashbin.sh

with what you want, like

#!/bin/sh

mkdir -p ~/.local/trashbin
/bin/mv -- "$@" ~/.local/trashbin
Answered By: Gilles Quénot
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.