home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************/
- /* Module name: getopt.c */
- /* Date: 8 Oct 87 */
- /* Environment: Turbo C 1.0 */
- /* Author: Synchronistic Software (Was it JUST a coincidence?) */
- /* Notice: Public Domain -- May not be used to gain profit */
- /***************************************************************/
- static char *__ATH__ =
- "@(#)getopt() v2.0 (31Dec87): Public Domain (P) 1987 Synchronistic Software";
-
- #include <stdio.h>
- #include <string.h>
- #include <dir.h>
-
- char *optarg;
- int opterr = 1; /* Default is to emit error messages */
- int optind = 1; /* Start on the first argument after command name */
- int optopt; /* The real argument -- for AT&T compatibility */
- /* This is the option the user selected, even if */
- /* the option is invalid */
- char SW = '-'; /* For semi-compatibility with Borland */
-
- #define PRINT_ERROR(message) if (opterr) { \
- if (argv[0] != '\0') \
- fprintf( stderr, "%s: ", argv[0] ); \
- else \
- fprintf( stderr, "getopt: " ); \
- fprintf ( stderr, "'%c' %s\n", ch, message ); \
- }
-
- int getopt( int argc, char **argv, char *opts )
- {
- register int ch;
- register char *pt;
- static char *next = NULL; /* Saves the next char */
-
- if (next == NULL) {
- next = &argv[optind][1];
-
- /* If there are no more options or */
- /* if this is not an option return EOF */
- if (optind >= argc || next[-1] != SW || next[0] == '\0')
- return EOF;
-
- /* If this is the special end-of-options indicator, */
- /* return EOF */
- if (next[0] == SW && next[1] == '\0') {
- ++optind; /* Skip this special indicator */
- return EOF;
- }
- }
-
- /* Get the option letter, and see if it's valid */
- /* Set next to point to the character AFTER ch */
- /* ':' is ALWAYS an invalid character, by definition of getopt() */
- if ((pt = strchr( opts, optopt = ch = *next++ )) == NULL || ch == ':') {
- PRINT_ERROR( "is an illegal option" );
- return '?';
- }
-
- /* Should there be an option argument? */
- if (*++pt == ':') {
- /* Is the argument part of the option? */
- if (*next != '\0') {
- optarg = next, next = NULL;
- ++optind;
- return ch;
- }
- /* If there are no more arguments, return EOF */
- if (++optind >= argc) {
- PRINT_ERROR( "requires an argument" );
- return '?';
- }
- /* Return the next argument in the command line */
- optarg = argv[optind++], next = NULL;
- return ch;
- }
-
- /* There is no option argument */
- optarg = NULL;
- if (*next == '\0')
- ++optind, next = NULL;
- return ch;
- }
-