view · edit · print · history


Compile a C program to make a CGI executable for thttpd - all on the NSLU2.


Installation of Unslung.
Successful completion of NativeNSLU2Toolchain, which provides a development environment on your NSLU2.


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! */
	       "Hello, World!<br>\n");       /* Do the hello thing... */

        /* Print out the current time */
        printf("The time is %s<br>\n", asctime(localtime(&tim)) );


	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:


5) Access the CGI file through a browser using:


That's it!

6) Final Note:

For simple CGI scripts, you can use a shell script like following:

 echo "Content-type: text/plain"
 echo ""
 echo "Hello World!"

Don't forget to chmod +x scriptname.cgi
(Tested with Linksys R29 Firmware? - the webserver pages are at /home/httpd/html/Management/ in the ramdisk - so it shall work on Unslung.HomePage, too.)

view · edit · print · history · Last edited by mwester.
Based on work by RXPKW, MK, UXAOX, fixer, ZluZm, tino, tman, and michaelebrownmindspringcom.
Originally by michaelebrownmindspringcom.
Page last modified on May 08, 2008, at 06:25 PM