#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "vars.h"


/**************************************
 ******** Users change in url *********/
int raw_change(void)
{
 int i,next_flag;
 char *k,*k1,*k2,*k3;


 #ifdef DEBUG_RAW_CHANGE
  fprintf(stderr,"Check url to change: %s\n",input_url);  
 #endif

 for (i=0;i<change_count;i++)
 {
  next_flag=1;

 while (next_flag)
 {
  k=strstr(input_url,change_from[i]);
  if (k)
  {
   #ifdef DEBUG_RAW_CHANGE
    fprintf(stderr," to change found: %s -> %s\n",change_from[i],change_to[i]);  
   #endif

  // Prepare pointer
  k1=k+strlen(change_from[i]);
  k2=k+strlen(change_to[i]);
  k3=strchr(k1,0);

  //Check to buffer size
  if (k3-input_url+k2-k1>ML_URL) return(-1);

 if (raw_change_log_flag) fprintf(change_log,"%s RAW_CHG: %d.%d.%d.%d %s %s", now(),(unsigned char)input_ip[0],(unsigned char)input_ip[1],(unsigned char)input_ip[2],(unsigned char)input_ip[3],input_ident,input_url);

  //Change
  memmove(k2,k1,k3-k1);
  memcpy(k,change_to[i],strlen(change_to[i]));
  input_url[k3-input_url+k2-k1]=0;

 if (raw_change_log_flag)
   {
    fprintf(change_log," -> %s\n", input_url);
    fflush (change_log);
    }

  #ifdef DEBUG_RAW_CHANGE
   fprintf(stderr," url after change: %s\n",input_url);
  #endif
  }
  else next_flag=0;
 }
 }

return(0);
}
