| 1 | #include <dirent.h> |
| 2 | |
| 3 | #include "common.h" |
| 4 | #include "listpass.h" |
| 5 | |
| 6 | void listpass(char *basePath, int level) { |
| 7 | char *lang = getlang(); |
| 8 | |
| 9 | struct dirent *entry; |
| 10 | DIR* dir = opendir(basePath); |
| 11 | if (!dir) { |
| 12 | if (strncmp(lang, "en", 2) == 0) |
| 13 | perror("Could not open directory"); |
| 14 | else perror("ディレクトリを開けられません"); |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | while ((entry = readdir(dir)) != NULL) { |
| 19 | if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { |
| 20 | continue; |
| 21 | } |
| 22 | |
| 23 | char path[1000]; |
| 24 | int needed = snprintf(path, sizeof(path), "%s/%s", basePath, entry->d_name); |
| 25 | if (needed >= (int)sizeof(path) || needed < 0) { |
| 26 | if (strncmp(lang, "en", 2) == 0) |
| 27 | perror("Error: Path is too long, or failed to get lenth"); |
| 28 | else perror("エラー:パスが長すぎる、又は長さを受取に失敗"); |
| 29 | continue; |
| 30 | } |
| 31 | |
| 32 | struct stat statbuf; |
| 33 | if (stat(path, &statbuf) == -1) { |
| 34 | if (strncmp(lang, "en", 2) == 0) |
| 35 | perror("Failed to read file status"); |
| 36 | else perror("ファイル状況を読込に失敗"); |
| 37 | continue; |
| 38 | } |
| 39 | |
| 40 | for (int i = 0; i < level; i++) { |
| 41 | printf(" "); |
| 42 | } |
| 43 | |
| 44 | if (S_ISDIR(statbuf.st_mode)) { |
| 45 | printf("|-- %s\n", entry->d_name); |
| 46 | listpass(path, level + 1); |
| 47 | } else if (S_ISREG(statbuf.st_mode)) { |
| 48 | char *filename = entry->d_name; |
| 49 | char *ext = strstr(filename, ".gpg"); |
| 50 | if (ext) *ext = '\0'; |
| 51 | printf("|-- %s\n", filename); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | closedir(dir); |
| 56 | } |