File: misc.c

package info (click to toggle)
cpbk 2.0-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 248 kB
  • ctags: 168
  • sloc: ansic: 2,193; makefile: 114; sh: 35
file content (53 lines) | stat: -rw-r--r-- 1,339 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*****************************************************************************
 *  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);
   }
}