From: Tomas Krizek Date: Wed, 11 Dec 2019 14:24:31 +0000 (+0100) Subject: lib/utils: create get_workdir() utility function X-Git-Tag: v5.0.0~18^2~3 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7030e77abb7859c3a0a7ecf7bf06a4e225a5f8d1;p=thirdparty%2Fknot-resolver.git lib/utils: create get_workdir() utility function --- diff --git a/daemon/bindings/cache.c b/daemon/bindings/cache.c index 855161a74..9733e59a5 100644 --- a/daemon/bindings/cache.c +++ b/daemon/bindings/cache.c @@ -19,8 +19,6 @@ #include "daemon/worker.h" #include "daemon/zimport.h" -#include - /** @internal return cache, or throw lua error if not open */ struct kr_cache * cache_assert_open(lua_State *L) { @@ -209,10 +207,7 @@ static int cache_open(lua_State *L) int ret = kr_cache_open(&engine->resolver.cache, api, &opts, engine->pool); if (ret != 0) { char cwd[PATH_MAX]; - if(getcwd(cwd, sizeof(cwd)) == NULL) { - const char errprefix[] = ""; - strncpy(cwd, errprefix, sizeof(cwd)); - } + get_workdir(cwd, sizeof(cwd)); return luaL_error(L, "can't open cache path '%s'; working directory '%s'", opts.path, cwd); } diff --git a/lib/utils.h b/lib/utils.h index 44e4e2264..baec16c71 100644 --- a/lib/utils.h +++ b/lib/utils.h @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -184,6 +185,14 @@ static inline long time_diff(struct timeval *begin, struct timeval *end) { return res.tv_sec * 1000 + res.tv_usec / 1000; } +/** Get current working directory with fallback value. */ +static inline void get_workdir(char *out, size_t len) { + if(getcwd(out, len) == NULL) { + static const char errprefix[] = ""; + strncpy(out, errprefix, len); + } +} + /** @cond internal Array types */ struct kr_context;