test/common: simplify test_get_path() Signed-off-by: Ran Benita <ran@unusedvar.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
diff --git a/test/common.c b/test/common.c
index 40565e5..e397e12 100644
--- a/test/common.c
+++ b/test/common.c
@@ -40,6 +40,7 @@
#include <termios.h>
#include "test.h"
+#include "utils.h"
/*
* Test a sequence of keysyms, resulting from a sequence of key presses,
@@ -159,25 +160,22 @@ test_key_seq(struct xkb_keymap *keymap, ...)
char *
test_get_path(const char *path_rel)
{
+ int ret;
char *path;
- size_t path_len;
- const char *srcdir = getenv("top_srcdir");
+ const char *srcdir;
+
+ srcdir = getenv("top_srcdir");
+ if (!srcdir)
+ srcdir = ".";
if (path_rel[0] == '/')
return strdup(path_rel);
- path_len = strlen(srcdir ? srcdir : ".") +
- strlen(path_rel) + 12;
- path = malloc(path_len);
- if (!path) {
- fprintf(stderr, "Failed to allocate path (%d chars) for %s\n",
- (int) path_len, path_rel);
+ ret = asprintf(&path, "%s/test/data/%s", srcdir, path_rel);
+ if (ret < 0) {
+ fprintf(stderr, "Failed to allocate path for %s\n", path_rel);
return NULL;
}
- snprintf(path, path_len,
- "%s/test/data/%s", srcdir ? srcdir : ".",
- path_rel ? path_rel : "");
-
return path;
}