0% found this document useful (0 votes)
60 views5 pages

Assignment 2: Description

This document describes Assignment 2 for the course CSPP 51086: Unix Systems Programming. The assignment has two parts: 1) Write a function makeargv() that parses a command line into an argument array. It takes a command string, a pointer to an argument array, and optional delimiter characters. 2) Write a program my_cat that implements the cat command, reading and writing files using open(), close(), read(), write(). Error handling and optional arguments like "-" for stdin are also required. Extra credit is offered for enhancing makeargv() to use delimiter characters and adding arguments to existing programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views5 pages

Assignment 2: Description

This document describes Assignment 2 for the course CSPP 51086: Unix Systems Programming. The assignment has two parts: 1) Write a function makeargv() that parses a command line into an argument array. It takes a command string, a pointer to an argument array, and optional delimiter characters. 2) Write a program my_cat that implements the cat command, reading and writing files using open(), close(), read(), write(). Error handling and optional arguments like "-" for stdin are also required. Extra credit is offered for enhancing makeargv() to use delimiter characters and adding arguments to existing programs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CSPP 51086: Unix Systems Programming command line parsing; cat

Assignment 2
Instructors: Todd Nugent Kenneth Harris

Purpose

The purpose of this assignment is two-fold

1. Work with strings and pointers.

2. Use the system calls open, close, read, write

Points

This assignment is 35 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.

II (25 points) Implement my_cat, a version of Unix cat

There are several extra credit points at the end.

Part I : Command Line parsing

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.

Here is the argument array for the command line

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

char **argv; // type of an array of strings


int n;
n = makeargv("ls -l file", &argv, " \t");

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.

Specification for makeargv

Purpose: makeargv–parse string into words


Prototype:
int makeargv(const char *cmd, char ***argvp, const char * delimiter)

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.

• Create a directory for my\_cat following the Course Policy guidelines.

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,

• makeargv("ls -l file", argv, " \t")

parses the string into the words "ls", "-l" and "file". And

• makeargv("ls -l | wc", argv, "|")

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

(2) (10points) Add arguments to showlog and savelog:

• 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.

You might also like