⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hitsubs.c

📁 this gives details of the network programming
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <time.h>#include <sys/types.h>#include "lock.h"/* Subroutines for managing the hitcount list *//* We keep track of the hitcount in a file names hitcount */char *hitfile = "hitcount";/* The format of the countfile is this:   1 line of ascii text for each day recorded.   each line contains the date as 3 integers mm dd yyyy followed by   24 integers, one for each hour (the number of hits for each hour).   there are spaces seperating each of these values.   Here is an example:4 16 1998 0 30 10101 22 28 0 0 0 0 0 0 0 0 1 3 4 7 8 9 6 5 2 3 4on 4/16/1998 there were 30 hits between 1 and 2 AM, 10,101 between 2 and 3 AM,and so on...*/typedef struct hitrec {  int mon,day,year;  int hours[24];} hitrec;#define MAXDAYS 1000	/* maximum number of days we keep track of */typedef hitrec hitlist[MAXDAYS];/* parse_countline  parses a single line of text and fills in  a hitrec; Return value is 1 if successful, 0 if not.  NOTE: the line is corrupted, strtok is used!*/int parse_countline( char *s, hitrec *h) {  int i;  char *ptr;  if (! (ptr = strtok(s," "))) return(0);  h->mon = atoi(ptr);  if (! (ptr = strtok(NULL," "))) return(0);  h->day = atoi(ptr);  if (! (ptr = strtok(NULL," "))) return(0);  h->year = atoi(ptr) ;  i=0;  while ((i<24)&&(ptr)) {    if (! (ptr = strtok(NULL," "))) return(0);    h->hours[i] = atoi(ptr);    i++;	  }	printf("GOT DATE AS %d %d %d\n",h->mon, h->day, h->year);  /* as long as i is 24 - we got a good line */  /* (we assume any date is OK) */  if (i!=24)    return(0);printf("Valid Stuff\n");  return(1);}hitlist hl;int n_days;#define MAXLINE 1000/* reads the hit count file and returns 1 on success, 0 on error *//* fills in the array hl with the records from the file */int read_countfile( FILE *f) {  char aline[MAXLINE];    n_days=0;  while (fgets(aline,MAXLINE,f)) {    if (!parse_countline(aline,&hl[n_days])) {      printf("Error parsing the hit file\n");      return(1);    } else {      n_days++;    }  }	  return(1);}/* writes the count file */void write_countfile( FILE *f) {  int i,j;  printf("Writing the countfile - ndays is %d\n",n_days);  for (i=0;i<n_days;i++) {    fprintf(f,"%d %d %d",hl[i].mon, hl[i].day, hl[i].year);    printf("%d %d %d",hl[i].mon, hl[i].day, hl[i].year);    for (j=0;j<24;j++) {      fprintf(f, " %d",hl[i].hours[j]);      printf(" %d",hl[i].hours[j]);    }    fprintf(f,"\n");    printf("\n");  }}int find_day( int mon, int day, int year) {  int i=0;  while ((i<MAXDAYS) && (i<n_days) && 	 ((hl[i].mon!=mon) || (hl[i].day!=day) || (hl[i].year!=year)))    i++;  return(i);}/* update the hitlist to reflect the new hit corresponding to the   date and time specified*/void update_hits(int mon, int day, int year, int hour) {  int recnum;  int j;  recnum = find_day(mon,day,year);  if ((n_days==0) || (recnum==MAXDAYS)) {    /* first record or no room left - overwrite the first record */    hl[0].mon=mon;    hl[0].day=day;    hl[0].year=year;    for (j=0;j<24;j++) {	hl[0].hours[j]=0;    }     hl[0].hours[hour]++;    /* if this is the first record - update the number of records */     if (n_days==0)      n_days++;  } else {    hl[recnum].hours[hour]++;  }}/* get the histlist for each hour on a specific day */int *hits_for_date(int mon, int day, int year) {  int recnum;  if (n_days==0)    return(NULL);  if ((recnum = find_day(mon,day,year))==MAXDAYS)    return(NULL);    return(hl[recnum].hours);} /* get the total number of hits */int totalhits( void ) {  int i,j,total;    total=0;  for (i=0;i<n_days;i++) {    for (j=0;j<24;j++) {      total+=hl[i].hours[j];    }  }  return(total);}/* hitme updates the hit count file with a new hit *//* returns 1 on success, 0 on error  and hiterrorstr() will   return an error string */int hiterror=0;/* these are the errors */enum { EHIT_NOERR=0, EHIT_HITFILE, EHIT_LOCK, EHIT_MAX };char *hiterrors[] = {  "No Problem (shouldn't see this !)",  "Problem with the hit database",  "Problem locking the hit database" };char *hiterrorstr(void) {  if ((hiterror<0)||(hiterror>=EHIT_MAX))    hiterror=0;  return(hiterrors[hiterror]);}int hit_me() {  FILE *f,*lock;  struct tm *timerec;  time_t t;  /* get the current time and date */  t = time(NULL);  timerec = localtime( &t );  /* Open the lock file and lock it */  if ((lock = fopen(".lock","r+"))==NULL) {    hiterror = EHIT_LOCK;    return(0);  }  /* lock the lock file so nobody else is mucking with it */    if (lock_fd(fileno(lock))!=0) {    hiterror = EHIT_LOCK;    return(0);  }  /* now we need to update the hitfile */  /* if file doesn't exist - create it */  if ((f = fopen(hitfile,"r+"))==NULL) {     if ((f=fopen(hitfile,"w+"))==NULL) {      hiterror = EHIT_HITFILE;      return(0);    }  }   if (!read_countfile(f)) {    printf("Error reading the file\n");    hiterror = EHIT_HITFILE;    return(0);  }  /* update the hit records to include our hit */  update_hits(timerec->tm_mon+1, timerec->tm_mday, 	      timerec->tm_year+1900, timerec->tm_hour);  /* and write the update to disk */  fclose(f);  if ((f=fopen(hitfile,"w+"))==NULL) {    hiterror = EHIT_HITFILE;    return(0);  }  write_countfile(f);  /* release the file */    if (unlock_fd(fileno(lock))<0) {    hiterror = EHIT_LOCK;    return(0);  }    fclose(f);  /* everything is OK */  return(1);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -