2 * save-config.c -- functions to save a Browser Switchboard configuration
4 * Copyright (C) 2009-2010 Steven Luo
5 * Derived from a Python implementation by Jason Simpson and Steven Luo
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
27 #include <sys/types.h>
30 #include "configfile.h"
33 /* Save the settings in the provided swb_config struct to the config file
34 Returns true on success, false otherwise */
35 int swb_config_save(struct swb_config *cfg) {
36 FILE *fp = NULL, *tmpfp = NULL;
37 char *homedir, *tempfile, *newfile;
40 struct swb_config_line line;
41 unsigned int oldcfg_seen = 0;
43 /* If CONFIGFILE_DIR doesn't exist already, try to create it */
44 if (!(homedir = getenv("HOME")))
45 homedir = DEFAULT_HOMEDIR;
46 len = strlen(homedir) + strlen(CONFIGFILE_DIR) + 1;
47 if (!(newfile = calloc(len, sizeof(char))))
49 snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_DIR);
50 if (access(newfile, F_OK) == -1 && errno == ENOENT) {
55 /* Put together the path to the new config file and the tempfile */
56 len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
57 if (!(newfile = calloc(len, sizeof(char))))
59 /* 4 = strlen(".tmp") */
60 if (!(tempfile = calloc(len+4, sizeof(char)))) {
64 snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_LOC);
65 snprintf(tempfile, len+4, "%s%s", newfile, ".tmp");
67 /* Open the temporary file for writing */
68 if (!(tmpfp = fopen(tempfile, "w"))) {
73 /* Open the old config file, if it exists */
74 if ((fp = open_config_file()) && parse_config_file_begin()) {
75 /* Copy the old config file over to the new one line by line,
76 replacing old config values with new ones
77 TODO: should we handle errors differently than EOF? */
78 while (!parse_config_file_line(fp, &line)) {
80 /* Is a config line, print the new value here */
81 if (!strcmp(line.key, "continuous_mode")) {
83 SWB_CONFIG_CONTINUOUS_MODE_SET)) {
84 fprintf(tmpfp, "%s = %d\n",
86 cfg->continuous_mode);
88 SWB_CONFIG_CONTINUOUS_MODE_SET;
90 } else if (!strcmp(line.key,
93 SWB_CONFIG_DEFAULT_BROWSER_SET)) {
94 fprintf(tmpfp, "%s = \"%s\"\n",
96 cfg->default_browser);
98 SWB_CONFIG_DEFAULT_BROWSER_SET;
100 } else if (!strcmp(line.key,
101 "other_browser_cmd")) {
103 SWB_CONFIG_OTHER_BROWSER_CMD_SET)) {
104 fprintf(tmpfp, "%s = \"%s\"\n",
106 cfg->other_browser_cmd);
108 SWB_CONFIG_OTHER_BROWSER_CMD_SET;
110 } else if (!strcmp(line.key,
113 SWB_CONFIG_LOGGING_SET)) {
114 fprintf(tmpfp, "%s = \"%s\"\n",
118 SWB_CONFIG_LOGGING_SET;
122 /* Just copy the old line over */
123 fprintf(tmpfp, "%s\n", line.key);
128 parse_config_file_end();
131 /* If we haven't written them yet, write out any new config values */
132 if (!(oldcfg_seen & SWB_CONFIG_CONTINUOUS_MODE_SET) &&
133 (cfg->flags & SWB_CONFIG_CONTINUOUS_MODE_SET))
134 fprintf(tmpfp, "%s = %d\n",
135 "continuous_mode", cfg->continuous_mode);
136 if (!(oldcfg_seen & SWB_CONFIG_DEFAULT_BROWSER_SET) &&
137 (cfg->flags & SWB_CONFIG_DEFAULT_BROWSER_SET))
138 fprintf(tmpfp, "%s = \"%s\"\n",
139 "default_browser", cfg->default_browser);
140 if (!(oldcfg_seen & SWB_CONFIG_OTHER_BROWSER_CMD_SET) &&
141 (cfg->flags & SWB_CONFIG_OTHER_BROWSER_CMD_SET))
142 fprintf(tmpfp, "%s = \"%s\"\n",
143 "other_browser_cmd", cfg->other_browser_cmd);
144 if (!(oldcfg_seen & SWB_CONFIG_LOGGING_SET) &&
145 (cfg->flags & SWB_CONFIG_LOGGING_SET))
146 fprintf(tmpfp, "%s = \"%s\"\n",
147 "logging", cfg->logging);
149 /* Replace the old config file with the new one */
152 rename(tempfile, newfile);