/* getopt.c * last update: 9, August, 1995. * * getopt for non-Unix * * Copyright (C) 1995 All rights reserved. * written by Gyudong Kim(chilly@iclab.snu.ac.kr) * set tabstop=5 * * No warranty on the functions of these routines are provided. * This is written simply for mimicking Unix getopt(3). * */ #include #define DIRDEL '\\' int opterr=1; int optind=1; char *optarg; static char *cmdname = NULL; static void opt_error(opt) char opt; { if (!opterr) return; (void)fprintf(stderr, "%s: illegal option -- %c\n",cmdname,opt); } static void opt_needs_arg(opt) char opt; { if (!opterr) return; (void)fprintf(stderr, "%s: option requires an argument -- %c\n",cmdname,opt); } int getopt(argc,argv,options) int argc; char **argv,*options; { static char *curptr=NULL; int curoption = '?'; char *opts; if (optind>argc) return(-1); if (argv[optind][0]=='-') { if (curptr==NULL) curptr=(argv[optind])+1; } else return(-1); if (!curptr[0]) return('-'); /* '-' is a valid option */ if (curptr[0]=='-') { /* '--' should terminate option proc. */ optind++; return(-1); } if (cmdname==NULL) cmdname=basename(argv[0]); if (curptr[0]==':') { /* ':' can not be a valid option. why ? */ opt_error(curptr[0]); return(curoption); } for(opts=options;opts[0];opts++) { if (curptr[0]!=opts[0]) continue; curoption=curptr[0]; if (opts[1]==':') { optarg=curptr+1; if (!optarg[0]) { optind++; optarg=argv[optind]; } if (optarg==NULL) { opt_needs_arg(curptr[0]); return('?'); } optind++; } else { optarg=""; curptr++; if (!curptr[0]) { curptr=(char *)NULL; optind++; } } return(curoption); } opt_error(curptr[0]); return(curoption); } /* getopt.c */