/**************************** file: count.c Written by Keith Oxenrider October 25, 1998 For use on the web as a cgi program. Is activated by calling the program as an image, i.e., IMG SRC="/cgi-bin/count.cgi?countdatafile". to increment counter without having a visible display, invoke as follows: IMG SRC="/cgi-bin/count.cgi?countdatafile" HEIGHT="0" WIDTH="0" If the last character in the input string is an '!' then the counter will not be incremented, just displayed. The program will automagically create a new count file if the filename selected does not exist. There is a companion program called 'view_counts' that is used to look at all the counter stored along with the last access time. Modified to include date/time stamp on 3/1/1999. Security fix on 11/18/2003 to eliminate directory transversals. ****************************/ #include #include #include #include #include #include #define TRUE 1 #define FALSE 0 #define MAXLINELENGTH 1024 /* hardcoded path to directory that contains data files */ #ifdef WIN32 char m_strPath[] = "countdata\\"; #else /* presumed *nix */ char m_strPath[] = "countdata/"; #endif char *digits[] ={ "0xff","0xff","0xff","0xc3","0x99","0x99","0x99","0x99", "0x99","0x99","0x99","0x99","0xc3","0xff","0xff","0xff", "0xff","0xff","0xff","0xcf","0xc7","0xcf","0xcf","0xcf", "0xcf","0xcf","0xcf","0xcf","0xcf","0xff","0xff","0xff", "0xff","0xff","0xff","0xc3","0x99","0x9f","0x9f","0xcf", "0xe7","0xf3","0xf9","0xf9","0x81","0xff","0xff","0xff", "0xff","0xff","0xff","0xc3","0x99","0x9f","0x9f","0xc7", "0x9f","0x9f","0x9f","0x99","0xc3","0xff","0xff","0xff", "0xff","0xff","0xff","0xcf","0xcf","0xc7","0xc7","0xcb", "0xcb","0xcd","0x81","0xcf","0x87","0xff","0xff","0xff", "0xff","0xff","0xff","0x81","0xf9","0xf9","0xf9","0xc1", "0x9f","0x9f","0x9f","0x99","0xc3","0xff","0xff","0xff", "0xff","0xff","0xff","0xc7","0xf3","0xf9","0xf9","0xc1", "0x99","0x99","0x99","0x99","0xc3","0xff","0xff","0xff", "0xff","0xff","0xff","0x81","0x99","0x9f","0x9f","0xcf", "0xcf","0xe7","0xe7","0xf3","0xf3","0xff","0xff","0xff", "0xff","0xff","0xff","0xc3","0x99","0x99","0x99","0xc3", "0x99","0x99","0x99","0x99","0xc3","0xff","0xff","0xff", "0xff","0xff","0xff","0xc3","0x99","0x99","0x99","0x99", "0x83","0x9f","0x9f","0xcf","0xe3","0xff","0xff","0xff" }; /* this will display all zeros in the event that an error is found if you get all zeros, there is a bug either in the code or in the parameters given */ void dumpDigits(const char * number){ int len = strlen(number); char cc[] = "0"; int x, y, c; printf ("Content-type: image/x-xbitmap\n\n"); printf ("#define count_width %d\n", len * 8); printf ("#define count_height 16\n"); printf ("static char count_bits[] = {\n"); for (x=0; x<16; x++){ for (y=0; y= MAXLINELENGTH){ dumpDigits("0000000000"); return 0; } /*check for invalid characters (thanks infamous42md!)*/ for (i=0; i10) /* hard-coded for 10 digits, must change if you want more */ wipe = TRUE; if (wipe){ strcpy(numb, "0"); break; } } count = atol(numb); if (count >= LONG_MAX)/*if the value rolls over, reset to zero*/ count = 0; if (write){/* write new counter data to file */ count++; fin = fopen(filename, "w"); if (fin){ fprintf(fin, "%ld", count); /* get the time and format to look nice*/ time(&timer); time_date = localtime(&timer); fprintf(fin, " %02d/%02d/%d %02d:%02d:%02d", time_date->tm_mon +1, time_date->tm_mday, time_date->tm_year+1900, time_date->tm_hour, time_date->tm_min, time_date->tm_sec); fclose(fin); }else{ dumpDigits("0000000000"); } } sprintf(numb, "%ld", count); dumpDigits(numb); /* should never get here, exit is called from dumpDigits() */ return 1; }/* end main */