Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can Somebody tell me the basic's of writing a CGI application in C

Status
Not open for further replies.

ambalavanan

Programmer
Aug 11, 2000
1
IN
If anybody around here has written a CGI script in C/ C++ or knows to write one... and feels that he can help somebody dying to learn that.. it would be very much appreciated...
 
If I remember correctly it's very similar to what you do in Perl, only obviously using C commands... :)<br><br>So you want to get any user input from the environment, rather than being able to read straight from %ENV as in perl, you'll need to use getenv:<br><br>*note the names of the variables can vary with which HTTP server you are using*<br><br><FONT FACE=monospace>#include &lt;stdlib.h&gt;<br><br>char *result = NULL;<br>char *env_name = NULL;<br><br>/* set our env_name to the envirable we want to read */<br>env_name = malloc(sizeof(&quot;VARIABLENAME&quot;);<br>strcpy(env_name,&quot;VARIABLENAME&quot;);<br>result = malloc(sizeof(getenv(env_name));<br><br>if (result == NULL)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;VARIABLENAME doesn't exist\n&quot;);<br>}<br>else<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;VARIABLENAME is %s \n&quot;, result);<br>}</font><br><br>Then you just write to the page as you would normally, e.g.<br><br><FONT FACE=monospace>/* print http header */<br>fprintf(STDOUT, &quot;Content-type:text/html\n\n&quot;);<br>fprintf(STDOUT, &quot;blah blah blah....</font><br><br>You can get a list of HTTP/CGI envirables via O'Reilly (webmaster in a nutshell Ch.11, p87 - latest edition)<br><br>Hope that helps<br>Loon<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top