Question
CMDLINE #include int main (int argc, char *argv[]) { int i=0; printf( cmdline args count=%d, argc); /* First argument is executable name only */ printf(
CMDLINE
#include
int main (int argc, char *argv[]) { int i=0; printf(" cmdline args count=%d", argc);
/* First argument is executable name only */ printf(" exe name=%s", argv[0]);
for (i=1; i< argc; i++) { printf(" arg%d=%s", i, argv[i]); }
printf(" "); return 0; }
GETOPT
/* 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); }
Download sample programs from module 2 for getopt. the two ^^^^^^^
Unzip them into a folder on your computer.
Add the line #include
Make a copy of getopt.c and call it activity.c
Create a Makefile to compile the activity program.
Modify activity.c for the following usage:
Usage: activity [-r] -b bval value [value ...]
Where the -r option is optional
The -b option is required and needs an argument to go with it called bval which is a string
At least one value is included but there may be more than one. The value(s) are also strings
The program should print out all the flags and values at the end so it should be modified throughout to match the new usage.
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