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.3.1
Date Wed, 05 Aug 1998 16:04:50 GMT
*** mod_include.c.orig	Fri Jul 24 14:50:22 1998
--- mod_include.c	Fri Jul 24 15:22:38 1998
***************
*** 100,105 ****
--- 100,107 ----
  #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
  #ifdef CHARSET_EBCDIC
  #define RAW_ASCII_CHAR(ch)  os_toebcdic[(unsigned char)ch]
  #else /*CHARSET_EBCDIC*/
***************
*** 2072,2077 ****
--- 2074,2202 ----
  }
  
  
+ static 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);
+ }
+ 
+ 
+ static int unlock_counter( int fd )
+ {
+   struct flock unlock = { F_UNLCK, 0, 0, 0 };
+   return(fcntl(fd, F_SETLKW, &unlock));
+ }
+ 
+ 
+ static int handle_counter(FILE *in, request_rec *r, const 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;
+ 	
+ 	ap_getparents(tag_val);
+ 	getwd(dir);
+ 	counter = ap_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)
+ 	      ap_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) {
+ 	  ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, r->server, 
+ 		       error_fmt, tag_val, r->filename);
+ 	  ap_rputs(error, r);
+ 	}
+ 	
+       }
+     else if (!strcmp(tag,"done"))
+       return 0;
+     else {
+       ap_log_error(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR,
+ 		   "unknown parameter %s to tag counter/counternp in %s",
+ 		   tag, r->filename);
+       ap_rputs(error, r);
+     }
+   }
+ }
+ 
  
  /* -------------------------- The main function --------------------------- */
  
***************
*** 2189,2194 ****
--- 2314,2325 ----
              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