Replaced system with execl, organised directory, added user scripts

pull/19/head
mananapr 5 years ago
parent 813dae2bed
commit be4124c5d8

414
cf.c

@ -19,11 +19,12 @@
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <signal.h>
#include <locale.h>
#include <fcntl.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <locale.h>
#include <sys/wait.h>
#include "config.h"
@ -484,7 +485,7 @@ void getMIME(char *filepath, char mime[50])
/*
Opens a file using xdg-open
Opens a file using FILE_OPENER
*/
void openFile(char *filepath)
{
@ -494,14 +495,26 @@ void openFile(char *filepath)
{
char *cmd;
// Allocate Memory to `cmd`
allocSize = snprintf(NULL, 0, "%s %s", editor, filepath);
allocSize = snprintf(NULL, 0, "%s/%s", "/usr/bin", editor);
cmd = malloc(allocSize+1);
snprintf(cmd, allocSize+1, "%s %s", editor, filepath);
snprintf(cmd, allocSize+1, "%s/%s", "/usr/bin", editor);
endwin();
system(cmd);
// Make a child process to edit file
pid_t pid;
pid = fork();
if (pid == 0)
{
execl(cmd, cmd, filepath, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
free(cmd);
return;
}
}
pid_t pid;
pid = fork();
if (pid == 0)
@ -568,14 +581,34 @@ void writeClipboard(char *filepath)
void removeClipboard(char *filepath)
{
char *cmd = NULL;
filepath[strlen(filepath)-1] = '\0';
char *arg = NULL;
// Allocate Memory to `cmd`
allocSize = snprintf(NULL, 0, "sed -i '\\|^%s|d' %s", filepath, clipboard_path);
allocSize = snprintf(NULL, 0, "%s", "/usr/bin/sed");
cmd = malloc(allocSize+1);
snprintf(cmd, PATH_MAX, "sed -i '\\|^%s|d' %s", filepath, clipboard_path);
system(cmd);
snprintf(cmd, allocSize+1, "%s", "/usr/bin/sed");
// Allocate Memory to `arg`
allocSize = snprintf(NULL, 0, "\\|^%s|d", filepath);
arg = malloc(allocSize + 1);
snprintf(arg, allocSize+1, "\\|^%s|d", filepath);
// Create a child process to run command
pid_t pid;
pid = fork();
if( pid == 0 )
{
execl(cmd, cmd, "-i", arg, clipboard_path, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
free(arg);
free(cmd);
}
}
/*
@ -649,17 +682,8 @@ void getTextPreview(char *filepath, int maxy, int maxx)
*/
void getVidPreview(char *filepath, int maxy, int maxx)
{
char *buf = NULL;
// Reallocate `temp_dir` and store `mediainfo` command
free(temp_dir);
allocSize = snprintf(NULL,0,"mediainfo \"%s\" > ~/.cache/cfiles/preview",filepath);
temp_dir = malloc(allocSize+1);
snprintf(temp_dir,allocSize+1,"mediainfo \"%s\" > ~/.cache/cfiles/preview",filepath);
// Execute the mediainfo command
endwin();
system(temp_dir);
pid_t pid;
int fd;
// Reallocate `temp_dir` and store path to preview file
free(temp_dir);
@ -667,17 +691,38 @@ void getVidPreview(char *filepath, int maxy, int maxx)
temp_dir = malloc(allocSize+1);
snprintf(temp_dir,allocSize+1,"%s/preview",cache_path);
// Allocate Memory to `buf` and store command to display preview file
allocSize = snprintf(NULL, 0, "less %s", temp_dir);
buf = malloc(allocSize+1);
snprintf(buf, allocSize+1, "less %s", temp_dir);
endwin();
// Execute the preview command and Free Memory
system(buf);
free(buf);
// Create a child process to run "mediainfo filepath > ~/.cache/cfiles/preview"
pid = fork();
if( pid == 0 )
{
remove(temp_dir);
fd = open(temp_dir, O_CREAT | O_WRONLY, 0755);
// Redirect stdout
dup2(fd, 1);
execl("/usr/bin/mediainfo", "mediainfo", filepath, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
pid = fork();
if( pid == 0 )
{
execl("/usr/bin/less", "less", temp_dir, (char *)0);
exit(1);
}
else
{
waitpid(pid, &status, 0);
}
refresh();
}
}
/*
@ -685,22 +730,37 @@ void getVidPreview(char *filepath, int maxy, int maxx)
*/
void getArchivePreview(char *filepath, int maxy, int maxx)
{
// Reallocate `temp_dir`
free(temp_dir);
allocSize = snprintf(NULL,0,"atool -lq \"%s\" > ~/.cache/cfiles/preview", filepath);
temp_dir = malloc(allocSize+1);
snprintf(temp_dir, allocSize+1, "atool -lq \"%s\" > ~/.cache/cfiles/preview",filepath);
pid_t pid;
int fd;
int null_fd;
// Execute comand and Free Memory
system(temp_dir);
// Reallocate `temp_dir` and store path to preview file
free(temp_dir);
// Reallocate `temp_dir`
allocSize = snprintf(NULL,0,"%s/preview",cache_path);
temp_dir = malloc(allocSize+1);
snprintf(temp_dir,allocSize+1,"%s/preview",cache_path);
// Create a child process to run "atool -lq filepath > ~/.cache/cfiles/preview"
pid = fork();
if( pid == 0 )
{
remove(temp_dir);
fd = open(temp_dir, O_CREAT | O_WRONLY, 0755);
null_fd = open("/dev/null", O_WRONLY);
// Redirect stdout
dup2(fd, 1);
// Redirect errors to /dev/null
dup2(null_fd, 2);
execl("/usr/bin/atool", "atool", "-lq", filepath, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
getTextPreview(temp_dir, maxy, maxx);
}
}
/*
@ -838,13 +898,18 @@ void copyFiles(char *present_dir)
while(fgets(buf, PATH_MAX, (FILE*) f))
{
buf[strlen(buf)-1] = '\0';
// Reallocate `temp_dir` and store command for copying file
free(temp_dir);
allocSize = snprintf(NULL,0,"cp -r -v \"%s\" \"%s\"",buf,present_dir);
temp_dir = malloc(allocSize+1);
snprintf(temp_dir,allocSize+1,"cp -r -v \"%s\" \"%s\"",buf,present_dir);
// Execute command
system(temp_dir);
// Create a child process to copy file
pid_t pid = fork();
if(pid == 0)
{
execl("/usr/bin/cp", "cp", "-r", "-v", buf, present_dir, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
}
}
refresh();
fclose(f);
@ -865,13 +930,18 @@ void removeFiles()
while(fgets(buf, PATH_MAX, (FILE*) f))
{
buf[strlen(buf)-1] = '\0';
// Reallocate `temp_dir` and store command for removing file
free(temp_dir);
allocSize = snprintf(NULL,0,"rm -r -v \"%s\"",buf);
temp_dir = malloc(allocSize+1);
snprintf(temp_dir,allocSize+1,"rm -r -v \"%s\"",buf);
// Execute command
system(temp_dir);
// Create a child process to remove file
pid_t pid = fork();
if(pid == 0)
{
execl("/usr/bin/rm", "rm", "-r", "-v", buf, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
}
}
refresh();
fclose(f);
@ -884,6 +954,9 @@ void removeFiles()
*/
void renameFiles()
{
// For storing pid of children
pid_t pid;
// For opening clipboard and temp_clipboard
FILE *f = fopen(clipboard_path, "r");
FILE *f2;
@ -899,23 +972,40 @@ void renameFiles()
int count = 0;
int count2 = 0;
// Allocate Memory tp `cmd`
allocSize = snprintf(NULL,0,"cp %s %s",clipboard_path,temp_clipboard_path);
cmd = malloc(allocSize+1);
snprintf(cmd,allocSize+1,"cp %s %s",clipboard_path,temp_clipboard_path);
// Make `temp_clipboard`
system(cmd);
// Make a child process to create `temp_clipboard`
pid = fork();
if( pid == 0 )
{
execl("/usr/bin/cp", "cp", clipboard_path, temp_clipboard_path, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
}
// Exit curses mode and open temp_clipboard_path in EDITOR
// Exit curses mode to open temp_clipboard_path in EDITOR
endwin();
// Reallocate `cmd`
free(cmd);
allocSize = snprintf(NULL,0,"%s %s", editor, temp_clipboard_path);
// Allocate `cmd` to store full path of editor
allocSize = snprintf(NULL,0,"/usr/bin/%s", editor);
cmd = malloc(allocSize + 1);
snprintf(cmd,allocSize+1,"%s %s", editor, temp_clipboard_path);
system(cmd);
snprintf(cmd,allocSize+1,"/usr/bin/%s", editor);
// Create a child process to edit temp_clipboard
pid = fork();
if( pid == 0 )
{
execl(cmd, cmd, temp_clipboard_path, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
free(cmd);
}
// Open clipboard and temp_clipboard and mv path from clipboard to adjacent entry in temp_clipboard
while(fgets(buf, PATH_MAX, (FILE*) f))
@ -932,12 +1022,18 @@ void renameFiles()
if(buf2[strlen(buf2)-1] == '\n')
buf2[strlen(buf2)-1] = '\0';
// Reallocate `cmd`
allocSize = snprintf(NULL,0,"mv \"%s\" \"%s\"",buf,buf2);
cmd = malloc(allocSize+1);
snprintf(cmd,allocSize+1,"mv \"%s\" \"%s\"",buf,buf2);
system(cmd);
free(cmd);
// Create a child process to rename
pid = fork();
if( pid == 0 )
{
execl("/usr/bin/mv", "mv", buf, buf2, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
}
}
}
count++;
@ -946,11 +1042,8 @@ void renameFiles()
fclose(f);
// Remove clipboard and temp_clipboard
allocSize = snprintf(NULL,0,"rm %s %s",temp_clipboard_path,clipboard_path);
cmd = malloc(allocSize+1);
snprintf(cmd,allocSize+1,"rm %s %s",temp_clipboard_path,clipboard_path);
system(cmd);
free(cmd);
remove(clipboard_path);
remove(temp_clipboard_path);
// Start curses mode
refresh();
@ -973,14 +1066,18 @@ void moveFiles(char *present_dir)
{
buf[strlen(buf)-1] = '\0';
// Reallocate `temp_dir` and store command for moving file
free(temp_dir);
allocSize = snprintf(NULL, 0, "mv -f \"%s\" \"%s\"",buf,present_dir);
temp_dir = malloc(allocSize+1);
snprintf(temp_dir, allocSize+1, "mv -f \"%s\" \"%s\"",buf,present_dir);
// Execute command
system(temp_dir);
// Create a child process to move file
pid_t pid = fork();
if(pid == 0)
{
execl("/usr/bin/mv", "mv", "-v", buf, present_dir, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
}
}
fclose(f);
refresh();
@ -1030,6 +1127,7 @@ void displayAlert(char *message)
/*
Refresh ncurses windows
*/
void refreshWindows()
{
@ -1044,20 +1142,9 @@ void refreshWindows()
/*
Checks if some flags are enabled and handles them accordingly
Clears Image
*/
void handleFlags(char** directories)
{
// Clear the preview_win
if(clearFlag == 1)
{
wclear(preview_win);
wrefresh(preview_win);
clearFlag = 0;
}
// Clear the preview_win for images
if(clearFlagImg == 1)
void clearImg()
{
// Store arguments for CLEARIMG script
char arg1[5];
@ -1079,6 +1166,26 @@ void handleFlags(char** directories)
execl(CLEARIMG,CLEARIMG,arg1,arg2,arg3,arg4,(char *)NULL);
exit(1);
}
}
/*
Checks if some flags are enabled and handles them accordingly
*/
void handleFlags(char** directories)
{
// Clear the preview_win
if(clearFlag == 1)
{
wclear(preview_win);
wrefresh(preview_win);
clearFlag = 0;
}
// Clear the preview_win for images
if(clearFlagImg == 1)
{
clearImg();
clearFlagImg = 0;
}
@ -1299,12 +1406,17 @@ int main(int argc, char* argv[])
// Draw borders and refresh windows
refreshWindows();
// For storing pid of children
pid_t pid;
// For fzf
char *cmd;
char *buf;
char *path;
// For popen
FILE *fp;
// For two key keybindings
char secondKey;
// For taking user confirmation
@ -1317,7 +1429,7 @@ int main(int argc, char* argv[])
selection--;
selection = ( selection < 0 ) ? 0 : selection;
// Scrolling
if(len >= maxy)
if(len >= maxy-1)
if(selection <= start + maxy/2)
{
if(start == 0)
@ -1335,7 +1447,7 @@ int main(int argc, char* argv[])
selection++;
selection = ( selection > len-1 ) ? len-1 : selection;
// Scrolling
if(len >= maxy)
if(len >= maxy-1)
if(selection - 1 > maxy/2)
{
if(start + maxy - 2 != len)
@ -1528,13 +1640,31 @@ int main(int argc, char* argv[])
// Opens bash shell in present directory
case KEY_SHELL:
// Clear Image Preview (If Present)
clearImg();
// Reallocate `temp_dir`
free(temp_dir);
allocSize = snprintf(NULL,0,"cd \"%s\" && bash",dir);
allocSize = snprintf(NULL, 0, "cd %s && bash", dir);
temp_dir = malloc(allocSize+1);
snprintf(temp_dir,allocSize+1,"cd \"%s\" && bash",dir);
snprintf(temp_dir, allocSize+1, "cd %s && bash", dir);
// End ncurses mode
endwin();
system(temp_dir);
// Create a child process to run bash
pid = fork();
if( pid == 0 )
{
execl("/usr/bin/bash", "bash", "-c", temp_dir, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
}
// Restart ncurses mode and set appropriate flags
start = 0;
selection = 0;
refresh();
@ -1617,6 +1747,8 @@ int main(int argc, char* argv[])
// Go to bookmark
case KEY_BOOKMARK:
// Clear Image Preview (If Present)
clearImg();
len_bookmarks = getNumberOfBookmarks();
if( len_bookmarks == -1 )
{
@ -1635,6 +1767,8 @@ int main(int argc, char* argv[])
// Add Bookmark
case KEY_ADDBOOKMARK:
// Clear Image Preview (If Present)
clearImg();
displayAlert("Enter Bookmark Key");
secondKey = wgetch(status_win);
if( bookmarkExists(secondKey) == 1 )
@ -1650,15 +1784,22 @@ int main(int argc, char* argv[])
// See selection list
case KEY_VIEWSEL:
// Exit curses mode to show clipboard
endwin();
if( access( clipboard_path, F_OK ) != -1 )
{
// Reallocate `temp_dir` to command to view clipboard
free(temp_dir);
allocSize = snprintf(NULL,0,"less %s",clipboard_path);
temp_dir = malloc(allocSize+1);
snprintf(temp_dir,allocSize+1,"less %s",clipboard_path);
endwin();
system(temp_dir);
// Create a child process to show clipboard
pid = fork();
if( pid == 0 )
{
execl("/usr/bin/less", "less", clipboard_path, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
}
refresh();
}
else
@ -1670,15 +1811,29 @@ int main(int argc, char* argv[])
// Edit selection list
case KEY_EDITSEL:
if( fileExists(clipboard_path) == 1 )
{
// Reallocate `temp_dir` to command to edit clipboard
// Exit curses mode to edit clipboard
endwin();
// Reallocate Memory to `temp_dir` to store full path of editor
free(temp_dir);
allocSize = snprintf(NULL,0,"%s %s", editor, clipboard_path);
allocSize = snprintf(NULL,0,"/usr/bin/%s", editor);
temp_dir = malloc(allocSize+1);
snprintf(temp_dir,allocSize+1,"%s %s", editor, clipboard_path);
endwin();
system(temp_dir);
snprintf(temp_dir,allocSize+1,"/usr/bin/%s", editor);
if( access( clipboard_path, F_OK ) != -1 )
{
// Create a child process to show clipboard
pid = fork();
if( pid == 0 )
{
execl(temp_dir, editor, clipboard_path, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
}
refresh();
}
else
@ -1713,6 +1868,8 @@ int main(int argc, char* argv[])
}
else
{
// Clear Image Preview (If Present)
clearImg();
int status;
char* scripts[len_scripts];
status = getFiles(scripts_path, scripts);
@ -1744,17 +1901,23 @@ int main(int argc, char* argv[])
buf = malloc(allocSize+1);
snprintf(buf, allocSize+1, "%s/%s", dir, directories[selection]);
// Allocate Memory to `cmd` to store the command to execute script with `buf` as argument
allocSize = snprintf(NULL, 0, "%s %s", temp_dir, buf);
cmd = malloc(allocSize+1);
snprintf(cmd, allocSize+1, "%s %s", temp_dir, buf);
// Exit ncurses mode and execute the script
// Exit ncurses mode to execute the script
endwin();
system(cmd);
// Free Memory
free(cmd);
// Create a child process to execute the script
pid = fork();
if( pid == 0 )
{
execl(temp_dir, scripts[option], buf, (char *)0);
exit(1);
}
else
{
int status;
waitpid(pid, &status, 0);
}
// Free Memory and restart curses mode
free(buf);
refresh();
}
@ -1797,6 +1960,9 @@ int main(int argc, char* argv[])
free(dir);
free(temp_dir);
// Clear Image Preview (If Present)
clearImg();
// End curses mode
endwin();
return 0;

@ -0,0 +1,5 @@
# Example Scripts to use with cfiles
This directory contains some example scripts you can use with `cfiles`.
### setwal
Uses [wal](https://github.com/dylanaraps/pywal) on selection to set image as wallpaper and generate a suitable colorscheme

@ -0,0 +1,3 @@
#!/usr/bin/env bash
wal -i $1 -o wal-set
Loading…
Cancel
Save