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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#include <stdio.h>
#include <stdlib.h>
// https://www.programmingsimplified.com/c/source-code/c-program-insert-substring-into-string
//
char *substring(char *string, int position, int length)
{
char *pointer;
int c;
pointer = malloc(length+1);
if( pointer == NULL )
exit(EXIT_FAILURE);
for( c = 0 ; c < length ; c++ )
*(pointer+c) = *((string+position-1)+c);
*(pointer+c) = '\0';
return pointer;
}
void insert_substring(char *a, char *b, int position)
{
char *f, *e;
int length;
length = strlen(a);
f = substring(a, 1, position - 1 );
e = substring(a, position, length-position+1);
strcpy(a, "");
strcat(a, f);
free(f);
strcat(a, b);
strcat(a, e);
free(e);
}
int main(int argc, char **argv)
{
char col_start[]="\033[31;40m";
char col_end[]="\033[37;40m";
FILE *in=stdin;
FILE *out=stdout;
int i=1;
char buf[256];
char buf2[255];
while(NULL!=fgets(buf,255,in))
{
char *pos=strstr(buf,argv[1]);
if(pos)
{
int p=pos-buf+1;
int l=strlen(argv[1])+strlen(col_start);
insert_substring(buf,col_start,p);
insert_substring(buf,col_end,p+l);
printf("%s",buf);
}
i++;
}
return EXIT_SUCCESS;
}
|