os-posix: simplify os_find_datadir

Use g_build_filename instead of sprintf, and g_autofree instead of
manual freeing.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
Marc-André Lureau 2019-09-18 12:24:10 +04:00 committed by Paolo Bonzini
parent 3819af6e6a
commit 6dd2dacedd
1 changed files with 12 additions and 27 deletions

View File

@ -80,41 +80,26 @@ void os_setup_signal_handling(void)
sigaction(SIGTERM, &act, NULL); sigaction(SIGTERM, &act, NULL);
} }
/* Find a likely location for support files using the location of the binary. /*
For installed binaries this will be "$bindir/../share/qemu". When * Find a likely location for support files using the location of the binary.
running from the build tree this will be "$bindir/../pc-bios". */ * When running from the build tree this will be "$bindir/../pc-bios".
#define SHARE_SUFFIX "/share/qemu" * Otherwise, this is CONFIG_QEMU_DATADIR.
#define BUILD_SUFFIX "/pc-bios" */
char *os_find_datadir(void) char *os_find_datadir(void)
{ {
char *dir, *exec_dir; g_autofree char *exec_dir = NULL;
char *res; g_autofree char *dir = NULL;
size_t max_len;
exec_dir = qemu_get_exec_dir(); exec_dir = qemu_get_exec_dir();
if (exec_dir == NULL) { g_return_val_if_fail(exec_dir != NULL, NULL);
return NULL;
}
dir = g_path_get_dirname(exec_dir);
max_len = strlen(dir) + dir = g_build_filename(exec_dir, "..", "pc-bios", NULL);
MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1; if (g_file_test(dir, G_FILE_TEST_IS_DIR)) {
res = g_malloc0(max_len); return g_steal_pointer(&dir);
snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
if (access(res, R_OK)) {
snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
if (access(res, R_OK)) {
g_free(res);
res = NULL;
}
} }
g_free(dir); return g_strdup(CONFIG_QEMU_DATADIR);
g_free(exec_dir);
return res;
} }
#undef SHARE_SUFFIX
#undef BUILD_SUFFIX
void os_set_proc_name(const char *s) void os_set_proc_name(const char *s)
{ {