Pathnames detection generalized, no longer 'configuration' specific.
[udpgate.git] / src / pathname.c
1 /* $Id$
2  * UDP Gateway persistent configuration pathname detection
3  * Copyright (C) 2005 Jan Kratochvil <project-udpgate@jankratochvil.net>
4  * 
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
8  * 
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.
13  * 
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
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include <glib/gmem.h>
24 #include <glib/gstrfuncs.h>
25 #include <glib/gutils.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <stdarg.h>
29
30 #include "pathname.h"   /* self */
31
32
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.
36  */
37
38
39 static gboolean verbose=FALSE;
40
41
42 static gboolean try_pairname(const gchar **static_pathname_pointer,const gchar *dirname,const gchar *filename)
43 {
44 gchar *pathname;
45
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);
50
51         if (access(dirname,W_OK|X_OK)) {
52                 if (verbose)
53                         g_warning(_("Directory not accessible(X) and/or writable(W): %s"),dirname);
54                 return FALSE;
55                 }
56         pathname=g_strdup_printf("%s/%s",dirname,filename);
57         if (access(pathname,R_OK|W_OK) && errno!=ENOENT) {
58                 if (verbose)
59                         g_warning(_("Pathname not readable(R) and/or writable(W): %s"),pathname);
60                 g_free(pathname);
61                 return FALSE;
62                 }
63         *static_pathname_pointer=pathname;
64         return TRUE;
65 }
66
67 G_CONST_RETURN gchar *vpathname_find(const gchar **static_pathname_pointer,va_list ap)
68 {
69         g_return_val_if_fail(static_pathname_pointer!=NULL,NULL);
70
71         if (!*static_pathname_pointer) {
72 const char *dirname,*filename;
73 va_list ap_copy;
74
75                 G_VA_COPY(ap_copy,ap);
76 retry:
77                 while ((dirname=va_arg(ap,const char *))) {
78                         filename=va_arg(ap,const char *);
79                         g_assert(filename!=NULL);
80                         /* Succeeded? */
81                         if (try_pairname(static_pathname_pointer,dirname,filename))
82                                 break;
83                         }
84                 if (!*static_pathname_pointer && !verbose) {
85                         verbose=TRUE;
86                         G_VA_COPY(ap,ap_copy);
87                         goto retry;
88                         }
89                 if (!*static_pathname_pointer) {
90                         g_warning(_("All automatic pathnames failed."));
91                         *static_pathname_pointer="";
92                         }
93                 }
94         g_assert(*static_pathname_pointer!=NULL);
95         return *static_pathname_pointer;
96 }
97
98 G_CONST_RETURN gchar *pathname_find(const gchar **static_pathname_pointer,...)
99 {
100 const gchar *r;
101 va_list ap;
102
103         g_return_val_if_fail(static_pathname_pointer!=NULL,NULL);
104
105         va_start(ap,static_pathname_pointer);
106         r=vpathname_find(static_pathname_pointer,ap);
107         va_end(ap);
108         return r;
109 }