/** * Build the absolute file path of given path and possible given directory. * * Returned path must be freed. */ char *util_build_path(const char *path, const char *dir) { char *fullPath = NULL, *fexp, *dexp, *p; int expflags = UTIL_EXP_TILDE|UTIL_EXP_DOLLAR; /* if the path could be expanded */ if ((fexp = util_expand(path, expflags))) { if (*fexp == '/') { /* path is already absolute, no need to use given dir - there is * no need to free fexp, bacuse this should be done by the caller * on fullPath later */ fullPath = fexp; } else if (dir && *dir) { /* try to expand also the dir given - this may be ~/path */ if ((dexp = util_expand(dir, expflags))) { /* use expanded dir and append expanded path */ fullPath = g_build_filename(dexp, fexp, NULL); g_free(dexp); } g_free(fexp); } } /* if full path not found use current dir */ if (!fullPath) { fullPath = g_build_filename(g_get_current_dir(), path, NULL); } if ((p = strrchr(fullPath, '/'))) { *p = '\0'; util_create_dir_if_not_exists(fullPath); *p = '/'; } return fullPath; }
static void check_expand(const char *str, const char *expected) { char *result = util_expand(str, UTIL_EXP_DOLLAR|UTIL_EXP_TILDE|UTIL_EXP_SPECIAL); g_assert_cmpstr(result, ==, expected); g_free(result); }