Homework 2: Shell Programming and Beginning C

Due October 16, 1997

In the first part of this assignment, you will extend your classified ads CGI scripts from the previous assignment, to make it possible for someone on the Web to post new advertisements to the system. In the second part of this assigmennt, you will begin to learn C programming by entering, compiling, running, and then modifying the classic ``Hello world'' program.

What to Submit

You will submit your work electronically, as described here.

Part I: Shell Programming

In Homework 1, you created some CGI scripts that would permit a person on the Web to view classified ads on the Web server, by first selecting a category, and then clicking on a link to one of the available ads in that category. If your script was done properly, it should automatically find out what the currently available categories and advertisements are by checking to see what subdirectories of the ``ads'' directory currently exist and what files are in those subdirectories. For this homework, you will extend the functionality of the scripts you created for Homework 1, so that a user on the Web can post new advertisements to the system.

When the user selects the ``POST'' option from the main classifieds menu, a form that looks like this should be displayed. (Ask your Web browser to save the HTML source of this form to a file for your own use.) This form asks the user to enter various kinds of identification and billing information, as well as the text of the article to be posted. When the user clicks on ``SUBMIT'', a confirmation form that looks like this should be displayed. If the user clicks on ``OK'', then the ad should be posted, and an entry should be made in a billing log, as described in more detail below. Then an acknowledgement screen that looks like this should be displayed.

Before accepting an ad to be posted, your scripts should verify that the user has in fact filled in something in the identification and billing fields, and has actually entered some text for the ad. If the user has not done this, then you should display an appropriate error screen, rather than the acknowledgement screen.

Once an ad is accepted, your scripts will post the ad by placing the text of the ad into a file in the subdirectory of the ``ads'' directory that corresponds to the category selected by the user. The only real issue here is how to select a unique filename for the new ad, so that there is no conflict with the filenames of any existing ads. One way to do this is to maintain a special file in each subdirectory in which ads are stored. This file could be given a name like ``.NEXT'' which starts with a dot so that it won't normally be visible using ls. This file should have as its contents the ASCII text representation of the next available sequence number to be used as the name of an ad. Thus, when you first create a subdirectory, such as "Real Estate Rentals", you would create the file .NEXT with contents, ``1.'' The first ad inserted in the directory would be placed in a file named ``1'', and then the value stored in the .NEXT file would be changed to ``2.''

Your scripts should also take care of computing the amount that should be billed to a person posting an ad. Your billing should be computed at a rate of $1 per word. You can use the ``wc'' command to count words in a file. The amount of money that will be billed for an ad should be displayed to the user on the confirmation screen, before actually posting the ad. When the ad is finally posted, you should log the billing information to a file .BILLING in the ``ads'' directory. This file should contain one line for each article posted, and it should have the format shown here. Each line corresponds to one posting of transaction, and it consists of eight TAB-separated fields containing the following items of information:

  1. The date of the posting, as produced by the Unix date command.
  2. The category under which the ad was posted.
  3. The name of the file under that directory in which the ad was placed.
  4. The total charge in dollars for the ad (equals the number of words in the ad).
  5. An indication of whether the credit card used was VISA, AMEX or MC.
  6. The card number used.
  7. The expiration date of the card.
  8. The name of the person posting the ad.

After writing and testing your scripts, submit electronically the entire contents of your classifieds directory as in Homework 1.

Part II: Beginning C

The purpose of the following simple exercises is just to get you started compiling and executing C programs.
  1. Enter the following C program into a file called hello.c: #include <stdio.h> main() { printf("Hello world\n"); return(0); } Compile the program using the command: cc hello.c If the compilation is successful, an executable file a.out will be produced. Run this executable by typing a.out. You should see the Hello world message printed on your screen.

  2. The C compiler understands various option flags which are sometimes useful in debugging problems. The command: cc -o hello hello.c will create an executable file hello instead of the default executable a.out.

    The command:

    cc -E hello.c runs only the C preprocessor. The expanded version of hello.c is printed on the standard output. Redirect this output to a file hello.cpp and have a look at it. Basically, what has taken place is the system header file stdio.h has been inserted in place of the #include.

    The command:

    cc -c hello.c creates a relocatable binary object file hello.o instead of an executable binary file. Relocatable object files are linked together by the link editor to build executables from several separately compiled source files.

    The command:

    cc -S hello.c translates hello.c into an assembly language program hello.s. Have a look a the file hello.s, so you recognize what it looks like.

    The command:

    cc -g hello.c compiles hello.c with an expanded symbol table that can be used by the gdb debugger. In general, if you intend to do debugging, you should compile your programs with this option.

  3. Using the examples in Chapter 1 of A Book on C as a guide, modify the program hello.c by introducing an integer variable i and a while loop, so that it prints the greeting 10 times instead of just once.
Submit your modified hello.c, together with the files hello.o, hello, hello.s, and hello.cpp generated above.


Eugene W. Stark