>How can you write CGI scripts in C?
It's surprisingly simple, here is a CGI program that takes a message and creates a simple web page:
Code:
#include <stdio.h>
#include <string.h>
#define OPEN "<HTML><BODY>"
#define CLOSE "</BODY></HTML>"
void parseMsg ( char *buf, char *s )
{
strcpy ( buf, s ); /* Insert a more suitable parse routine here */
}
int main ( int argc, char **argv )
{
char msg[BUFSIZ];
if ( argc > 1 ) {
parseMsg ( msg, argv[1] );
printf ( "Content-type: text/html\n\n" );
printf ( "%s\n%s\n%s\n", OPEN, msg, CLOSE );
}
return 0;
}
Or the C++ equivalent:
Code:
#include <iostream>
#include <string>
const char OPEN[] = "<HTML><BODY>";
const char CLOSE[] = "</BODY></HTML>";
void parseMsg ( std::string& buf, char *s )
{
buf = s; /* Insert a more suitable parse routine here */
}
int main ( int argc, char **argv )
{
std::string msg;
if ( argc > 1 ) {
parseMsg ( msg, argv[1] );
std::cout<<"Content-type: text/html\n\n";
std::cout<< OPEN <<'\n'<< msg <<'\n'<< CLOSE <<std::endl;
}
}
-Prelude