


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for completing assignment 1 in the cs201 course during the winter 2008 semester. Students are required to write their own version of the 'tail' utility, which prints the last n lines of a file. The design and implementation details, including memory management and handling command-line arguments.
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Exercise 5-13, K&R page 118 Write your own version of the tail utility, as specified in this problem.
Simplifications:
Requirements are specified below. These will be used by the grader in grading your submission, so use them as guidelines in testing your program before submitting it. Among some of the important requirements:
The major design issue is how to store the data in memory. The example function readlines on page 109 of K&R shows how to read lines from a file, allocate memory, and store the data. To read a line from a file, you can use the library function fgets ; you don't have to duplicate the function getline from the book.
The twist for this problem is that we do not want to store the whole file, only the last N lines. Here is a suggested way to do it. You can do it differently if you want to.
Once we know how many lines we need, let's say nlines , we can dynamically allocate an array of nlines pointers to characters. As we read each line of text into a buffer, we can dynamically allocate an array for that line of text, pointed to by one of our pointers. Here is a suggested way to allocate an array of pointers to characters:
char **linearray; ...
if((linearray = calloc(nlines, sizeof(char *))) == 0){ fprintf(stderr, "Couldn't allocate array\n"); exit(1); }
Now we will read the file into a buffer line by line, and for each line we will allocate an array of bytes with one of our linearray[i] pointing to it, and then copy the line of text into that array. Since the file may have more than nlines, this will have to be a circular list. Each time we want to assign a member of linearray to a new line, we need to make sure it doesn't already point to something, and if so, free that thing in order to avoid a memory leak.
Here is an example for how you might allocate the memory for a line of text and copy it there. Assume the line has been read from the file into a buffer called buf , and we have determined that the length of the line is len.
if((linearray[curlineptr] = malloc(len+1)) == 0){ fprintf(stderr, "Couldn't allocate string\n"); exit(1); } strcpy(linearray[curlineptr], buf);
Once we've solved the major design issues, the first thing the program has to do is to decide whether there is a command line argument. If you don't know how to use argc and argv , see section 5.10 of K&R. You need to bypass the minus sign on the argument, and then you can use the library function strtol to convert the string to an integer.
Finally, once the file has been read, you must print the correct lines. You can use puts or printf to print a line. This is a question of keeping the bookkeeping straight in your
circular list of pointers, knowing where to begin and when to stop. In case the file is actually shorter than nlines , it's necessary to stop at the actual end of the file.
Please read the Deliverables shown on the homework page.
The assignment is worth a total of 50 points. Each item below indicates attributes the assignment should have, and points are deducted as shown from 50 accordingly if those attributes are lacking.