From 18fefd8847f27e84b01edc9fe5d5a9eaae9055f0 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Mon, 24 Jul 2023 23:23:55 +0200 Subject: auth.c: Make use of crealpath crealpath already provides a mechanism to determine the current working directory from getcwd(3). --- auth.c | 48 ++++-------------------------------------------- 1 file changed, 4 insertions(+), 44 deletions(-) diff --git a/auth.c b/auth.c index 1db2683..ad37ae6 100644 --- a/auth.c +++ b/auth.c @@ -1,4 +1,5 @@ #include "auth.h" +#include "crealpath.h" #include "hex.h" #include "jwt.h" #include @@ -511,42 +512,6 @@ static int init_db(struct auth *const a) return 0; } -static char *resolve_cwd(void) -{ - size_t len = 1; - char *p = NULL; - - for (;;) - { - char *const pp = realloc(p, len); - - if (!pp) - { - fprintf(stderr, "%s: realloc(3): %s\n", __func__, strerror(errno)); - break; - } - - p = pp; - - if (!getcwd(pp, len)) - { - if (errno != ERANGE) - { - fprintf(stderr, "%s: getcwd(3): %s\n", - __func__, strerror(errno)); - break; - } - else - len++; - } - else - return p; - } - - free(p); - return NULL; -} - struct auth *auth_alloc(const char *const dir) { struct auth *const a = malloc(sizeof *a), *ret = NULL; @@ -563,21 +528,16 @@ struct auth *auth_alloc(const char *const dir) dynstr_init(&a->db); dynstr_init(&a->dir); - if (*dir != '/' && !(abspath = resolve_cwd())) + if (!(abspath = crealpath(dir))) { - fprintf(stderr, "%s: resolve_cwd failed\n", __func__); + fprintf(stderr, "%s: crealpath failed\n", __func__); goto end; } - else if (abspath && dynstr_append(&a->dir, "%s", abspath)) + else if (dynstr_append(&a->dir, "%s", abspath)) { fprintf(stderr, "%s: dynstr_append abspath failed\n", __func__); goto end; } - else if (dynstr_append(&a->dir, "%s%s", abspath ? "/" : "", dir)) - { - fprintf(stderr, "%s: dynstr_append dir failed\n", __func__); - goto end; - } else if (dynstr_append(&a->db, "%s/db.json", a->dir.str)) { fprintf(stderr, "%s: dynstr_append db failed\n", __func__); -- cgit v1.2.3