* パスワードの検索の追加
* 静的リンクはデフォルトに
* simpas情報の表示
+* ベースディレクトリを関数化
# 1.3.0
* 英訳の追加 (レミリア・スカーレットさん)
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;
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)
}
// ファイルが既に存在するかどうか確認
- 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)
// 鍵を受け取る
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';
// ディレクトリを創作
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';
}
}
- 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) {
#include "common.h"
#include <string.h>
+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;
size_t size;
} List;
+char *getbasedir(int trailing);
char *getlang();
int mkdir_r(const char *path, mode_t mode);
// パスを準備
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)
}
// ファイルが既に存在するかどうか確認
- 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");
}
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");
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';
}
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;
}
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;
}
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)
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) {
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)
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) {