Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Writing Unix Utilities in C (not C++ or C#) my-grep The second utility you will build is called my-grep , a variant of the UNIX

Writing Unix Utilities in C (not C++ or C#)

my-grep

The second utility you will build is called my-grep, 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:

$ ./my-grep 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 my-grep 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, ). my-grep 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 my-grep is passed no command-line arguments, it should print "my-grep: searchterm [file ...]" (followed by a newline) and exit with status 1.
  • If my-grep encounters a file that it cannot open, it should print "my-grep: cannot open file" (followed by a newline) and exit with status 1.
  • In all other cases, my-grep should exit with return code 0.
  • If a search term, but no file, is specified, my-grep should work, but instead of reading from a file, my-grep 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, my-grep can either match NO lines or match ALL lines, both are acceptable.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

Please make it fast 4 6 1 .

Answered: 1 week ago