ee0e085bc91a131f462d2d1a840ea94104cbaee6
[browser-switch] / configfile.c
1 /*
2  * configfile.c -- config file functions for browser-switchboard
3  *
4  * Copyright (C) 2009 Steven Luo
5  * Derived from a Python implementation by Jason Simpson and Steven Luo
6  *
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.
11  *
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.
16  *
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,
20  * USA.
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26
27 #include "configfile.h"
28
29 FILE *open_config_file(void) {
30         char *homedir, *configfile;
31         size_t len;
32         FILE *fp;
33
34         /* Put together the path to the config file */
35         if (!(homedir = getenv("HOME")))
36                 homedir = DEFAULT_HOMEDIR;
37         len = strlen(homedir) + strlen(CONFIGFILE_LOC) + 1;
38         if (!(configfile = calloc(len, sizeof(char))))
39                 return NULL;
40         snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC);
41
42         /* Try to open the config file */
43         if (!(fp = fopen(configfile, "r"))) {
44                 /* Try the legacy config file location before giving up
45                    XXX we assume here that CONFIGFILE_LOC_OLD is shorter
46                    than CONFIGFILE_LOC! */
47                 snprintf(configfile, len, "%s%s", homedir, CONFIGFILE_LOC_OLD);
48                 fp = fopen(configfile, "r");
49         }
50
51         free(configfile);
52         return fp;
53 }