Assignment 2: Description
Assignment 2: Description
Assignment 2
Instructors: Todd Nugent Kenneth Harris
Purpose
Points
0 Fix any bugs in lgsh from Assignment 1. I will give back any points deducted, provided you
make all the fixes.
I (10 points) Write a function makeargv which parses the command line.
Description
A command line consists of arguments separated by spaces or tabs. Each argument is a string of
characters containing no white space unless quotation marks are used, in which case everything
between the pair of quotation marks is considered a single argument. The shell parses the command
line into arguments and creates an argument array: an array of pointers to strings in which the
1
last pointer is a NULL pointer to mark the end of the array.
ls -l file
argv[ ]
[0] / l s ’\0’
[1] / - l ’\0’
[2] / f i l e ’\0’
[3] NULL
Your task is to write a function which parses a command line. You will write a function makeargv
which when given
sets n to 3 (the number of arguments in the argument array), and creates an argument array to
which argv points. The delimiters separating arguments are the space ( ) and the tab (\t). The
third argument is used in Extra Credit Problem 1. You can ignore it for the basic module here.
2
Definition: White space is defined to be either a space ( ) or a tab (\t). A word is a string
which does not include any white space. An argument array is an array of words. The C
type of an argument array is char **.
Arguments:
char *cmd : String to be parsed. makeargv will not modify this string.
char ***argvp: Pointer to argv, an array of strings
char *delimiter : String of delimiters, these are used to separate cmd into words. For
the basic module in Problem I, ignore this argument, and use white space to separate
cmd into words.
Return: makeargv builds an array of words separated by white space. The end of the array
is set to NULL. There are two effects on return:
– The return value is the number of arguments in the argument array, or -1 on error.
– The argument argvp points to the argument array which is produced. The memory for
this array is allocated by makeargv.
Errors: makeargv checks to determine that neither cmd nor argvp are NULL. It returns -1 if
either are NULL.
Implementation Points
• You will find the string library strtok and strtok_r in the library <string.h> valuable for
the assignment. strtok is a very dangerous function to use because it use a static variable. It
is preferable to use strtok_r. You will find an explanation of these functions in the manpages.
• You will need to handle white space at the beginning of the command line:
ls -l file
You will find the string library function strspn in the library <string.h> useful.
• I will provide a main routine which will test your function makeargv. The file is called test.c.
• Once you get makeargv working try Extra Credit Problem 1. It handles the third argument
delimiters. It is a simple modification to your code for makeargv
• I have provided a directory, makeargv with everything you need for the assignment. You
simple need to fill-in the body of your function makeargv in the file makeargv.c. You will
need to submit a typescript.
3
Part II: Implementing cat
Description
Write you own version of cat called my_cat to conform ot the POSIX specification of cat. I have
provided a link to the POSIX specification on the Assignment 2 webpage. You will not implement
the option -u. You must implement all file I/O using the system calls open, close, read, write.
You may use perror for printing error messages.
Implementation Points
• I have provided a restart library, librestart.c which restarts I/O system calls if EINTR. I
have also provided some useful operations which use these restart functions. You are welcome
to use the library. Read the functions carefully, understand how they work.
• Execute the cat command with regular files and try to get my_cat to behave just like this.
• Execute cat with no arguments. Study how it behaves, and try to get my_cat to behave just
like this. The way to generate an EOF on the terminal is <cntl>-d.
• Execute cat with one or more instances of ”-” on the command line. Study how it behaves,
and try to get my_cat to behave just like this.
Extra Credit
(1) (5 points) Re-write makeargv to use the third argument, delimiter: a string of charac-
ters which will be used to separate words. Now, words are separated by some character
in delimiter, and words consist of characters, none of which are among delimiter. For
example,
parses the string into the words "ls", "-l" and "file". And
parses the string into the words "ls -l" and "wc" This is an easy modification to your your
function makeargv. You will need to make a change to the DOC file I provided for makeargv.
You will not need to modify test.c
• showlog [n] : show the last n commands. If no argument given show all the commands.
4
• savelog [n] : save the last n commands to the logfile. If no argument given then save
all commands to logfile.
showlog should now behave like the bash shell command history. You should handle error
conditions like too many arguments: showlog 5 10. Use makeargv to parse the command
line so you correctly handle extra white space.