httpd-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mystic Zen Biker Twigboy <ala...@babcom.com>
Subject [PATCH] mod_include.c SSI #counter for v1.2.6
Date Wed, 05 Aug 1998 16:04:35 GMT
*** mod_include.c.orig	Tue Feb  3 02:00:49 1998
--- mod_include.c	Mon Mar 30 01:53:45 1998
***************
*** 95,100 ****
--- 95,102 ----
  #define DEFAULT_TIME_FORMAT "%A, %d-%b-%Y %H:%M:%S %Z"
  #define SIZEFMT_BYTES 0
  #define SIZEFMT_KMG 1
+ #define PRINT 1
+ #define NOPRINT 0
  
  
  static void safe_copy(char *dest, const char *src, size_t max_len)
***************
*** 2009,2014 ****
--- 2011,2137 ----
  }
  
  
+ int lock_counter( int fd )
+ {
+   int ret;
+   struct flock lock = { F_WRLCK, 0, 0, 0 };
+   
+   while ((ret = fcntl(fd, F_SETLKW, &lock)) < 0 && errno == EINTR)
+     continue;
+   
+   return(ret);
+ }
+ 
+ 
+ int unlock_counter( int fd )
+ {
+   struct flock unlock = { F_UNLCK, 0, 0, 0 };
+   return(fcntl(fd, F_SETLKW, &unlock));
+ }
+ 
+ 
+ int handle_counter(FILE *in, request_rec *r, char *error, int print)
+ {
+   char tag[MAX_STRING_LEN];
+   char *tag_val;
+   
+   while(1) {
+     if(!(tag_val = get_tag (r->pool, in, tag, MAX_STRING_LEN, 1)))
+       return 1;
+     
+     if(!strcmp(tag,"file"))
+       {
+ 	char *count_fmt = NULL;
+ 	char *error_fmt = NULL;
+ 	char *counter = NULL;
+ 	char tmp[MAX_STRING_LEN+2];
+ 	char dir[MAX_STRING_LEN];
+ 	int count;
+ 	int counter_fd;
+ 	FILE * fp;
+ 	long fsize;
+ 	long offset;
+ 	
+ 	getparents(tag_val);
+ 	getwd(dir);
+ 	counter = make_full_path (r->pool, dir, tag_val);
+ 
+ 	/* Open counter file for reading */
+ 	
+ 	if (fp = fopen(counter, "r+")) {
+ 	  if (lock_counter(fileno(fp))) {
+ 	    fclose(fp);
+ 	    error_fmt = "could not lock counter file %s in parsed file %s";
+ 	  }
+ 	}
+ 	else {
+ 	  error_fmt = "could not open counter file %s for reading in parsed file %s";
+ 	}
+ 	
+ 	if (!error_fmt) {
+ 	  fseek(fp, 0, SEEK_END);
+ 	  fsize = ftell(fp);
+ 	  fseek(fp, 0, SEEK_SET);
+ 	  if (fsize) {
+ 	    fseek(fp, 0, SEEK_SET);
+ 	    if (fscanf(fp, "%d", &count) != 1)
+ 	      error_fmt = "file %s in parsed file %s does not look like a counter";
+ 	    fseek(fp, 0, SEEK_SET);
+ 	  }
+ 	  else {
+ 	    count = 0;
+ 	  }
+ 	  
+ 	  if (!error_fmt) {
+ 	    count++;
+ 	    switch (count % 10)
+ 	      {
+ 	    case 1:
+ 	      count_fmt = "%dst";
+ 	      break;
+ 	    case 2:
+ 	      count_fmt = "%dnd";
+ 	      break;
+ 	    case 3:
+ 	      count_fmt = "%drd";
+ 	      break;
+ 	    default:
+ 	      count_fmt = "%dth";
+ 	      break;
+ 	      }
+ 	    
+ 	    sprintf (tmp, count_fmt, count);
+ 	    if (print)
+ 	      rputs(tmp, r);
+ 	    
+ 	    /* reopen counter file for writing */
+ 	    if (!(fp = fopen(counter, "w")))
+ 	      error_fmt = "could not reopen counter file %s for writing in parsed file %s";
+ 	    
+ 	    if (!error_fmt)
+ 	      fprintf (fp, "%d\n", count);
+ 	  }
+ 	  
+ 	  unlock_counter(fileno(fp));
+ 	  fclose(fp);
+ 	}
+ 	
+ 	if (error_fmt) {
+ 	  log_printf(r->server, error_fmt, tag_val, r->filename);
+ 	  rputs(error, r);
+ 	}            
+ 	
+       }
+     else if (!strcmp(tag,"done"))
+       return 0;
+     else {
+       log_printf(r->server, "unknown parameter %s to tag counter/counternp in %s",
+                  tag, r->filename);
+       rputs(error, r);
+     }
+   }
+ }
+ 
  
  /* -------------------------- The main function --------------------------- */
  
***************
*** 2124,2129 ****
--- 2247,2258 ----
              else if (!strcmp(directive, "printenv")) {
                  ret = handle_printenv(f, r, error);
              }
+ 	    else if (!strcmp(directive, "counter")) {
+ 	        ret = handle_counter(f, r, error, PRINT);
+ 	    }
+ 	    else if (!strcmp(directive, "counternp")) {
+ 	        ret = handle_counter(f, r, error, NOPRINT);
+ 	    }
  #ifdef USE_PERL_SSI
              else if (!strcmp(directive, "perl")) {
                  ret = handle_perl(f, r, error);

Mime
View raw message