home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / XIOS213C / CGI-SRC / TESTCGI.C next >
Text File  |  1997-04-29  |  4KB  |  134 lines

  1. /*  ----------------------------------------------------------------<Prolog>-
  2.     Name:       testcgi.c
  3.     Title:      CGI test program
  4.     Package:    Xitami web server
  5.  
  6.     Written:    96/10/01  Pieter Hintjens <ph@imatix.com>
  7.     Revised:    97/04/29  Pieter Hintjens <ph@imatix.com>
  8.  
  9.     Synopsis:   Generates an HTML test page containing the arguments passed
  10.                 to the CGI process.
  11.  
  12.     Copyright:  Copyright (c) 1997 iMatix
  13.     License:    This is free software; you can redistribute it and/or modify
  14.                 it under the terms of the XITAMI License Agreement as provided
  15.                 in the file LICENSE.TXT.  This software is distributed in
  16.                 the hope that it will be useful, but without any warranty.
  17.  ------------------------------------------------------------------</Prolog>-*/
  18.  
  19. #include "sfl.h"                        /*  SFL functions                    */
  20.  
  21. static Bool dump_symbol (SYMBOL *symbol, ...);
  22. static char *time_str   (void);
  23.  
  24. int
  25. main (int argc, char *argv [])
  26. {
  27.     SYMTAB
  28.         *symbols;
  29.     int
  30.         index;
  31.     char
  32.         *curdir;
  33.     size_t
  34.         content_length;
  35.  
  36.     printf ("Content-Type: text/html\n\n");
  37.     printf ("<HTML><HEAD><TITLE>CGI Test Program</TITLE></HEAD>\n");
  38.     printf ("<BODY>\n");
  39.     printf ("<H1>CGI Test Program</H1>\n");
  40.  
  41.     /*  Print Argument variables                                             */
  42.     if (argc > 1)
  43.       {
  44.         printf ("<H2>Arguments To Testcgi</H2>\n");
  45.         printf ("<PRE>\n");
  46.         for (index = 1; index < argc; index++)
  47.             printf ("<B>Argument %d </B>: %s\n", index, argv [index]);
  48.         printf ("</PRE>\n");
  49.       }
  50.  
  51.     /*  Print STDIN variables                                                */
  52.     content_length = (size_t) env_get_number ("HTTP_CONTENT_LENGTH", 0);
  53.     if (content_length)
  54.       {
  55.         symbols = sym_create_table ();
  56.         if (symbols)
  57.           {
  58.             if (cgi_parse_file_vars (symbols, stdin, "", content_length) > 0)
  59.               {
  60.                 printf ("<H2>Input CGI Variables</H2>\n");
  61.                 printf ("<PRE>\n");
  62.                 sym_exec_all (symbols, dump_symbol);
  63.                 printf ("</PRE>\n");
  64.               }
  65.             sym_delete_table (symbols);
  66.           }
  67.       }
  68.  
  69.     /*  Print environment variables                                          */
  70.     symbols = env2symb ();
  71.     if (symbols)
  72.       {
  73.         printf ("<H2>Environment Variables</H2>\n");
  74.         printf ("<PRE>\n");
  75.         sym_exec_all (symbols, dump_symbol);
  76.         sym_delete_table (symbols);
  77.         printf ("</PRE>\n");
  78.       }
  79.  
  80.     curdir = get_curdir ();
  81.     printf ("<H2>Miscellaneous Information</H2>\n");
  82.     printf ("<P>Working directory: %s\n", curdir);
  83.     printf ("<P>Current date and time: %s\n", time_str ());
  84.     printf ("</BODY></HTML>\n");
  85.  
  86.     mem_free (curdir);
  87.     mem_assert ();
  88.  
  89.     return (EXIT_SUCCESS);
  90. }
  91.  
  92. /*  -------------------------------------------------------------------------
  93.  *  This function is invoked by sym_exec_all() to process each item in
  94.  *  the symbol table.  It always returns TRUE to keep sym_exec_all() going.
  95.  */
  96.  
  97. static Bool
  98. dump_symbol (SYMBOL *symbol, ...)
  99. {
  100.     printf ("<B>%-20s</B> = %s\n", symbol-> name, symbol-> value);
  101.     return (TRUE);
  102. }
  103.  
  104.  
  105. /*  -------------------------------------------------------------------------
  106.  *  time_str
  107.  *
  108.  *  Returns the current date and time formatted as: "yy/mm/dd hh:mm:ss".
  109.  *  The formatted time is in a static string that each call overwrites.
  110.  */
  111.  
  112. static char *
  113. time_str (void)
  114. {
  115.     static char
  116.         formatted_time [18];
  117.     time_t
  118.         time_secs;
  119.     struct tm
  120.         *time_struct;
  121.  
  122.     time_secs   = time (NULL);
  123.     time_struct = localtime (&time_secs);
  124.  
  125.     sprintf (formatted_time, "%2d/%02d/%02d %2d:%02d:%02d",
  126.                               time_struct-> tm_year,
  127.                               time_struct-> tm_mon + 1,
  128.                               time_struct-> tm_mday,
  129.                               time_struct-> tm_hour,
  130.                               time_struct-> tm_min,
  131.                               time_struct-> tm_sec);
  132.     return (formatted_time);
  133. }
  134.