lab07 for this lab
~wcbrown/courses/IC221/labs/L07/lab07.tar.gz
mmame < foo.txt". This we'll do at the file
descriptor level (though there's a handy function
called freopen you could use instead). So ask
yourself: Which file descriptor is "standard in"? How
do I change that file descriptor so that it refers to
a connection to some other file?
mmame: a better mmamma (min-max-average)
utility called mmame --- the last m
is for median, and the e is for enhanced. This
version acts like
mma when reading from stdin, i.e. reading until
end-of-file (^D, CONTROL-D)
is encountered, then printing the results:
$ mmame mmame reads from stdin 1 2 3 the user enters data ^D the user indicates end-of-input 1.000 3.000 2.000 2.000 results are printed $ back to the shell promptThe enhanced version will also read from a file named on the command line, but after the file has been processed, the program continues reading from standard input! $ mmame test_data.dat mmame reads data from test_data.dat ^D the user indicates end-of-input 55.000 32767.000 16650.824 16633.000 results are printed $ back to the shell prompt Note that results are not printed until the user enters ^D. This allows the user to continue entering data from stdin after mmame first reads data from the named file:
$ mmame test_data.dat mmame reads data from test_data.dat 1 2 3 the user continues to enter values ^D the user then indicates end-of-input 1.000 32767.000 16601.027 16617.000 results are printed $ back to the shell promptmmame behaves differently when the shell redirects stdin to read from a file, as follows:
$ mmame < test_data.dat mmame reads from stdin (redirected from test_data.dat) 55.000 32767.000 16650.824 16633.000 results are printed $ back to the shell promptNotice that ^D is not entered by the user when IO redirection is handled by the shell.
The command make mmame will compile-and-link
the program. Do that, and experiment with it a little bit.
minishell.
You are given skeleton code for minishell in a file named
minishell.c.
This is your starting point. If you give
the make command, it'll be compiled-and-linked
... although the version your given doesn't do anything.
Read the source code
and follow the numbered comments! (Note: comments are the the
red pieces of text between the /* and */ ... just in case some
of you are unfamiliar with them.)
minishell works as follows:
$ minishell Start the minishell IC221> mmame IC221 is the minishell prompt. Here we're running the mmame program 30 25 60 30 User enters data (mmame reading from stdin) ^D User indicates end-of-input with the ^D character 25.000 60.000 36.250 30.000 This is mmame output IC221> mmame terminated: back to the minishell prompt
$ minishell IC221> User enters nothing on the command line: just presses the ENTER key IC221> (returns the user to the minishell prompt)
$ minishell IC221> grok User enters a command that can't be executed IC221> minishell: grok: command not found minishell prints an error message IC221> ...and returns the user to the minishell prompt
$ minishell IC221> mmame test_data.dat ^D 55.000 32767.000 16650.824 16633.000 IC221> mmame test_data.dat 1 2 3 ^D 1.000 32767.000 16601.027 16617.000 IC221>
IC221> mmame < test_data.dat
55.000 32767.000 16650.824 16633.000
You may assume that only well formed commands are entered on your
minishell command line. A well formed command looks like this:
mmame < filename
You DO NOT have to worry about handling lines like these:
mmame Test_Data.Dat < (< In The Wrong Location)
mmame <Test_Data.Dat (Missing Space After <)
mmame < (Missing Filename After <)
< mmame Test_data.dat (mmame not first)
You also do NOT need to deal with background jobs: you can assume
the '&' character will never appear on your minishell
command line.
Of course, a shell like bash does have to deal with these
things, but for this project, we're interested in you showing
how the shell prepares for and does an exec, not in you
showing how to parse the command line.
answer.txt:
1) Do a man on fgets and answer the following questions:
a) Briefly explain what fgets does.
b) Why is it recommended that fgets is used instead of gets?
c) Will '\n' appear in the string fgets returns?
d) What does fgets return if end-of-file is reached and no bytes
have been read?
2) Explain what the function strrchr does.
3) Explain the difference between sscanf and fscanf?
4) Explain how sscanf and strstr are used together to extract tokens
(or words) from the command line string.
5) If your minishell was going to support "&" with the same
meaning as bash, what (briefly) would change in your implementation.
lab07 directory. It must include
your answers to the above questions
in answers.txt, and a file README
that includes your name, alpha and whether the
file minishell.c in your submission is a Part 1,
Part 2 or Part 3 solution. As usual, your submission should
be the last one that you got functioning properly!