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 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'src/implementation.c') 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 -- cgit v1.2.3-18-g5258