Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 #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 at the top of the getopt.c file with the other includes.

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Decisions Based On Data Analytics For Business Excellence

Authors: Bastian Weber

1st Edition

9358681683, 978-9358681680

More Books

Students also viewed these Databases questions

Question

=+. Does the source have any moral leverage with this audience?

Answered: 1 week ago