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 --- Makefile | 9 +++------ README.md | 2 -- lpass_copy.sh | 24 ------------------------ man/lpass.1 | 4 +--- src/implementation.c | 33 ++++++++++++++++++++++++++++----- src/main.c | 5 ++--- 6 files changed, 34 insertions(+), 43 deletions(-) delete mode 100755 lpass_copy.sh diff --git a/Makefile b/Makefile index c0e734f..3598def 100755 --- a/Makefile +++ b/Makefile @@ -1,12 +1,11 @@ PREFIX = /usr/local/bin CC = gcc -CFLAGS = -Wall -g +CFLAGS = -Wall -g # -DDEBUG MAN_PATH = /usr/share/man/man1 -SOURCES = src/easydir.c src/handerror.c src/implementation.c src/main.c -OBJECTS = easydir.o handerror.o implementation.o main.o +SOURCES = src/*.c +OBJECTS = *.o MAN_SOURCES = man/lpass.1 MAN_OBJECTS = lpass.1.gz -BASH = lpass_copy.sh EXECUTABLE = lpass .PHONY: all clean install uninstall @@ -26,7 +25,6 @@ $(EXECUTABLE): $(OBJECTS) install: all @echo installing files to $(PREFIX) @install $(EXECUTABLE) $(PREFIX) && chmod 755 $(PREFIX)/$(EXECUTABLE) - @install $(BASH) $(PREFIX) && chmod 755 $(PREFIX)/$(BASH) @echo installing man page @cat $(MAN_SOURCES) | gzip > $(MAN_OBJECTS) @install $(MAN_OBJECTS) $(MAN_PATH) @@ -36,5 +34,4 @@ uninstall: @echo deleting man page @rm -rf \ $(PREFIX)/$(EXECUTABLE) \ - $(PREFIX)/$(BASH) \ $(MAN_PATH)/$(MAN_OBJECTS) \ No newline at end of file diff --git a/README.md b/README.md index 95a2209..bd5f90f 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,6 @@ helloitismypassword123 * Copy password to clipboard: ``` [joursoir@archlin ~]$ lpass -c games/iko/LordOfNight -Password copied to clipboard. ``` * Generate password: @@ -198,7 +197,6 @@ helloitismypassword123 * Скопировать пароль в буфер обмена: ``` [joursoir@archlin ~]$ lpass -c games/iko/LordOfNight -Password copied to clipboard. ``` * Сгенерировать пароль: diff --git a/lpass_copy.sh b/lpass_copy.sh deleted file mode 100755 index 9499dec..0000000 --- a/lpass_copy.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env bash - -error() { - echo "$@" - exit 1 -} - -if [[ -z $1 ]]; then - error "Error: nothing to copy" -fi - -if [[ -n $WAYLAND_DISLPLAY ]]; then - command=(wl-copy) - if [[ $X_SELECTION == primary ]]; then - command+=( --primary ) - fi -elif [[ -n $DISPLAY ]]; then - command=(xclip -selection clipboard) -else - error "Error: X11 or Wayland display were not detected" -fi - -echo "$1" | "${command[@]}" || error "Error: failed to copy data to clipboard" -echo "Password copied to clipboard." \ No newline at end of file diff --git a/man/lpass.1 b/man/lpass.1 index 02ed320..171c35f 100644 --- a/man/lpass.1 +++ b/man/lpass.1 @@ -133,9 +133,7 @@ helloitismypassword123 Copy password to clipboard: .RS 4 -\fB[joursoir@archlin ~]$ lpass -c games/iko/LordOfNight\fR -.br -Password copied to clipboard. +\fB[joursoir@archlin ~]$ lpass -c games/iko/LordOfNight .RE .PP 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