fish: Fix incorrect path passed to perror.
[libguestfs.git] / fish / config.c
1 /* libguestfs - guestfish and guestmount shared option parsing
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #ifdef HAVE_LIBCONFIG
25 #include <libconfig.h>
26 #endif
27
28 #include "guestfs.h"
29
30 #include "options.h"
31
32 static const char *home_filename = /* $HOME/ */ ".libguestfs-tools.rc";
33 static const char *etc_filename = "/etc/libguestfs-tools.conf";
34
35 #ifdef HAVE_LIBCONFIG
36
37 /* Note that parse_config is called very early, before command line
38  * parsing and before the verbose flag has been set.
39  */
40
41 void
42 parse_config (void)
43 {
44   const char *home;
45   size_t len;
46   char *path;
47   FILE *fp;
48   config_t conf;
49
50   config_init (&conf);
51
52   /* Try $HOME first. */
53   home = getenv ("HOME");
54   if (home != NULL) {
55     len = strlen (home) + 1 + strlen (home_filename) + 1;
56     path = malloc (len);
57     if (path == NULL) {
58       perror ("malloc");
59       exit (EXIT_FAILURE);
60     }
61     snprintf (path, len, "%s/%s", home, home_filename);
62
63     fp = fopen (path, "r");
64     if (fp != NULL) {
65       /*
66       if (verbose)
67         fprintf (stderr, "%s: reading configuration from %s\n",
68                  program_name, path);
69       */
70
71       if (config_read (&conf, fp) == CONFIG_FALSE) {
72         fprintf (stderr,
73                  _("%s: %s: line %d: error parsing configuration file: %s\n"),
74                  program_name, path, config_error_line (&conf),
75                  config_error_text (&conf));
76         exit (EXIT_FAILURE);
77       }
78
79       if (fclose (fp) == -1) {
80         perror (path);
81         exit (EXIT_FAILURE);
82       }
83
84       /* Notes:
85        *
86        * (1) It's not obvious from the documentation, that config_read
87        * completely resets the 'conf' structure.  This means we cannot
88        * call config_read twice on the two possible configuration
89        * files, but instead have to copy out settings into our
90        * variables between calls.
91        *
92        * (2) If the next call fails then 'read_only' variable is not
93        * updated.  Failure could happen just because the setting is
94        * missing from the configuration file, so we ignore it here.
95        */
96       config_lookup_bool (&conf, "read_only", &read_only);
97     }
98
99     free (path);
100   }
101
102   fp = fopen (etc_filename, "r");
103   if (fp != NULL) {
104     /*
105     if (verbose)
106       fprintf (stderr, "%s: reading configuration from %s\n",
107                program_name, etc_filename);
108     */
109
110     if (config_read (&conf, fp) == CONFIG_FALSE) {
111       fprintf (stderr,
112                _("%s: %s: line %d: error parsing configuration file: %s\n"),
113                program_name, etc_filename, config_error_line (&conf),
114                config_error_text (&conf));
115       exit (EXIT_FAILURE);
116     }
117
118     if (fclose (fp) == -1) {
119       perror (etc_filename);
120       exit (EXIT_FAILURE);
121     }
122
123     config_lookup_bool (&conf, "read_only", &read_only);
124   }
125
126   config_destroy (&conf);
127 }
128
129 #else /* !HAVE_LIBCONFIG */
130
131 void
132 parse_config (void)
133 {
134   /*
135   if (verbose)
136     fprintf (stderr,
137              _("%s: compiled without libconfig, guestfish configuration file ignored\n"),
138              program_name);
139   */
140 }
141
142 #endif /* !HAVE_LIBCONFIG */