Hi like to write a program that can take arguments like %program -t <type> <path> <path> <name>

where -t follow by type is the file type I like to look for in a find program, and path's is where I want to look, followed by the name of the file I like to search for....

like

%program -t f (file) ~testfolder1 ~testfolder2 help

The problem is I don't know how to get getopt to read -t and then take next char as an argument to look for S_ISREG(m) for example..

Would be great with a lite help, will try to update the thread later on with code if someone have same problem.. :)

#include <stdio.h>
#include <unistd.h>

int main (int argc, char *argv[]){

	int c;
	char *path, filetype;
	extern char *optarg;
	extern int optind, optopt, opterr;

	while ((c = getopt(argc, argv, "t")) != -1){

		switch(c){

			case 't':
				filetype = ????;  // What the *** should be here? :D
				printf("Type you are looking for is: 
                                               %c\n", filetype);
				printf("option: %c\n",c);
				break;

			case '?':
				fprintf(stderr, "Unrecognised option:
                                              -%c\n",optopt);
		}
	}
	// This is just for listing witch arguments there is.. 
	for(i=1;i<argc;i++){
		printf("argv: %s\n",argv[i]);
	}
	
	return 0;
	
}

Nothing serious. I don't like a rigid and unclear (in my opinion) getopt loop (and option string too ;)). It's a matter of personal preferences only.
Probably the getopt is the best solution in C, but in C++ it looks like a steam-engine in Tokkaido Line train... But it's the other story...

filetype = ????;

So if the flag matches, you can get the argument by the global variable optarg. That should hold pointer to the argument which you are looking for!

filetype = optarg;

ssharish

This article has been dead for over six months. Start a new discussion instead.