Question: I need help with the below program. I made the changes needed to run the program for the below behavior. it doesn't run as expected

I need help with the below program. I made the changes needed to run the program for the below behavior. it doesn't run as expected plus the there are 7 tests from which only one pass I am not sure what the issue is. Please help. I didn't two included two files testrunner.h testrunner. c because the question is being too long if you need it please let me know I will provide you

/*

About this program:

- This program counts words.

- The specific words that will be counted are passed in as command-line

arguments.

- The program reads words (one word per line) from standard input until EOF or

an input line starting with a dot '.'

- The program prints out a summary of the number of times each word has

appeared.

- Various command-line options alter the behavior of the program.

E.g., count the number of times 'cat', 'nap' or 'dog' appears.

> ./main cat nap dog

Given input:

cat

.

Expected output:

Looking for 3 words

Result:

cat:1

nap:0

dog:0

*/

///////////////////////////////////////// MAIN.C ///////////////////////////////////////////////

#include

#include

#include

#include "smp0_tests.h"

/* Structures */

typedef struct {

char *word;

int counter;

} WordCountEntry;

//definition of the function process_stream()

int process_stream(WordCountEntry entries[], int entry_count)

{

//define the variables

short line_count = 0;

char buffer[30];

while (fgets(buffer, sizeof(buffer), stdin))

{

//check if the word has '.'

if (*buffer == '.')

break;

//find the length of the word

size_t len = strlen(buffer);

//check for the new line

if (buffer[len - 1] == ' ')

// assign 0

buffer[--len] = 0;

/* Compare against each entry */

if (!strcmp(entries[line_count].word, buffer))

//increment the count

entries[line_count].counter++;

if (++line_count == entry_count)

break;

}

//return the line_count

return line_count;

}

//definition of the function print_result()

//it uses fprintf to print the result

void print_result(WordCountEntry entries[], int entry_count)

{

fprintf(stdout, " Result: ");

for (int i = 0; i < entry_count; i++) {

fprintf("%s:%d ", entries[i].word, entries[i].counter);

}

}

//definition of the function printHelp()

//it uses fprintf to print the Help

void printHelp(const char *name)

{

fprintf(stderr, "usage: %s [-h] [-f FILENAME] ... ", name);

}

//definition of the function main()

int main(int argc, char **argv)

{

const char *prog_name = *argv;

//Add support for matching arbitrary number of words, not just 5.

//WordCountEntry entries[argc];

WordCountEntry entries[5];

int entryCount = 0;

/* Entry point for the testrunner program */

if (argc > 1 && !strcmp(argv[1], "-test")) {

run_smp0_tests(argc - 1, argv + 1);

return EXIT_SUCCESS;

}

for (int i = 1; i < argc; i++)

{

if (*argv[i] == '-')

{

switch (argv[i][1])

{

// h to print help message.

// printHelp()

case 'h':

printHelp(prog_name);

break;

case 'f':

// f to open file

// freopen()

freopen((*argv)[2], "w", stdout);

break;

default:

//error and help messages are sent to the standard error stream (stderr).

fprintf(stderr, "%s: Invalid option %s. Use -h for help. ", prog_name, *argv);

}

}

else

{

entries[entryCount].word = argv[i];

entries[entryCount++].counter = 0;

}

}

if (!entryCount)

{

//error and help messages are sent to the standard error stream (stderr).

fprintf(stderr, "%s: Please supply at least one word. Use -h for help. ", prog_name);

return EXIT_FAILURE;

}

if (entryCount == 1) {

fprintf(stdout, "Looking for a single word ");

}

else {

fprintf(stdout, "Looking for %d words ", entryCount);

}

process_stream(entries, entryCount);

print_result(entries, entryCount);

return EXIT_SUCCESS;

}

_____________________

( smp0.tests.c )

/*************** YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE ***************/

#define _GNU_SOURCE

#include

#undef _GNU_SOURCE

#include

#include

#include

#include "testrunner.h"

#include "smp0_tests.h"

#define quit_if(cond) do {if (cond) exit(EXIT_FAILURE);} while(0)

/* test of -h switch behavior (B3) */

int test_help_switch(int argc, char **argv)

{

char *args[] = {"./main", "-h", NULL};

FILE *out, *err, *tmp;

char buffer[100];

freopen("/dev/null", "r", stdin);

freopen("smp0.out", "w", stdout);

freopen("smp0.err", "w", stderr);

quit_if(main(2, args) != EXIT_FAILURE);

fclose(stdout);

fclose(stderr);

out = fopen("smp0.out", "r");

err = fopen("smp0.err", "r");

if (fgets(buffer, 100, out) != NULL && !strncmp(buffer, "usage:", 6)) {

tmp = out;

}

else {

quit_if(fgets(buffer, 100, err) == NULL);

quit_if(strncmp(buffer, "usage:", 6));

tmp = err;

}

if (fgets(buffer, 100, tmp) != NULL) {

quit_if(!strcmp(buffer, "./main: Invalid option -h. Use -h for help. "));

}

fclose(out);

fclose(err);

return EXIT_SUCCESS;

}

/* test of basic functionality (B4, B5) */

int test_basic_functionality(int argc, char **argv)

