From 1d51722361bb8199e12eb6b99bbff8a7399642f0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Sat, 3 Feb 2024 19:10:20 +0900 Subject: [PATCH] =?utf8?q?showpass:=20void=20=E2=86=92=20const=20char*?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- main.c | 6 +++++- showpass.c | 37 +++++++++++++++++++++---------------- showpass.h | 2 +- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/main.c b/main.c index 762ef50..1d712a1 100644 --- a/main.c +++ b/main.c @@ -9,6 +9,7 @@ #include "showpass.h" #include "yankpass.h" #include "listpass.h" +/* #include "chkpass.h" */ #include "addpass.h" #include "delpass.h" #include "genpass.h" @@ -25,6 +26,7 @@ void helpme() { printf("%s -s <パスワード名> :パスワードを表示\n", sofname); printf("%s -y <パスワード名> :パスワードを表示せずクリップボードにコピーする\n", sofname); printf("%s -l :パスワード一覧を表示\n", sofname); + printf("%s -c :複数サイトで同じパスワードを利用かどうかの確認\n", sofname); printf("%s -a <パスワード名> :パスワードを追加\n", sofname); printf("%s -d <パスワード名> :パスワードを削除\n", sofname); printf("%s -e <パスワード名> :パスワードを変更\n", sofname); @@ -43,6 +45,7 @@ void helpme_en() { printf("%s -s : Show password\n", sofname); printf("%s -y : Copy password to clipboard without show\n", sofname); printf("%s -l : Show me list of password\n", sofname); + printf("%s -c : Check if you use the same password on multiple website\n", sofname); printf("%s -a : Add password\n", sofname); printf("%s -d : Delete password\n", sofname); printf("%s -e : Edit password\n", sofname); @@ -62,7 +65,7 @@ int main (int argc, char* argv[]) { } if (argc == 3 && strcmp(argv[1], "-i") == 0) initpass(argv[2]); - else if (argc == 3 && strcmp(argv[1], "-s") == 0) showpass(argv[2]); + else if (argc == 3 && strcmp(argv[1], "-s") == 0) printf("%s", showpass(argv[2])); else if (argc == 3 && strcmp(argv[1], "-y") == 0) yankpass(argv[2]); else if (argc == 2 && strcmp(argv[1], "-l") == 0) { char basePath[512]; @@ -78,6 +81,7 @@ int main (int argc, char* argv[]) { listpass(basePath, 0); } + /* else if (argc == 3 && strcmp(argv[1], "-c") == 0) chkpass(); */ else if (argc == 3 && strcmp(argv[1], "-a") == 0) addpass(argv[2]); else if (argc == 3 && strcmp(argv[1], "-d") == 0) delpass(argv[2], 0); else if (argc == 3 && strcmp(argv[1], "-e") == 0) { diff --git a/showpass.c b/showpass.c index 1262775..8305734 100644 --- a/showpass.c +++ b/showpass.c @@ -16,7 +16,7 @@ void clean_up(gpgme_ctx_t ctx, gpgme_data_t in, gpgme_data_t out, FILE* gpgfile, gpgme_release(ctx); } -void showpass(char* file) { +const char* showpass(char* file) { char *lang = getenv("SP_LANG"); gpgme_ctx_t ctx; @@ -34,7 +34,7 @@ void showpass(char* file) { if (err) { if (lang != NULL && strncmp(lang, "en", 2) == 0) fprintf(stderr, "Failed to generating GPGME: %s\n", gpgme_strerror(err)); else fprintf(stderr, "GPGMEを創作に失敗:%s\n", gpgme_strerror(err)); - return; + return NULL; } // OpenPGPプロトコールを設定 @@ -45,7 +45,7 @@ void showpass(char* file) { if (homedir == NULL) { if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to getting home directory"); else perror("ホームディレクトリを受取に失敗"); - return; + return NULL; } char* basedir = "/.local/share/sp/"; @@ -55,7 +55,7 @@ void showpass(char* file) { if (gpgpath == NULL) { if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to allocating memeory"); else perror("メモリを割当に失敗"); - return; + return NULL; } snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext); @@ -69,7 +69,7 @@ void showpass(char* file) { fprintf(stderr, "失敗したパス: %s\n", gpgpath); } free(gpgpath); - return; + return NULL; } // ファイルからinデータオブジェクトを創作 @@ -77,7 +77,7 @@ void showpass(char* file) { if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to generating the GPGME data object"); else perror("GPGMEデータオブジェクトを創作に失敗"); clean_up(ctx, in, out, gpgfile, gpgpath); - return; + return NULL; } // outデータオブジェクトを創作 @@ -85,7 +85,7 @@ void showpass(char* file) { if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to generating the GPGME data object"); else perror("GPGMEデータオブジェクトを創作に失敗"); clean_up(ctx, in, out, gpgfile, gpgpath); - return; + return NULL; } // データオブジェクトを創作 @@ -99,26 +99,31 @@ void showpass(char* file) { // 掃除 clean_up(ctx, in, out, gpgfile, gpgpath); - return; + return NULL; } // 復号化したパスワードを表示する gpgme_data_seek(out, 0, SEEK_SET); char buffer[512]; + char* res = malloc(512 * sizeof(char)); + if (res == NULL) { + if (lang != NULL && strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); + else perror("メモリを役割に失敗"); + clean_up(ctx, in, out, gpgfile, gpgpath); + return NULL; + } + ssize_t read_bytes; - bool islastnl = false; + int i = 0; while ((read_bytes = gpgme_data_read(out, buffer, sizeof(buffer) - 1)) > 0) { - fwrite(buffer, 1, read_bytes, stdout); - if (buffer[read_bytes - 1] == '\n') { - islastnl = true; - } + memcpy(res + i, buffer, read_bytes); + i += read_bytes; } - if (!islastnl) { - putchar('\n'); - } + res[i] = '\0'; // 掃除 clean_up(ctx, in, out, gpgfile, gpgpath); + return res; } diff --git a/showpass.h b/showpass.h index 3c47d0f..7b56584 100644 --- a/showpass.h +++ b/showpass.h @@ -1,6 +1,6 @@ #ifndef SHOWPASS_H #define SHOWPASS_H -void showpass(char* file); +const char* showpass(char* file); #endif -- 2.43.0