#include <glib/ghash.h>
#include <glib/gstring.h>
#include <glib/gmem.h>
+#include <glib/gstrfuncs.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
-#include <glib/gstrfuncs.h>
#include <string.h>
+#include <stdlib.h>
#include "configuration.h" /* self */
+#include "pathname.h"
#include "network.h"
#include "main.h"
-/* OK, GConf would be nice.
+/* OK, FIXME: GConf would be nice.
* Unfortunately the fully-static build does not support GConf process spawn at
- * all. Also /etc/sysconfig directory is standard for the daemon services.
+ * all.
*/
/* Config: */
-#define CONFIGURATION_FILE "/etc/sysconfig/udpgate"
#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,
static gboolean configuration_file_write(const gchar *file_content)
{
FILE *f;
+const gchar *pathname;
g_return_val_if_fail(file_content!=NULL,FALSE);
- if (!(f=fopen(CONFIGURATION_FILE,("w")))) {
- g_warning(_("Error write opening configuration file \"%s\": %m"),CONFIGURATION_FILE);
+ if (!(pathname=configuration_pathname()))
+ return FALSE;
+ if (!(f=fopen(pathname,"w"))) {
+ g_warning(_("Error write opening configuration file \"%s\": %m"),pathname);
return FALSE;
}
if (fputs(file_content,f)<0) {
- g_warning(_("Error writing configuration file \"%s\": %m"),CONFIGURATION_FILE);
+ g_warning(_("Error writing configuration file \"%s\": %m"),pathname);
fclose(f); /* errors ignored */
return FALSE;
}
if (fclose(f))
- g_warning(_("Error closing configuration file \"%s\": %m"),CONFIGURATION_FILE);
+ g_warning(_("Error closing configuration file \"%s\": %m"),pathname);
return TRUE;
}
GString *gstring;
gboolean modified=FALSE; /* 'gstring' contains modified value */
gboolean already_written=FALSE;
+const gchar *pathname;
+ if (!(pathname=configuration_pathname()))
+ return NULL;
open_retry:
- if (!(f=fopen(CONFIGURATION_FILE,(!hash_flush ? "r" : "rw")))) {
+ if (!(f=fopen(pathname,(!hash_flush ? "r" : "rw")))) {
if (errno!=ENOENT)
- g_warning(_("Error r/o opening configuration file \"%s\": %m"),CONFIGURATION_FILE);
+ g_warning(_("Error r/o opening configuration file \"%s\": %m"),pathname);
if (!hash_flush || already_written)
return NULL;
else {
while (errno=0,fgets(line,sizeof(line),f)) {
char *s;
char *varname_start,*varname_stop,varname_stop_orig;
-char *varcontent_start,*varcontent_stop,varcontent_stop_orig;
+char *varcontent_start,*varcontent_stop,varcontent_stop_orig=0 /* shut up GCC */;
const gchar *value;
lineno++;
*varname_stop=varname_stop_orig;
if (varcontent_stop)
*varcontent_stop=varcontent_stop_orig;
- g_warning(_("Error parsing line %d of the configuration file \"%s\": %s"),lineno,CONFIGURATION_FILE,line);
+ g_warning(_("Error parsing line %d of the configuration file \"%s\": %s"),lineno,pathname,line);
goto err_append;
}
while (*s && isspace(*s)) s++;
goto err_append;
}
if (errno) {
- g_warning(_("Error reading line from the configuration file \"%s\": %s"),CONFIGURATION_FILE,strerror(errno));
+ g_warning(_("Error reading line from the configuration file \"%s\": %s"),pathname,strerror(errno));
if (hash_fill)
g_hash_table_destroy(hash_fill);
fclose(f); /* errors ignored */
return NULL;
}
if (fclose(f))
- g_warning(_("Error closing configuration file \"%s\": %m"),CONFIGURATION_FILE);
+ g_warning(_("Error closing configuration file \"%s\": %m"),pathname);
if (hash_flushed) {
struct configuration_hash_readwrite_hash_flush_foreach_param param;
/* nop */;
else
g_warning(_("Unknown configuration key \"%s\" with value \"%s\" found in the file \"%s\""),
- key,value,CONFIGURATION_FILE);
+ key,value,configuration_pathname());
}
gboolean configuration_read(void)