--- /dev/null
+/* $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 <glib/gutils.h>
+#include <unistd.h>
+#include <errno.h>
+#include <stdarg.h>
+
+#include "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 gboolean verbose=FALSE;
+
+
+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 (access(dirname,W_OK|X_OK)) {
+ if (verbose)
+ 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(_("Pathname not readable(R) and/or writable(W): %s"),pathname);
+ g_free(pathname);
+ return FALSE;
+ }
+ *static_pathname_pointer=pathname;
+ return TRUE;
+}
+
+G_CONST_RETURN gchar *vpathname_find(const gchar **static_pathname_pointer,va_list ap)
+{
+ 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;
+ G_VA_COPY(ap,ap_copy);
+ goto retry;
+ }
+ if (!*static_pathname_pointer) {
+ g_warning(_("All automatic pathnames failed."));
+ *static_pathname_pointer="";
+ }
+ }
+ 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;
+}