/*----------------------------------------------------- Implementation of Opts for command line options Generated by aargh 1.1.3 Sun Feb 12 10:05:58 2006 -----------------------------------------------------*/ #include #include #include #include #include #include #include "opts.h" static void initialize( Opts * pOpts ) { pOpts->new_argc = 0; pOpts->new_argv = NULL; pOpts->infile_found = 0; pOpts->infile = NULL; pOpts->counter_found = 0; pOpts->counter = 0; pOpts->verbose = 0; } int get_Opts( int argc, char * argv[], Opts * pOpts ) { int rc = 0; /* return code */ unsigned long counter_value = 0; char * tail = NULL; int opt; /* Define valid option characters */ const char optstring[] = ":f:n:v"; /* Initialize members of struct */ initialize( pOpts ); /* Suppress error messages from getopt() */ opterr = 0; /* Examine command line options */ while( ( opt = getopt( argc, argv, optstring ) ) != -1 ) { switch( opt ) { case 'f' : /* Get infile */ if( pOpts->infile_found ) { fprintf( stderr, "Only one occurrence of -f option allowed\n" ); rc = 1; break; } pOpts->infile_found = 1; pOpts->infile = optarg; break; case 'n' : /* Get counter */ if( pOpts->counter_found ) { fprintf( stderr, "Only one occurrence of -n option allowed\n" ); rc = 1; break; } pOpts->counter_found = 1; /* Skip white space; check for negative */ while( isspace( (unsigned char) *optarg ) ) ++optarg; if( '-' == *optarg ) { fprintf( stderr, "Negative argument not allowed for " "-n option: \"%s\"\n", optarg ); rc = 1; break; } /* Convert to numeric value */ errno = 0; counter_value = strtoul( optarg, &tail, 10 ); if( *tail != '\0' ) { fprintf( stderr, "Invalid or non-numeric argument " "to -n option: \"%s\"\n", optarg ); rc = 1; break; } else if( errno != 0 ) { fprintf( stderr, "Too large argument " "to -n option: \"%s\"\n", optarg ); rc = 1; break; } pOpts->counter = counter_value; break; case 'v' : /* Get verbose */ pOpts->verbose = 1; break; case ':' : /* Missing argument */ fprintf( stderr, "Required argument missing on -%c option\n", (char) optopt ); rc = 1; break; case '?' : /* Invalid option */ fprintf( stderr, "Invalid option '-%c' on command line\n", (char) optopt ); rc = 1; break; default : /* Programmer error */ fprintf( stderr, "Internal error: unexpected value '-%c'" "for optopt", (char) optopt ); rc = 1; break; } /* end switch */ } /* end while */ if( optind > argc ) { /* This should never happen! */ fprintf( stderr, "Program error: found more arguments than expected\n" ); rc = 1; } else { /* Calculate new_argcv and new_argc to reflect */ /* the number of arguments consumed */ pOpts->new_argc = argc - optind + 1; pOpts->new_argv = argv + optind - 1; } return rc; }