1 /* libguestfs - guestfish and guestmount shared option parsing
2 * Copyright (C) 2011 Red Hat Inc.
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.
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 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.
26 #include <libconfig.h>
33 static const char *home_filename = /* $HOME/ */ ".libguestfs-tools.rc";
34 static const char *etc_filename = "/etc/libguestfs-tools.conf";
38 /* Note that parse_config is called very early, before command line
39 * parsing and before the verbose flag has been set.
53 /* Try $HOME first. */
54 home = getenv ("HOME");
56 len = strlen (home) + 1 + strlen (home_filename) + 1;
62 snprintf (path, len, "%s/%s", home, home_filename);
64 fp = fopen (path, "r");
68 fprintf (stderr, "%s: reading configuration from %s\n",
72 if (config_read (&conf, fp) == CONFIG_FALSE) {
74 _("%s: %s: line %d: error parsing configuration file: %s\n"),
75 program_name, path, config_error_line (&conf),
76 config_error_text (&conf));
80 if (fclose (fp) == -1) {
87 * (1) It's not obvious from the documentation, that config_read
88 * completely resets the 'conf' structure. This means we cannot
89 * call config_read twice on the two possible configuration
90 * files, but instead have to copy out settings into our
91 * variables between calls.
93 * (2) If the next call fails then 'read_only' variable is not
94 * updated. Failure could happen just because the setting is
95 * missing from the configuration file, so we ignore it here.
97 config_lookup_bool (&conf, "read_only", &read_only);
103 fp = fopen (etc_filename, "r");
107 fprintf (stderr, "%s: reading configuration from %s\n",
108 program_name, etc_filename);
111 if (config_read (&conf, fp) == CONFIG_FALSE) {
113 _("%s: %s: line %d: error parsing configuration file: %s\n"),
114 program_name, etc_filename, config_error_line (&conf),
115 config_error_text (&conf));
119 if (fclose (fp) == -1) {
120 perror (etc_filename);
124 config_lookup_bool (&conf, "read_only", &read_only);
127 config_destroy (&conf);
130 #else /* !HAVE_LIBCONFIG */
138 _("%s: compiled without libconfig, guestfish configuration file ignored\n"),
143 #endif /* !HAVE_LIBCONFIG */