diff --git a/src/common/options.c b/src/common/options.c --- a/src/common/options.c +++ b/src/common/options.c @@ -22,18 +22,47 @@ #include #include #include +#include + +struct options_common_data +{ + cfg_t *cfg; +}; + int options_init(int argc, char *argv[], cfg_t **mycfg, cfg_opt_t *myopts, char *myname, struct options_common **allopts) { char *configfile; - + + /* For those of you who don't know, the following is an example of concatenation of constant strings by the C compiler. Now, that doesn't mean you can do run-time string concatenation ;-) + strdup is because someday configfile will be customizable via argv[] + */ configfile = strdup(SYSCONFDIR "/distren.conf"); if(!configfile) { perror("strdup"); return 1; } - + + *allopts = malloc(sizeof(struct options_common)); + if(!*allopts) + { + perror("malloc"); + free(configfile); + return 1; + } + memset(*allopts, '\0', sizeof(struct options_common)); + + (*allopts)->data = malloc(sizeof(struct options_common_data)); + if(!(*allopts)->data) + { + perror("malloc"); + free(configfile); + free(*allopts); + return 1; + } + memset((*allopts)->data, '\0', sizeof(struct options_common_data)); + /* Conf File Parser */ cfg_opt_t common_opts[] = { @@ -61,28 +90,41 @@ int options_init(int argc, char *argv[], 0), CFG_END() }; - cfg_t *cfg; + - cfg = cfg_init(opts, 0); - switch(cfg_parse(cfg, configfile)) + (*allopts)->data->cfg = cfg_init(opts, 0); + switch(cfg_parse((*allopts)->data->cfg, configfile)) { default: fprintf(stderr, "cfg_parse returned an unknown error code\n"); case CFG_FILE_ERROR: - cfg_error(cfg, "Couldn't open file ``%s''\n", configfile); + cfg_error((*allopts)->data->cfg, "Couldn't open file ``%s''\n", configfile); /* no break; on purpose */ case CFG_PARSE_ERROR: + + free((*allopts)->data); + free(*allopts); + free(configfile); + return 1; case CFG_SUCCESS: break; } - /* End Conf File Parser */ - - printf("Client Username: %s\n", cfg_getstr(cfg_getsec(cfg, "client"), "username")); - cfg_free(cfg); + free(configfile); + return 0; } + +int options_free(struct options_common *opts) +{ + cfg_free(opts->data->cfg); + + free(opts->data); + free(opts); + + return 0; +}