Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

wgrep The second utility you will build is called wgrep , a variant of the UNIX tool grep . This tool looks through a file,

wgrep

The second utility you will build is called wgrep, a variant of the UNIX tool grep. This tool looks through a file, line by line, trying to find a user-specified search term in the line. If a line has the word within it, the line is printed out, otherwise it is not.

Here is how a user would look for the term foo in the file bar.txt:

prompt> ./wgrep foo bar.txt

this line has foo in it

so does this foolish line; do you see where?

even this line, which has barfood in it, will be printed.

Details

  • Your program wgrep is always passed a search term and zero or more files to grep through (thus, more than one is possible). It should go through each line and see if the search term is in it; if so, the line should be printed, and if not, the line should be skipped.
  • The matching is case sensitive. Thus, if searching for foo, lines with Foo will not match.
  • Lines can be arbitrarily long (that is, you may see many many characters before you encounter a newline character, ). wgrep should work as expected even with very long lines. For this, you might want to look into the getline() library call (instead of fgets()), or roll your own.
  • If wgrep is passed no command-line arguments, it should print "wgrep: searchterm [file ...]" (followed by a newline) and exit with status 1.
  • If wgrep encounters a file that it cannot open, it should print "wgrep: cannot open file" (followed by a newline) and exit with status 1.
  • In all other cases, wgrep should exit with return code 0.
  • If a search term, but no file, is specified, wgrep should work, but instead of reading from a file, wgrep should read from standard input. Doing so is easy, because the file stream stdin is already open; you can use fgets() (or similar routines) to read from it.
  • For simplicity, if passed the empty string as a search string, wgrep can either match NO lines or match ALL lines, both are acceptable.

image text in transcribed

Wcat . Check the value of argc -> if no command line arguments were passed i.e., argc == 1), then simply exit. If there are command line arguments, iterate through each file name in argv[] use fopen() to open each file name in read mode use fgets or getline to read each line from the file print each line to stdout close the file before continuing the the next filename, if there is one Matthew Majoki C W Grep . Check the value of argc . If it is 1. exit with a status of 1 If it is 2. treat the first argument as the search term, and search using stdin instead of an open FILE . If it is > 2, treat the first argument as the search tearm, and treat the rest of the arguments as search files Iterate over each file like in wcat . read each line from the file using getline . Check if the line contains the search term using strstr() If it does, print the line Close the file before continuing to the next, if there is one Mam Maj Wcat . Check the value of argc -> if no command line arguments were passed i.e., argc == 1), then simply exit. If there are command line arguments, iterate through each file name in argv[] use fopen() to open each file name in read mode use fgets or getline to read each line from the file print each line to stdout close the file before continuing the the next filename, if there is one Matthew Majoki C W Grep . Check the value of argc . If it is 1. exit with a status of 1 If it is 2. treat the first argument as the search term, and search using stdin instead of an open FILE . If it is > 2, treat the first argument as the search tearm, and treat the rest of the arguments as search files Iterate over each file like in wcat . read each line from the file using getline . Check if the line contains the search term using strstr() If it does, print the line Close the file before continuing to the next, if there is one Mam Maj

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

Hands On Database

Authors: Steve Conger

1st Edition

013610827X, 978-0136108276

More Books

Students also viewed these Databases questions

Question

Verify the statement made in the remark following Example 10.2.

Answered: 1 week ago