From 47166b41e05090c595de633c67dfe8ae4cb19bc0 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Wed, 17 Apr 2024 10:12:12 +0530 Subject: c: rename error reporting marcos --- include/util.h | 6 ++-- src/libnpass/gpg.c | 8 ++--- src/libnpass/libnpass.c | 80 ++++++++++++++++++++++++------------------------- src/libnpass/util.c | 10 +++---- src/npass/npass.c | 34 ++++++++++----------- 5 files changed, 69 insertions(+), 69 deletions(-) diff --git a/include/util.h b/include/util.h index ca4e6d8..2f1c341 100644 --- a/include/util.h +++ b/include/util.h @@ -1,10 +1,10 @@ #include -#define err_print(fmt, ...) \ +#define print_err(fmt, ...) \ fprintf(stderr, "[%s:%d] " fmt "\n", __FILE__, __LINE__, ##__VA_ARGS__) -#define err_ret(r, fmt, ...) \ +#define return_err(r, fmt, ...) \ do { \ - err_print(fmt, ##__VA_ARGS__); \ + print_err(fmt, ##__VA_ARGS__); \ return r; \ } while (0) diff --git a/src/libnpass/gpg.c b/src/libnpass/gpg.c index 671397e..0565f0c 100644 --- a/src/libnpass/gpg.c +++ b/src/libnpass/gpg.c @@ -15,8 +15,8 @@ __gpg_err_ret = EPERM; \ \ gpg_cleanup(); \ - err_ret(-__gpg_err_ret, "%s: %s", gpgme_strsource(err), \ - gpgme_strerror(err)); \ + return_err(-__gpg_err_ret, "%s: %s", gpgme_strsource(err), \ + gpgme_strerror(err)); \ } while (0) static gpgme_ctx_t ctx = NULL; @@ -107,7 +107,7 @@ out_gpg_cleanup: gpg_cleanup(); if (r < 0) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); else return 0; } @@ -152,7 +152,7 @@ out_gpg_cleanup: gpg_cleanup(); if (r < 0) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); else return 0; } diff --git a/src/libnpass/libnpass.c b/src/libnpass/libnpass.c index 779a42a..fd6e447 100644 --- a/src/libnpass/libnpass.c +++ b/src/libnpass/libnpass.c @@ -58,7 +58,7 @@ static int set_pass_dir(void) r = snprintf(pass_dir, sizeof(pass_dir), "%s/%s", env, ".password-store"); if (r < 0 || (size_t)r > sizeof(pass_dir)) - err_ret(-ENAMETOOLONG, "Path exceeded PATH_MAX"); + return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX"); r = stat(pass_dir, &statbuf); if (!r) @@ -70,7 +70,7 @@ static int set_pass_dir(void) r = snprintf(pass_dir, sizeof(pass_dir), "%s/%s", env, DEF_PASS_DIR); if (r < 0 || (size_t)r > sizeof(pass_dir)) - err_ret(-ENAMETOOLONG, "Path exceeded PATH_MAX"); + return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX"); return 0; } @@ -79,7 +79,7 @@ static int set_pass_dir(void) r = snprintf(pass_dir, sizeof(pass_dir), "%s/%s/%s", env, ".local/share", DEF_PASS_DIR); if (r < 0 || (size_t)r > sizeof(pass_dir)) - err_ret(-ENAMETOOLONG, "Path exceeded PATH_MAX"); + return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX"); return 0; } @@ -130,27 +130,27 @@ int pass_store_type(const char *spath) r = set_pass_dir(); if (r < 0) - err_ret(-EINVAL, "PASSWORD_STORE_DIR must be set"); + return_err(-EINVAL, "PASSWORD_STORE_DIR must be set"); r = snprintf(abs_path, sizeof(abs_path), "%s/%s", pass_dir, (spath) ? spath : ""); if (r < 0 || (size_t)r >= sizeof(abs_path)) - err_ret(-ENAMETOOLONG, "Path exceeded PATH_MAX"); + return_err(-ENAMETOOLONG, "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 < 0 || (size_t)r >= sizeof(abs_path)) - err_ret(-ENAMETOOLONG, "Path exceeded PATH_MAX"); + return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX"); r = stat(abs_path, &sbuf); if (r < 0) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); if ((sbuf.st_mode & S_IFMT) == S_IFREG) { return PASS_STORE_ENC; } else { - err_ret(-EINVAL, "%s Not a regular file", abs_path); + return_err(-EINVAL, "%s Not a regular file", abs_path); } } @@ -163,13 +163,13 @@ static int openstore(const char *spath, DIR **dirp) store_type = pass_store_type(spath); if (store_type != PASS_STORE_DIR) - err_ret(-EINVAL, "%s Not a passwordstore path", spath); + return_err(-EINVAL, "%s Not a passwordstore path", spath); if (spath) { r = snprintf(abs_path, sizeof(abs_path), "%s/%s", pass_dir, spath); if (r < 0 || (size_t)r >= sizeof(abs_path)) - err_ret(-ENAMETOOLONG, "Path exceeded PATH_MAX"); + return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX"); path = abs_path; } else { @@ -178,7 +178,7 @@ static int openstore(const char *spath, DIR **dirp) *dirp = opendir(path); if (!*dirp) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); return 0; } @@ -196,7 +196,7 @@ static int readstore(DIR *dirp, struct store *s) if (dir == NULL) { if (errno) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); return 1; } @@ -229,7 +229,7 @@ int readstore_all(const char *path, struct store **stor) size = DEF_STOR_SIZE; *stor = malloc(sizeof(struct store) * size); if (!*stor) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); ret = openstore(path, &dirp); if (ret < 0) { @@ -247,7 +247,7 @@ int readstore_all(const char *path, struct store **stor) *stor = p; } else { free(*stor); - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); } } if (len == 0) @@ -264,30 +264,30 @@ int pass_init(const char *fpr) r = set_pass_dir(); if (r < 0) - err_ret(r, "PASSWORD_STORE_DIR must be set"); + return_err(r, "PASSWORD_STORE_DIR must be set"); r = gpg_key_validate(fpr); if (r < 0) - err_ret(r, "Try gpg --full-generate-key"); + return_err(r, "Try gpg --full-generate-key"); r = r_mkdir(pass_dir, S_IRWXU); if (r < 0) - err_ret(r, "%s %s", pass_dir, strerror(errno)); + return_err(r, "%s %s", pass_dir, strerror(errno)); r = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir, ".gpg-id"); if (r < 0 || (size_t)r >= sizeof(gpg_id_path)) - err_ret(-ENAMETOOLONG, "Path exceeded PATH_MAX"); + return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX"); gpg_id = fopen(gpg_id_path, "w"); if (!gpg_id) - err_ret(-errno, "%s %s", gpg_id_path, strerror(errno)); + return_err(-errno, "%s %s", gpg_id_path, strerror(errno)); fpr_len = strlen(fpr); r = fwrite(fpr, sizeof(*fpr), fpr_len, gpg_id); fclose(gpg_id); if (r != fpr_len) - err_ret(-EPERM, "%s %s", gpg_id_path, "Write failed"); + return_err(-EPERM, "%s %s", gpg_id_path, "Write failed"); return 0; } @@ -301,12 +301,12 @@ int pass_cat(FILE *out, const char *path) if (ret < 0) return ret; if (ret != PASS_STORE_ENC) - err_ret(-EINVAL, "%s is Not a password", path); + return_err(-EINVAL, "%s is Not a password", path); ret = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", pass_dir, path); if (ret < 0 || (size_t)ret >= sizeof(pass_path)) - err_ret(-ENAMETOOLONG, "Path exceeded PATH_MAX"); + return_err(-ENAMETOOLONG, "Path exceeded PATH_MAX"); return gpg_decrypt(out, pass_path); } @@ -325,13 +325,13 @@ ssize_t pass_getpass(char **lineptr, FILE *stream) new.c_lflag &= ~ECHO; ret = tcsetattr(fileno(stream), TCSAFLUSH, &new); if (ret < 0) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); len = 0; *lineptr = NULL; ret = getline(lineptr, &len, stream); if (ret < 0) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); else if (ret > 0 && (*lineptr)[ret - 1] == '\n') (*lineptr)[--ret] = '\0'; len = ret; @@ -339,7 +339,7 @@ ssize_t pass_getpass(char **lineptr, FILE *stream) ret = tcsetattr(fileno(stream), TCSAFLUSH, &old); if (ret < 0) { free(*lineptr); - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); } return len; @@ -352,15 +352,15 @@ int pass_gen(pass_gen_t gen, char *pass, int len) int ret; if (len <= 0) - err_ret(-EINVAL, "Small password"); + return_err(-EINVAL, "Small password"); rand = fopen("/dev/urandom", "r"); if (!rand) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); ret = fread(pass, sizeof(pass[0]), len, rand); if (ret != len) - err_ret(-EPERM, "Failed to read /dev/urandom"); + return_err(-EPERM, "Failed to read /dev/urandom"); pass_gen_len = strlen(pass_gen_set[gen]); for (int i = 0; i < len; i++) @@ -378,32 +378,32 @@ int pass_add(const char *path, const char *pass, size_t n) ret = set_pass_dir(); if (ret < 0) - err_ret(ret, "PASSWORD_STORE_DIR must be set"); + return_err(ret, "PASSWORD_STORE_DIR must be set"); ret = snprintf(gpg_id_path, sizeof(gpg_id_path), "%s/%s", pass_dir, ".gpg-id"); if (ret < 0 || (size_t)ret >= sizeof(gpg_id_path)) - err_ret(-EINVAL, "Path exceeded PATH_MAX"); + return_err(-EINVAL, "Path exceeded PATH_MAX"); gpg_id = fopen(gpg_id_path, "r"); if (!gpg_id) - err_ret(-errno, "%s %s", gpg_id_path, strerror(errno)); + return_err(-errno, "%s %s", gpg_id_path, strerror(errno)); p = fgets(fpr, sizeof(fpr), gpg_id); if (!p) - err_ret(-EPERM, "Failed to read %s", gpg_id_path); + return_err(-EPERM, "Failed to read %s", gpg_id_path); fclose(gpg_id); util_strtrim(fpr); ret = gpg_key_validate(fpr); if (ret < 0) - err_ret(ret, "Invalid key , try gpg --list-keys"); + return_err(ret, "Invalid key , try gpg --list-keys"); /* TODO: guard against .*\.gpg\.gpg[/$] */ ret = snprintf(pass_path, sizeof(pass_path), "%s/%s.gpg", pass_dir, path); if (ret < 0 || (size_t)ret >= sizeof(pass_path)) - err_ret(-errno, "Path exceeded PATH_MAX"); + return_err(-errno, "Path exceeded PATH_MAX"); ret = r_mkdir(dirname(p), S_IRWXU); if (ret < 0) @@ -412,11 +412,11 @@ int pass_add(const char *path, const char *pass, size_t n) errno = 0; ret = access(pass_path, F_OK); if (errno != ENOENT) - err_ret(-EEXIST, "%s %s", path, strerror(EEXIST)); + return_err(-EEXIST, "%s %s", path, strerror(EEXIST)); out_stream = fopen(pass_path, "w"); if (!out_stream) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); ret = gpg_encrypt(out_stream, fpr, pass, n); fclose(out_stream); @@ -431,20 +431,20 @@ int pass_rm(const char *path) ret = set_pass_dir(); if (ret < 0) - err_ret(ret, "PASSWORD_STORE_DIR must be set"); + return_err(ret, "PASSWORD_STORE_DIR must be set"); ret = snprintf(gpg_path, sizeof(gpg_path), "%s.gpg", path); if (ret < 0 || (size_t)ret >= sizeof(gpg_path)) - err_ret(-EINVAL, "Path exceeded PATH_MAX"); + return_err(-EINVAL, "Path exceeded PATH_MAX"); ret = snprintf(abs_path, sizeof(gpg_path), "%s/%s", pass_dir, gpg_path); if (ret < 0 || (size_t)ret >= sizeof(abs_path)) - err_ret(-EINVAL, "Path exceeded PATH_MAX"); + return_err(-EINVAL, "Path exceeded PATH_MAX"); /* TODO: guard against .*\.gpg\.gpg[/$] */ ret = unlink(abs_path); if (ret < 0) - err_ret(-errno, "%s %s", abs_path, strerror(errno)); + return_err(-errno, "%s %s", abs_path, strerror(errno)); return r_rmdir_empty(pass_dir, dirname(gpg_path)); } diff --git a/src/libnpass/util.c b/src/libnpass/util.c index 3b5e052..ffa44a4 100644 --- a/src/libnpass/util.c +++ b/src/libnpass/util.c @@ -19,7 +19,7 @@ int r_mkdir(const char *path, mode_t mode) len = strlen(path); if (len >= PATH_MAX) - err_ret(-EINVAL, "%s", "Path exceeded PATH_MAX"); + return_err(-EINVAL, "%s", "Path exceeded PATH_MAX"); strcpy(path_cp, path); for (p = path_cp + 1; *p; ++p) { @@ -28,7 +28,7 @@ int r_mkdir(const char *path, mode_t mode) ret = mkdir(path_cp, mode); if (ret < 0 && !(errno & EEXIST)) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); *p = '/'; } @@ -36,7 +36,7 @@ int r_mkdir(const char *path, mode_t mode) ret = mkdir(path, mode); if (ret < 0 && !(errno & EEXIST)) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); return 0; } @@ -52,11 +52,11 @@ int r_rmdir_empty(const char *prefix_path, char *rm_path) r = snprintf(abs_path, sizeof(abs_path), "%s/%s", prefix_path, rm_path); if (r < 0 || (size_t)r >= sizeof(abs_path)) - err_ret(-EINVAL, "%s", "Path exceeded PATH_MAX"); + return_err(-EINVAL, "%s", "Path exceeded PATH_MAX"); r = rmdir(abs_path); if (r < 0 && errno != EEXIST && errno != ENOTEMPTY) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); return r_rmdir_empty(prefix_path, dirname(rm_path)); } diff --git a/src/npass/npass.c b/src/npass/npass.c index cf6338f..f78df9d 100644 --- a/src/npass/npass.c +++ b/src/npass/npass.c @@ -7,8 +7,8 @@ #include "libnpass/libnpass.h" #include "util.h" -#define invalid_usage_err_ret(err) \ - err_ret(err, "%s", "Invalid usage, try $pass help") +#define return_invalid_usage(err) \ + return_err(err, "%s", "Invalid usage, try $pass help") #define DEF_DEPTTH 16; #define BLUE "\e[0;34m" @@ -77,7 +77,7 @@ static int ls(const char *path, size_t depth) depth_state = malloc(sizeof(depth_state_t) * depth_state_len); if (!depth_state) { free(stor); - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); } puts((path) ? path : "Password Store"); @@ -86,7 +86,7 @@ static int ls(const char *path, size_t depth) p = realloc(stor, sizeof(*depth_state) * depth_state_len); if (!p) { free(stor); - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); } depth_state = p; } @@ -112,7 +112,7 @@ static int ls(const char *path, size_t depth) (path) ? path : "", stor[i].name); if (ret < 0 || (size_t)ret >= sizeof(new_path)) { ret = -ENAMETOOLONG; - err_print("%s", "Path exceeded PATH_MAX"); + print_err("%s", "Path exceeded PATH_MAX"); goto out_free_depth_state; } @@ -143,7 +143,7 @@ static int add(const char *path) fputs("Password: ", stdout); ret = pass_getpass(&p1, in); if (ret < 0) { - err_print("%s", strerror(errno)); + print_err("%s", strerror(errno)); goto out_close_in; } @@ -151,12 +151,12 @@ static int add(const char *path) ret = pass_getpass(&p2, in); putchar('\n'); if (ret < 0) { - err_print("%s", strerror(errno)); + print_err("%s", strerror(errno)); goto out_free_p1; } if (strcmp(p1, p2)) { - err_print("%s", "Passwords do not match"); + print_err("%s", "Passwords do not match"); ret = -EINVAL; goto out_free_p2; } @@ -199,11 +199,11 @@ static int gen(int argc, char *argv[]) } if (optind != argc - 1) - invalid_usage_err_ret(-EINVAL); + return_invalid_usage(-EINVAL); pass = malloc(sizeof(char) * len); if (!pass) - err_ret(-errno, "%s", strerror(errno)); + return_err(-errno, "%s", strerror(errno)); r = pass_gen(gen, pass, len); if (r) @@ -230,32 +230,32 @@ int main(int argc, char *argv[]) print_usage(); } else if (!strcmp("init", *argv)) { if (argc != 2) - invalid_usage_err_ret(EXIT_FAILURE); + return_invalid_usage(EXIT_FAILURE); r = pass_init(argv[1]); } else if (!strcmp("ls", *argv)) { if (argc > 2) - invalid_usage_err_ret(EXIT_FAILURE); + return_invalid_usage(EXIT_FAILURE); r = ls(argv[1], 0); } else if (!strcmp("cat", *argv)) { if (argc != 2) - invalid_usage_err_ret(EXIT_FAILURE); + return_invalid_usage(EXIT_FAILURE); r = cat(argv[1]); } else if (!strcmp("add", *argv)) { if (argc != 2) - invalid_usage_err_ret(EXIT_FAILURE); + return_invalid_usage(EXIT_FAILURE); r = add(argv[1]); } else if (!strcmp("rm", *argv)) { if (argc != 2) - invalid_usage_err_ret(EXIT_FAILURE); + return_invalid_usage(EXIT_FAILURE); r = pass_rm(argv[1]); } else if (!strcmp("gen", *argv)) { if (argc < 2) - invalid_usage_err_ret(EXIT_FAILURE); + return_invalid_usage(EXIT_FAILURE); r = gen(argc, argv); } else if (argc == 1) { @@ -273,7 +273,7 @@ int main(int argc, char *argv[]) break; } } else { - invalid_usage_err_ret(EXIT_FAILURE); + return_invalid_usage(EXIT_FAILURE); } return (r < 0) ? EXIT_FAILURE : EXIT_SUCCESS; -- cgit v1.2.3