/*
                           I C G E T O P T . C
    ported for non-msdos pwp 93 07 16

    % 1 name
\functoc {ic\_getopt}
    % 2 declaration
{int \fname (\params\ )}
    % 3 arguments
{
    {int *}{p_argc}{pointer to argument counter}
    {char **}{argv}{pointer to argument strings}
}
    % 4 return value
{The character value of the next option or -1 if no more options were found}
    % 5 functions used
{}
    % 6 see also
{ic\_getoptval, setoptchars, ic\_getoptindex}
    % 7 source file
{icgetopt.c}
    % 8 description
{The function scans the strings in the \Var{argv} array for the next
occurrence of an option character, and returns the character immediately
following it. If no (more) option characters were found -1 is returned. The
value 0 is returned if the separator character is not followed by another
character as in, e.g.,
\begin{center}
    {\tt program -}
\end{center}
The function will reduce the value of \Var{*p_argc}, and will remove the option
from the \Var{argv} array.

The switchcharacters may be defined by function \Function{setoptchar}, and
contains initially the characters {\tt `-'} and {\tt `/'}.
}
*/
#ifndef MSDOS

#include <stdio.h>
#include <string.h>
#include "icrss.h"

char
    *near icce_optchar = "-",
    *near icce_optval;

int
    near icce_next_option = 0;

int ic_getopt(int *p_argc, char **argv)
{
    register int
        argc,
        index;
    char
        **org_argv;

    org_argv = argv;                        /* save original address */
    argv += icce_next_option;               /* point to next option string */
    argc = *p_argc;                         /* local argc counter */

    for
    (                                       /* walk along the options */
        index = icce_next_option;
            index < argc;                   /* as long as there are args */
                index++, argv++             /* do next after code */
    )
    {
        if (strchr(icce_optchar, **argv))  /* option found ? */
        {
            icce_next_option = index;       /* prepare for next call */
            icce_optval = *argv + 1;        /* optval points to option string*/
            while (++index <= argc)         /* shift the argv's + extra NULL */
                org_argv[index - 1] = org_argv[index];
            (*p_argc)--;                    /* reduce external count */
            return (*icce_optval);          /* return the option. */
        }
    }
    return (-1);                            /* no options */
}
#endif
