From 449337bb425c193812b143c14a53a5f0f9197320 Mon Sep 17 00:00:00 2001 From: short <> Date: Sun, 26 Jun 2005 04:43:42 +0000 Subject: [PATCH] Pathnames detection generalized, no longer 'configuration' specific. --- src/configuration.c | 18 ++++++- src/{configuration-pathname.c => pathname.c} | 71 ++++++++++++++++++---------- src/{configuration-pathname.h => pathname.h} | 9 ++-- 3 files changed, 68 insertions(+), 30 deletions(-) rename src/{configuration-pathname.c => pathname.c} (50%) rename src/{configuration-pathname.h => pathname.h} (77%) diff --git a/src/configuration.c b/src/configuration.c index 9543064..8be581b 100644 --- a/src/configuration.c +++ b/src/configuration.c @@ -23,14 +23,15 @@ #include #include #include +#include #include #include #include -#include #include +#include #include "configuration.h" /* self */ -#include "configuration-pathname.h" +#include "pathname.h" #include "network.h" #include "main.h" @@ -45,6 +46,19 @@ #define LOCATION_LINK "/proc/self/exe" /* for Linux kernel */ +static G_CONST_RETURN gchar *configuration_pathname(void) +{ +static const gchar *static_pathname; + + return pathname_find(&static_pathname, + G_STRINGIFY(SYSCONFDIR) "/sysconfig",PACKAGE, + G_STRINGIFY(SYSCONFDIR) "/default",PACKAGE, + G_STRINGIFY(SYSCONFDIR) "",PACKAGE, + /* 'getenv("HOME")' may return NULL and terminate the list prematurely. */ + getenv("HOME"),"." PACKAGE "rc", + NULL); +} + static GHashTable *configuration_hash_new(void) { return g_hash_table_new_full(g_str_hash,g_str_equal, 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; } diff --git a/src/configuration-pathname.h b/src/pathname.h similarity index 77% rename from src/configuration-pathname.h rename to src/pathname.h index 102624e..4673dab 100644 --- a/src/configuration-pathname.h +++ b/src/pathname.h @@ -17,8 +17,8 @@ */ -#ifndef _UDPGATE_CONFIGURATION_PATHNAME_H -#define _UDPGATE_CONFIGURATION_PATHNAME_H 1 +#ifndef _UDPGATE_PATHNAME_H +#define _UDPGATE_PATHNAME_H 1 #include @@ -26,9 +26,10 @@ G_BEGIN_DECLS -G_CONST_RETURN gchar *configuration_pathname(void); +G_CONST_RETURN gchar *vpathname_find(const gchar **static_pathname_pointer,va_list ap); +G_CONST_RETURN gchar *pathname_find(const gchar **static_pathname_pointer,...); G_END_DECLS -#endif /* _UDPGATE_CONFIGURATION_PATHNAME_H */ +#endif /* _UDPGATE_PATHNAME_H */ -- 1.8.3.1