Changeset - ca0e56faefb0
[Not reviewed]
default
0 2 0
Nathan Brink (binki) - 16 years ago 2009-07-26 16:25:10
ohnobinki@ohnopublishing.net
options_init() now respects DISTREN_CONFIG_PREFIX
2 files changed with 24 insertions and 10 deletions:
0 comments (0 inline, 0 general)
src/common/options.c
Show inline comments
 
@@ -36,6 +36,9 @@ struct options_common_data
 
};
 

	
 

	
 
/**
 
   Not reentrant because of call to getenv()
 
 */
 
int options_init(int argc, char *argv[], cfg_t **mycfg, cfg_opt_t *myopts, char *myname, struct options_common **allopts)
 
{
 
  char *configfileprefix;
 
@@ -71,27 +74,38 @@ int options_init(int argc, char *argv[],
 
  /* restore optind for other people who use getopt */
 
  optind = 1;
 
  
 
  if(!configfileprefix
 
     && (configfileprefix = getenv("DISTREN_CONFIG_PREFIX")) )
 
    {
 
      configfileprefix = strdup(configfileprefix);
 
      if(!configfileprefix)
 
	{
 
	  fprintf(stderr, "OOM\n");
 
	  return 1;
 
	}
 
    }
 
  /* 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[]
 
  */
 
  if(!configfileprefix)
 
    configfileprefix = strdup(SYSCONFDIR "/distren");
 
      configfileprefix = strdup(SYSCONFDIR "/distren");
 
  if(!configfileprefix)
 
    {
 
    fprintf(stderr, "OOM\n");
 
    return 1;
 
      fprintf(stderr, "OOM\n");
 
      return 1;
 
    }
 
  /* 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[]
 
   */
 

	
 
  i = strlen(SYSCONFDIR "/distren.conf") + strlen(myname) + 1;
 
  
 
  i = strlen(configfileprefix) + strlen(myname) + strlen(".conf") + 1;
 
  configfile = malloc(i);
 
  if(!configfile)
 
    {
 
      perror("malloc");
 
      return 1;
 
    }
 
  strncpy(configfile, SYSCONFDIR "/distren", strlen(SYSCONFDIR "/distren") + 1);
 
  strncpy(configfile, configfileprefix, strlen(configfileprefix) + 1);
 
  strcat(configfile, myname);
 
  strcat(configfile, ".conf");
 
  fprintf(stderr, "using configuration file: %s\n", configfile);
 

	
 
  *allopts = malloc(sizeof(struct options_common));
 
  if(!*allopts)
src/common/options.h
Show inline comments
 
@@ -45,7 +45,7 @@ struct options_common
 

	
 
  Options should be kept alive during the program's life.
 

	
 
  The environment variable DISTREN_CONFIG, the built-in default config file location, or arguments passed on the commandline are used to determine the prefix of the config file name. Order of precedence: The command line is used first, then the environment variable, and then the default if no others sources specify the prefix.
 
  The environment variable DISTREN_CONFIG_PREFIX, the built-in default config file location, or arguments passed on the commandline are used to determine the prefix of the config file name. Order of precedence: The command line is used first, then the environment variable, and then the default if no others sources specify the prefix.
 

	
 
  A calling function must call options_init() before processing its own arguments so that the libdistrencommon-specific help listing may be shown. The caller must also ignore libdistrencommon specific options. To avoid complication, those arguments are "c:h". Obviously, "h" should be handled sspecially ;-) (i.e., the caller should not ignore the "h" switch... it should output information about arguments it supports). Of course, calling functions could just rely on the 
 

	
0 comments (0 inline, 0 general)