#include #include #include #include char **flag[NFLAG]; char cmdline[NCMDLINE+1]; char *cmdname; char *flagset[]; char *flagset[]={""}; static char *flagarg=""; static void reverse(char **, char **); static int scanflag(int, char *); static int reason; #define RESET 1 #define ARGCNT 2 #define FLAGSYN 3 #define BADFLAG 4 static int badflag; char *getflagsargv[NGETFLAGSARGV+2]; /* original argv stored here for people who need it */ int getflags(int argc, char *argv[], char *flags){ register char *s, *t; register i, j, c, count; flagarg=flags; if(cmdname==0){ cmdname=argv[0]; for(i=0;i!=argc && i!=NGETFLAGSARGV;i++) getflagsargv[i]=argv[i]; if(argc>NGETFLAGSARGV) getflagsargv[i++]="..."; getflagsargv[i]=0; } s=cmdline; for(i=0;i!=argc;i++){ for(t=argv[i];*t;) if(s!=&cmdline[NCMDLINE]) *s++=*t++; if(i!=argc-1 && s!=&cmdline[NCMDLINE]) *s++=' '; } *s='\0'; i=1; while(i!=argc && argv[i][0]=='-'){ s=argv[i]+1; if(*s=='\0'){ /* if argument is "-", stop scanning and delete it */ for(j=i+1;j<=argc;j++) argv[j-1]=argv[j]; return argc-1; } while(*s){ c=*s++; count=scanflag(c, flags); if(count==-1) return -1; if(flag[c]){ reason=RESET; badflag=c; return -1; } if(count==0){ flag[c]=flagset; if(*s=='\0'){ for(j=i+1;j<=argc;j++) argv[j-1]=argv[j]; --argc; } } else{ if(*s=='\0'){ for(j=i+1;j<=argc;j++) argv[j-1]=argv[j]; --argc; s=argv[i]; } if(argc-i