/* * getopt.c * * Replacement for a Unix style getopt function * * Quick, clean, and portable to funky systems that don't have getopt() * for whatever reason. * */#include<stdio.h>#include<string.h>#ifndef MSDOS_16BIT#define cdecl#endifintoptind=1;intoptopt=0;constchar*optarg=NULL;intcdeclgetopt(intargc,char*argv[],constchar*options){staticintpos=1;constchar*p;if(optind>=argc||argv[optind][0]!='-'||argv[optind][1]==0)returnEOF;optopt=argv[optind][pos++];optarg=NULL;if(argv[optind][pos]==0){pos=1;optind++;}p=strchr(options,optopt);if(optopt==':'||p==NULL){fputs("illegal option -- ",stdout);gotoerror;}elseif(p[1]==':'){if(optind>=argc){fputs("option requires an argument -- ",stdout);gotoerror;}else{optarg=argv[optind];if(pos!=1)optarg+=pos;pos=1;optind++;}}returnoptopt;error:fputc(optopt,stdout);fputc('\n',stdout);return'?';}/* getopt */