+/* $Id$
+ * UDP Gateway persistent configuration pathname detection
+ * Copyright (C) 2005 Jan Kratochvil <project-udpgate@jankratochvil.net>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; exactly version 2 of June 1991 is required
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+
+#include "config.h"
+
+#include <glib/gmessages.h>
+#include <glib/gmem.h>
+#include <glib/gstrfuncs.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "configuration-pathname.h" /* self */
+
+
+/* OK, 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.
+ */
+
+
+static const gchar *static_pathname;
+static gboolean verbose=FALSE;
+
+
+static gboolean try_pairname(const gchar *dirname,const gchar *filename)
+{
+gchar *pathname;
+
+ 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);
+ 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_free(pathname);
+ return FALSE;
+ }
+ static_pathname=pathname;
+ return TRUE;
+}
+
+G_CONST_RETURN gchar *configuration_pathname(void)
+{
+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) {
+ verbose=TRUE;
+ configuration_pathname();
+ g_warning(_("All automatic configuration pathnames failed; configuration persistency disabled."));
+ }
+ first=FALSE;
+ }
+ return static_pathname;
+}