Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1) Alter the program such that only the correct output is sent to the standard output stream (stdout), while error and help messages are sent

1) Alter the program such that only the correct output is sent to the standard output stream (stdout), while error and help messages are sent to the standard error stream (stderr). (Hint: use fprintf.)

2) Implement an optional command-line switch '-fFILENAME' that sends program output to a file named FILENAME (i.e., filename specified as a command line argument).

3) Add support for matching arbitrary numbers of words, not just 5. (Hint: use malloc, and don't worry too much about memory efficiency).

4) Safeguard the program from buffer overflow attacks. (Hint: 'gets' is BAD. Use fgets instead, which specifies the maximum number of characters to be read in.)

5) Allow multiple words to be specified per line. (Hint 1: Understand 'strtok'. Hint 2: Be careful about the newline character ' ' at the end of the line.)

/* 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 */ #include #include

#include #include "smp0_tests.h" #define LENGTH(s) (sizeof(s) / sizeof(*s)) /* Structures */ typedef struct { char *word; int counter; } WordCountEntry; int process_stream(WordCountEntry entries[], int entry_count) { short line_count = 0; char buffer[30]; while (gets(buffer)) { if (*buffer == '.') break; /* Compare against each entry */ int i = 0; while (i < entry_count) { if (!strcmp(entries[i].word, buffer)) entries[i].counter++; i++; } line_count++; } return line_count; } void print_result(WordCountEntry entries[], int entry_count) { int i = 0;

printf("Result: "); while (i < entry_count) { printf("%s:%d ", entries[i].word, entries[i].counter); i++; } } void printHelp(const char *name) { printf("usage: %s [-h] ... ", name); } int main(int argc, char **argv) { const char *prog_name = *argv; 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; }

argv++; while (*argv != NULL) { if (**argv == '-') { switch ((*argv)[1]) { case 'h': printHelp(prog_name); break; default: printf("%s: Invalid option %s. Use -h for help. ", prog_name, *argv); } } else{ if (entryCount < LENGTH(entries)) { entries[entryCount].word = *argv; entries[entryCount++].counter = 0; } } argv++; } // end while if (entryCount == 0) { printf("%s: Please supply at least one word. Use -h for help. ", prog_name); return EXIT_FAILURE; } if (entryCount == 1) { printf("Looking for a single word "); } else { printf("Looking for %d words ", entryCount); } process_stream(entries, entryCount); print_result(entries, entryCount); return EXIT_SUCCESS; }

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

Advanced Database Systems For Integration Of Media And User Environments 98

Authors: Yahiko Kambayashi, Akifumi Makinouchi, Shunsuke Uemura, Katsumi Tanaka, Yoshifumi Masunaga

1st Edition

9810234368, 978-9810234362

More Books

Students also viewed these Databases questions

Question

=+28.13. Show that the normal and Cauchy laws are stable.

Answered: 1 week ago

Question

What must a creditor do to become a secured party?

Answered: 1 week ago

Question

When should the last word in a title be capitalized?

Answered: 1 week ago