home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / WEBSERVE / PI3 / PI3WEB.EXE / DEVEL / CGI / WViewEnv.c < prev   
C/C++ Source or Header  |  1997-10-23  |  8KB  |  289 lines

  1. /*____________________________________________________________________________*\
  2.  *
  3.  
  4.  Copyright (c) 1997 John Roy. All rights reserved.
  5.  
  6.  These sources, libraries and applications are
  7.  FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  8.  as long as the following conditions are adhered to.
  9.  
  10.  Redistribution and use in source and binary forms, with or without
  11.  modification, are permitted provided that the following conditions
  12.  are met:
  13.  
  14.  1. Redistributions of source code must retain the above copyright
  15.     notice, this list of conditions and the following disclaimer. 
  16.  
  17.  2. Redistributions in binary form must reproduce the above copyright
  18.     notice, this list of conditions and the following disclaimer in
  19.     the documentation and/or other materials provided with the
  20.     distribution.
  21.  
  22.  3. Redistributions of any form whatsoever and all advertising materials 
  23.     mentioning features must contain the following
  24.     acknowledgment:
  25.     "This product includes software developed by John Roy
  26.     (http://www.johnroy.com/pi3/)."
  27.  
  28.  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  29.  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30.  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  31.  IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  32.  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33.  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34.  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35.  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36.  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37.  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  38.  OF THE POSSIBILITY OF SUCH DAMAGE.
  39.  
  40.  *____________________________________________________________________________*|
  41.  *
  42.  * $Source: WViewEnv.c$
  43.  * $Date: Sun Aug 10 06:41:31 1997$
  44.  *
  45.  Description:
  46.     Print environment and form input in Windows CGI.
  47.     
  48.     Speaks HTTP/1.0
  49.     
  50.     Also works on non-windows operating systems, but the implementation
  51.     of GetProfile...() is not efficient.
  52.  
  53. \*____________________________________________________________________________*/
  54. /* $SourceTop:$ */
  55.  
  56. #include <assert.h>
  57. #include <stdlib.h>
  58. #include <stdio.h>
  59. #include <string.h>
  60. #include <ctype.h>
  61. #include <time.h>
  62.  
  63. /*____________________________________________________________________________*\
  64.  *
  65.  Function:
  66.  Synopsis:
  67.  Description:
  68. \*____________________________________________________________________________*/
  69. void WriteEnv( FILE *fp, int iLen, char *pInput )
  70. {
  71.     const char *pCurrent;
  72.     int iIndex;
  73.  
  74.     fprintf( fp, "Content-Type: text/html\n");
  75.     fprintf( fp, "\n" );
  76.     fprintf( fp, "<HTML>\n");
  77.     fprintf( fp, "<TITLE>View CGI Environment</TITLE>\n");
  78.     fprintf( fp, "<BODY>\n");
  79.     fprintf( fp, "<H2>View CGI Environment</H2>\n");
  80.     fprintf( fp, "<BR>Stdin data length:<BR><B>%d</B><BR>\n", iLen );
  81.     if ( iLen>0 && pInput )
  82.         {
  83.         fprintf( fp, "<BR>Stdin data:<BR><B>%s</B><BR>\n", pInput );
  84.         };
  85. #if 0
  86.     fprintf( fp, "<BR>Environment variables follow:<BR>\n");
  87.     fprintf( fp, "<BR><B>\n");
  88.  
  89.     for( iIndex = 0; ; iIndex++ )
  90.         {
  91.         pCurrent = _environ[iIndex];
  92.         if ( !pCurrent ) { break; };
  93.  
  94.         fprintf( fp, "<CODE>%s</CODE><BR>\n", pCurrent );
  95.         };
  96. #endif
  97.     fprintf( fp, "<BR></B>\n");
  98.  
  99.     fprintf( fp, "</BODY>\n");
  100. }
  101.  
  102. #if (WIN32)
  103.     /*
  104.     ** Windows environment, use real version of ::GetPrivateProfileString()
  105.     */
  106. #    define MyGetPrivateProfileString(a,b,c,d,e,f) \
  107.         GetPrivateProfileString(a,b,c,d,e,f)
  108. #    include <windows.h>
  109.  
  110. #else
  111.     /*
  112.     ** Not a windows environment. Use the super efficient, non-caching-
  113.     ** -reopen-and-close-the-file-every-time version
  114.     */
  115.  
  116. /*____________________________________________________________________________*\
  117.  *
  118.  Function:
  119.  Synopsis:
  120.  Description:
  121.     Inefficent lookup of values in a profile file
  122. \*____________________________________________________________________________*/
  123. int MyGetPrivateProfileString(
  124.     const char *pSection,
  125.     const char *pKey,
  126.     const char *pDefault,
  127.     char *pszBuffer,
  128.     int iBufferSize,
  129.     const char *pFileName )
  130. {
  131.     FILE *fp = 0;
  132.     int iCorrectSection = 0;    /* is current section the correct one ? */
  133.     assert( pSection && pKey && pszBuffer && iBufferSize && pFileName );
  134.     if ( !pFileName || !*pFileName )
  135.         { goto done; };
  136.     fp = fopen( pFileName, "r" );
  137.     if ( fp )
  138.         {
  139.         enum { BUF_SIZE=1023 };
  140.         char szBuf[BUF_SIZE+1];
  141.         char *pS = fgets( szBuf, BUF_SIZE, fp );
  142.         szBuf[BUF_SIZE]='\0';
  143.         for( ; pS!=NULL ; pS = fgets( szBuf, BUF_SIZE, fp ) )
  144.             {
  145.             int i, j, k;
  146.             for( i=0; pS[i] && (isspace(pS[i])); i++);    /* no leading space */ 
  147.             k = strlen( pS )-1;
  148.             for( j=k ; j>=0 && (isspace(pS[j])); j--);    /* no trailing space */ 
  149.             if ( j < k )
  150.                 { pS[j+1]='\0'; };                        /* terminate line */
  151.             /*
  152.             ** The segment (pS[i]..pS[j]) is the interesting part of the string
  153.             */
  154.  
  155.             /*
  156.             ** section begins with '[' and ends with ']'
  157.             */
  158.             if ( (j-i)<2 ) { goto key_value_line; };
  159.             if ( pS[i]!='[' || pS[j]!=']' ) { goto key_value_line; };
  160.             i++; j--;
  161.  
  162.             /*    
  163.             ** match section name
  164.             */
  165.             if ( (j>i) && !strncmp( &(pS[i]), pSection, j-i ) )
  166.                 { iCorrectSection = 1; }
  167.             else
  168.                 { iCorrectSection = 0; };
  169.             continue;
  170.  
  171. key_value_line:
  172.             if ( iCorrectSection )
  173.                 {
  174.                 /* --- attempt to match key --- */
  175.                 for( i=0; pS[i] && (isspace(pS[i])); i++);    /* remove leading */ 
  176.                 if ( pS[i]==';' ) { continue; };    /* comment line */
  177.                 for( j=i; pS[j] && pS[j]!='='; j++);/* find '=' */
  178.                 if ( pS[j] )
  179.                     {
  180.                     if ( (j>i) && !strncmp( &(pS[i]), pKey, j-i ) )
  181.                         {
  182.                         j++;
  183.                         strncpy( pszBuffer, &(pS[j]), iBufferSize );
  184.                         return 1;
  185.                         };  /* key matches */
  186.                     };    /* section matches */
  187.                 };     /* loop through lines in file */
  188.             };    /* opened file */
  189.         fclose( fp );
  190.         };
  191.  
  192. done:
  193.     if ( pDefault )
  194.         { strncpy( pszBuffer, pDefault, iBufferSize ); };
  195.  
  196.     return 1;
  197. }
  198.  
  199. #endif
  200.  
  201. /*____________________________________________________________________________*\
  202.  *
  203.  Function:
  204.  Synopsis:
  205.  Description:
  206. \*____________________________________________________________________________*/
  207. int main( int iArgc, const char *ppArgv[] )
  208. {
  209.     FILE *fp = 0;
  210.     int iRet = 0;
  211.     int iInLen;
  212.     char *pInputData = 0;
  213.  
  214.     enum { BUF_LEN=511 };
  215.     char szBuf[BUF_LEN+1];
  216.     szBuf[BUF_LEN]='\0';
  217.  
  218.     if ( iArgc<2 )
  219.         {
  220.         fprintf( stderr, "WViewEnv: bad arguments.\n" );
  221.         return -1;
  222.         };
  223.  
  224.     /* --- get output filename --- */
  225.     MyGetPrivateProfileString( "System", "Output File", "", szBuf, BUF_LEN, 
  226.         ppArgv[1] );
  227.  
  228.     /* --- and open it --- */
  229.     if ( !*szBuf )
  230.         {
  231.         fprintf( stderr, "WViewEnv: could not get output filename" );
  232.         iRet = -1; goto done;
  233.         };
  234.  
  235.     fp = fopen( szBuf, "w" );
  236.     if ( !fp )
  237.         {
  238.         perror( "WViewEnv: could not open output file" );
  239.         iRet = -1; goto done;
  240.         };
  241.  
  242.     MyGetPrivateProfileString( "CGI", "Content Length", "", szBuf, BUF_LEN,
  243.         ppArgv[1] );
  244.     iInLen = atoi( szBuf );
  245.  
  246.     /*
  247.     ** Allocate buffer for data
  248.     */
  249.     if ( iInLen>0 )
  250.         { pInputData = (char *)malloc( iInLen+1 );  };
  251.  
  252.     /*
  253.     ** Read data
  254.     */
  255.     if ( pInputData )
  256.         {
  257.         FILE *ifp;
  258.  
  259.         MyGetPrivateProfileString( "System", "Content File", "", szBuf,
  260.             BUF_LEN, ppArgv[1] );
  261.  
  262.         if ( !*szBuf )
  263.             {
  264.             fprintf( stderr, "WViewEnv: could not get content filename" );
  265.             iRet = -1; goto done;
  266.             };
  267.  
  268.         ifp = fopen( szBuf, "r" );
  269.         if ( !ifp )
  270.             {
  271.             perror( "WViewEnv: could not open input file" );
  272.             iRet = -1; goto done;
  273.             };
  274.         iInLen = fread( pInputData, sizeof( char ), iInLen, ifp );
  275.         pInputData[iInLen] = '\0';
  276.  
  277.         fclose( ifp );
  278.         };
  279.  
  280.     WriteEnv( fp, iInLen, pInputData );
  281.  
  282. done:
  283.     if ( pInputData ) { free( pInputData ); };
  284.     fclose( fp );
  285.  
  286.     return iRet;
  287. }
  288.  
  289.