Question: activity [-r] -b bval value Modify the activity program from last week with the usage shown. The value for bval is required to be an
activity [-r] -b bval value
Modify the activity program from last week with the usage shown. The value for bval is required to be an integer as is the value at the end. In addition, there should only be one value.
Since everything on the command line is read in as a string, these now need to be converted to numbers.
You can use the function atoi() to do that conversion.
You can do the conversion in the switch or you can do it at the end of the program.
The number coming in as bval and the value should be added together to get a total.
That should be the only value printed out at the end. Total = x should be the only output from the program upon success. Remove all the other print statements after testing is complete.
Compile the program with the -g option to load the symbol table.
Run the program using gdb and use watch on the result so the program stops when the addition is performed. Then let it continue.
makefile
activity: activity.c gcc -o activity activity.c
clean: rm -f activity
#include
#include
#include
int debug = 0;
int main(int argc, char **argv)
{
extern char *optarg;
extern int optind;
int c, err = 0;
int rflag=0, bflag=0;
char *bval = "default_bval", *value;
static char usage[] = "usage: %s [-r] -b bval value [value ...] ";
while ((c = getopt(argc, argv, "rb:")) != -1)
switch (c) {
case 'r':
rflag = 1;
break;
case 'b':
bflag = 1;
bval = optarg;
break;
case '?':
err = 1;
break;
}
if (bflag == 0) { /* -b was mandatory */
fprintf(stderr, "%s: missing -b option ", argv[0]);
fprintf(stderr, usage, argv[0]);
exit(1);
} else if ((optind+1) > argc) {
/* need at least one value */
fprintf(stderr, "%s: missing value ", argv[0]);
fprintf(stderr, usage, argv[0]);
exit(1);
} else if (err) {
fprintf(stderr, usage, argv[0]);
exit(1);
}
/* see what we have */
printf("rflag = %d ", rflag);
printf("bval = \"%s\" ", bval);
if (optind < argc) /* these are the values after the command-line options */
for (; optind < argc; optind++)
printf("value: \"%s\" ", argv[optind]);
else {
printf("no values left to process ");
}
exit(0);
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
