/* count4.c Usage: count2 Most likely usage: by Rimas Remeza Version 0.0.3 Created: 08/03/95 modified by Ivans Chou Updated: 04/18/96 Count2 will read a number from text file, increment it, write the new number to the text file, and return the new number as a GIF file. This is the pretty version of my count.c. I still like to keep count.c around because the GIF it produces is much smaller. (Although, the output from count2.c is still less then 4k when using the default font.) You will need the gd1.1.1 library (http://siva.cshl.org/gd/gd.html) to make this exe. An additional feature has been added to count3. The count file will consist of two or three lines now. The second is the hostname of the referring document and the third is the document name. This will limit access to your counter to prevent anyone from "stealing" it. ------------------------- gd 1.1.1 is copyright 1994, Quest Protein Database Center, Cold Spring Harbor Labs. ------------------------- This is freeware, I think the comments are longer then the code. If you use it just drop me an e-mail so I can know how much demand there is to keep developing this thing, and so I can feel good about my minor contributions. I've only tested this with CERN server, but it should work with almost any server. I've only compiled this under SUN4 and WinNT, but it should work under most compilers. Look at the gd1.1.1 Makefile for help with compiling and linking. */ #include #include #include #include #include "gd.h" #include "gdfontl.h" #include "gdfonts.h" #include #include #include #include /* Look for the file holding the count in this directory: */ #define DATA_DIR "/home/trinity/www/cgi-bin/gif-counter/counters/" /* The font GIF to use: */ #define FONT_FILE "/home/trinity/www/cgi-bin/gif-counter/fonts/digits2.gif" #define FONT_DIR "/home/trinity/www/cgi-bin/gif-counter/fonts/" void errorbox(char *message) { gdImagePtr im_out; int bg_color; int fore_color; printf("Content-type: image/gif%c%c",10,10); im_out = gdImageCreate(strlen(message)*6+10, 13); bg_color = gdImageColorAllocate(im_out, 255, 255, 255); gdImageColorTransparent(im_out, bg_color); fore_color = gdImageColorAllocate(im_out, 0, 0, 255); gdImageString(im_out, gdFontSmall, 4, 1, message, fore_color); gdImageGif(im_out, stdout); gdImageDestroy(im_out); } int main(int argc, char *argv[]) { char data_dir[180] = DATA_DIR; char *full_path; char font_file[30] = ""; char font_path[180] = ""; char error_out[200] = ""; char file_out[256] = ""; char* outp = NULL; int outcount = 0; char referer[200] = ""; char allow_host[200] = ""; char allow_doc[200] = ""; int sizex, sizey, num_digits=8; float incx; /* Output image */ gdImagePtr im_out, im_in; FILE *fp; int fd; unsigned int access_count; char count_string[11] = "\0\0\0\0\0\0\0\0\0\0"; char template_s[11]= "0000000000"; int i, k; int bg_color; char c; /* first argument is the counter file */ if(argc<2) { errorbox("File name not specified!"); return(1); } full_path = strcat(data_dir, argv[1]); /* second argument is the font file */ if(argc == 3) { strcpy(font_file,argv[2]); strcat(font_path,FONT_DIR); strcat(font_path,font_file); if(strstr(font_file,".gif")==NULL) strcat(font_path,".gif"); } else { strcat(font_path,FONT_FILE); } /* open font file and load into Gif */ fp = fopen(font_path, "rb"); if (!fp) { fp = fopen(FONT_FILE, "rb"); if (!fp) { errorbox("Can't load digit image!"); return(1); } else { im_in = gdImageCreateFromGif(fp); fclose(fp); } } else { im_in = gdImageCreateFromGif(fp); fclose(fp); } if(getenv("HTTP_REFERER")!=NULL) strcpy(referer,getenv("HTTP_REFERER")); sizex = gdImageSX(im_in); sizey = gdImageSY(im_in); incx = (float) sizex / 10; /* Create output image */ im_out = gdImageCreate((int)(incx*num_digits), sizey); /* Get the current count */ fp = fopen(full_path,"r"); if (!fp) { strcpy(error_out, "Can't find file: "); strcat(error_out, full_path); errorbox(error_out); return(1); } c=fgetc(fp); /* Get the current count */ for (i=0; i<10, c!='\n'; i++, c=fgetc(fp)) count_string[i] = c; fscanf(fp,"%s\n",&allow_host); fscanf(fp,"%s\n",&allow_doc); fclose(fp); if(strlen(referer)) { if(strlen(allow_host)) if(strstr(referer,allow_host)==NULL) { strcpy(error_out, "Unauthorized referring host: "); strcat(error_out, referer); errorbox(error_out); return(1); } if(strlen(allow_doc)) if(strstr(referer,allow_doc)==NULL) { strcpy(error_out, "Unauthorized referring document: "); strcat(error_out, referer); errorbox(error_out); return(1); } } /* Increment the count and write it back to the file */ if (count_string[0]==0) { access_count=1; count_string[0]='1'; } else { sscanf(count_string,"%u",&access_count); access_count++; } /* fp = fopen(full_path,"w"); */ /* use exclusive locking */ fd = open(full_path, O_WRONLY); lockf(fd, F_LOCK, 0); if (fd < 0) { strcpy(error_out, "Can't open file: "); strcat(error_out, full_path); strcat(error_out, ". Contact ichou@bih.harvard.edu."); errorbox(error_out); return(1); } sprintf(file_out,"%d\n%s\n%s\n",access_count,allow_host,allow_doc); outp = file_out; while(outcount < strlen(outp)) { outcount += write(fd, outp, strlen(outp)); outp+= outcount; } lockf(fd, F_ULOCK, 0); close(fd); /* fprintf(fp,"%d\n%s\n%s\n",access_count,allow_host,allow_doc); fclose(fp); */ /* Put formated string in output buffer */ for (i=num_digits-strlen(count_string), k=0; i