Changeset - 803cabc0a886
[Not reviewed]
default
0 1 0
Nathan Brink (binki) - 16 years ago 2009-07-25 16:48:49
ohnobinki@ohnopublishing.net
added getopt to options code

I still must actually get the configfile_prefix variable working
1 file changed with 37 insertions and 0 deletions:
0 comments (0 inline, 0 general)
src/common/options.c
Show inline comments
 
@@ -28,6 +28,7 @@
 
#include <string.h>
 
#include <stdio.h>
 
#include <stdlib.h>
 
#include <unistd.h>
 

	
 
struct options_common_data
 
{
 
@@ -37,10 +38,46 @@ struct options_common_data
 

	
 
int options_init(int argc, char *argv[], cfg_t **mycfg, cfg_opt_t *myopts, char *myname, struct options_common **allopts)
 
{
 
  char *configfileprefix;
 
  char *configfile;
 

	
 
  size_t i;
 

	
 
  char *optstring = "hc:";
 
  char curopt;
 

	
 
  configfileprefix = NULL;
 
  while((curopt = getopt(argc, argv, optstring)) != -1)
 
    switch(curopt)
 
      {
 
      case 'h':
 
	fprintf(stderr, "libdistren common options\n\
 
\n\
 
\t-h\tShow this help.\n\
 
\t-c <path>\tBasename for configuration files. For instance, if distrencommon.conf is located at /etc/distren/distrencommon.conf, set this option /etc/distren/distren . Default value: %s\n\
 
\n", SYSCONFDIR "/distren");
 
	return 2;
 
	break;
 
      case 'c':
 
	configfileprefix = strdup(optarg);
 
	if(!configfileprefix)
 
	  /* return is unnecessary here */
 
	  fprintf(stderr, "OOM\n");
 
	break;
 
      case ':':
 
	fprintf(stderr, "Option -%c requires an argument\n", optopt);
 
	return 1;
 
      }
 
  /* restore optind for other people who use getopt */
 
  optind = 1;
 
  
 
  if(!configfileprefix)
 
    configfileprefix = strdup(SYSCONFDIR "/distren");
 
  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[]
 
   */
0 comments (0 inline, 0 general)