| 1 | #include "common.h" |
| 2 | |
| 3 | #include <stdio.h> |
| 4 | #include <string.h> |
| 5 | #include <dirent.h> |
| 6 | #include <sys/stat.h> |
| 7 | |
| 8 | #define MAXFINDLEN 1024 |
| 9 | |
| 10 | char *getbasedir(int trailing) { |
| 11 | char *lang = getlang(); |
| 12 | |
| 13 | char *homedir = getenv("HOME"); |
| 14 | if (homedir == NULL) { |
| 15 | if (strncmp(lang, "en", 2) == 0) |
| 16 | perror("Failed to get home directory"); |
| 17 | else perror("ホームディレクトリを受取に失敗"); |
| 18 | return NULL; |
| 19 | } |
| 20 | |
| 21 | #if defined(__HAIKU__) |
| 22 | char *basedir = "/config/settings/sp"; |
| 23 | char *slash = "/"; |
| 24 | #elif defined(_WIN32) |
| 25 | char *basedir = "\\AppData\\Local\\076\\sp"; |
| 26 | char *slash = "\\"; |
| 27 | #else |
| 28 | char *basedir = "/.local/share/sp"; |
| 29 | char *slash = "/"; |
| 30 | #endif |
| 31 | |
| 32 | size_t len = strlen(homedir) + strlen(basedir) + strlen(slash) + 4; |
| 33 | char *res = malloc(len); |
| 34 | if (res == NULL) { |
| 35 | if (strncmp(lang, "en", 2) == 0) |
| 36 | perror("Failed to allocate memory"); |
| 37 | else perror("メモリの役割に失敗"); |
| 38 | return NULL; |
| 39 | } |
| 40 | if (trailing == 1) |
| 41 | snprintf(res, len, "%s%s%s", homedir, basedir, slash); |
| 42 | else |
| 43 | snprintf(res, len, "%s%s", homedir, basedir); |
| 44 | |
| 45 | return res; |
| 46 | } |
| 47 | |
| 48 | char *getlang() { |
| 49 | char *lang = NULL; |
| 50 | |
| 51 | lang = getenv("SP_LANG"); |
| 52 | if (lang == NULL) lang = "ja"; |
| 53 | |
| 54 | return lang; |
| 55 | } |
| 56 | |
| 57 | int mkdir_r(const char *path, mode_t mode) { |
| 58 | char tmp[256]; |
| 59 | char *p = NULL; |
| 60 | size_t len; |
| 61 | |
| 62 | snprintf(tmp, sizeof(tmp), "%s", path); |
| 63 | |
| 64 | len = strlen(tmp); |
| 65 | if (tmp[len - 1] == '/') { |
| 66 | tmp[len - 1] = 0; |
| 67 | } |
| 68 | |
| 69 | for (p = tmp + 1; *p; p++) { |
| 70 | if (*p == '/') { |
| 71 | *p = 0; |
| 72 | if (mkdir(tmp, mode) != 0 && errno != EEXIST) return -1; |
| 73 | *p = '/'; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (mkdir(tmp, mode) != 0 && errno != EEXIST) { |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | int tmpcopy(const char *inpath, const char *outpath) { |
| 85 | FILE *src = fopen(inpath, "rb"); |
| 86 | if (!src) { |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | FILE *dst = fopen(outpath, "wb"); |
| 91 | if (!dst) { |
| 92 | fclose(src); |
| 93 | return -1; |
| 94 | } |
| 95 | |
| 96 | size_t n, m; |
| 97 | unsigned char buf[8192]; |
| 98 | do { |
| 99 | n = fread(buf, 1, sizeof buf, src); |
| 100 | if (n) m = fwrite(buf, 1, n, dst); |
| 101 | else m = 0; |
| 102 | } while ((n > 0) && (n == m)); |
| 103 | if (m) return -1; |
| 104 | |
| 105 | fclose(src); |
| 106 | fclose(dst); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | #if defined(__linux__) |
| 112 | char *strcasestr(const char *haystack, const char *needle) { |
| 113 | size_t needle_len = strlen(needle); |
| 114 | if (needle_len == 0) { |
| 115 | return (char *)haystack; |
| 116 | } |
| 117 | |
| 118 | while (*haystack) { |
| 119 | if (strncasecmp(haystack, needle, needle_len) == 0) { |
| 120 | return (char *)haystack; |
| 121 | } |
| 122 | haystack++; |
| 123 | } |
| 124 | return NULL; |
| 125 | } |
| 126 | #endif |
| 127 | |
| 128 | void rmext(char *filename) { |
| 129 | char *ext = strrchr(filename, '.'); |
| 130 | if (ext != NULL && strcmp(ext, ".gpg") == 0) { |
| 131 | *ext = '\0'; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void scanDir(const char *dpath, const char *rpath, List *fpaths, |
| 136 | List *fullpaths, List *dispaths) { |
| 137 | char *lang = getlang(); |
| 138 | |
| 139 | DIR *dir = opendir(dpath); |
| 140 | if (!dir) { |
| 141 | if (strncmp(lang, "en", 2) == 0) |
| 142 | perror("Could not open directory"); |
| 143 | else perror("ディレクトリを開けられません"); |
| 144 | exit(EXIT_FAILURE); |
| 145 | } |
| 146 | |
| 147 | struct dirent *entry; |
| 148 | while ((entry = readdir(dir)) != NULL) { |
| 149 | const char *name = entry->d_name; |
| 150 | if (strncmp(name, ".", 1) == 0 || strncmp(name, "..", 2) == 0) continue; |
| 151 | |
| 152 | char fpath[MAXFINDLEN]; |
| 153 | snprintf(fpath, sizeof(fpath), "%s/%s", dpath, name); |
| 154 | |
| 155 | struct stat s; |
| 156 | if (stat(fpath, &s) != 0) { |
| 157 | closedir(dir); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | if (S_ISDIR(s.st_mode)) { |
| 162 | scanDir(fpath, rpath, fpaths, fullpaths, dispaths); |
| 163 | } else if (strstr(name, ".gpg") != NULL) { |
| 164 | const char *rel = fpath + strlen(rpath) + 1; |
| 165 | addElement(fpaths, rel); |
| 166 | addElement(fullpaths, fpath); |
| 167 | char *disname = strdup(rel); |
| 168 | rmext(disname); |
| 169 | addElement(dispaths, disname); |
| 170 | |
| 171 | free(disname); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | closedir(dir); |
| 176 | } |
| 177 | |
| 178 | void initList(List *list) { |
| 179 | list->head = NULL; |
| 180 | list->tail = NULL; |
| 181 | list->size = 0; |
| 182 | } |
| 183 | |
| 184 | void addElement(List *list, const char *data) { |
| 185 | Node *newNode = (Node *)malloc(sizeof(Node)); |
| 186 | if (!newNode) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | newNode->data = strdup(data); |
| 191 | if (!newNode->data) { |
| 192 | free(newNode); |
| 193 | return; |
| 194 | } |
| 195 | newNode->next = NULL; |
| 196 | |
| 197 | if (list->tail) { |
| 198 | list->tail->next = newNode; |
| 199 | } else { |
| 200 | list->head = newNode; |
| 201 | } |
| 202 | |
| 203 | list->tail = newNode; |
| 204 | list->size++; |
| 205 | } |
| 206 | |
| 207 | char *getElement(List *list, size_t idx) { |
| 208 | if (idx >= list->size) return NULL; |
| 209 | |
| 210 | Node *current = list->head; |
| 211 | for (size_t i = 0; i < idx; i++) |
| 212 | current = current->next; |
| 213 | |
| 214 | return current->data; |
| 215 | } |
| 216 | |
| 217 | void rmElement(List *list, size_t idx) { |
| 218 | if (idx >= list->size) return; |
| 219 | |
| 220 | Node *current = list->head; |
| 221 | Node *previous = NULL; |
| 222 | |
| 223 | if (idx == 0) { |
| 224 | list->head = current->next; |
| 225 | if (list->size == 1) list->tail = NULL; |
| 226 | } else { |
| 227 | for (size_t i = 0; i < idx; i++) { |
| 228 | previous = current; |
| 229 | current = current->next; |
| 230 | } |
| 231 | previous->next = current->next; |
| 232 | if (idx == list->size - 1) { |
| 233 | list->tail = previous; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | free(current->data); |
| 238 | free(current); |
| 239 | list->size--; |
| 240 | } |
| 241 | |
| 242 | void freeList(List *list) { |
| 243 | Node *current = list->head; |
| 244 | |
| 245 | while (current) { |
| 246 | Node *next = current->next; |
| 247 | free(current->data); |
| 248 | free(current); |
| 249 | current = next; |
| 250 | } |
| 251 | |
| 252 | list->head = NULL; |
| 253 | list->tail = NULL; |
| 254 | list->size = 0; |
| 255 | } |
| 256 | |
| 257 | void handle_sigint(int sig) { |
| 258 | (void)sig; |
| 259 | |
| 260 | if (getenv("WAYLAND_DISPLAY") != NULL) { |
| 261 | system("echo -n \"\" | wl-copy"); |
| 262 | } else { |
| 263 | system("echo -n \"\" | xclip -selection clipboard"); |
| 264 | } |
| 265 | |
| 266 | if (strncmp(getlang(), "en", 2) == 0) { |
| 267 | printf("\nClipboard cleared and program aborted.\n"); |
| 268 | } else { |
| 269 | printf("\nクリップボードをクリアし、プログラムが中止されました。\n"); |
| 270 | } |
| 271 | |
| 272 | exit(0); |
| 273 | } |