/* * Minimum getopt, original version was: */ /* getopt -- public domain version of standard System V routine Strictly enforces the System V Command Syntax Standard; provided by D A Gwyn of BRL for generic ANSI C implementations */ /* $Id: getopt.c,v 1.2 1992/12/07 11:12:52 nickc Exp $ */ #include "common.h" /* definition of FLASH */ #include int optind = 1; /* next argv[] index */ char *optarg; /* option parameter if any */ int getopt( /* returns letter, '?', EOF */ int argc, /* argument count from main */ char *const argv[], /* argument vector from main */ const FLASH char *optstring ) /* allowed args, e.g. "ab:c" */ { static int sp = 1; /* position within argument */ int osp; /* saved `sp' for param test */ int c; /* option letter */ const FLASH char *cp; /* -> option in `optstring' */ optarg = NULL; if ( sp == 1 ) /* fresh argument */ { if ( optind >= argc /* no more arguments */ || argv[optind][0] != '-' /* no more options */ || argv[optind][1] == '\0' /* not option; stdin */ ) return -1; } c = argv[optind][sp]; /* option letter */ osp = sp++; /* get ready for next letter */ if ( argv[optind][sp] == '\0' ) /* end of argument */ { ++optind; /* get ready for next try */ sp = 1; /* beginning of next argument */ } if ( c == ':' /* optstring syntax conflict */ || (cp = strchr_P( optstring, c )) == NULL /* not found */ ) return '?'; if ( cp[1] == ':' ) /* option takes parameter */ { if ( osp != 1 ) return '?'; if ( sp != 1 ) /* reset by end of argument */ return '?'; if ( optind >= argc ) return '?'; optarg = argv[optind]; /* make parameter available */ ++optind; /* skip over parameter */ } return c; }