home *** CD-ROM | disk | FTP | other *** search
- /*____________________________________________________________________________*\
- *
-
- Copyright (c) 1997 John Roy. All rights reserved.
-
- These sources, libraries and applications are
- FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
- as long as the following conditions are adhered to.
-
- Redistribution and use in source and binary forms, with or without
- modification, are permitted provided that the following conditions
- are met:
-
- 1. Redistributions of source code must retain the above copyright
- notice, this list of conditions and the following disclaimer.
-
- 2. Redistributions in binary form must reproduce the above copyright
- notice, this list of conditions and the following disclaimer in
- the documentation and/or other materials provided with the
- distribution.
-
- 3. Redistributions of any form whatsoever and all advertising materials
- mentioning features must contain the following
- acknowledgment:
- "This product includes software developed by John Roy
- (http://www.johnroy.com/pi3/)."
-
- THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
- WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- OF THE POSSIBILITY OF SUCH DAMAGE.
-
- *____________________________________________________________________________*|
- *
- * $Source: WViewEnv.c$
- * $Date: Sun Aug 10 06:41:31 1997$
- *
- Description:
- Print environment and form input in Windows CGI.
-
- Speaks HTTP/1.0
-
- Also works on non-windows operating systems, but the implementation
- of GetProfile...() is not efficient.
-
- \*____________________________________________________________________________*/
- /* $SourceTop:$ */
-
- #include <assert.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <time.h>
-
- /*____________________________________________________________________________*\
- *
- Function:
- Synopsis:
- Description:
- \*____________________________________________________________________________*/
- void WriteEnv( FILE *fp, int iLen, char *pInput )
- {
- const char *pCurrent;
- int iIndex;
-
- fprintf( fp, "Content-Type: text/html\n");
- fprintf( fp, "\n" );
- fprintf( fp, "<HTML>\n");
- fprintf( fp, "<TITLE>View CGI Environment</TITLE>\n");
- fprintf( fp, "<BODY>\n");
- fprintf( fp, "<H2>View CGI Environment</H2>\n");
- fprintf( fp, "<BR>Stdin data length:<BR><B>%d</B><BR>\n", iLen );
- if ( iLen>0 && pInput )
- {
- fprintf( fp, "<BR>Stdin data:<BR><B>%s</B><BR>\n", pInput );
- };
- #if 0
- fprintf( fp, "<BR>Environment variables follow:<BR>\n");
- fprintf( fp, "<BR><B>\n");
-
- for( iIndex = 0; ; iIndex++ )
- {
- pCurrent = _environ[iIndex];
- if ( !pCurrent ) { break; };
-
- fprintf( fp, "<CODE>%s</CODE><BR>\n", pCurrent );
- };
- #endif
- fprintf( fp, "<BR></B>\n");
-
- fprintf( fp, "</BODY>\n");
- }
-
- #if (WIN32)
- /*
- ** Windows environment, use real version of ::GetPrivateProfileString()
- */
- # define MyGetPrivateProfileString(a,b,c,d,e,f) \
- GetPrivateProfileString(a,b,c,d,e,f)
- # include <windows.h>
-
- #else
- /*
- ** Not a windows environment. Use the super efficient, non-caching-
- ** -reopen-and-close-the-file-every-time version
- */
-
- /*____________________________________________________________________________*\
- *
- Function:
- Synopsis:
- Description:
- Inefficent lookup of values in a profile file
- \*____________________________________________________________________________*/
- int MyGetPrivateProfileString(
- const char *pSection,
- const char *pKey,
- const char *pDefault,
- char *pszBuffer,
- int iBufferSize,
- const char *pFileName )
- {
- FILE *fp = 0;
- int iCorrectSection = 0; /* is current section the correct one ? */
- assert( pSection && pKey && pszBuffer && iBufferSize && pFileName );
- if ( !pFileName || !*pFileName )
- { goto done; };
- fp = fopen( pFileName, "r" );
- if ( fp )
- {
- enum { BUF_SIZE=1023 };
- char szBuf[BUF_SIZE+1];
- char *pS = fgets( szBuf, BUF_SIZE, fp );
- szBuf[BUF_SIZE]='\0';
- for( ; pS!=NULL ; pS = fgets( szBuf, BUF_SIZE, fp ) )
- {
- int i, j, k;
- for( i=0; pS[i] && (isspace(pS[i])); i++); /* no leading space */
- k = strlen( pS )-1;
- for( j=k ; j>=0 && (isspace(pS[j])); j--); /* no trailing space */
- if ( j < k )
- { pS[j+1]='\0'; }; /* terminate line */
- /*
- ** The segment (pS[i]..pS[j]) is the interesting part of the string
- */
-
- /*
- ** section begins with '[' and ends with ']'
- */
- if ( (j-i)<2 ) { goto key_value_line; };
- if ( pS[i]!='[' || pS[j]!=']' ) { goto key_value_line; };
- i++; j--;
-
- /*
- ** match section name
- */
- if ( (j>i) && !strncmp( &(pS[i]), pSection, j-i ) )
- { iCorrectSection = 1; }
- else
- { iCorrectSection = 0; };
- continue;
-
- key_value_line:
- if ( iCorrectSection )
- {
- /* --- attempt to match key --- */
- for( i=0; pS[i] && (isspace(pS[i])); i++); /* remove leading */
- if ( pS[i]==';' ) { continue; }; /* comment line */
- for( j=i; pS[j] && pS[j]!='='; j++);/* find '=' */
- if ( pS[j] )
- {
- if ( (j>i) && !strncmp( &(pS[i]), pKey, j-i ) )
- {
- j++;
- strncpy( pszBuffer, &(pS[j]), iBufferSize );
- return 1;
- }; /* key matches */
- }; /* section matches */
- }; /* loop through lines in file */
- }; /* opened file */
- fclose( fp );
- };
-
- done:
- if ( pDefault )
- { strncpy( pszBuffer, pDefault, iBufferSize ); };
-
- return 1;
- }
-
- #endif
-
- /*____________________________________________________________________________*\
- *
- Function:
- Synopsis:
- Description:
- \*____________________________________________________________________________*/
- int main( int iArgc, const char *ppArgv[] )
- {
- FILE *fp = 0;
- int iRet = 0;
- int iInLen;
- char *pInputData = 0;
-
- enum { BUF_LEN=511 };
- char szBuf[BUF_LEN+1];
- szBuf[BUF_LEN]='\0';
-
- if ( iArgc<2 )
- {
- fprintf( stderr, "WViewEnv: bad arguments.\n" );
- return -1;
- };
-
- /* --- get output filename --- */
- MyGetPrivateProfileString( "System", "Output File", "", szBuf, BUF_LEN,
- ppArgv[1] );
-
- /* --- and open it --- */
- if ( !*szBuf )
- {
- fprintf( stderr, "WViewEnv: could not get output filename" );
- iRet = -1; goto done;
- };
-
- fp = fopen( szBuf, "w" );
- if ( !fp )
- {
- perror( "WViewEnv: could not open output file" );
- iRet = -1; goto done;
- };
-
- MyGetPrivateProfileString( "CGI", "Content Length", "", szBuf, BUF_LEN,
- ppArgv[1] );
- iInLen = atoi( szBuf );
-
- /*
- ** Allocate buffer for data
- */
- if ( iInLen>0 )
- { pInputData = (char *)malloc( iInLen+1 ); };
-
- /*
- ** Read data
- */
- if ( pInputData )
- {
- FILE *ifp;
-
- MyGetPrivateProfileString( "System", "Content File", "", szBuf,
- BUF_LEN, ppArgv[1] );
-
- if ( !*szBuf )
- {
- fprintf( stderr, "WViewEnv: could not get content filename" );
- iRet = -1; goto done;
- };
-
- ifp = fopen( szBuf, "r" );
- if ( !ifp )
- {
- perror( "WViewEnv: could not open input file" );
- iRet = -1; goto done;
- };
- iInLen = fread( pInputData, sizeof( char ), iInLen, ifp );
- pInputData[iInLen] = '\0';
-
- fclose( ifp );
- };
-
- WriteEnv( fp, iInLen, pInputData );
-
- done:
- if ( pInputData ) { free( pInputData ); };
- fclose( fp );
-
- return iRet;
- }
-
-