Changeset - 2b4b4fb95800
[Not reviewed]
default
0 3 0
Nathan Brink (binki) - 16 years ago 2009-04-18 23:08:05
ohnobinki@ohnopublishing.net
fixed leak, implemented options_free function
3 files changed with 56 insertions and 13 deletions:
0 comments (0 inline, 0 general)
src/client/distren.c
Show inline comments
 
@@ -99,18 +99,17 @@ if( i == 1 ){
 
      buf[readlen] = '\0';
 
      fprintf(stderr, "read \"%s\"\n", buf);
 
    }
 
  return 0;
 
}
 

	
 
else
 
{
 
	printf("Well now, you must have registered!\n");
 
/* put code here to ssh to zserver2 w/ execio as the user from the conf file */
 
return 0;
 
}
 

	
 

	
 
//cfg_free(cfg);
 
 options_free(options);
 
 return 0;
 
};
 

	
 

	
src/common/options.c
Show inline comments
 
@@ -22,18 +22,47 @@
 
#include <confuse.h>
 
#include <string.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 

	
 
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;
 
}
src/common/options.h
Show inline comments
 
@@ -30,9 +30,11 @@
 
/**
 
  incomplete:
 
  maybe remoteio.h should define its own struct which is injected into this struct and have its own handlers that parse its section of the confuse config file... (it'd be more modular)?
 
  Maybe we should just stick with compile-time modularity :-D (did I just suggest we make tons of macros?)
 
 */
 
struct options_common
 
{
 
  struct options_common_data *data;
 
  struct remoteio_opts *remoteio;
 
  struct client_opts *client;
 
  struct daemon_opts *daemon;
0 comments (0 inline, 0 general)