CSCI 415/515: Fall 2023
Systems Programming
Project 1: sgrep

Due: Thu, Sept 14 @7:00am


In this project, you will implement a simple version of grep, which we will call sgrep.

Name

sgrep - print lines of a file that match a string

Synopsis

sgrep [-c] [-h] [-n] [-q] [-B NUM] STR FILE

Options

-c, --count

Suppress normal output; instead print a count of matching lines for the input file. With the -v option, count non-matching lines.

-h, --help

Print a usage statement to stdout and exit with status 0.

-n, --line-number

Prefix each line of output with the 1-based line number of the file, followed immediately by a colon (e.g., 1:foo).

-q, --quiet

Quiet; do not write anything to stdout. Exit immediately with zero status if any match was found. If a match is not found, exit with a non-zero status.

-B NUM, --before-context NUM

Print NUM lines of leading context before matching lines.

Exit Status

The exit status is 0 if at least one line was matched, and non-zero (conventionally 1 or 2) if a line was not found or an error occurred.

Bonus 1

Implement the following feature:

-i, --ignore-case

Perform a case-insenstive match for STR.

Remember to include a blank file called bonus1 in your project submission so that I know to grade this feature.

Bonus 2

Implement the following feature:

-v, --invert-match

Invert the sense of matching, to print non-matching lines.

Remember to include a blank file called bonus2 in your project submission so that I know to grade this feature.

Submitting

Submit your project as a zip file via gradescope. Your project must include a Makefile that builds an executable called sgrep. Please refer to the instructions for submitting an assignment for details on how to login to gradescope and properly zip your project.

Rubric

Input Files: alice.txt dorothy.txt

Helper Source Files: You can download a zip file with the starter code (sgrep.c, mu.c, mu.h, list.h, Makefile).

Invalid Input


1.1 Wrong number of arguments (7 pts)


        ./sgrep apple
        

Prints a a one line messge to stderr.

1.2 No such file (7 pts)


          ./sgrep apple nonexistent.txt
          

Prints a message to stderr that includes the text No such file (case-insenstive).

1.3 No such file exit status (7 pts)


          ./sgrep apple nonexistent.txt
          

Exits with a nonzero status.

-h, --help option


2.1 Usage statement (7 pts)


          ./sgrep -h
          

Prints a usage statement to stdout. The statement should start with either Usage or usage. You can decide the rest of the content. Conventionally, this option either prints the synopsis or a more verbose statement that also includes a description of the options.

2.2 Exit status (7 pts)


          ./sgrep -h
          

Exits with a status of 0.

-c, --count option


3.1 No matches (3 pts)


          ./sgrep -c pie alice.txt
          (out) 0
          

Has the above output (stdout).

3.2 No matches exit status (3 pts)


          ./sgrep -c pie alice.txt
          

Exits with nonzero status.

3.3 Match (3 pts)


          ./sgrep -c cake alice.txt
          (out) 3
          

Has the above output (stdout).

3.4 Match exit status (3 pts)


          ./sgrep -c cake alice.txt
          

Exits with status of 0.

-q, --quiet option


4.1 No matches, no output (3 pts)


            ./sgrep -q rainbox alice.txt
        

Prints nothing.

4.2 No matches exit status (3 pts)


          ./sgrep -q rainbox alice.txt
          

Exits with nonzero status.

4.3 Match, no output (3 pts)


          ./sgrep -q golden alice.txt
          

Prints nothing.

4.4 Match exit status (3 pts)


          ./sgrep -q golden alice.txt
          

Exits with status of 0.

-n, --line-number option


5.1 Match (13 pts)

output

          ./sgrep -n key alice.txt
          (out) 107:glass; there was nothing on it except a tiny golden key, and Alice's
          (out) 109:but, alas! either the locks were too large, or the key was too small,
          (out) 113:little golden key in the lock, and to her great delight it fitted!
          (out) 128:back to the table, half hoping she might find another key on it, or at
          (out) 149:flavour of cherry-tart, custard, pine-apple, roast turkey, toffee, and
          (out) 167:door, she found she had forgotten the little golden key, and when she
          (out) 188:Alice, "and if it makes me grow larger, I can reach the key; and if it
          

Has the above output (stdout).

-B, --before-context option


6.1 -B1 (7 pts)

output

          ./sgrep -B1 garden alice.txt
          (out) much larger than a rat-hole: she knelt down and looked along the
          (out) passage into the loveliest garden you ever saw. How she longed to get
          (out) brightened up at the thought that she was now the right size for going
          (out) through the little door into that lovely garden. First, however, she
          (out) After a while, finding that nothing more happened, she decided on going
          (out) into the garden at once; but, alas for poor Alice! when she got to the
          (out) makes me grow smaller, I can creep under the door; so either way I'll
          (out) get into the garden, and I don't care which happens!"
          

Has the above output (stdout).

6.2 -B3 (7 pts)

output

          ./sgrep -B3 cupboards alice.txt
          (out) was going to happen next. First, she tried to look down and make out
          (out) what she was coming to, but it was too dark to see anything; then she
          (out) looked at the sides of the well, and noticed that they were filled with
          (out) cupboards and book-shelves; here and there she saw maps and pictures
          (out) passed; it was labelled "ORANGE MARMALADE", but to her great
          (out) disappointment it was empty: she did not like to drop the jar for fear
          (out) of killing somebody underneath, so managed to put it into one of the
          (out) cupboards as she fell past it.
          

Has the above output (stdout).

Multiple options


7.1 -n -B2 (7 pts)


          ./sgrep -n -B2 mouse alice.txt
          (out) 78:tea-time. Dinah my dear! I wish you were down here with me! There are
          (out) 79:no mice in the air, I'm afraid, but you might catch a bat, and that's
          (out) 80:very like a mouse, you know. But do cats eat bats, I wonder?" And here
          

Has the above output (stdout).

7.2 -c -B2 (7 pts)


           ./sgrep -c -B2 mouse alice.txt
           (out) 1
          

Has the above output (stdout).

Bonus 1: -i, --ignore-case option


100.1 Ignore case (10 pts)

output

          ./sgrep -i rabbit alice.txt
          (out) Down the Rabbit-Hole
          (out) picking the daisies, when suddenly a White Rabbit with pink eyes ran
          (out) so _very_ much out of the way to hear the Rabbit say to itself, "Oh
          (out) time it all seemed quite natural); but when the Rabbit actually _took a
          (out) had never before seen a rabbit with either a waistcoat-pocket, or a
          (out) large rabbit-hole under the hedge.
          (out) The rabbit-hole went straight on like a tunnel for some way, and then
          (out) long passage, and the White Rabbit was still in sight, hurrying down
          (out) turned the corner, but the Rabbit was no longer to be seen: she found
          

Has the above output (stdout).

Bonus 2: -v, --invert-match option


200.1 Invert match (10 pts)

output

          ./sgrep -v the dorothy.txt
          by wagon many miles. There were four walls, a floor and a roof,
          which made one room; and this room contained a rusty looking cooking
          whirlwinds arose, mighty enough to crush any building in its path. It 
           
          

Has the above output (stdout).