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,
28 #include <sys/types.h>
31 #include "configfile.h"
34 extern struct swb_config_option swb_config_options[];
36 /* Outputs a config file line for the named option to a file descriptor */
37 static void swb_config_output_option(FILE *fp, unsigned int *oldcfg_seen,
38 struct swb_config *cfg, char *name) {
39 struct swb_config_option *opt;
42 for (opt = swb_config_options; opt->name; ++opt) {
43 if (strcmp(opt->name, name))
46 i = opt - swb_config_options;
47 if (!(*oldcfg_seen & opt->set_mask) &&
48 (cfg->flags & opt->set_mask)) {
50 case SWB_CONFIG_OPT_STRING:
51 fprintf(fp, "%s = \"%s\"\n",
53 *(char **)cfg->entries[i]);
54 *oldcfg_seen |= opt->set_mask;
56 case SWB_CONFIG_OPT_INT:
57 fprintf(fp, "%s = %d\n",
59 *(int *)cfg->entries[i]);
60 *oldcfg_seen |= opt->set_mask;
68 /* Save the settings in the provided swb_config struct to the config file
69 Returns true on success, false otherwise */
70 int swb_config_save(struct swb_config *cfg) {
71 FILE *fp = NULL, *tmpfp = NULL;
72 char *homedir, *tempfile, *newfile;
75 struct swb_config_line line;
76 unsigned int oldcfg_seen = 0;
79 /* If CONFIGFILE_DIR doesn't exist already, try to create it */
80 if (!(homedir = getenv("HOME")))
81 homedir = DEFAULT_HOMEDIR;
82 len = strlen(homedir) + strlen(CONFIGFILE_DIR) + 1;
83 if (!(newfile = calloc(len, sizeof(char))))
85 snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_DIR);
86 if (access(newfile, F_OK) == -1 && errno == ENOENT) {
91 /* Put together the path to the new config file and the tempfile */
92 len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
93 if (!(newfile = calloc(len, sizeof(char))))
95 /* 4 = strlen(".tmp") */
96 if (!(tempfile = calloc(len+4, sizeof(char)))) {
100 snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_LOC);
101 snprintf(tempfile, len+4, "%s%s", newfile, ".tmp");
103 /* Open the temporary file for writing */
104 if (!(tmpfp = fopen(tempfile, "w"))) {
109 /* Open the old config file, if it exists */
110 if ((fp = open_config_file()) && parse_config_file_begin()) {
111 /* Copy the old config file over to the new one line by line,
112 replacing old config values with new ones
113 TODO: should we handle errors differently than EOF? */
114 while (!parse_config_file_line(fp, &line)) {
116 /* Is a config line, print the new value here */
117 swb_config_output_option(tmpfp, &oldcfg_seen,
120 /* Just copy the old line over */
121 fprintf(tmpfp, "%s\n", line.key);
126 parse_config_file_end();
129 /* If we haven't written them yet, write out any new config values */
130 for (i = 0; swb_config_options[i].name; ++i)
131 swb_config_output_option(tmpfp, &oldcfg_seen, cfg,
132 swb_config_options[i].name);
134 /* Replace the old config file with the new one */
137 rename(tempfile, newfile);