50 points
Due: Wednesday, May 1, 2018, at 11:59pm
The purpose of this assignment is to gain some experience with C
programming by implementing a utility program that is similar
to grep
, but without the ability to process regular
expressions (i.e., a lot like a simple version
of fgrep
). In particular, in this assignment, you will:
gdb
, andThis assignment does not include any particularly complicated logic or algorithms, but it will require you to organize your code well and make effective use of the C language and libraries. You will also have to explore the details of the C string and file I/O libraries to discover how to do various operations that should already be familiar from your programming experience in other languages, but which are different in C. It is meant as an orientation to the Unix/Linux C programming environment. You should do this assignment by yourself.
The material that we have learned in lecture is not enough to complete this assignment. It is expected that you will investigate the resources and libraries mentioned in this document to learn about how to use them.
Implement in C a Unix utility program gasp
. The command
$ ./gasp [options] STRING FILE...should read the listed files (
FILE...
) and copy each line from the input
to stdout
if it contains STRING
anywhere in
the input line. Each output line should be preceded by the name of
the file that contains it. The argument STRING
may be any
sequence of characters (as expanded, of course, by the shell depending
on how the argument is quoted).
Your output should look like the following (for a hypothetical pair of files):
$ ./gasp aardvark file1.txt file2.txt file1.txt:and the next day the aardvark ate a file1.txt: The aardvark had a great day after that, file2.txt:not the most peaceful, but aardvarks
There are two available options, which may appear in any order if both are present:
-i
Ignore case when searching for lines that
contain STRING
. If the -i
option is
used, the strings "this
",
"This
", "THIS
", and
"thiS
" all match; if -i
is not
used, they are all considered different.-n
Number lines in output. Each line copied
to stdout
should include the line number in the file
where it was found in addition to the file name. The lines in each
file are numbered from 1.Your program does not need to be able to handle combinations of
option letters written as a single multi-character option like
-in
or -ni
. But it does need to be able to
handle any combination of either or both (or neither) option when they
appear separately on the command line preceding the STRING
argument.
You may assume that no option
occurs more than once (you do not need to check for this).
(This is basically the same output produced by fgrep
or by grep
if the STRING
argument is
treated as literal data and not as a regular expression. You should
pretty much match the output format of grep
or fgrep
, although your program's output does not need
to be byte-for-byte identical. One difference, though, is that a
file name should be printed on every output line, even if only one
file is specified on the gasp
command line.)
Besides the general specification given above, your program should meet the following requirements to receive full credit.
\0
byte). This number
should be specified with an appropriate #define
preprocessor command so it can be changed easily. Your program is
allowed to produce incorrect results or fail if presented with
input data files containing lines longer than this limit.\0
). This length should also be specified
by an appropriate #define
.strncpy
in <string.h>
can be used to copy \0
-terminated strings; you should
not be writing loops to copy such strings one character at a
time.getopt
function in the Linux
library that provides simplified handling of command line
options. For this assignment, only, you may not
use this function. You should implement the processing of
command line options yourself, of course using the string library
functions when these are helpful.fgets
and strncpy
instead of routines
like gets
and strcpy
. The safe
functions allow specification of maximum buffer or array lengths
and will not overrun adjacent memory if used properly.-i
option, two characters are considered to
be equal ignoring case if they are the same when translated by
the tolower(c)
function (or, alternatively,
toupper(c)
) in <ctype.h>
.stderr
and continue processing any remaining files on the command
line.gasp.c
.
Since this program is relatively short, all of the functions should be
in this single file. You should arrange your code so that related
functions are grouped together in a logical order in the file.klaatu
or the current CSE
Linux VM using gcc
with the -Wall
and -std=c11
options. Since this assignment should
not need to use any unusual or system-dependent code you can
probably develop and test your code on any recent Linux
system or other system that supports a standard C
compiler. However, we will test your submissions using the CSE
systems, so you should verify your program there before the
submission deadline.stderr
and
exit with an exit code of EXIT_FAILURE
(defined in
<stdlib.h>
-- see the description of the
exit()
function).EXIT_SUCCESS
(see <stdlib.h>
).
This is normally done by returning this value as the int
result of the main
function.As with any program you write, your code should be readable and understandable to anyone who knows C. In particular, for full credit your code must observe the following requirements.
main
function
that does everything. However it should not contain tiny functions
that only contain isolated statements or code fragments instead of
dividing the program into coherent pieces. If you wish, you may
include all of your functions in a single C source file, since the
total size of this program will be fairly small. Be sure to
include appropriate function prototype declarations near the
beginning of the file so the actual function definitions can
appear in whatever order is most appropriate for presenting the
code in the remainder of the file in a logical sequence and so that
related functions are grouped together.void
functions that perform an action without
returning a value. Variables of local significance like loop
counters, indices, or pointers should be given simple names
like i
, k
, n
,
or p
, and often do not require further comments.-i
or -n
options are selected or not.STRING
argument to lower- or
upper-case, translate it (or a copy of it) once; don't do this
repeatedly every time you read a new line from an input file.
Don't make unnecessary copies of
large data structures; use pointers. (Copies of int
s,
pointers, and similar things are cheap; copies of arrays and large
structs are expensive.) Don't read the input by calling a library
function to read each individual character. Read the input a line
at a time (it costs just about the same to call an I/O function to
read an entire line into a char array as it does to read a single
character). But don't overdo it. Your code should be simple and
clear, not complex containing lots of micro-optimizations that
don't matter.
You should use the clint.py style checker
(right-click to download, and chmod +x
to make it
executable if needed) to review your code. While this checker may
flag a few things that you wish to leave as-is, most of the things it
catches, including whitespace errors in the code, should be fixed. We
will run this style checker on your code to check for any issues that
should have been fixed. Use the discussion board or office hours if
you have questions about particular clint warnings.
Hint: All reasonable programming text editors have commands or
settings to use spaces instead of tabs at the beginning of lines,
which is required by the style checker and is much more robust than
having tabs in the code. For example, if you are a emacs user, you
can add the following line to the .emacs
file in your
home directory to ensure that emacs translates leading tabs to
spaces:(setq-default indent-tabs-mode nil)
.
stdout
before
you add the code to search for the STRING
argument
and selectively print lines containing it. Be able to search for
exact matches before adding the -i
option. Add
the -n
option separately when you're not trying to do
something else.printf
can also be your friend to print values
while executing and testing the code. <stdio.h>
,
<string.h>
, <ctype.h>
and <stdlib.h>
libraries. Use the reference
link on the course home page to look up details about functions
and their parameters; use a good book like The C Programming
Language for background and perspective about how they are
intended to be used.
-Wall
option and (if you can) the
runtime assert
function (in assert.h
) to
catch coding bugs and to check for things that "must happen" or
"can't happen" during execution. Don't waste time manually
searching for errors that the compiler or run-time tests could have
caught for you.-i
option is to
translate both the STRING
argument and each input
line to lowercase, then search for the
translated STRING
in the translated input
line. (Translating a string to lower-case sure sounds like a
well-defined operation that should be in a separate function!)
However, if the string is found, the original line from the input
file should be printed, not the translated one.Learning how to use a debugger effectively can be a big help in
getting your programs to work (although it is no substitute for
thinking and careful programming). To encourage you to gain a basic
familiarity with gdb
, you are required to do the
following:
-g
option, to include debugging information in the executable
file.script
program to capture the following
console session in a text file named debug.script
.gdb
with your executable program as an
argument.main
,
and the other at the point in your program right after
the fopen
function call that opens the input
files.gdb
run
command, providing a search string and at least one file name as
arguments.main
, use the appropriate gdb
command
to display the contents of the variable containing the search
string (the first argument to the program following any options
that are present). When you've done that, continue execution of
the program.gdb
commands to display (i) a backtrace showing the functions active
at the time the breakpoint was reached, (ii) source code showing
the line containing the breakpoint and a couple of surrounding
lines, (iii) the name of the file that was supplied to
the fopen
function (this string should be in a
variable somewhere), and (iv) the pointer value returned
by fopen
(presumably just a hex address, although it
might be NULL
if the file can't be opened).gdb
and exit from the script
program's shell. Save the debug.script
output file
from script
to turn in later.You should use gdb
's basic command-line interface for
this part of the assignment, even if you use the -tui
option for your routine debugging. The full-screen -tui
interface generates a great deal of extra output in the script file,
which makes it almost impossible to read.
A small amount of extra credit will be awarded for adding the following extensions to an already complete, working assignment. No extra credit will be awarded if the basic program is not fully implemented and substantially bug-free.
If you do any extra credit parts, you should turn in both your
original program without the extra credit and your extended program. The
extra credit version should be in a separate file named
gasp-extra.c
. Your README
file (see turn-in
instructions below) should contain a brief description of your extensions.
stdin
. This should be fairly easy to add
if your code is organized as a well-designed collection of
functions.-w
to search for words separated by
whitespace. If -w
is specified,
the STRING
on the gasp
command line
should only be found if it is surrounded by whitespace (blanks,
tabs, newlines, etc.) in the input file(s) and not as part of some
other string. For example, the STRING foo
would
match foo
but not food
. A
character c
in the input should be treated as
whitespace if the library function call isspace(c)
returns true. If you implement this option, the program should
find the word if it appears at the beginning or end of a line, as
well as in the middle. You may also use an additional global
variable to record the state of this option if you wish.-n
option to count line numbers incorrectly.
However you decide to implement this, long input lines should not
cause your program to fail or crash.STRING
parameter anywhere in the line.
If you read arbitrarily long input lines in chunks that contain only
parts of an input line, be sure you can correctly handle situations
where the STRING
value appears in a line but spans two
parts of the line instead of falling entirely inside of one chunk. For
efficiency reasons you should continue to read the input in large
chunks, not a character at a time.Use the Canvas assignment page to turn in this assignment. You should submit:
gasp.c
),debug.script
file with the script
(console) output from the Debugging exercise above,gasp-extra.c
source file containing the extra-credit
version of the program, andREADME
that briefly describes the
extra credit parts that you added to your program, or contains a
note that you did not implement any of the extra credit parts.README
files. Turn in separate files; do not turn in a tar
,
zip
, or other kind of archive file.