Week 5- February 16, 1999

5.1 Writing your C program
5.2 My first CGI program in C
5.3 Parsing the arguments: tough job!
5.4 Perl or C/C++?

5.1 Writing your C program

In order to create a C program on a UNIX system, you have to create the source file using a text editor (i.e. xemacs or vi) and to compile the program using the C compiler. In order to compile a source file, one should enter at the command line

gcc -o output_name source_name.c

where the output_name is the name we want for our final (compiled) program and source_name.c is the name of our source file. You do not have to change the rights for the compiled program in order to let everybody run it. The rights are set by the compiler to be rwxr-xr-x.You can keep the source file in another directory, you do not have to keep it in the cgi-bin directory. The only file you need there is the compiled version.

All the libraries and header files are similar to any other C fragrance, so you can use all you know about C.

5.2 My first CGI program in C

We will now write the first program in C. The code is stored in the file first.c. We used the gcc -o first-c first.c command, thus the executable is called first-c.

You can take a look at the same program written in perl. The code is stored in first.perl.

5.3 Parsing the arguments: tough job!

You remember how easy was to parse the parameters in a Perl program. We used the &ReadParse function and… here they are, stored in the in associative array. With C, things are more complicated…

If you use the same GET method, your query string will be passed to your C/C++ program in the argv variable. But instead of having it split over a number of elements from the argv array, we have everything in the second entry (remember that the first one, argv[0] contains the name of the program). Thus, the query string will have the form name=Bogdan&date=02/16/99&class=cs246&time=18:00. We have to write our own function to extract the values we need from this string. One can use the string functions strchr and strcpy to do the job. On some CGI gateways (like the one on SUN computers L ) this does not work. Thus, you have to use the POST method. This will take the same query string and will send it to the stdin of your CGI program, thus your first gets will read it. You still have to parse the parameter string the same way as above. The nice part about this method (POST) is that the user does not see the query string in the URL box of the browser, so it is better for passwords and other things like that. Let's see how this thing works. We will take a look at the example first, and then at the C code.

You can use the POST method with Perl programs too. The &ReadParse function will take care of the input so you do not have to do anything else. Look at the following example and Perl code to see how it works.

5.4 Perl or C/C++?

Well, the only time you need to use C instead of Perl is when you really need SPEED. If the time you have to spend writing the program is worth compared with the time difference between the two languages at runtime. Also, sometimes there are things that you can do in C and can not (or at least can not easily) do in Perl. For example, GIF-on-the-fly and binary file access.