Question
Language is C. Use sample program from called getopt.c . Make a copy of getopt.c and call it gactivity.c (from samples to activities directory) Create
Language is C.
- Use sample program from called getopt.c .
- Make a copy of getopt.c and call it gactivity.c (from samples to activities directory)
- Create a Makefile to compile the activity program.
- Modify gactivity.c for the following usage:
- Usage: gactivity [-ab] -c cname -d number value1 value2 [value ...]
- Where the-a and -b flag is optional and does not take a argument
- The -c flag is required and needs an argument to go with it. This argument is a string.
- The -d flag is required and needs an argument to go with it. This argument is an integer.
- In addition, at least two parameters are included but there may be more than two. No more than 3. This can be strings or integers.
- The program should print out all the options entered, arguments and parameters.
- Program should print error if invalid options are used or missing option arguments or parameters.
input to test:
gactivity -ab -c car -d 4 jose smith
output:
Options: -a -b -c -d
Arguments: car 4
Parameters: jose smith
Other examples:
gactivity -b -d 8 juanes perez
gactivity -b -c marco -d 17 suerez pegasu lipuz
getopt.c ----------------------------------------
/* example of command line parsing via getopt usage: getopt [-dmp] -f fname [-s sname] name [name ...]
Paul Krzyzanowski */
#include
int debug = 0;
int main(int argc, char **argv) { extern char *optarg; extern int optind; int c, err = 0; int mflag=0, pflag=0, fflag = 0, sflag=0; char *sname = "default_sname", *fname; static char usage[] = "usage: %s [-dmp] -f fname [-s sname] name [name ...] ";
while ((c = getopt(argc, argv, "df:mps:")) != -1) switch (c) { case 'd': debug = 1; break; case 'm': mflag = 1; break; case 'p': pflag = 1; break; case 'f': fflag = 1; fname = optarg; break; case 's': sflag = 1; sname = optarg; break; case '?': err = 1; break; } if (fflag == 0) { /* -f was mandatory */ fprintf(stderr, "%s: missing -f option ", argv[0]); fprintf(stderr, usage, argv[0]); exit(1); } else if ((optind+1) > argc) { /* need at least one argument (change +1 to +2 for two, etc. as needeed) */
printf("optind = %d, argc=%d ", optind, argc); fprintf(stderr, "%s: missing name ", argv[0]); fprintf(stderr, usage, argv[0]); exit(1); } else if (err) { fprintf(stderr, usage, argv[0]); exit(1); } /* see what we have */ printf("debug = %d ", debug); printf("pflag = %d ", pflag); printf("mflag = %d ", mflag); printf("fname = \"%s\" ", fname); printf("sname = \"%s\" ", sname); if (optind < argc) /* these are the arguments after the command-line options */ for (; optind < argc; optind++) printf("argument: \"%s\" ", argv[optind]); else { printf("no arguments left to process "); } exit(0); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started