diff --git a/src/engine/gnc-engine.c b/src/engine/gnc-engine.c index 7f1d4c3827..7e36b6c7fe 100644 --- a/src/engine/gnc-engine.c +++ b/src/engine/gnc-engine.c @@ -86,11 +86,15 @@ gnc_engine_init(int argc, char ** argv) { NULL, NULL, FALSE } }, *lib; gnc_engine_init_hook_t hook; GList * cur; + gchar *tracefilename; if (1 == engine_is_initialized) return; /* initialize logging to our file. */ - qof_log_init_filename("/tmp/gnucash.trace"); + tracefilename = g_build_filename(g_get_tmp_dir(), "gnucash.trace", + (gchar *)NULL); + qof_log_init_filename(tracefilename); + g_free(tracefilename); /* Only set the core log_modules here the rest can be set locally. */ qof_log_set_level(GNC_MOD_ENGINE, QOF_LOG_WARNING); qof_log_set_level(GNC_MOD_IO, QOF_LOG_WARNING); diff --git a/src/engine/gnc-filepath-utils.c b/src/engine/gnc-filepath-utils.c index 3d917aefc0..14a1ec6207 100644 --- a/src/engine/gnc-filepath-utils.c +++ b/src/engine/gnc-filepath-utils.c @@ -65,15 +65,15 @@ MakeHomeDir (void) { int rc; struct stat statbuf; - char *home; + const gchar *home; char *path; char *data; /* Punt. Can't figure out where home is. */ - home = getenv ("HOME"); + home = g_get_home_dir(); if (!home) return; - path = g_strconcat(home, "/.gnucash", NULL); + path = g_build_filename(home, ".gnucash", (gchar *)NULL); rc = stat (path, &statbuf); if (rc) @@ -85,7 +85,7 @@ MakeHomeDir (void) g_mkdir (path, S_IRWXU); /* perms = S_IRWXU = 0700 */ } - data = g_strconcat (path, "/data", NULL); + data = g_build_filename (path, "data", (gchar *)NULL); rc = stat (data, &statbuf); if (rc) g_mkdir (data, S_IRWXU); @@ -97,27 +97,23 @@ MakeHomeDir (void) /* ====================================================================== */ /* XXX hack alert -- we should be yanking this out of some config file */ +/* These are obviously meant to be hard-coded paths to the gnucash + data file. That is insane. These should be thrown out + altogether. On non-Unix systems (Windows) these paths would not + only have different directory separator characters but these + would certainly be completely different paths. I'd vote to + throw this out completely. -- cstim, 2006-07-19 */ static char * searchpaths[] = { - "/usr/share/gnucash/data/", - "/usr/local/share/gnucash/data/", - "/usr/share/gnucash/accounts/", - "/usr/local/share/gnucash/accounts/", + "/usr/share/gnucash/data", + "/usr/local/share/gnucash/data", + "/usr/share/gnucash/accounts", + "/usr/local/share/gnucash/accounts", NULL, }; typedef gboolean (*pathGenerator)(char *pathbuf, int which); -static gboolean -xaccAddEndPath(char *pathbuf, const char *ending, int len) -{ - if(len + strlen(pathbuf) >= PATH_MAX) - return FALSE; - - strcat (pathbuf, ending); - return TRUE; -} - static gboolean xaccCwdPathGenerator(char *pathbuf, int which) { @@ -131,7 +127,6 @@ xaccCwdPathGenerator(char *pathbuf, int which) if (getcwd (pathbuf, PATH_MAX) == NULL) return FALSE; - strcat (pathbuf, "/"); return TRUE; } } @@ -139,23 +134,28 @@ xaccCwdPathGenerator(char *pathbuf, int which) static gboolean xaccDataPathGenerator(char *pathbuf, int which) { - char *path; - if(which != 0) { return FALSE; } else { - path = getenv ("HOME"); - if (!path) + const gchar *home; + gchar *tmppath; + + home = g_get_home_dir (); + if (!home) return FALSE; - if (PATH_MAX <= (strlen (path) + 20)) - return FALSE; + tmppath = g_build_filename (home, ".gnucash", "data", (gchar *)NULL); + if (strlen(tmppath) >= PATH_MAX) + { + g_free (tmppath); + return FALSE; + } - strcpy (pathbuf, path); - strcat (pathbuf, "/.gnucash/data/"); + g_strlcpy (pathbuf, tmppath, PATH_MAX); + g_free (tmppath); return TRUE; } } @@ -176,7 +176,7 @@ xaccUserPathPathGenerator(char *pathbuf, int which) if (PATH_MAX <= strlen(path)) return FALSE; - strcpy (pathbuf, path); + g_strlcpy (pathbuf, path, PATH_MAX); return TRUE; } } @@ -206,14 +206,12 @@ xaccResolveFilePath (const char * filefrag) /* OK, now we try to find or build an absolute file path */ /* check for an absolute file path */ - if (*filefrag == '/') + if (g_path_is_absolute(filefrag)) return g_strdup (filefrag); if (!g_ascii_strncasecmp(filefrag, "file:", 5)) { - char *ret = g_new(char, strlen(filefrag) - 5 + 1); - strcpy(ret, filefrag + 5); - return ret; + return g_strdup(filefrag + 5); } /* get conservative on the length so that sprintf(getpid()) works ... */ @@ -230,14 +228,14 @@ xaccResolveFilePath (const char * filefrag) int j; for(j = 0; gens[i](pathbuf, j) ; j++) { - if(xaccAddEndPath(pathbuf, filefrag, namelen)) - { - int rc = stat (pathbuf, &statbuf); - if ((!rc) && (S_ISREG(statbuf.st_mode))) - { - return (g_strdup (pathbuf)); - } + gchar *fullpath = g_build_filename(pathbuf, filefrag, (gchar *)NULL); + + int rc = stat (fullpath, &statbuf); + if ((!rc) && (S_ISREG(statbuf.st_mode))) + { + return fullpath; } + g_free (fullpath); } } /* OK, we didn't find the file. */ @@ -262,22 +260,20 @@ xaccResolveFilePath (const char * filefrag) /* Lets try creating a new file in $HOME/.gnucash/data */ if (xaccDataPathGenerator(pathbuf, 0)) { - if(xaccAddEndPath(pathbuf, filefrag_dup, namelen)) - { - g_free (filefrag_dup); - return (g_strdup (pathbuf)); - } + gchar *result; + result = g_build_filename(pathbuf, filefrag_dup, (gchar *)NULL); + g_free (filefrag_dup); + return result; } /* OK, we still didn't find the file */ /* Lets try creating a new file in the cwd */ if (xaccCwdPathGenerator(pathbuf, 0)) { - if(xaccAddEndPath(pathbuf, filefrag_dup, namelen)) - { - g_free (filefrag_dup); - return (g_strdup (pathbuf)); - } + gchar *result; + result = g_build_filename(pathbuf, filefrag_dup, (gchar *)NULL); + g_free (filefrag_dup); + return result; } g_free (filefrag_dup); diff --git a/src/engine/test/test-resolve-file-path.c b/src/engine/test/test-resolve-file-path.c index 2261933f0f..6ba0270041 100644 --- a/src/engine/test/test-resolve-file-path.c +++ b/src/engine/test/test-resolve-file-path.c @@ -39,10 +39,14 @@ struct test_strings_struct typedef struct test_strings_struct test_strings; test_strings strs[] = { - { "/.gnucash/test-account-name", "/.gnucash/test-account-name", 1 }, - { "/tmp/test-account-name2", "/tmp/test-account-name2", 0 }, - { "postgres://localhost/foo/bar", "/.gnucash/data/postgres:,,localhost,foo,bar", 2 }, - { "file:/tmp/test-account-name3", "/tmp/test-account-name3", 0 }, + { G_DIR_SEPARATOR_S ".gnucash" G_DIR_SEPARATOR_S "test-account-name", + G_DIR_SEPARATOR_S ".gnucash" G_DIR_SEPARATOR_S "test-account-name", 1 }, + { G_DIR_SEPARATOR_S "tmp" G_DIR_SEPARATOR_S "test-account-name2", + G_DIR_SEPARATOR_S "tmp" G_DIR_SEPARATOR_S "test-account-name2", 0 }, + { "postgres://localhost/foo/bar", + G_DIR_SEPARATOR_S ".gnucash" G_DIR_SEPARATOR_S "data" G_DIR_SEPARATOR_S "postgres:,,localhost,foo,bar", 2 }, + { "file:" G_DIR_SEPARATOR_S "tmp" G_DIR_SEPARATOR_S "test-account-name3", + G_DIR_SEPARATOR_S "tmp" G_DIR_SEPARATOR_S "test-account-name3", 0 }, { NULL, NULL, 0 }, }; @@ -59,15 +63,16 @@ main(int argc, char **argv) if(strs[i].prefix_home == 1) { - dain = g_strdup_printf("%s/%s", g_get_home_dir(), strs[i].input); - wantout = g_strdup_printf("%s/%s", g_get_home_dir(), - strs[i].output); + dain = g_build_filename(g_get_home_dir(), strs[i].input, + (gchar *)NULL); + wantout = g_build_filename(g_get_home_dir(), strs[i].output, + (gchar *)NULL); } else if(strs[i].prefix_home == 2) { dain = g_strdup(strs[i].input); - wantout = g_strdup_printf("%s%s", g_get_home_dir(), - strs[i].output); + wantout = g_build_filename(g_get_home_dir(), strs[i].output, + (gchar *)NULL); } else {