/**************************** view_counts.c Written by Keith Oxenrider March 1, 1999 For use on the web as a cgi program to display all counters for site as generated by the counts.c program. Is activated by calling the program from the HTML browser command line with the (optional) name of the web site for a title: /cgi-bin/view_counts.cgi?sol-biotech.com ****************************/ #include #include #include #include #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 int main(){ FILE *pipe = NULL, *fin; long counts; int i, len; char *sitename, input[MAXLINELENGTH]; char filename[MAXLINELENGTH]; char datetime[MAXLINELENGTH]; char pipeCommand[MAXLINELENGTH]; struct tm *time_date; time_t timer; printf("Content-type: text/html\n\n"); sitename = getenv("QUERY_STRING"); if (sitename){ printf("Page Hit Data for %s\n", sitename); printf("

Page Hit Counts and Date/Time of Last Hit for %s

\n", sitename); }else{ printf("Page Hit Data\n"); printf("

Page Hit Counts and Date/Time of Last Hit

\n"); } /* get the time and format to look nice*/ time(&timer); time_date = localtime(&timer); printf("Current Date and Time: %02d/%02d/%d %02d:%02d:%02d

\n", 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); #ifdef WIN32 strncpy(pipeCommand, "dir /b ", MAXLINELENGTH); strncat(pipeCommand, m_strPath, MAXLINELENGTH); if ((pipe = _popen(pipeCommand, "r")) == NULL){ #else /* presumed *nix */ strncpy(pipeCommand, "ls -1 ", MAXLINELENGTH); strncat(pipeCommand, m_strPath, MAXLINELENGTH); if ((pipe = popen(pipeCommand, "r")) == NULL){ #endif printf("Unable to open pipe to count files directory: %s!\n", pipeCommand); return 1; }else{ printf("\n"); printf(" "); printf(" "); printf("\n"); while (fgets(input, MAXLINELENGTH, pipe)){ printf("\n"); len = strlen(input); /* remove possible line feed at end of name */ if (input[len-1] == '\n') input[len-1] = '\0'; strncpy(filename, m_strPath, MAXLINELENGTH); strncat(filename, input, MAXLINELENGTH); /* open the file that holds numeric counter value */ if ((fin = fopen(filename, "r")) == NULL) printf("\n", filename); else{ fgets(datetime, MAXLINELENGTH, fin); fclose(fin); sscanf(datetime, "%ld", &counts); i = 0; while (datetime[i] != ' ' && datetime[i] != '\0') i++; len = strlen(&datetime[i]); if (len < 2) printf("\n", counts, input); else printf("\n", counts, &datetime[i], input); } printf(""); } printf("
Hits:Date/Time of Last Hit:Name of input file:
Unable to open file: %s%ldNone Available%s%ld%s%s

"); } printf("\n"); #ifdef WIN32 _pclose(pipe); #else /* presumed *nix */ pclose(pipe); #endif return 0; }/*end main*/