home *** CD-ROM | disk | FTP | other *** search
- <HTML><TITLE>WHello.c</TITLE><BODY><PRE>/*____________________________________________________________________________*\
- *
-
- 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: WHello.c$
- * $Date: Sun Aug 10 06:41:31 1997$
- *
- Description:
- Hello for Windows CGI.
-
- Speaks HTTP/1.0
-
- Also works on non-windows operating systems, but the implementation
- of GetProfile...() is really slow.
- \*____________________________________________________________________________*/
- /* $SourceTop:$ */
-
- #include <assert.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <time.h>
-
- #if (WIN32)
- # define MyGetPrivateProfileString(a,b,c,d,e,f) \
- GetPrivateProfileString(a,b,c,d,e,f)
- # include <windows.h>
-
- #else
-
- /*____________________________________________________________________________*\
- *
- 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 );
- 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 );
- };
- if ( pDefault )
- { strncpy( pszBuffer, pDefault, iBufferSize ); };
- return 1;
- }
-
- #endif
-
- /*____________________________________________________________________________*\
- *
- Function:
- Synopsis:
- Description:
- \*____________________________________________________________________________*/
- int main( int iArgc, const char *ppArgv[] )
- {
- FILE *fp = 0;
-
- enum { PATH_LEN=511 };
- char szPath[PATH_LEN+1];
- szPath[PATH_LEN]='\0';
-
- if ( iArgc<2 )
- {
- fprintf( stderr, "WHello: bad arguments.\n" );
- return -1;
- };
-
- /* --- get output filename --- */
- MyGetPrivateProfileString( "System", "Output File", "", szPath, PATH_LEN,
- ppArgv[1] );
-
- if ( !*szPath )
- { exit( -1 ); };
-
- /* --- and open it --- */
- fp = fopen( szPath, "w" );
- if ( !fp )
- {
- perror( "WHello: could not open output file" );
- return -1;
- };
-
- /* --- Hello World! --- */
- fprintf( fp, "Content-Type: text/html\n");
- fprintf( fp, "\n" );
- fprintf( fp, "<HTML>\n");
- fprintf( fp, "<TITLE>Hello World <A HREF="/pidocs/Objects/HTTP.html#CGI">CGI</A> Program Output</TITLE>\n");
- fprintf( fp, "<BODY>\n");
- fprintf( fp, "<H1>Hello World!</H1>\n");
- fprintf( fp, "Welcome to the wonderful world of Windows <A HREF="/pidocs/Objects/HTTP.html#CGI">CGI</A> programming!");
-
- fclose( fp );
- return 0;
- }
-
- </PRE></BODY></HTML>