From 45e90e71446d082b43154fddec7004d52e6d02c5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=E8=AB=8F=E8=A8=AA=E5=AD=90?= Date: Sun, 15 Sep 2024 23:01:57 +0900 Subject: [PATCH] =?utf8?q?=E3=83=99=E3=83=BC=E3=82=B9=E3=83=87=E3=82=A3?= =?utf8?q?=E3=83=AC=E3=82=AF=E3=83=88=E3=83=AA=E3=82=92=E9=96=A2=E6=95=B0?= =?utf8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + main.c | 27 +++++---------------------- src/addpass.c | 24 ++++++------------------ src/common.c | 38 ++++++++++++++++++++++++++++++++++++++ src/common.h | 1 + src/delpass.c | 36 +++++++----------------------------- src/initpass.c | 38 ++++++++++++-------------------------- src/showpass.c | 18 +++--------------- src/yankpass.c | 18 +++--------------- 9 files changed, 76 insertions(+), 125 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ee1ff1..bcbce91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * パスワードの検索の追加 * 静的リンクはデフォルトに * simpas情報の表示 +* ベースディレクトリを関数化 # 1.3.0 * 英訳の追加 (レミリア・スカーレットさん) diff --git a/main.c b/main.c index 9385d46..d8bfe70 100644 --- a/main.c +++ b/main.c @@ -22,41 +22,24 @@ void usage() { char *getfullpath(char *arg) { char *lang = getlang(); - char *homedir = getenv("HOME"); - if (homedir == NULL) { - if (strncmp(lang, "en", 2) == 0) - perror("Failed to getting home directory"); - else perror("ホームディレクトリを受取に失敗"); - return NULL; - } - -#if defined(__HAIKU__) - char *basedir = "/config/settings/sp/"; -#else - char *basedir = "/.local/share/sp/"; -#endif + char *basedir = getbasedir(1); size_t fullPathLen; char *fullPath; - if (arg != NULL) { - fullPathLen = strlen(homedir) + strlen(basedir) + strlen(arg) + 5; - } else { - fullPathLen = strlen(homedir) + strlen(basedir); - } + fullPathLen = strlen(basedir) + (arg != NULL ? strlen(arg) + 5 : 0); fullPath = (char *)malloc(fullPathLen); if (fullPath == NULL) { if (strncmp(lang, "en", 2) == 0) perror("Failed to allocating memory"); else perror("メモリの役割に失敗"); - free(fullPath); - free(homedir); + free(basedir); return NULL; } if (arg != NULL) { - snprintf(fullPath, fullPathLen, "%s%s%s.gpg", homedir, basedir, arg); + snprintf(fullPath, fullPathLen, "%s%s.gpg", basedir, arg); } else { - snprintf(fullPath, fullPathLen, "%s%s", homedir, basedir); + snprintf(fullPath, fullPathLen, "%s", basedir); } return fullPath; diff --git a/src/addpass.c b/src/addpass.c index 81e01e4..ae1df5e 100644 --- a/src/addpass.c +++ b/src/addpass.c @@ -40,25 +40,13 @@ void addpass(char *file) { char *lang = getlang(); // パスを準備 - char *homedir = getenv("HOME"); - if (homedir == NULL) { - if (strncmp(lang, "en", 2) == 0) - perror("Failed to retrieve home directory"); - else perror("ホームディレクトリを受取に失敗"); - return; - } - -#if defined(__HAIKU__) - char *basedir = "/config/settings/sp/"; -#else - char *basedir = "/.local/share/sp/"; -#endif + char *basedir = getbasedir(1); char *ext = ".gpg"; char pass[256]; char knin[256]; - int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1; + int alllen = snprintf(NULL, 0, "%s%s%s", basedir, file, ext) + 1; char *gpgpathchk = malloc(alllen); if (gpgpathchk == NULL) { if (strncmp(lang, "en", 2) == 0) @@ -68,7 +56,7 @@ void addpass(char *file) { } // ファイルが既に存在するかどうか確認 - snprintf(gpgpathchk, alllen, "%s%s%s%s", homedir, basedir, file, ext); + snprintf(gpgpathchk, alllen, "%s%s%s", basedir, file, ext); if (access(gpgpathchk, F_OK) != -1) { if (strncmp(lang, "en", 2) == 0) @@ -153,7 +141,7 @@ void addpass(char *file) { // 鍵を受け取る char keypath[256]; - snprintf(keypath, sizeof(keypath), "%s%s%s", homedir, basedir, ".gpg-id"); + snprintf(keypath, sizeof(keypath), "%s%s", basedir, ".gpg-id"); keypath[sizeof(keypath) - 1] = '\0'; @@ -222,7 +210,7 @@ void addpass(char *file) { // ディレクトリを創作 char dirpath[512]; - snprintf(dirpath, sizeof(dirpath), "%s%s%s", homedir, basedir, file); + snprintf(dirpath, sizeof(dirpath), "%s%s", basedir, file); dirpath[sizeof(dirpath) - 1] = '\0'; @@ -239,7 +227,7 @@ void addpass(char *file) { } } - snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext); + snprintf(gpgpath, alllen, "%s%s%s", basedir, file, ext); struct stat statbuf; if (stat(gpgpath, &statbuf) == 0) { diff --git a/src/common.c b/src/common.c index cd76fb8..59f26f2 100644 --- a/src/common.c +++ b/src/common.c @@ -1,6 +1,44 @@ #include "common.h" #include +char *getbasedir(int trailing) { + char *lang = getlang(); + + char *homedir = getenv("HOME"); + if (homedir == NULL) { + if (strncmp(lang, "en", 2) == 0) + perror("Failed to get home directory"); + else perror("ホームディレクトリを受取に失敗"); + return NULL; + } + +#if defined(__HAIKU__) + char *basedir = "/config/settings/sp"; + char *slash = "/"; +#elif defined(_WIN32) + char *basedir = "\\AppData\\Local\\076\\sp"; + char *slash = "\\"; +#else + char *basedir = "/.local/share/sp"; + char *slash = "/"; +#endif + + size_t len = strlen(homedir) + strlen(basedir) + strlen(slash) + 4; + char *res = malloc(len); + if (res == NULL) { + if (strncmp(lang, "en", 2) == 0) + perror("Failed to allocate memory"); + else perror("メモリの役割に失敗"); + return NULL; + } + if (trailing == 1) + snprintf(res, len, "%s%s%s", homedir, basedir, slash); + else + snprintf(res, len, "%s%s", homedir, basedir); + + return res; +} + char *getlang() { char *lang = NULL; diff --git a/src/common.h b/src/common.h index c8b74dd..a8a8b8e 100644 --- a/src/common.h +++ b/src/common.h @@ -21,6 +21,7 @@ typedef struct { size_t size; } List; +char *getbasedir(int trailing); char *getlang(); int mkdir_r(const char *path, mode_t mode); diff --git a/src/delpass.c b/src/delpass.c index 62bbe87..91f5d69 100644 --- a/src/delpass.c +++ b/src/delpass.c @@ -62,22 +62,10 @@ int delpass(char *file, int force) { // パスを準備 char pwfile[512]; - char *homedir = getenv("HOME"); - if (homedir == NULL) { - if (strncmp(lang, "en", 2) == 0) - perror("Failed to get home directory"); - else perror("ホームディレクトリを受取に失敗"); - return -1; - } - -#if defined(__HAIKU__) - char *basedir = "/config/settings/sp/"; -#else - char *basedir = "/.local/share/sp/"; -#endif + char *basedir = getbasedir(1); char *ext = ".gpg"; - int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1; + int alllen = snprintf(NULL, 0, "%s%s%s", basedir, file, ext) + 1; char *gpgpathchk = malloc(alllen); if (gpgpathchk == NULL) { if (strncmp(lang, "en", 2) == 0) @@ -87,7 +75,7 @@ int delpass(char *file, int force) { } // ファイルが既に存在するかどうか確認 - snprintf(gpgpathchk, alllen, "%s%s%s%s", homedir, basedir, file, ext); + snprintf(gpgpathchk, alllen, "%s%s%s", basedir, file, ext); if (access(gpgpathchk, F_OK) != 0) { if (strncmp(lang, "en", 2) == 0) perror("Password does not exist"); @@ -97,15 +85,7 @@ int delpass(char *file, int force) { } free(gpgpathchk); - int needed = snprintf( - pwfile, - sizeof(pwfile), - "%s%s%s%s", - homedir, - basedir, - file, - ext - ); + int needed = snprintf( pwfile, sizeof(pwfile), "%s%s%s", basedir, file, ext); if (needed >= (int)sizeof(pwfile)) { if (strncmp(lang, "en", 2) == 0) perror("Error: Path is too long"); @@ -140,11 +120,9 @@ int delpass(char *file, int force) { int numt; char **tokens = explode(file, '/', &numt); - char basepath[1024]; char passpath[1024]; - snprintf(basepath, sizeof(basepath), "%s%s", homedir, basedir); - snprintf(passpath, sizeof(passpath), "%s%s%s", homedir, basedir, tokens[0]); - char *ls = strrchr(basepath, '/'); + snprintf(passpath, sizeof(passpath), "%s%s", basedir, tokens[0]); + char *ls = strrchr(basedir, '/'); if (ls != NULL) { *ls = '\0'; } @@ -157,7 +135,7 @@ int delpass(char *file, int force) { for (int i = numt - 1; i >= 0; i--) { // ~/.local/share/sp を削除したら危険 - if (strncmp(passpath, basepath, sizeof(passpath)) == 0) { + if (strncmp(passpath, basedir, sizeof(passpath)) == 0) { break; } diff --git a/src/initpass.c b/src/initpass.c index 0eab1b5..c73cfb7 100644 --- a/src/initpass.c +++ b/src/initpass.c @@ -4,52 +4,38 @@ void initpass(char *gpgid) { char *lang = getlang(); - char *homedir = getenv("HOME"); - if (homedir == NULL) { - if (strncmp(lang, "en", 2) == 0) - perror("Failed to get home directory."); - else perror("ホームディレクトリを受取に失敗。"); - return; - } - -#if defined(__HAIKU) - char *basedir = "/config/settings/sp/"; -#else - char *basedir = "/.local/share/sp/"; -#endif - char dirpath[256]; - snprintf(dirpath, sizeof(dirpath), "%s%s", homedir, basedir); + char *basedir = getbasedir(1); - if (mkdir_r(dirpath, 0755) != 0 && errno != EEXIST) { + if (mkdir_r(basedir, 0755) != 0 && errno != EEXIST) { if (strncmp(lang, "en", 2) == 0) - perror("Failed to create directory."); - else perror("ディレクトリを作成に失敗。"); + fprintf(stderr, "Failed to create directory.\n"); + else fprintf(stderr, "ディレクトリを作成に失敗。\n"); return; } char gpgidpath[512]; - snprintf(gpgidpath, sizeof(gpgidpath), "%s/.gpg-id", dirpath); + snprintf(gpgidpath, sizeof(gpgidpath), "%s/.gpg-id", basedir); struct stat statbuf; if (stat(gpgidpath, &statbuf) == 0) { if (strncmp(lang, "en", 2) == 0) - perror(".gpg-id file already exists."); - else perror(".gpg-idファイルは既に存在します。"); + fprintf(stderr, ".gpg-id file already exists.\n"); + else fprintf(stderr, ".gpg-idファイルは既に存在します。\n"); return; } FILE *gpgidfile = fopen(gpgidpath, "w"); if (gpgidfile == NULL) { if (strncmp(lang, "en", 2) == 0) - perror("Failed to write .gpg-id file."); - else perror(".gpg-idファイルを書き込めません。"); + fprintf(stderr, "Failed to write .gpg-id file.\n"); + else fprintf(stderr, ".gpg-idファイルを書き込めません。\n"); return; } - if (fputs(gpgid, gpgidfile) == EOF) { + if (fprintf(gpgidfile, "%s\n", gpgid) < 0) { if (strncmp(lang, "en", 2) == 0) - perror("Failed to write .gpg-id file."); - else perror(".gpg-idファイルへの書き込みに失敗しました。"); + fprintf(stderr, "Failed to write .gpg-id file.\n"); + else fprintf(stderr, ".gpg-idファイルへの書き込みに失敗しました。\n"); fclose(gpgidfile); return; } diff --git a/src/showpass.c b/src/showpass.c index d291db5..713d362 100644 --- a/src/showpass.c +++ b/src/showpass.c @@ -43,21 +43,9 @@ const char *showpass(char *file) { gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP); // 暗号化したタイルを開く - char *homedir = getenv("HOME"); - if (homedir == NULL) { - if (strncmp(lang, "en", 2) == 0) - perror("Failed to get home directory"); - else perror("ホームディレクトリを受取に失敗"); - return NULL; - } - -#if defined(__HAIKU__) - char *basedir = "/config/settings/sp/"; -#else - char *basedir = "/.local/share/sp/"; -#endif + char *basedir = getbasedir(1); char *ext = ".gpg"; - int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1; + int alllen = snprintf(NULL, 0, "%s%s%s", basedir, file, ext) + 1; char *gpgpath = malloc(alllen); if (gpgpath == NULL) { if (strncmp(lang, "en", 2) == 0) @@ -66,7 +54,7 @@ const char *showpass(char *file) { return NULL; } - snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext); + snprintf(gpgpath, alllen, "%s%s%s", basedir, file, ext); gpgfile = fopen(gpgpath, "rb"); if (gpgfile == NULL) { diff --git a/src/yankpass.c b/src/yankpass.c index 40c67fe..ecc30f7 100644 --- a/src/yankpass.c +++ b/src/yankpass.c @@ -40,21 +40,9 @@ void yankpass(char *file) { gpgme_set_protocol(ctx, GPGME_PROTOCOL_OpenPGP); // 暗号化したタイルを開く - char* homedir = getenv("HOME"); - if (homedir == NULL) { - if (strncmp(lang, "en", 2) == 0) - perror("Failed to get home directory"); - else perror("ホームディレクトリを受取に失敗"); - return; - } - -#if defined(__HAIKU__) - char *basedir = "/config/settings/sp/"; -#else - char* basedir = "/.local/share/sp/"; -#endif + char *basedir = getbasedir(1); char* ext = ".gpg"; - int alllen = snprintf(NULL, 0, "%s%s%s%s", homedir, basedir, file, ext) + 1; + int alllen = snprintf(NULL, 0, "%s%s%s", basedir, file, ext) + 1; char* gpgpath = malloc(alllen); if (gpgpath == NULL) { if (strncmp(lang, "en", 2) == 0) @@ -63,7 +51,7 @@ void yankpass(char *file) { return; } - snprintf(gpgpath, alllen, "%s%s%s%s", homedir, basedir, file, ext); + snprintf(gpgpath, alllen, "%s%s%s", basedir, file, ext); gpgfile = fopen(gpgpath, "rb"); if (gpgfile == NULL) { if (strncmp(lang, "en", 2) == 0) { -- 2.43.0