]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib/utils: create get_workdir() utility function
authorTomas Krizek <tomas.krizek@nic.cz>
Wed, 11 Dec 2019 14:24:31 +0000 (15:24 +0100)
committerPetr Špaček <petr.spacek@nic.cz>
Thu, 19 Dec 2019 08:31:25 +0000 (09:31 +0100)
daemon/bindings/cache.c
lib/utils.h

index 855161a74b3d477623d5561f4cb1039f592c9f4d..9733e59a5fd584f1f1ef939833775c183c4a424c 100644 (file)
@@ -19,8 +19,6 @@
 #include "daemon/worker.h"
 #include "daemon/zimport.h"
 
-#include <unistd.h>
-
 /** @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[] = "<invalid working directory>";
-                       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);
        }
 
index 44e4e226446c2641dd47ac7f887e72ddf995e8a6..baec16c71baa464280980b82f33dded7bd9e4848 100644 (file)
@@ -22,6 +22,7 @@
 #include <sys/socket.h>
 #include <sys/time.h>
 #include <netinet/in.h>
+#include <unistd.h>
 
 #include <gnutls/gnutls.h>
 #include <gnutls/crypto.h>
@@ -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[] = "<invalid working directory>";
+               strncpy(out, errprefix, len);
+       }
+}
+
 /** @cond internal Array types */
 struct kr_context;