Assignment 2, due Aug. 31

Part of the homework for 22C:112, Fall 2012
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

On every assignment, write your name legibly as it appears on your University ID and on the class list! Assignments are due at the start of class on the day indicated (usually Friday). Exceptions will be by advance arrangement unless there is what lawyers call "an act of God" (something outside your control). Homework must be turned in on paper, either in class or in the teaching assistant's mailbox. Never push late work under someone's door!

  1. A problem: Write a TCSH shell script called fib that outputs the first n Fibonacci numbers. So, ./fib 5 should output 0 1 1 2 3 (on separate lines, if that is easier). (1 point)

    Note: This is not a machine problem. You need not run your program on a real computer, although that is an excellent way to check your work. You are, however, responsible for making the code readable enough that we can check your work.

  2. A problem: Rewrite the parseargv() routine in mush.c so that it uses the strtok routine. This will considerably shorten the code. (1 point)

    Note: The note above applies here too!

  3. Background: The getcommand() routine from mush.c could be rewritten as follows:
    void getcommand() {
            putchar('>');
            gets( command );
    }
    
    This was not done because it requires understanding more routines in the standard library and it teaches less about C. Both the original and this replaced version have a bug: They let the user type more than 100 characters, and if the user does so, the consequences are unpredictable. The following code is almost, but not quite, equivalent:
    void getcommand() {
            putchar('>');
            fgets( command, 100, stdin );
    }
    

    a) How is it not equivalent? To answer this, read the man pages for gets and fgets, and figure out what changes. Or try it and see what the consequences are. (0.5 points)

    b) Fix the original version of getcommand() from mush.c so that it does not overflow the command array. The fix can be done by changing as little as one line of code. You may turn in just the change (as in, replace this with that) or you may turn in the repaired text of getcommand(). (0.5 points)