Combine GetOpt And Variadic Function In C

Been a while since my last post. Had a laptop die with my last project and still working to get it back. In the meantime I started another.

In my main function I define some arguments with getopt:

int main ( int argc, char **argv) {
  int option_index = 0;
  char *fname = "./commands";

  if(argc == 1){ 
	  //pass: error defined in file_open function.
	  file_open(fname);
  } 
  
while(optind < argc) {
  if(( option_index = getopt(argc, argv, "f:h:u:p:k:o:w:e")) != -1){	
   switch(option_index){
     case 'f':
       file_open(optarg);	     
       break;	     
     case 'h':
       host_open(optarg);
       break;
     case 'u':
       user_open(optarg);
       break;
     case 'p':
       port_open(optarg);
       break;
     case 'w':
       pass_open(optarg);
       break;
     default:
      usage();
      break;
     } 
   }
   else {
      file_open(fname);
      optind++;
   }
}
  
  return 0;
} 

The above is missing some optional arguments for brevity. Since there were optional arguments, I found I would need to use a variadic function with something like this:

void args(char *howmany, ...){
  
  va_list ap;
  char *host, *user, *pass, *port;
  va_start(ap, howmany);
  
  while(*howmany) { 
     switch(*(howmany++)){
	     case 'h':
		host = va_arg(ap, char *);		
		printf("%s\n", host);
		break;
	     case 'u':
		user = va_arg(ap, char *);
		printf("%s\n", user);
		break;
	     case 'w':
		pass = va_arg(ap, char *);
		printf("%s\n", pass);
		break;
	     case 'p':
	        port = va_arg(ap, char *);
	        printf("%s\n", port);
	        break;
	     }	
  }

  va_end(ap);  

}

Looking at these I could see they were very similar with their switch statements. Instead of calling a function from main and then using that function to call the variadic function, it seems there should be a way to combine them. I'm just having trouble getting the logic/syntax for this and not finding much with my google searches.

Any suggestions much appreciated.

Your question is not clear at all. In first code Input arguments are always defined in the variable argc. In the second example, the parameters for the 64-bit system are enumerated. Since the transition from 32 to 64-bit broke the compatibility of the transfer in a function an indefinite number of parameters. Some parameters were passed to the function through the registers and not through the stack. Therefore, macros were invented va_list, va_start, va_end. These are all different things. Choose what you need.

1 Like

main and getopt aren't variadic, they're array-based.

So one solution could be converting the variadic arguments into an array.

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

void argfunction(char *howmany, ...);

int main(void) {
        argfunction("abc", "atext", "btext", "ctext");
        return(0);
}

void argfunction(char *howmany, ...)
{
        int n=0;
        // Never forget the NULLs, they convert mysterious "why" errors
        //  into "oh, I forgot to assign X" errors
        char *host=NULL, *user=NULL, *pass=NULL, *port=NULL;
        int argc=0;
        char *argv[16];
        va_list ap; // IIRC ap should always be the very last variable declared in a function.
        va_start(ap, howmany);

        argv[0]=""; // just tradition, argv[0] in main() is not an argument

        // checks howmany[0], assigns argv[1].
        // checks howmany[1], assigs argv[2], etc.
        while((argc<(16-2))&&howmany[argc++]) argv[argc]=va_arg(ap, char *);

        argv[argc]=NULL;         // Also traditional that argv[argc] be NULL.

        va_end(ap);

        printf("Howmany is: %s\n", howmany);
        printf("Arguments are:\n");
        for(n=1; n<argc; n++)
                printf("\t%d\t%s\n", n, argv[n]);

        optind=1; // This is global, so reset it

        // You now have argv, argc, and optind just like main to feed getopt with.
}
1 Like

I guess I didn't explain things well. I assumed using a variadic function would be the only way to get optional and required parameters. I tried to implement the code from Corona688 in my own, but could not get it to work. I did more searching and came back to example 2 on this page I had been to before:

CS 417 Documents

From that example I was able to see the flags in the switch case that could make arguments required.

$ ./basic -dp -s "more stuff" blah
./basic: missing -f option
usage: ./basic [-dmp] -f fname [-s sname] name [name ...]

Thank you for the response.

1 Like