auth.c: Make use of crealpath
crealpath already provides a mechanism to determine the current working directory from getcwd(3).
This commit is contained in:
parent
d2bf6e69c4
commit
edf8cd99a3
48
auth.c
48
auth.c
|
@ -1,4 +1,5 @@
|
|||
#include "auth.h"
|
||||
#include "crealpath.h"
|
||||
#include "hex.h"
|
||||
#include "jwt.h"
|
||||
#include <libweb/http.h>
|
||||
|
@ -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__);
|
||||
|
|
Loading…
Reference in New Issue
Block a user