Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
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 [you@machine] ./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 If my-grep encounters a file that it cannot open, it should print "my-grep: cannot open file" (followed by a newline) and In all other cases, my-grep should exit with return code 0. newline) and exit with status 1 exit with status 1. . 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started