CS 3733 Operating Systems, Fall 1999 Assignment 1


Warning: This is not for the current semester.


Due Friday, February 5


The purpose of this assignment is to make sure that you are comfortable writing programs that use pointers and manipulate strings. Put the programs for this assignment in a clean directory and create a makefile for compiling and linting these programs. Use the cover sheet in /usr/local/courses/cs3733/spring99/assign01/cover1.ps and turn in a copy of the source, the makefile, the lint output, and any other output requested. If your browser can display postscript or pdf files click one of the following for the cover sheet: PS or PDF.

Assignment 1 has 2 parts.


Part 1:
Write a program, showargs.c that displays its command line arguments. This program will first output a line in the form:
This program was written by ...
and will display your name. It will then print out the number of command line arguments as reported in argc, followed by the argv array with each command line argument on a separate line.

For example, if I wrote the program and called it with:

   showargs abc "def ghi" j k l
the output would look like:
   This program was writen by Steven Robbins
   Number of command line arguments: 6
   showargs
   abc
   def ghi
   j
   k
   l

Test the program with this input and turn in the results.

Part 2:
Write a program, exefile.c, which takes one command line argument, the name of a file. The program will first identify the programmer as in Part 1. It will then check for the correct number of command line argumnts. It will print an error message and exit if this is incorrect. If the file cannot be read an appropriate error message should be given and the program should exit. The format of the file is as follows: The first line contains the name of a command to execute. The next line contains just an integer, giving the number of following lines of options for the command. This is followed by that number of lines, each containing an option. Each line after that contains the name of a file to perform the command on.

For example, if the file myfile contained:

   cc
   1
   -c
   showargs.c
   exefile.c
and the program was called with
   exefile myfile
then the following two command would be executed:
   cc -c showargs.c
   cc -c exefile.c

The commands are executed by having the program fork and exec for each of the lines in the file after the options. Use execvp.

Test the program with a file containing

   showargs
   3
   a
   bbb
   cc
   def
   g
   hij
   x

and turn in a copy of the output produced.

Note that when you read lines from the file you should take care that your buffer is not excessively large and that you handle the case of a line being larger than the buffer. You can consider this an error, but do not under any circumstances read more bytes than will fit in the buffer. One way to do this is to use fgets. Be aware that there is a bug in the buffered read system calls which can cause the parent to not correctly detect end of file and to read the file more than once. You can avoid this problem by turning off the buffering. You can turn off buffering for a file pointer, f, by executing setbuf(f,NULL) after the file is open but before any reading is done from that file.

Be prepared to demonstrate your programs.