![]() |
Goal: Compile a C program to make a CGI executable for thttpd - all on the NSLU2. Prerequisites: Installation of Unslung. Steps: Note: The locations used in this example may vary for your installation. 1) Create the C source file, webhello.c, using the editor of your choice. Here's the code: #include <stdio.h>
#include <time.h>
int main()
{
time_t tim = time(NULL);
printf("Content-type: text/html\n" /* Necessary to specify the type */
"\n" /* This blank line is critical! */
"<html>\n"
"<body>\n"
"Hello, World!<br>\n"); /* Do the hello thing... */
/* Print out the current time */
printf("The time is %s<br>\n", asctime(localtime(&tim)) );
printf("</body>\n"
"</html>\n");
return 0;
}
2) Compile the C source file: gcc webhello.c -o webhello.cgi
strip webhello.cgi
3) Move the executable into the web hierarchy: mv webhello.cgi /home/www/ 4) Identify the CGI pattern to thttpd. This can be done two ways: On the command line:
/usr/bin/thttpd -c '**.cgi'
Or, in the config file as:
cgipat=**.cgi
5) Access the CGI file through a browser using: http://www.ByteRed.com/webhello.cgi That's it! 6) Final Note: For simple CGI scripts, you can use a shell script like following: #!/bin/ash echo "Content-type: text/plain" echo "" echo "Hello World!" set Don't forget to |