X-Git-Url: http://git.jankratochvil.net/?p=udpgate.git;a=blobdiff_plain;f=src%2Fpathname.c;fp=src%2Fconfiguration-pathname.c;h=fe4fd18120801a6230d7e121327a0162f2de5c03;hp=cc816b528fc076e23044ce32d774a5d570d15081;hb=449337bb425c193812b143c14a53a5f0f9197320;hpb=841083f5a7a6501423467275c5086e8d3a648621 diff --git a/src/configuration-pathname.c b/src/pathname.c similarity index 50% rename from src/configuration-pathname.c rename to src/pathname.c index cc816b5..fe4fd18 100644 --- a/src/configuration-pathname.c +++ b/src/pathname.c @@ -22,11 +22,12 @@ #include #include #include -#include +#include #include #include +#include -#include "configuration-pathname.h" /* self */ +#include "pathname.h" /* self */ /* OK, GConf would be nice. @@ -35,52 +36,74 @@ */ -static const gchar *static_pathname; static gboolean verbose=FALSE; -static gboolean try_pairname(const gchar *dirname,const gchar *filename) +static gboolean try_pairname(const gchar **static_pathname_pointer,const gchar *dirname,const gchar *filename) { gchar *pathname; + g_return_val_if_fail(static_pathname_pointer!=NULL,FALSE); + g_return_val_if_fail(*static_pathname_pointer==NULL,FALSE); g_return_val_if_fail(dirname!=NULL,FALSE); g_return_val_if_fail(filename!=NULL,FALSE); - if (static_pathname) - return FALSE; if (access(dirname,W_OK|X_OK)) { if (verbose) - g_warning(_("Configuration directory not accessible(X) and/or writable(W): %s"),dirname); + g_warning(_("Directory not accessible(X) and/or writable(W): %s"),dirname); return FALSE; } pathname=g_strdup_printf("%s/%s",dirname,filename); if (access(pathname,R_OK|W_OK) && errno!=ENOENT) { if (verbose) - g_warning(_("Configuration pathname not readable(R) and/or writable(W): %s"),pathname); + g_warning(_("Pathname not readable(R) and/or writable(W): %s"),pathname); g_free(pathname); return FALSE; } - static_pathname=pathname; + *static_pathname_pointer=pathname; return TRUE; } -G_CONST_RETURN gchar *configuration_pathname(void) +G_CONST_RETURN gchar *vpathname_find(const gchar **static_pathname_pointer,va_list ap) { -const gchar *home_val; -static gboolean first=TRUE; - - if (!static_pathname && first) { - try_pairname(G_STRINGIFY(SYSCONFDIR) "/sysconfig",PACKAGE); - try_pairname(G_STRINGIFY(SYSCONFDIR) "/default",PACKAGE); - try_pairname(G_STRINGIFY(SYSCONFDIR) "",PACKAGE); - if ((home_val=getenv("HOME"))) - try_pairname(home_val,"." PACKAGE "rc"); - if (!static_pathname && !verbose) { + g_return_val_if_fail(static_pathname_pointer!=NULL,NULL); + + if (!*static_pathname_pointer) { +const char *dirname,*filename; +va_list ap_copy; + + G_VA_COPY(ap_copy,ap); +retry: + while ((dirname=va_arg(ap,const char *))) { + filename=va_arg(ap,const char *); + g_assert(filename!=NULL); + /* Succeeded? */ + if (try_pairname(static_pathname_pointer,dirname,filename)) + break; + } + if (!*static_pathname_pointer && !verbose) { verbose=TRUE; - configuration_pathname(); - g_warning(_("All automatic configuration pathnames failed; configuration persistency disabled.")); + G_VA_COPY(ap,ap_copy); + goto retry; + } + if (!*static_pathname_pointer) { + g_warning(_("All automatic pathnames failed.")); + *static_pathname_pointer=""; } - first=FALSE; } - return static_pathname; + g_assert(*static_pathname_pointer!=NULL); + return *static_pathname_pointer; +} + +G_CONST_RETURN gchar *pathname_find(const gchar **static_pathname_pointer,...) +{ +const gchar *r; +va_list ap; + + g_return_val_if_fail(static_pathname_pointer!=NULL,NULL); + + va_start(ap,static_pathname_pointer); + r=vpathname_find(static_pathname_pointer,ap); + va_end(ap); + return r; }