diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2023-07-24 23:23:55 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-09-24 11:03:39 +0200 |
| commit | 7e34b2d141858eeae3ffb9e514bd55a767a52492 (patch) | |
| tree | 843ac4de27b68683e59a3fe45d66741d41c8d73e /auth.c | |
| parent | a74f4a72504d1c5923bd2b4e85941f34d9fce79f (diff) | |
auth.c: Make use of crealpath
crealpath already provides a mechanism to determine the current working
directory from getcwd(3).
Diffstat (limited to 'auth.c')
| -rw-r--r-- | auth.c | 48 |
1 files changed, 4 insertions, 44 deletions
@@ -1,4 +1,5 @@ #include "auth.h" +#include "crealpath.h" #include "hex.h" #include "jwt.h" #include <libweb/http.h> @@ -527,42 +528,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; @@ -579,21 +544,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__); |
