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 extern struct swb_config_option swb_config_options[];
35 /* Outputs a config file line for the named option to a file descriptor */
36 static void swb_config_output_option(FILE *fp, unsigned int *oldcfg_seen,
37 struct swb_config *cfg, char *name) {
39 struct swb_config_option opt;
41 for (i = 0; swb_config_options[i].name; ++i) {
42 opt = swb_config_options[i];
43 if (strcmp(opt.name, name))
46 if (!(*oldcfg_seen & opt.set_mask) &&
47 (cfg->flags & opt.set_mask)) {
49 case SWB_CONFIG_OPT_STRING:
50 fprintf(fp, "%s = \"%s\"\n",
52 *(char **)cfg->entries[i]);
53 *oldcfg_seen |= opt.set_mask;
55 case SWB_CONFIG_OPT_INT:
56 fprintf(fp, "%s = %d\n",
58 *(int *)cfg->entries[i]);
59 *oldcfg_seen |= opt.set_mask;
67 /* Save the settings in the provided swb_config struct to the config file
68 Returns true on success, false otherwise */
69 int swb_config_save(struct swb_config *cfg) {
70 FILE *fp = NULL, *tmpfp = NULL;
71 char *homedir, *tempfile, *newfile;
74 struct swb_config_line line;
75 unsigned int oldcfg_seen = 0;
78 /* If CONFIGFILE_DIR doesn't exist already, try to create it */
79 if (!(homedir = getenv("HOME")))
80 homedir = DEFAULT_HOMEDIR;
81 len = strlen(homedir) + strlen(CONFIGFILE_DIR) + 1;
82 if (!(newfile = calloc(len, sizeof(char))))
84 snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_DIR);
85 if (access(newfile, F_OK) == -1 && errno == ENOENT) {
90 /* Put together the path to the new config file and the tempfile */
91 len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
92 if (!(newfile = calloc(len, sizeof(char))))
94 /* 4 = strlen(".tmp") */
95 if (!(tempfile = calloc(len+4, sizeof(char)))) {
99 snprintf(newfile, len, "%s%s", homedir, CONFIGFILE_LOC);
100 snprintf(tempfile, len+4, "%s%s", newfile, ".tmp");
102 /* Open the temporary file for writing */
103 if (!(tmpfp = fopen(tempfile, "w"))) {
108 /* Open the old config file, if it exists */
109 if ((fp = open_config_file()) && parse_config_file_begin()) {
110 /* Copy the old config file over to the new one line by line,
111 replacing old config values with new ones
112 TODO: should we handle errors differently than EOF? */
113 while (!parse_config_file_line(fp, &line)) {
115 /* Is a config line, print the new value here */
116 swb_config_output_option(tmpfp, &oldcfg_seen,
119 /* Just copy the old line over */
120 fprintf(tmpfp, "%s\n", line.key);
125 parse_config_file_end();
128 /* If we haven't written them yet, write out any new config values */
129 for (i = 0; swb_config_options[i].name; ++i)
130 swb_config_output_option(tmpfp, &oldcfg_seen, cfg,
131 swb_config_options[i].name);
133 /* Replace the old config file with the new one */
136 rename(tempfile, newfile);