{

char *args[] = {"./main", "cat", "dog", "nap", NULL};

char *result[] = {"Looking for 3 words ",

"Result: ",

"cat:1 ",

"dog:0 ",

"nap:0 "};

FILE *out;

int i;

char buffer[100];

out = fopen("smp0.in", "w");

fprintf(out, "cat ");

fprintf(out, ". ");

fclose(out);

freopen("smp0.in", "r", stdin);

freopen("smp0.out", "w", stdout);

quit_if(main(4, args) != EXIT_SUCCESS);

fclose(stdin);

fclose(stdout);

out = fopen("smp0.out", "r");

for (i = 0; i < 5; i++) {

quit_if(fgets(buffer, 100, out) == NULL);

quit_if(strcmp(buffer, result[i]));

}

fclose(out);

return EXIT_SUCCESS;

}

/* test of stderr output support (C1) */

int test_stderr_output(int argc, char **argv)

{

char *args[] = {"./main", "-wrong", NULL};

char *result[] = {"./main: Invalid option -wrong. Use -h for help. ",

"./main: Please supply at least one word. Use -h for help. "};

FILE *err;

int i;

char buffer[100];

freopen("/dev/null", "r", stdin);

freopen("/dev/null", "w", stdout);

freopen("smp0.err", "w", stderr);

quit_if(main(2, args) != EXIT_FAILURE);

fclose(stderr);

err = fopen("smp0.err", "r");

for (i = 0; i < 2; i++) {

quit_if(fgets(buffer, 100, err) == NULL);

quit_if(strcmp(buffer, result[i]));

}

fclose(err);

return EXIT_SUCCESS;

}

/* test of -fFILENAME switch behavior (C2) */

int test_file_output(int argc, char **argv)

{

char *args[] = {"./main", "-fsmp0.out", "cat", "dog", "nap", NULL};

char *result[] = {"Looking for 3 words ",

"Result: ",

"cat:1 ",

"dog:0 ",

"nap:0 "};

FILE *out;

int i;

char buffer[100];

out = fopen("smp0.in", "w");

fprintf(out, "cat ");

fprintf(out, ". ");

fclose(out);

freopen("/dev/null", "w", stdout);

freopen("smp0.in", "r", stdin);

quit_if(main(5, args) != EXIT_SUCCESS);

fflush(0);

quit_if((out = fopen("smp0.out", "r")) == NULL);

for (i = 0; i < 5; i++) {

quit_if(fgets(buffer, 100, out) == NULL);

quit_if(strcmp(buffer, result[i]));

}

fclose(out);

return EXIT_SUCCESS;

}

/* test of supporting an arbitrary number of words (C3) */

int test_malloc(int argc, char **argv)

{

char *args[] = {"./main", "cat", "dog", "nap", "c", "a", "t", NULL};

char *result[] = {"Looking for 6 words ",

"Result: ",

"cat:1 ",

"dog:0 ",

"nap:0 ",

"c:0 ", "a:0 ", "t:0 "};

FILE *out;

int i;

char buffer[100];

quit_if(system("grep malloc main.c > /dev/null"));

out = fopen("smp0.in", "w");

fprintf(out, "cat ");

fprintf(out, ". ");

fclose(out);

freopen("smp0.in", "r", stdin);

freopen("smp0.out", "w", stdout);

quit_if(main(7, args) != EXIT_SUCCESS);

fclose(stdin);

fclose(stdout);

out = fopen("smp0.out", "r");

for (i = 0; i < 8; i++) {

quit_if(fgets(buffer, 100, out) == NULL);

quit_if(strcmp(buffer, result[i]));

}

fclose(out);

return EXIT_SUCCESS;

}

/* test of fgets usage (C4) */

int test_fgets(int argc, char **argv)

{

quit_if(system("grep fgets main.c > /dev/null"));

return EXIT_SUCCESS;

}

/* test of multiple words per line support (C5) */

int test_strtok(int argc, char **argv)

{

char *args[] = {"./main", "cat", "dog", "nap", NULL};

char *result[] = {"Looking for 3 words ",

"Result: ",

"cat:1 ",

"dog:2 ",

"nap:1 "};

FILE *out;

int i;

char buffer[100];

out = fopen("smp0.in", "w");

fprintf(out, "cat ");

fprintf(out, "dog dog nap ");

fprintf(out, ". ");

fclose(out);

freopen("smp0.in", "r", stdin);

freopen("smp0.out", "w", stdout);

quit_if(main(4, args) != EXIT_SUCCESS);

fclose(stdin);

fclose(stdout);

out = fopen("smp0.out", "r");

for (i = 0; i < 5; i++) {

quit_if(fgets(buffer, 100, out) == NULL);

quit_if(strcmp(buffer, result[i]));

}

fclose(out);

return EXIT_SUCCESS;

}

void delete_temp_files()

{

unlink("smp0.in");

unlink("smp0.out");

unlink("smp0.err");

}

/*

* Main entry point for SMP0 test harness

*/

int run_smp0_tests(int argc, char **argv)

{

/* Tests can be invoked by matching their name or their suite name

or 'all'*/

testentry_t tests[] = {

{"help_switch", "suite1", test_help_switch},

{"basic_functionality", "suite1",

test_basic_functionality},

{"stderr_output", "suite1", test_stderr_output},

{"file_output", "suite1", test_file_output},

{"malloc", "suite1", test_malloc},

{"fgets", "suite1", test_fgets},

{"strtok", "suite1", test_strtok}};

atexit(delete_temp_files);

return run_testrunner(argc, argv, tests, sizeof(tests) / sizeof (testentry_t));

}

____________________________

smp0.tests.h

/*************** YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE ***************/ int run_smp0_tests(int argc, char **argv); int main(int argc, char **argv);

--------------------------------------------------------------------------------------

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!