2 * UDP Gateway persistent configuration pathname detection
3 * Copyright (C) 2005 Jan Kratochvil <project-udpgate@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <glib/gmessages.h>
23 #include <glib/gmem.h>
24 #include <glib/gstrfuncs.h>
25 #include <glib/gutils.h>
30 #include "pathname.h" /* self */
33 /* OK, GConf would be nice.
34 * Unfortunately the fully-static build does not support GConf process spawn at
35 * all. Also /etc/sysconfig directory is standard for the daemon services.
39 static gboolean verbose=FALSE;
42 static gboolean try_pairname(const gchar **static_pathname_pointer,const gchar *dirname,const gchar *filename)
46 g_return_val_if_fail(static_pathname_pointer!=NULL,FALSE);
47 g_return_val_if_fail(*static_pathname_pointer==NULL,FALSE);
48 g_return_val_if_fail(dirname!=NULL,FALSE);
49 g_return_val_if_fail(filename!=NULL,FALSE);
51 if (access(dirname,W_OK|X_OK)) {
53 g_warning(_("Directory not accessible(X) and/or writable(W): %s"),dirname);
56 pathname=g_strdup_printf("%s/%s",dirname,filename);
57 if (access(pathname,R_OK|W_OK) && errno!=ENOENT) {
59 g_warning(_("Pathname not readable(R) and/or writable(W): %s"),pathname);
63 *static_pathname_pointer=pathname;
67 G_CONST_RETURN gchar *vpathname_find(const gchar **static_pathname_pointer,va_list ap)
69 g_return_val_if_fail(static_pathname_pointer!=NULL,NULL);
71 if (!*static_pathname_pointer) {
72 const char *dirname,*filename;
75 G_VA_COPY(ap_copy,ap);
77 while ((dirname=va_arg(ap,const char *))) {
78 filename=va_arg(ap,const char *);
79 g_assert(filename!=NULL);
81 if (try_pairname(static_pathname_pointer,dirname,filename))
84 if (!*static_pathname_pointer && !verbose) {
86 G_VA_COPY(ap,ap_copy);
89 if (!*static_pathname_pointer) {
90 g_warning(_("All automatic pathnames failed."));
91 *static_pathname_pointer="";
94 g_assert(*static_pathname_pointer!=NULL);
95 return *static_pathname_pointer;
98 G_CONST_RETURN gchar *pathname_find(const gchar **static_pathname_pointer,...)
103 g_return_val_if_fail(static_pathname_pointer!=NULL,NULL);
105 va_start(ap,static_pathname_pointer);
106 r=vpathname_find(static_pathname_pointer,ap);