Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

head and tail As seen above, the Linux/ UNIX/POSIX head and tail programs allow the beginning resp. the end of a text file to be

head and tail As seen above, the Linux/UNIX/POSIX head and tail programs allow the beginning resp. the end of a text file to be extracted. In this section of the homework assignment, you are asked to re-program the Linux head and tail commands. Your programs must be written in C and must be based solely on POSIX file-system handling system calls (level-2 calls). This means, you can use open, close, read and write but calls to fopen, fread or fprintf are prohibited. You may use malloc, calloc and free; however, see the remark below. Your head and tail programs must handle the following use-cases and associated options: When run without any option nor filename argument, the head command must copy the first 10 lines of the input it receives on standard input (stdin) to standard output (stdout) and disregard any other lines seen on input. Similarly, the tail command must read the input on standard input and output only the last 10 lines of that input to standard output. Note that head can perform its task without any buffering (in memory), while tail needs to maintain a memory buffer of the current last 10 lines it just read in. When the input contains less than 10 lines, the input is completely copied from standard input to standard output. When run with a filename argument, the head and tail commands do not read from standard input but from the file designated by that filename argument. If they cannot open the file in read-mode, they display an appropriate error message on standard error and exit. Your head and tail commands just handle one filename argument, whereas the Linux/UNIX/POSIX programs handle several filename options. 2 When run with the -n [num] option, the head and tail commands behave as specified above but replace the number of 10 lines by the appropriate amount specified by [num]. If that amount is zero, they do nothing. If the amount is negative, your head and tail programs do nothing either; the Linux/UNIX/POSIX commands execute a special behavior in this case. When the -n option is given incorrectly or incompletely, e.g. by specifying -nose instead of -n or by specifying -n without specifying a non-negative amount in the next argument, the head and tail commands display an appropriate error message on standard error and exit. The amount [num] is supposed to be less than or equal to 2 64 1; if it is larger, the behavior of the programs is unspecified. You are supposed to perform the string-to-integer conversion with code written on your own; not using atoi or strtol, which would be level-3 operations. The filename and -n options can be combined and used in any reasonable order, i.e. as -n [num] [filename] or as [filename] -n [num]. If no error condition arises, the head and tail commands return the success condition code, i.e. zero. If an error occurs, they return a non-zero failure condition code. Note that an error might occur for any system call, i.e. for any call to read, write, malloc etc. Before exiting, an error message must be displayed on standard error in these cases, too. These are examples of well-formed calls to head and tail: cat nanpa | ./head cat nanpa | ./head -n 42 ./head -n 42 nanpa ./head nanpa ./head nanpa -n 42 echo -e Hello world! Have a nice day. | ./head cat nanpa | ./tail cat nanpa | ./tail -n 42 ./tail -n 42 nanpa ./tail nanpa ./tail nanpa -n 42 echo -e Hello world! Have a nice day. | ./tail Your head and tail commands must not access any memory out of their addressing space and must not leak any memory. This means any memory allocated on the heap with malloc or calloc must be returned using free. All accesses to arrays must use correct indices; you may want to double-check on this. All files opened with open must be closed with close. These memory and file handling specifications must be met even though the operating system would perform these tasks automatically on process termination if they are forgotten. Your instructor will check for these points, using, amongst others, strace and valgrind. You may want to utilize these two tools for testing, too. You must write C code (not C++). You are supposed to do some software engineering in order to end up with well structured source code (no spaghetti!). Your source code must contain some comments at the most decisive places, i.e. before functions to document their meaning and at places where the functioning of the code is not obvious to someone reading your code for the first time. As the head and tail commands exist on the system you will be developing on, be sure to execute your head and tail implementations when testing! For this section, you have to submit:

The source file head.c which can be compiled to head using gcc -Wall -O3 -o head head.c.

The source file tail.c which can be compiled to tail using gcc -Wall -O3 -o tail tail.c. A section in your report explaining the software engineering decisions you made, the problems you encountered and the testing you performed.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

25.0 m C B A 52.0 m 65.0 m

Answered: 1 week ago