From bfdad0baf319da3d3f6fb88736ba0f6454cc93d8 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Thu, 11 Apr 2024 18:04:35 +0530 Subject: c/format: init --- .clang-format | 11 +++++ flake.nix | 5 +- include/libnpass/gpg.h | 3 +- include/libnpass/libnpass.h | 8 ++-- include/util.h | 11 +++-- src/libnpass/gpg.c | 23 +++++----- src/libnpass/libnpass.c | 109 +++++++++++++++++++++++--------------------- src/libnpass/util.c | 11 +++-- src/npass/npass.c | 35 +++++++------- 9 files changed, 119 insertions(+), 97 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..e4b15e9 --- /dev/null +++ b/.clang-format @@ -0,0 +1,11 @@ +BasedOnStyle: LLVM +BreakBeforeBraces: Linux +IndentCaseLabels: false + +AlignConsecutiveMacros: true +AllowShortIfStatementsOnASingleLine: false + +ContinuationIndentWidth: 8 +IndentWidth: 8 +TabWidth: 8 +UseTab: Always diff --git a/flake.nix b/flake.nix index 7f6568b..c3e1b2b 100644 --- a/flake.nix +++ b/flake.nix @@ -22,8 +22,11 @@ gpgme meson ninja - ccls + gdb + ccls + clang-tools # clang-format + self.packages.${system}.npass ]; diff --git a/include/libnpass/gpg.h b/include/libnpass/gpg.h index f5053bf..752a0a7 100644 --- a/include/libnpass/gpg.h +++ b/include/libnpass/gpg.h @@ -1,6 +1,7 @@ -#include #include +#include + int gpg_key_validate(const char *fpr); int gpg_decrypt(FILE *pass_out, const char *pass_path); int gpg_encrypt(FILE *stream, const char *fpr, const char *pass, size_t n); diff --git a/include/libnpass/libnpass.h b/include/libnpass/libnpass.h index f1e3d74..d82de9c 100644 --- a/include/libnpass/libnpass.h +++ b/include/libnpass/libnpass.h @@ -1,8 +1,8 @@ -#include #include #include +#include -#define PASS_DEF_LEN 32 +#define PASS_DEF_LEN 32 typedef enum { PASS_GEN_DIGIT = 0, @@ -17,8 +17,8 @@ typedef enum { } pass_store_t; struct store { - char name[NAME_MAX]; - pass_store_t type; + char name[NAME_MAX]; + pass_store_t type; }; DIR *openstore(const char *spath); diff --git a/include/util.h b/include/util.h index 8d735bb..9c650e1 100644 --- a/include/util.h +++ b/include/util.h @@ -1,7 +1,8 @@ #include -#define err_ret(r, fmt, ...) do { \ - fprintf(stderr, "[%s:%d] " fmt "\n", \ - __FILE__, __LINE__, ##__VA_ARGS__); \ - return r; \ -} while (0) +#define err_ret(r, fmt, ...) \ + do { \ + fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, \ + ##__VA_ARGS__); \ + return r; \ + } while (0) diff --git a/src/libnpass/gpg.c b/src/libnpass/gpg.c index dedc149..0b47f42 100644 --- a/src/libnpass/gpg.c +++ b/src/libnpass/gpg.c @@ -1,21 +1,20 @@ -#include #include -#include - #include +#include +#include #include "libnpass/gpg.h" #include "libnpass/util.h" - #include "util.h" -#define gpg_err_ret(err) do { \ - if (err) { \ - gpg_cleanup(); \ - err_ret(1, "%s: %s", \ - gpgme_strsource(err), gpgme_strerror(err)); \ - } \ -} while (0) +#define gpg_err_ret(err) \ + do { \ + if (err) { \ + gpg_cleanup(); \ + err_ret(1, "%s: %s", gpgme_strsource(err), \ + gpgme_strerror(err)); \ + } \ + } while (0) static gpgme_ctx_t ctx = NULL; static gpgme_key_t key[2] = {0}; @@ -27,7 +26,7 @@ static int gpg_init(void) { gpgme_error_t err; const char *local = setlocale(LC_ALL, ""); - + gpgme_check_version(NULL); gpgme_set_locale(NULL, LC_CTYPE, local); #ifdef LC_MESSAGES diff --git a/src/libnpass/libnpass.c b/src/libnpass/libnpass.c index bc1d774..538fe03 100644 --- a/src/libnpass/libnpass.c +++ b/src/libnpass/libnpass.c @@ -1,23 +1,23 @@ -#include -#include -#include -#include #include -#include + +#include +#include #include -#include +#include #include -#include +#include +#include +#include +#include -#include "libnpass/libnpass.h" #include "libnpass/gpg.h" +#include "libnpass/libnpass.h" #include "libnpass/util.h" - #include "util.h" -#define DEF_PASS_DIR "pass" -#define DEF_STOR_LEN 64 -#define FPR_MAX NAME_MAX +#define DEF_PASS_DIR "pass" +#define DEF_STOR_LEN 64 +#define FPR_MAX NAME_MAX static char pass_dir[PATH_MAX] = {0}; const char *pass_gen_set[] = { @@ -36,7 +36,8 @@ const char *pass_gen_set[] = { static int set_pass_dir(void); static int is_storeobj(struct dirent *dir); -static int set_pass_dir(void) { +static int set_pass_dir(void) +{ const char *env; env = getenv("PASSWORD_STORE_DIR"); @@ -48,21 +49,22 @@ static int set_pass_dir(void) { env = getenv("XDG_DATA_HOME"); if (env) { snprintf(pass_dir, sizeof(pass_dir), "%s/%s", env, - DEF_PASS_DIR); + DEF_PASS_DIR); return 0; } env = getenv("HOME"); if (env) { snprintf(pass_dir, sizeof(pass_dir), "%s/%s/%s", env, - ".local/share", DEF_PASS_DIR); + ".local/share", DEF_PASS_DIR); return 0; } return 1; } -static int is_storeobj(struct dirent *dir) { +static int is_storeobj(struct dirent *dir) +{ int r; char *s; @@ -88,15 +90,17 @@ static int is_storeobj(struct dirent *dir) { } } -int pass_store_cmp(const void *vp1, const void *vp2) { +int pass_store_cmp(const void *vp1, const void *vp2) +{ struct store *sp1, *sp2; - sp1 = (struct store *) vp1; - sp2 = (struct store *) vp2; + sp1 = (struct store *)vp1; + sp2 = (struct store *)vp2; return strcmp(sp1->name, sp2->name); } -pass_store_t pass_store_type(const char *spath) { +pass_store_t pass_store_type(const char *spath) +{ int r; struct stat sbuf; char abs_path[PATH_MAX]; @@ -105,17 +109,16 @@ pass_store_t pass_store_type(const char *spath) { if (r) err_ret(PASS_STORE_INV, "PASSWORD_STORE_DIR not set"); - r = snprintf(abs_path, sizeof(abs_path), "%s/%s", - pass_dir, (spath) ? spath : ""); - if (r >= (int) sizeof(abs_path)) + r = snprintf(abs_path, sizeof(abs_path), "%s/%s", pass_dir, + (spath) ? spath : ""); + if (r >= (int)sizeof(abs_path)) err_ret(PASS_STORE_INV, "path exceeded PATH_MAX"); r = stat(abs_path, &sbuf); if (!r && (sbuf.st_mode & S_IFMT) == S_IFDIR) return PASS_STORE_DIR; - r = snprintf(abs_path, sizeof(abs_path), "%s/%s.gpg", - pass_dir, spath); - if (r >= (int) sizeof(abs_path)) + r = snprintf(abs_path, sizeof(abs_path), "%s/%s.gpg", pass_dir, spath); + if (r >= (int)sizeof(abs_path)) err_ret(PASS_STORE_INV, "path exceeded PATH_MAX"); r = stat(abs_path, &sbuf); if (r) @@ -128,7 +131,8 @@ pass_store_t pass_store_type(const char *spath) { } } -DIR *openstore(const char *spath) { +DIR *openstore(const char *spath) +{ int r; DIR *d; const char *path; @@ -140,9 +144,9 @@ DIR *openstore(const char *spath) { err_ret(NULL, "%s is not a passwordstore directory", spath); if (spath) { - r = snprintf(abs_path, sizeof(abs_path), "%s/%s", - pass_dir, spath); - if (r >= (int) sizeof(abs_path)) + r = snprintf(abs_path, sizeof(abs_path), "%s/%s", pass_dir, + spath); + if (r >= (int)sizeof(abs_path)) err_ret(NULL, "path exceeded PATH_MAX"); path = abs_path; @@ -157,7 +161,8 @@ DIR *openstore(const char *spath) { return d; } -int readstore(DIR *dirp, struct store *s) { +int readstore(DIR *dirp, struct store *s) +{ struct dirent *dir; errno = 0; @@ -174,7 +179,7 @@ int readstore(DIR *dirp, struct store *s) { return EOF; } - strncpy(s->name , dir->d_name, sizeof(s->name) - 1); + strncpy(s->name, dir->d_name, sizeof(s->name) - 1); switch (dir->d_type) { case DT_DIR: s->type = PASS_STORE_DIR; @@ -193,7 +198,8 @@ int readstore(DIR *dirp, struct store *s) { return 0; } -int readstore_all(const char *path, struct store **stor) { +int readstore_all(const char *path, struct store **stor) +{ int r; void *p; size_t i; @@ -241,20 +247,20 @@ int pass_init(const char *fpr) if (r) err_ret(1, "key not usable, try gpg --full-generate-key"); - r = r_mkdir(pass_dir, S_IRWXU); if (r) err_ret(1, "%s %s", pass_dir, strerror(errno)); - r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir, ".gpg-id"); - if (r > (int) sizeof(gpg_id_path)) + r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir, + ".gpg-id"); + if (r > (int)sizeof(gpg_id_path)) err_ret(1, "path exceeded PATH_MAX"); gpg_id = fopen(gpg_id_path, "w"); if (!gpg_id) err_ret(1, "%s %s", gpg_id_path, strerror(errno)); - r = fwrite(fpr, strlen(fpr), 1,gpg_id); + r = fwrite(fpr, strlen(fpr), 1, gpg_id); fclose(gpg_id); if (!r) err_ret(1, "write failed"); @@ -277,7 +283,7 @@ int pass_cat(FILE *out, const char *path) err_ret(1, "PASSWORD_STORE_DIR not set"); r = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", pass_dir, path); - if (r >= (int) sizeof(pass_path)) + if (r >= (int)sizeof(pass_path)) err_ret(1, "path exceeded PATH_MAX"); r = gpg_decrypt(out, pass_path); @@ -299,11 +305,11 @@ ssize_t pass_getpass(char **lineptr, size_t *n, FILE *stream) if (r) return -1; - r = getline (lineptr, n, stream); + r = getline(lineptr, n, stream); if (r > 0 && (*lineptr)[r - 1] == '\n') (*lineptr)[r - 1] = '\0'; - (void) tcsetattr (fileno (stream), TCSAFLUSH, &old); + (void)tcsetattr(fileno(stream), TCSAFLUSH, &old); return r; } @@ -312,8 +318,7 @@ int pass_gen(pass_gen_t gen, char *pass, size_t n) { int r; FILE *rand; - size_t pass_gen_len; - + size_t pass_gen_len; if (n <= 0) err_ret(1, "small password"); @@ -323,7 +328,7 @@ int pass_gen(pass_gen_t gen, char *pass, size_t n) err_ret(1, "%s", strerror(errno)); r = fread(pass, sizeof(pass[0]), n, rand); - if (r != (int) n) + if (r != (int)n) err_ret(1, "fread failed"); pass_gen_len = strlen(pass_gen_set[gen]); @@ -344,8 +349,9 @@ int pass_add(const char *path, const char *pass, size_t n) if (r) err_ret(1, "PASSWORD_STORE_DIR not set"); - r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir, ".gpg-id"); - if (r > (int) sizeof(gpg_id_path)) + r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir, + ".gpg-id"); + if (r > (int)sizeof(gpg_id_path)) err_ret(1, "path exceeded PATH_MAX"); gpg_id = fopen(gpg_id_path, "r"); @@ -362,15 +368,15 @@ int pass_add(const char *path, const char *pass, size_t n) if (r) err_ret(1, "invalid key , try gpg --list-keys"); - // TODO: guard against .*\.gpg\.gpg[/$] + /* TODO: guard against .*\.gpg\.gpg[/$] */ r = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", pass_dir, path); - if (r > (int) sizeof(pass_path)) + if (r > (int)sizeof(pass_path)) err_ret(1, "path exceeded PATH_MAX"); rc = strdup(pass_path); if (!rc) err_ret(1, "%s", strerror(errno)); - (void) r_mkdir(dirname(rc), S_IRWXU); + (void)r_mkdir(dirname(rc), S_IRWXU); free(rc); r = access(pass_path, F_OK); @@ -389,7 +395,6 @@ int pass_add(const char *path, const char *pass, size_t n) int pass_rm(const char *path) { - int r = 0; char gpg_path[PATH_MAX], abs_path[PATH_MAX]; @@ -398,14 +403,14 @@ int pass_rm(const char *path) err_ret(1, "PASSWORD_STORE_DIR not set"); r = snprintf(gpg_path, sizeof(gpg_path), "%s.gpg", path); - if (r > (int) sizeof(gpg_path)) + if (r > (int)sizeof(gpg_path)) err_ret(1, "path exceeded PATH_MAX"); r = snprintf(abs_path, sizeof(gpg_path), "%s/%s", pass_dir, gpg_path); - if (r > (int) sizeof(abs_path)) + if (r > (int)sizeof(abs_path)) err_ret(1, "path exceeded PATH_MAX"); - // TODO: guard against .*\.gpg\.gpg[/$] + /* TODO: guard against .*\.gpg\.gpg[/$] */ r = unlink(abs_path); if (r) err_ret(1, "%s %s", abs_path, strerror(errno)); diff --git a/src/libnpass/util.c b/src/libnpass/util.c index e2bbbff..04f2a37 100644 --- a/src/libnpass/util.c +++ b/src/libnpass/util.c @@ -1,10 +1,11 @@ #include -#include + +#include #include +#include #include -#include +#include #include -#include #include "libnpass/util.h" #include "util.h" @@ -18,7 +19,7 @@ int r_mkdir(const char *path, mode_t mode) strncpy(tmp, path, sizeof(tmp)); len = strlen(tmp); - if(tmp[len - 1] == '/') + if (tmp[len - 1] == '/') tmp[len - 1] = '\0'; for (p = tmp + 1; *p; ++p) { @@ -45,7 +46,7 @@ int r_rmdir(const char *prefix_path, char *rm_path) return 0; r = snprintf(abs_path, sizeof(abs_path), "%s/%s", prefix_path, rm_path); - if (r > (int) sizeof(abs_path)) + if (r > (int)sizeof(abs_path)) err_ret(1, "path exceeded PATH_MAX"); r = rmdir(abs_path); diff --git a/src/npass/npass.c b/src/npass/npass.c index 23c3fa9..f3ed9ac 100644 --- a/src/npass/npass.c +++ b/src/npass/npass.c @@ -1,18 +1,17 @@ +#include #include -#include -#include #include -#include +#include +#include #include "libnpass/libnpass.h" #include "util.h" -#define invalid_usage_err_ret() \ - err_ret(1, "invalid usage, try pass help") +#define invalid_usage_err_ret() err_ret(1, "invalid usage, try pass help") -#define DEF_DEPTTH 16; -#define BLUE "\e[0;34m" -#define NCOL "\e[0m" +#define DEF_DEPTTH 16; +#define BLUE "\e[0;34m" +#define NCOL "\e[0m" typedef enum { DEPTH_ITS_OVER = 0, @@ -43,8 +42,7 @@ static void print_usage(void) " init key_id | fingerprint\n" " Initialize new password storage\n" " gen [-a | -d | -g] [-l length] pass_name\n" - " Generate new password\n" - ); + " Generate new password\n"); } static int cat(const char *path) @@ -55,10 +53,11 @@ static int cat(const char *path) if (!r && isatty(STDOUT_FILENO)) putchar('\n'); - return r; + return r; } -static int ls(const char *path, size_t depth) { +static int ls(const char *path, size_t depth) +{ void *p; char *prefix; int i, j, len; @@ -92,14 +91,15 @@ static int ls(const char *path, size_t depth) { depth_state[depth] = DEPTH_NOT_OVER; } - for (j = 0; (size_t) j < depth; j++) - printf("%s ", (depth_state[j]) == DEPTH_ITS_OVER ? " " : "│"); + for (j = 0; (size_t)j < depth; j++) + printf("%s ", + (depth_state[j]) == DEPTH_ITS_OVER ? " " : "│"); if (stor[i].type == PASS_STORE_DIR) { printf("%s %s%s%s\n", prefix, BLUE, stor[i].name, NCOL); snprintf(new_path, sizeof(new_path), "%s/%s", - (path) ? path : "", stor[i].name); + (path) ? path : "", stor[i].name); ls(new_path, depth + 1); } else { printf("%s %s\n", prefix, stor[i].name); @@ -159,7 +159,8 @@ static int add(const char *path) return r; } -static int gen(int argc, char *argv[]) { +static int gen(int argc, char *argv[]) +{ char *pass; int opt, r = 0; size_t len = PASS_DEF_LEN; @@ -242,7 +243,7 @@ int main(int argc, char *argv[]) invalid_usage_err_ret(); r = gen(argc, argv); - } else if (argc == 1){ + } else if (argc == 1) { store_type = pass_store_type(*argv); switch (store_type) { -- cgit v1.2.3