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.
25 #include <libconfig.h>
32 static const char *home_filename = /* $HOME/ */ ".libguestfs-tools.rc";
33 static const char *etc_filename = "/etc/libguestfs-tools.conf";
37 /* Note that parse_config is called very early, before command line
38 * parsing and before the verbose flag has been set.
52 /* Try $HOME first. */
53 home = getenv ("HOME");
55 len = strlen (home) + 1 + strlen (home_filename) + 1;
61 snprintf (path, len, "%s/%s", home, home_filename);
63 fp = fopen (path, "r");
67 fprintf (stderr, "%s: reading configuration from %s\n",
71 if (config_read (&conf, fp) == CONFIG_FALSE) {
73 _("%s: %s: line %d: error parsing configuration file: %s\n"),
74 program_name, path, config_error_line (&conf),
75 config_error_text (&conf));
79 if (fclose (fp) == -1) {
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.
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.
96 config_lookup_bool (&conf, "read_only", &read_only);
102 fp = fopen (etc_filename, "r");
106 fprintf (stderr, "%s: reading configuration from %s\n",
107 program_name, etc_filename);
110 if (config_read (&conf, fp) == CONFIG_FALSE) {
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));
118 if (fclose (fp) == -1) {
119 perror (etc_filename);
123 config_lookup_bool (&conf, "read_only", &read_only);
126 config_destroy (&conf);
129 #else /* !HAVE_LIBCONFIG */
137 _("%s: compiled without libconfig, guestfish configuration file ignored\n"),
142 #endif /* !HAVE_LIBCONFIG */