The CGI-LIB Library

An ANSI C Library for CGI Programming


Noel V Aguilar

Table of Contents

What is CGI-LIB?


CGI-LIB is a free ANSI C library that will facilitate the creation of CGI programs. The library is written entirely in C using ANSI C functions. The functions are easy to use and the source code is included.

Files Included in this Package

This package contains several files. Below is a list of all these files with a brief description of their purpose.

Obtaining CGI-LIB

You may obtain the CGI-LIB library by clicking on any of the two links located below, all the source code is included along with the makefile to compile the program.

ZIP Format

To unzip the file you:

TAR Format

To uncompress files with a gz extension:


The library needs to be compiled, a makefile is included in the distribution. Please modify the makefile accordingly and run make. The final output will be an archive file.

You may install the compiled archive file in your lib subdirectory and the corresponding header files in your include subdirecory, or you may place in a directory and call them from there when building your CGI program.


Once you have your archive file you may delete the rest of the files except the archive file, cgi-lib.h, html-lib.h and list-lib.h.

In order to use the library all you will need is the archive file and to include the cgi-lib.h and the html-lib.h file in your cgi program. As mentioned earlier, the html-lib.h file contains functions that will help you in writing html. If you do not wish to use these functions you do not need to include this header file in your program. You will need to include the cgi-lib.h file in order to make use of the cgi data manipulation functions. You can find an example program below.

One of the important things that you must also do is to declare a variable of type pointer to LIST in order to maintian track of the linked list the CGI-LIB library will create. ie: LIST *head;


The CGI-LIB library has a variety of functions that will assist you in generating html and manipulating CGI data. The cgi-lib.h file has the declarations of the functions that can be used to help create and manipulate CGI data, and the html-lib.h file has the declarations of the functions that will help you generate html.


The following Environment Variables are declared and can be used:

The following functions can be used to manipulate CGI data. When I refer to a True value I am referring to a non-zero value (in the case of CGI-LIB: 1), and when I refer to a False value, I am referring to zero value.


Example Programs

Below is a sample program using some of the functions described above. Please look at the program code in order to get a better understanding, the code is documented.

#include <stdio.h>

#include "cgi-lib.h" /* include the cgi-lib.h header file */

#include "html-lib.h" /* include the html-lib.h header file */

int main()


	/* need to declare a pointer variable of type LIST to keep track of our list */

	LIST *head;

	/* need to call this function at the beginning to initiate and setup out list */

	head = cgi_input_parse();

	/* send the mime header to the server using our function in html-lib */


	/* send the top of our html page to the server */

	html_begin("CGI Sample Application",NULL);

	/* send the text enclosed in the heading tags */

	h1("CGI Sample Application");

	/* send break lines */


	/* send the text enclosed in the heading tags */

	h2("Parsed Values");


	we need to check if head is NULL, this will tell us if we have

	any data in our linked list, if it is NULL then we don't need  to

	call any other CGI-LIB functions because there is nothing to do.


	if(head == NULL)

		h3("List Empty");




	/* send text enclosed in heading tags */

	h2("Environment Variables");

	/* print out all the environment variables */


	/* send the html closing tags */


	return 0;


Below is a sample makefile that you can use in order to compile your application

# CGI-LIB Library Sample Makefile


# This is a sample makefile that will help you

# include the CGI-LIB library in your cgi script.

# The CGI-LIB library must already be compiled,

# before you you use it.

# CC is the variable that will contain your compiler



# CGI is the variable that contains the cgi file name


# CGI-LIB-ARCHIVE contains the path and name of CGI-LIB


# CGI-LIB-HEADERS contains the path and names of your header files

CGI-LIB-HEADERS=html-lib.h cgi-lib.h list-lib.h


$(CGI): cgisamp.c $(CGI-LIB-HEADERS)

        $(CC) $(CCFLAGS) $@ cgisamp.c $(CGI-LIB-ARCHIVE)


Release Notes

CGI-LIB has been compiled and tested on unix (HP-UX and Linux) using the GNU compiler, and it has also been compiled and tested on Windows NT 4.0 using Visual C++ 4.2, 5.0, and 6.0.

For Updates please check

What's New in Release 1.4

What's New in Release 1.3

What's New in Release 1.2

What's New in Release 1.1

Fixed a bug in html_begin(), the attributes being passed for the body tag were not being displayed.

What's New in Release 1.01

Updated documentation with a sample makefile and inform that list-lib.h should not be deleted once the library has been created. Since it is declared in cgi-lib.h

What's New in Release 1.0

Initial Release

Future Releases

For Comments and Reporting Bugs

If you have any comments or suggestions please let me know by filling a comments form. If you encounter a bug of any kind please fill out a comments form.


Thank you for supporting public domain software.