LIBRARY
on the command line (or, if necessary, by editing uncgi.c.) See your compiler's
documentation to find out how to compile to an object file. This usually works:
cc -DLIBRARY -c uncgi.c
man getenv
to see the manual page.) The environment variables
are called exactly the same thing they are when Un-CGI invokes a script for
you.
Note that getenv() can return NULL if an environment variable isn't defined. It's good programming practice to always check getenv()'s return value to see if an error occurred. If a variable you expect to be defined isn't, it usually means that a user tried to invoke your program without going through your form, or there's a typo in your form or the variable name in your getenv() call.
Here's a small example program, that spits some numbers out at a user. If you don't understand what this is doing, please consult a programmer at your site; I'm not interested in teaching you how to use C. The example is called with a very simple form:
<form method=GET action="/cgi-bin/myprog">
How many numbers?
<input type=text size=4 name="count">
<input type=submit value=" Submit ">
</form>
And here's the program.
#include <stdio.h> char *getenv(); main() { char *count_raw; int count; /* Decode the form results. */ uncgi(); /* Always print a content type and a blank line. */ printf("Content-type: text/html\n\n"); /* Get the form value, which will be a string. */ count_raw = getenv("WWW_count"); if (count_raw == NULL) { printf("<h1>Type something!</h1>\n"); exit(1); } /* Convert the form value to an integer. */ count = atoi(count_raw); if (count == 0) { printf("<h1>Enter a number, 1 or higher</h1>\n"); exit(1); } /* Now print some numbers. */ while (count--) printf("%d<br>\n", count); printf("That's all!\n"); }
cc -o myprog myprog.o uncgi.o
If your program is in a single source file, you may prefer to compile it directly without generating a .o file:
cc -o myprog myprog.c uncgi.o
The two commands shown will work on most compilers; consult your system's compiler documentation for more details.
That's all there is to it!