From 83b900fb4649346162e8f8165bcff1927177c31f Mon Sep 17 00:00:00 2001 From: Derek Foreman Date: Tue, 6 Feb 2018 15:18:36 -0600 Subject: [PATCH] shared: Add a function to prefix filenames with datadir Currently we look for png files in their install directory, and we manufacture filenames with string pasting at compile time. This new function will allow overriding the compile time setting with the env var WESTON_DATA_DIR so we can do neat tricks like allow our test suite to pass when we haven't yet installed icons system-wide. Signed-off-by: Derek Foreman Acked-by: Daniel Stone Reviewed-by: Quentin Glidic [Pekka: split if-branch into two lines.] Signed-off-by: Pekka Paalanen --- shared/file-util.c | 19 +++++++++++++++++++ shared/file-util.h | 3 +++ 2 files changed, 22 insertions(+) diff --git a/shared/file-util.c b/shared/file-util.c index 979a3811..e254217c 100644 --- a/shared/file-util.c +++ b/shared/file-util.c @@ -31,6 +31,7 @@ #include #include #include +#include #include "file-util.h" @@ -119,3 +120,21 @@ file_create_dated(const char *path_prefix, const char *suffix, return fdopen(fd, "w"); } + +char * +file_name_with_datadir(const char *filename) +{ + const char *base = getenv("WESTON_DATA_DIR"); + char *out; + int len; + + if (base) + len = asprintf(&out, "%s/%s", base, filename); + else + len = asprintf(&out, "%s/weston/%s", DATADIR, filename); + + if (len == -1) + return NULL; + + return out; +} diff --git a/shared/file-util.h b/shared/file-util.h index f639c446..dbda07a8 100644 --- a/shared/file-util.h +++ b/shared/file-util.h @@ -36,6 +36,9 @@ FILE * file_create_dated(const char *path_prefix, const char *suffix, char *name_out, size_t name_len); +char * +file_name_with_datadir(const char *); + #ifdef __cplusplus } #endif