From 3165436ed14cb353843abf01e9208c088583bc40 Mon Sep 17 00:00:00 2001 From: Joursoir Date: Thu, 14 Jan 2021 19:26:56 +0000 Subject: rewrite shell script 'copy' to c language --- src/implementation.c | 33 ++++++++++++++++++++++++++++----- src/main.c | 5 ++--- 2 files changed, 30 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/implementation.c b/src/implementation.c index 3ba2be7..7d19282 100644 --- a/src/implementation.c +++ b/src/implementation.c @@ -22,13 +22,36 @@ extern char *gPath_pass; // example: programming/github.com/joursoir.gpg static void copyText(char *password) { - size_t size = (strlen(password) + strlen(BASH_EXEC_COPY) + 1) * sizeof(char); - char *command = malloc(size); + size_t size = (strlen(gPath_rootdir) + 5 + 1) * sizeof(char); + char *simple_path = malloc(size); + snprintf(simple_path, size, "%s%s", gPath_rootdir, ".pass"); - snprintf(command, size, "%s %s", BASH_EXEC_COPY, password); - system(command); + if(getenv("DISPLAY") != NULL) + { + FILE *f_pass; + f_pass = fopen(simple_path, "w"); + if(f_pass == NULL) { + callError(130); + } + fputs(password, f_pass); + fclose(f_pass); + + char *xclip[] = {"xclip", "-selection", "clipboard", "-i", simple_path, NULL}; + easyFork("xclip", xclip); - free(command); + remove(simple_path); + free(simple_path); + } + else if(getenv("WAYLAND_DISPLAY") != NULL) + { + char *wl_copy[] = {"wl-copy", password, NULL}; + easyFork("wl-copy", wl_copy); + } + else printError("Error: No X11 or Wayland"); + + #if defined(DEBUG) + printf("Password copied to clipboard\n"); + #endif } void checkForbiddenPaths(char *path) // check two dot in path diff --git a/src/main.c b/src/main.c index edcc517..fd1345b 100644 --- a/src/main.c +++ b/src/main.c @@ -18,9 +18,8 @@ #include "handerror.h" #include "implementation.h" -#define VERSION "1.0b" -#define DATE_RELEASE "6 January, 2021" -#define DEBUG +#define VERSION "1.0c" +#define DATE_RELEASE "14 January, 2021" #define STANDARD_TEXTEDITOR "vim" #define MAXLEN_TEXTEDITOR 16 #define MINLEN_PASSWORD 1 -- cgit v1.2.3-18-g5258