Question
All programming done in xv6. Please submit source code in file head.c and provide screenshots. Write a program that prints the first 10 lines of
All programming done in xv6. Please submit source code in file head.c and provide screenshots.
Write a program that prints the first 10 lines of its input. If a filename is provided on the command line (i.e., head FILE) then head should open it, read and print the first 10 lines, and then close it. If no filename is provided, head should read from standard input.
You should also be able to invoke it without a file, and have it read from standard input. For example, you can use a pipe to direct the output of another xv6 command into head :
The above command searches for all instances of the word the in the file README , and then prints the first 10 matching lines.
Hints:
1. Many aspects of this are similar to the wc program: both can read from standard input if no arguments are passed or read from a file if one is given on the command line. Reading its code will help you if you get stuck.
The traditional UNIX head utility can print out a configurable number of lines from the start of a file. Implement this behavior in your version of head . The number of lines to be printed should be specified via a command line argument as head -NUM FILE, for example head -3 README to print the first 3 lines of the file README. The expected output of that command is:
If the number of lines is not given (i.e., if the first argument does not start with - ), the number of lines to be printed should default to 10 as in the previous part.
Hints:
-
You can convert a string to an integer with the atoi function.
-
You may want to use pointer arithmetic (discussed in class in Lecture 2) to get a string
suitable for passing to atoi.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started