Back to index

php5  5.3.10
Functions | Variables
getopt.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int ap_getopt (int argc, char *const *argv, const char *optstr)

Variables

char * ap_optarg
int ap_optind

Function Documentation

int ap_getopt ( int  argc,
char *const *  argv,
const char *  optstr 
)

Definition at line 45 of file getopt.c.

{
    static int optchr = 0;
    static int dash = 0; /* have already seen the - */

    char *cp;

    if (ap_optind >= argc)
        return(EOF);
    if (!dash && (argv[ap_optind][0] !=  '-'))
        return(EOF);
    if (!dash && (argv[ap_optind][0] ==  '-') && !argv[ap_optind][1])
    {
        /*
         * use to specify stdin. Need to let pgm process this and
         * the following args
         */
        return(EOF);
    }
    if ((argv[ap_optind][0] == '-') && (argv[ap_optind][1] == '-'))
    {
        /* -- indicates end of args */
        ap_optind++;
        return(EOF);
    }
    if (!dash)
    {
        assert((argv[ap_optind][0] == '-') && argv[ap_optind][1]);
        dash = 1;
        optchr = 1;
    }

    /* Check if the guy tries to do a -: kind of flag */
    assert(dash);
    if (argv[ap_optind][optchr] == ':')
    {
        dash = 0;
        ap_optind++;
        return(ap_optiserr(argc, argv, ap_optind-1, optstr, optchr, OPTERRCOLON));
    }
    if (!(cp = strchr(optstr, argv[ap_optind][optchr])))
    {
        int errind = ap_optind;
        int errchr = optchr;

        if (!argv[ap_optind][optchr+1])
        {
            dash = 0;
            ap_optind++;
        }
        else
            optchr++;
        return(ap_optiserr(argc, argv, errind, optstr, errchr, OPTERRNF));
    }
    if (cp[1] == ':')
    {
        /* Check for cases where the value of the argument 
           is in the form -<arg> <val> or in the form -<arg><val> */
        dash = 0;
        if(!argv[ap_optind][2]) {
            ap_optind++;
            if (ap_optind == argc)
                return(ap_optiserr(argc, argv, ap_optind-1, optstr, optchr, OPTERRARG));
            ap_optarg = argv[ap_optind++];
        }
        else
        {
            ap_optarg = &argv[ap_optind][2];
            ap_optind++;
        }
        return(*cp);
    }
    else
    {
        if (!argv[ap_optind][optchr+1])
        {
            dash = 0;
            ap_optind++;
        }
        else
            optchr++;
        return(*cp);
    }
    assert(0);
    return(0);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

char* ap_optarg

Definition at line 13 of file getopt.c.

Definition at line 14 of file getopt.c.