suwako / sp アーカイブ

このリポジトリはアーカイブされています。読むだけで、プッシュやイシューの変更は出来ません。

master
ブランチ master
sp / src / delpass.c
4 KB、162行 生 Blame 履歴
1#include <unistd.h>
2#include <dirent.h>
3
4#include "common.h"
5#include "delpass.h"
6
7int delcnt(const char *str, char delimiter) {
8 int count = 0;
9 while (*str) {
10 if (*str == delimiter) {
11 count++;
12 }
13 str++;
14 }
15
16 return count;
17}
18
19char **explode(const char *str, char delimiter, int *numtokens) {
20 int count = delcnt(str, delimiter) + 1;
21 *numtokens = count;
22
23 char **tokens = malloc(count * sizeof(char *));
24 if (tokens == NULL) {
25 perror("malloc");
26 exit(EXIT_FAILURE);
27 }
28
29 char *strcopy = strdup(str);
30 if (strcopy == NULL) {
31 perror("strdup");
32 exit(EXIT_FAILURE);
33 }
34
35 char *token = strtok(strcopy, &delimiter);
36 int i = 0;
37 while (token != NULL) {
38 tokens[i] = strdup(token);
39 if (tokens[i] == NULL) {
40 perror("strdup");
41 exit(EXIT_FAILURE);
42 }
43 i++;
44 token = strtok(NULL, &delimiter);
45 }
46
47 free(strcopy);
48
49 return tokens;
50}
51
52void freetokens(char **tokens, int numtokens) {
53 for (int i = 0; i < numtokens; i++) {
54 free(tokens[i]);
55 }
56
57 free(tokens);
58}
59
60int delpass(char *file, int force) {
61 char *lang = getlang();
62
63 // パスを準備
64 char pwfile[512];
65 char *basedir = getbasedir(1);
66 char *ext = ".gpg";
67
68 int alllen = snprintf(NULL, 0, "%s%s%s", basedir, file, ext) + 1;
69 char *gpgpathchk = malloc(alllen);
70 if (gpgpathchk == NULL) {
71 if (strncmp(lang, "en", 2) == 0)
72 perror("Failed to allocate memory");
73 else perror("メモリを割当に失敗");
74 return -1;
75 }
76
77 // ファイルが既に存在するかどうか確認
78 snprintf(gpgpathchk, alllen, "%s%s%s", basedir, file, ext);
79 if (access(gpgpathchk, F_OK) != 0) {
80 if (strncmp(lang, "en", 2) == 0)
81 perror("Password does not exist");
82 else perror("パスワードが存在しません");
83 free(gpgpathchk);
84 return -1;
85 }
86 free(gpgpathchk);
87
88 int needed = snprintf( pwfile, sizeof(pwfile), "%s%s%s", basedir, file, ext);
89 if (needed >= (int)sizeof(pwfile)) {
90 if (strncmp(lang, "en", 2) == 0)
91 perror("Error: Path is too long");
92 else perror("エラー:パスが長すぎる");
93 return -1;
94 }
95
96 // 削除を確認する
97 if (force == 0) { // パスワードの変更の場合、確認は不要
98 if (strncmp(lang, "en", 2) == 0)
99 printf("Are you sure you want to delete the password '%s'? (y/N): ", file);
100 printf("パスワード「%s」を本当に削除する事が宜しいでしょうか? (y/N): ", file);
101 int confirm = getchar();
102 if (confirm != 'y' && confirm != 'Y') {
103 if (strncmp(lang, "en", 2) == 0) puts("Not deleted");
104 else puts("削除しませんでした");
105 return -1;
106 }
107
108 int ch;
109 while ((ch = getchar()) != '\n' && ch != EOF);
110 }
111
112 if (unlink(pwfile) == -1) {
113 if (strncmp(lang, "en", 2) == 0)
114 perror("Password cannot be deleted");
115 else perror("パスワードを削除出来ませんでした");
116 return -1;
117 }
118
119 // 空のディレクトリの場合
120 int numt;
121 char **tokens = explode(file, '/', &numt);
122
123 char passpath[1024];
124 snprintf(passpath, sizeof(passpath), "%s%s", basedir, tokens[0]);
125 char *ls = strrchr(basedir, '/');
126 if (ls != NULL) {
127 *ls = '\0';
128 }
129
130 for (int i = 1; i < numt; i++) {
131 if (i == (numt-1)) continue;
132 strncat(passpath, "/", sizeof(passpath) - strlen(passpath) - 1);
133 strncat(passpath, tokens[i], sizeof(passpath) - strlen(passpath) - 1);
134 }
135
136 for (int i = numt - 1; i >= 0; i--) {
137 // ~/.local/share/sp を削除したら危険
138 if (strncmp(passpath, basedir, sizeof(passpath)) == 0) {
139 break;
140 }
141
142 // ディレクトリが空じゃない場合、削除を止める
143 if (rmdir(passpath) == -1) {
144 break;
145 }
146
147 char *last_slash = strrchr(passpath, '/');
148 if (last_slash != NULL) {
149 *last_slash = '\0';
150 }
151 }
152
153 freetokens(tokens, numt);
154
155 // sp -e の場合、「パスワードを削除しました」って要らない
156 if (force == 1) return 0;
157
158 if (strncmp(lang, "en", 2) == 0) puts("The password got deleted");
159 else puts("パスワードを削除しました");
160
161 return 0;
162}