/*****************************************************************************
 *  Backup Copy                                                              *
 *  Programmed by: Kevin Lindsay                                             *
 *  Copyright (c) 1998 NetNation Communications Inc                          *
 * ***************************************************************************/ 

#include <string.h>
#include <stdlib.h>

/* Changes all oldstring to newstring in a variable */
char *
repasubstr(char *oldstr, char *newstr, char *line)
{
   char *newline;
   int osnum = 0;
   char *ptr1;
   char *ptr2;
   int i;
   
   
   ptr1 = line;
   
   while ((ptr1 = (char *)strstr(ptr1,oldstr)) != NULL) {
      ptr1 += strlen(oldstr);
      osnum++;
   }
   
   if (osnum > 0) {
      
      newline = (char *) malloc((strlen(line)-(strlen(oldstr)*osnum))+(strlen(newstr)*osnum)+5);
      
      newline[0] = '\0';
      
      ptr1 = line;
      
      for (i = 0; i < osnum; i++) {
         ptr2 = (char *)strstr(ptr1,oldstr);
         strncat(newline,ptr1,ptr2-ptr1);
         strcat(newline,newstr);
         ptr1 = ptr2+strlen(oldstr);
      }
      
      ptr2 = line+strlen(line);
      
      strncat(newline,ptr1,ptr2-ptr1);
      
      free(line);
      
      return(newline);
   } else {
      return(line);
   }
}


syntax highlighted by Code2HTML, v. 0.9.1