#include "src/delpass.h"
#include "src/genpass.h"
#include "src/otppass.h"
+#include "src/findpass.h"
const char *sofname = "sp";
const char *version = "1.4.0";
void usage() {
- printf("%s-%s\nusage: %s [-adegilosvy]\n", sofname, version, sofname);
+ printf("%s-%s\nusage: %s [-adefgilosvy]\n", sofname, version, sofname);
}
char *getfullpath(char *arg) {
if (fullPath == NULL) return -1;
otppass(fullPath);
free(fullPath);
+ } else if (strcmp(argv[1], "-f") == 0) {
+ char *fullPath = getfullpath(NULL);
+ if (fullPath == NULL) return -1;
+ findpass(fullPath, argv[2]);
+ free(fullPath);
} else {
usage();
return 1;
#include "common.h"
+#include <string.h>
char *getlang() {
char *lang = NULL;
return 0;
}
+
+void initList(List *list) {
+ list->head = NULL;
+ list->tail = NULL;
+ list->size = 0;
+}
+
+void addElement(List *list, const char *data) {
+ Node *newNode = (Node *)malloc(sizeof(Node));
+ if (!newNode) {
+ return;
+ }
+
+ newNode->data = strdup(data);
+ newNode->next = NULL;
+
+ if (list->tail) {
+ list->tail->next = newNode;
+ } else {
+ list->head = newNode;
+ }
+
+ list->tail = newNode;
+ list->size++;
+}
+
+char *getElement(List *list, size_t idx) {
+ if (idx >= list->size) return NULL;
+
+ Node *current = list->head;
+ for (size_t i = 0; i < idx; i++)
+ current = current->next;
+
+ return current->data;
+}
+
+void rmElement(List *list, size_t idx) {
+ if (idx >= list->size) return;
+
+ Node *current = list->head;
+ Node *previous = NULL;
+
+ if (idx == 0) {
+ list->head = current->next;
+ if (list->size == 1) list->tail = NULL;
+ } else {
+ for (size_t i = 0; i < idx; i++) {
+ previous = current;
+ current = current->next;
+ }
+ previous->next = current->next;
+ if (idx == list->size - 1) {
+ list->tail = previous;
+ }
+ }
+
+ free(current->data);
+ free(current);
+ list->size--;
+}
+
+void freeList(List *list) {
+ Node *current = list->head;
+
+ while (current) {
+ Node *next = current->next;
+ free(current->data);
+ free(current);
+ current = next;
+ }
+
+ list->head = NULL;
+ list->tail = NULL;
+ list->size = 0;
+}
#include <gpgme.h>
+typedef struct Node {
+ char *data;
+ struct Node *next;
+} Node;
+
+typedef struct {
+ Node *head;
+ Node *tail;
+ size_t size;
+} List;
+
char *getlang();
int mkdir_r(const char *path, mode_t mode);
+// C言語のvector
+void initList(List *list);
+void addElement(List *list, const char *data);
+char *getElement(List *list, size_t idx);
+void rmElement(List *list, size_t idx);
+void freeList(List *list);
+
#endif
--- /dev/null
+#include "common.h"
+#include "findpass.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dirent.h>
+#include <sys/stat.h>
+
+#define MAXFINDLEN 1024
+
+List fullpaths;
+List dispaths;
+
+#if defined(__linux__)
+char *strcasestr(const char *haystack, const char *needle) {
+ size_t needle_len = strlen(needle);
+ if (needle_len == 0) {
+ return (char *)haystack;
+ }
+
+ while (*haystack) {
+ if (strncasecmp(haystack, needle, needle_len) == 0) {
+ return (char *)haystack;
+ }
+ haystack++;
+ }
+ return NULL;
+}
+#endif
+
+void rmext(char *filename) {
+ char *ext = strrchr(filename, '.');
+ if (ext != NULL && strcmp(ext, ".gpg") == 0) {
+ *ext = '\0';
+ }
+}
+
+void scanDir(const char *dpath, const char *rpath, List *fpaths) {
+ char *lang = getlang();
+
+ DIR *dir = opendir(dpath);
+ if (!dir) {
+ if (strncmp(lang, "en", 2) == 0)
+ perror("Could not open directory");
+ else perror("ディレクトリを開けられません");
+ exit(EXIT_FAILURE);
+ }
+
+ struct dirent *entry;
+ while ((entry = readdir(dir)) != NULL) {
+ const char *name = entry->d_name;
+ if (strncmp(name, ".", 1) == 0 || strncmp(name, "..", 2) == 0) continue;
+
+ char fpath[MAXFINDLEN];
+ snprintf(fpath, sizeof(fpath), "%s/%s", dpath, name);
+
+ struct stat s;
+ if (stat(fpath, &s) != 0) {
+ closedir(dir);
+ return;
+ }
+
+ if (S_ISDIR(s.st_mode)) {
+ scanDir(fpath, rpath, fpaths);
+ } else if (strstr(name, ".gpg") != NULL) {
+ const char *rel = fpath + strlen(rpath) + 1;
+ addElement(fpaths, rel);
+ addElement(&fullpaths, fpath);
+ char *disname = strdup(rel);
+ rmext(disname);
+ addElement(&dispaths, disname);
+ }
+ }
+
+ closedir(dir);
+}
+
+void findpass(const char *dpath, const char *txt) {
+ List fpaths;
+ initList(&fpaths);
+ initList(&fullpaths);
+ initList(&dispaths);
+ scanDir(dpath, dpath, &fpaths);
+
+ for (size_t i = 0; i < dispaths.size; i++) {
+ if (strcasestr(getElement(&dispaths, i), txt) != NULL) {
+ printf("%s\n", getElement(&dispaths, i));
+ }
+ }
+
+ freeList(&fpaths);
+}
--- /dev/null
+#ifndef FINDPASS_H
+#define FINDPASS_H
+
+#include "common.h"
+
+void scanDir(const char *dpath, const char *rpath, List *fpaths);
+void findpass(const char *dpath, const char *txt);
+
+#endif