Implemented crossplatform+$HOME configuration file management.
[udpgate.git] / src / configuration.c
index 3dee3cb..d112ed9 100644 (file)
 #include <string.h>
 
 #include "configuration.h"     /* self */
+#include "configuration-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 */
 
 
@@ -67,20 +67,23 @@ static GHashTable *hash;
 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;
 }
 
@@ -128,11 +131,14 @@ GHashTable *hash_flushed;
 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 {
@@ -186,7 +192,7 @@ err_line:
                                *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++;
@@ -228,14 +234,14 @@ const char *line_new=udpgate_printf_alloca("%s=%s\n",varname_start,value);
                        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;
 
@@ -269,7 +275,7 @@ static void configuration_read_hash_foreach(const gchar *key,const gchar *value,
                /* 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)