first batch of license fixes (boring)
[oweals/gnunet.git] / src / util / configuration_loader.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2006, 2007, 2008, 2009, 2013 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14 */
15
16 /**
17  * @file src/util/configuration_loader.c
18  * @brief configuration loading
19  * @author Christian Grothoff
20  */
21
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24
25 #define LOG(kind,...) GNUNET_log_from (kind, "util-configuration", __VA_ARGS__)
26
27
28 /**
29  * Load configuration (starts with defaults, then loads
30  * system-specific configuration).
31  *
32  * @param cfg configuration to update
33  * @param filename name of the configuration file, NULL to load defaults
34  * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
35  */
36 int
37 GNUNET_CONFIGURATION_load (struct GNUNET_CONFIGURATION_Handle *cfg,
38                            const char *filename)
39 {
40   char *baseconfig;
41   const char *base_config_varname;
42
43   base_config_varname = GNUNET_OS_project_data_get ()->base_config_varname;
44
45   if (NULL != (baseconfig = getenv (base_config_varname)))
46   {
47     baseconfig = GNUNET_strdup (baseconfig);
48   }
49   else
50   {
51     char *ipath;
52
53     ipath = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
54     if (NULL == ipath)
55       return GNUNET_SYSERR;
56     GNUNET_asprintf (&baseconfig, "%s%s", ipath, "config.d");
57     GNUNET_free (ipath);
58   }
59
60   if (GNUNET_SYSERR ==
61       GNUNET_CONFIGURATION_load_from (cfg,
62                                       baseconfig))
63   {
64     GNUNET_free (baseconfig);
65     return GNUNET_SYSERR;       /* no configuration at all found */
66   }
67   GNUNET_free (baseconfig);
68   if ((NULL != filename) &&
69       (GNUNET_OK != GNUNET_CONFIGURATION_parse (cfg, filename)))
70   {
71     /* specified configuration not found */
72     return GNUNET_SYSERR;
73   }
74   if (((GNUNET_YES !=
75         GNUNET_CONFIGURATION_have_value (cfg, "PATHS", "DEFAULTCONFIG"))) &&
76       (filename != NULL))
77     GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG",
78                                            filename);
79   return GNUNET_OK;
80 }
81
82 /* end of configuration_loader.c */