#include "stdio.h"
#include "stdlib.h"

/*
   A general purpose counter for WWW pages.
	Paul Bourke, March 1997

   Requirements: Server side include support
                 Designed for the Apache server
   It has been tested with the Apache "suexec" for secure cgi support.

   The counter supports plain text output or graphical characters.
   Multiple character sets can be created.

	For each page the counter appears on, a count file is created.
   The file is called count_<filename>. The format of this file is
     count typeface
   Both are integers, the typeface is -1 for text, 0,1...for the
   different fonts. The fonts consist of a number of gif files
   named 
      n0.gif, n1.gif, n2.gif....n9.gif
   where n is the typeface integer. These images are assumed to reside
   in the /icons/ directory, see code below.

   Usage: <!--#exec cgi="./counter.cgi" -->
*/

int main(argc,argv)
int argc;
char **argv;
{
	int i,j,c,typeface = -1;
	long count = 0;
	char s[32],fname[64],document[256];
	FILE *fptr;

	printf("Content-type: text/html\n\n");

	/* Get the file name */
	strcpy(document,getenv("DOCUMENT_NAME"));
	if (document == NULL)
		exit(0);
	for (i=0;i<strlen(document);i++)
		if (document[i] == '/')
			document[i] = '_';
	sprintf(fname,"count_%s",document);

	/* Read the current count and typeface */
	if ((fptr = fopen(fname,"r")) != NULL) {
		if (fscanf(fptr,"%ld %d",&count,&typeface) != 2) { 
			count = 0;
			typeface = -1;
		}
		fclose(fptr);
	}
	count++;

	/* Write the new count and typeface */
	if ((fptr = fopen(fname,"w")) != NULL) {
		fprintf(fptr,"%ld %d\n",count,typeface);
		fclose(fptr);
	}

	/* Display the count in the appropriate typeface */
	if (typeface < 0) {
      printf("%ld",count);
	} else {
		sprintf(s,"%ld",count);
		for (i=0;i<strlen(s);i++) 
			printf("<img src=\"/icons/%1d%c.gif\">",typeface,s[i]);
	}
}



