Get CGI Input

Data is passed via environment variables and the standard input to a CGI script. This is a simple set of C functions that will ease the handling of these data. The functions currently handle the following cases:
  1. The GET request method. Variables are read from the environment variable QUERY_STRING.
  2. The POST request method, in which all data is read from standard input.
  3. If the CGI-BIN was called with additional pathname information, and this extra pathname was in variable style, it is evaluated as well.
  4. You can also pass the command line to the functions to add them as variables.
The library defines the following functions:
int InitInputValues (char *conffile, int argc, char *argv[])
Initializes the internal structures. argc and argv are supposed to be the values that were passed to the main function. This way, the command line can also feature variables. You can pass a filename as conffile. This file is read in before the CGI data or the command line is evaluated. Each line in the file can contain an expression Variable=Value. Lines beginning with a '#' are ignored. Returns a zero value on success, and the following positive values on failure:
_CGI_NOMETHOD
Either an unknown method was used (other than GET and POST or expected environment variables weren't defined).
Shouldn't happen anymore. Previously, this forbade scripts using only the extra path information.
_CGI_ERRINP
Data could not be read, or was incompletely read, from standard input. Only possible if the method was POST.
_CGI_NOMEM
A memory allocation request failed.
If you won't want this function to evaluate your command line, you can pass argc=0 and argv=NULL. Similarly, conffile=NULL means not to read any file. If conffile is not NULL but not readable, it is ignored.

char *GetInputValue (char *name)
Returns the value of variable name. Returns NULL if the variable is not defined. If a variable does exist but does not have a value, the empty string is returned.

char *GetInputNameByNum (int index)
Returns the name of the indexth variable in the environment. Returns NULL if index is equal or greater to the number of defined variables. The first variable has index 0.

char *GetInputValueByNum (int index)
Returns the value of the indexth variable in the environment. Returns NULL if index is equal or greater to the number of defined variables. The first variable has index 0.
The comparison GetInputValue (GetInputNameByNum (index)) == GetInputValueByNum (index) is always true.

void DelInputValues (void)
Frees all memory that was previously allocated, and deletes all variable definitions. All subsequent calls to the above three functions will return NULL. Note that you cannot reestablish the values by re-calling InitInputValues() because the data from standard input can not be reread.
Note: If the functions return non-NULL values, they point to static text which must be copied before changed.

The function will also define the variables RemoteIP, the IP address of the remote server and Remote with the full qualified hostname of the remote server. This name is not always available; if it is not, this variable will also be set to the IP address.
The variables SCRIPT_NAME, SERVER_NAME, SERVER_PORT will be defined if the program was run as a CGI script with the full CGI pathname of the script, the full qualified hostname (IP address) of the Server, and the port number of the server, respectively.

The source code: getcgi.tar.gz (3 kB).


Frank Pilhofer <fp -AT- fpx.de> Back to the Homepage
Last modified: Fri Mar 31 18:41:22 1995