Setup
Before reading further, please connect to your lab machine and begin using the Linux
virtual machine it is running. Start Firefox there and continue reading this assignment
description. (Applications > Internet > Web Browsers > Firefox).
Create a directory within your home account in which
you will work on programming assignments. For example, you might open a shell window
(right-click and select Open Terminal) and type:
mkdir ProgrammingAssignments
at the prompt. See the mkdir manpage (man mkdir) if you have forgotten how to
use mkdir.
Then, using Firefox, connect to Drew
Netstorage (netstorage.drew.edu) and download the file:
K:\CSCI_124\Downloads\ProgrammingAssignment1.zip
Firefox will save this file to your Desktop. Move it to the directory you have just created,
using the mv command. If you've forgotten how to use mv, see the man page.
cd to your assignment directory and unzip the file by issuing the command:
unzip ProgrammingAssignment1.zip
Assignment (Due Friday, 29 Sept at 8:00am)
In this assignment, you are asked to write a very simple version of the UNIX utility grep, building
on our discussion of the code on page 69 of K&R. grep(), allows the user to search for lines within
a file matching a particular pattern. See the grep manpage by typing:
man grep
at a UNIX shell prompt. Try it out on a couple of files.
To complete this assignment you will begin by using a modified version
of the program on page 69 of K&R that is less obfuscated and therefore, easier to understand.
You can find this code in the file simpleGrep.c in the now unzipped directory of files,
ProgrammingAssignment1. Note also, that this directory contains a sub-directory called Test, which
contains a number of files you can using in testing simpleGrep.
Before continuing further, you should make sure you can compile simpleGrep.c and successfully run it.
simpleGrep works in the same way as the most basic call to grep. See the manpage for grep
in order to determine how to run it. As the comments for main() in the code indicate, Section 5.10 of K&R
provides a discussion of using command line arguments in C.
This assignment is worth 10 points and has three parts:
- (4 points) Use a pointer for a line instead of an array:
- Revise simpleGrep.c so that the variable, line in function main() is a char*. Allocate memory for line appropriately using malloc().
- Revise the function getLine() so that it uses a char* instead of an array for the variable, s.
- (4 points) Revise the rest of the code that it uses no arrays at all. Convert all use of arrays to use of pointers.
- (2 points) Revise getLine() so that it can get lines of any size:
- Change the way that getLine() works so that it no longer accepts a pre-allocated line as an argument, but instead
manages the memory for line itself. getLine() should return the line of text it has gathered instead of an integer. The
new function declaration header for getLine() should be as follows:
char* getLine(FILE* fileToSearch, int lim)
In order to enable getLine() to read lines of any size, you will need to use a chunking approach. Continue reading in characters
until you really have read in the whole line, but allocate new memory for the line in chunks of 1000 characters as needed.
Be careful to ensure that no data you've already read in is lost in allocating a larger memory block for the line.
You can do this using malloc, but it is more difficult than the most elegant solution. I strongly urge you to familiarize yourself
with the standard library (stdlib.h) before attempting this part of the assignment. You may read up on stdlib in Appendix B5.
-
Revise main() to handle the new type of return value correctly. You need change very little if you use a function in the
string library (string.h) to get the number of characters in the line returned.
Please write your code in three stages, saving each part as simpleGrep-v1.c, simpleGrep-v2.c, and simpleGrep-v3.c, respectively. There is no
need to upload your code anywhere. I will access each of your virtual machines in turn and evaluate the code there.
|