/* gt: getopt() test program */
#include <stdio.h>
#include <unistd.h>

int myoptind;

int		/* TRUE: more_opts; FALSE: last_one */
argNEXT (
  int	argc,	/* ro	count of arguments */
  char	**argv,	/* ro	vector of arg ptrs */
  char	*ops,	/* ro	option string */
  int	*opt,	/* wo	current option or 0: lone_arg */
  char	**arg	/* wo	ptr to option_arg or lone_arg */
)
{
  int	c;

  c = getopt(argc, argv, ops);
  if(c == -1 && myoptind < argc) {
    /* unbound arg */
    if( myoptind == 0 ) myoptind = optind;
    *opt = 0, *arg = argv[myoptind++];
    optind++;
  }
  else {
    /* option */
    *opt = c, *arg = (c == '?' ? NULL : optarg);
  }
  return( optind < argc && myoptind < argc);
} /* argNEXT */

int		/* process exit status */
main
(
  int	argc,	/* ro	number of cmdline args */
  char*	argv[]	/* ro	vector of cmdline arg strings */
)
{
  int	more, opt, acount = 0;
  char	*arg;

  if(argc > 1) {
    opterr = 0, optind = 1, myoptind = 0;
    do {
      more = argNEXT(argc, argv, "ab:c", &opt, &arg);
      if(opt == 0 && arg != NULL) {
	printf( "unbound more=%d myoptind=%d optind=%d acount=%d arg=%s\n", more, myoptind, optind, acount, arg );
	acount++;
      }
      else {
	printf( "option more=%d myoptind=%d optind=%d opt=%c arg=%s\n", more, myoptind, optind, opt, arg );
      }
    } while(more);
  }
} /* main */
