comparison rcg.c @ 7:0b9cf47655fe

bugfixes
author paulo@hit-nxdomain.opendns.com
date Thu, 06 May 2010 23:15:40 -0700
parents 910e059abd6d
children 5ab8d6c1a37c
comparison
equal deleted inserted replaced
5:ab7fda588dae 6:2bc53e45316e
81 81
82 size_t CLR_START_len = strlen(CLR_START); 82 size_t CLR_START_len = strlen(CLR_START);
83 size_t CLR_END_len = strlen(CLR_END); 83 size_t CLR_END_len = strlen(CLR_END);
84 size_t CLR_CLEAR_len = strlen(CLR_CLEAR); 84 size_t CLR_CLEAR_len = strlen(CLR_CLEAR);
85 85
86 char *out = NULL;
87
86 while (fgets(buf, BUFSIZE, stdin)) 88 while (fgets(buf, BUFSIZE, stdin))
87 { 89 {
88 int so = -1; 90 int so = -1;
89 int eo = -1; 91 int eo = -1;
90 92
91 size_t out_len = strlen(buf) + 1; 93 size_t out_len = strlen(buf) + 1;
92 char *out = calloc(out_len, sizeof(char));
93 unsigned int out_pos = 0; 94 unsigned int out_pos = 0;
94 unsigned int buf_pos = 0; 95 unsigned int buf_pos = 0;
95 96
96 for (re_err = 0; re_err != REG_NOMATCH;) 97 for (re_err = 0; re_err != REG_NOMATCH;)
97 { 98 {
100 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA 101 //fprintf(stderr, "%s", &buf[buf_pos]); //d// 20100405 PBA
101 102
102 if (out_len >= (BUFSIZE - buf_pos)) 103 if (out_len >= (BUFSIZE - buf_pos))
103 out_len = (BUFSIZE - buf_pos) - 1; 104 out_len = (BUFSIZE - buf_pos) - 1;
104 105
106 out = (char *)realloc(out, out_len*sizeof(char));
107 if (!out)
108 exit(realloc_error());
109
105 so = rem[0].rm_so; 110 so = rem[0].rm_so;
106 eo = rem[0].rm_eo; 111 eo = rem[0].rm_eo;
112 int match_len = eo - so;
107 113
108 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA 114 //fprintf(stderr, "%d %d \n", so, eo); //d// 20100327 PBA
109 115
110 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0) 116 if (re_err != REG_NOMATCH && so >= 0 && eo >= 0 && match_len > 0)
111 { 117 {
112 int match_len = eo - so;
113 out_len += CLR_START_len + CLR_END_len + CLR_CLEAR_len; 118 out_len += CLR_START_len + CLR_END_len + CLR_CLEAR_len;
114 out = (char *)realloc(out, out_len*sizeof(char)); 119 out = (char *)realloc(out, out_len*sizeof(char));
115 if (!out) 120 if (!out)
116 exit(realloc_error()); 121 exit(realloc_error());
117 strncpy(&out[out_pos], &buf[buf_pos], so); 122 strncpy(&out[out_pos], &buf[buf_pos], so);
129 buf_pos += match_len; 134 buf_pos += match_len;
130 } 135 }
131 else 136 else
132 { 137 {
133 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos); 138 strncpy(&out[out_pos], &buf[buf_pos], out_len - out_pos);
134 out[out_len] = '\0'; 139 out[out_len - 1] = '\0';
135 } 140 }
136 } 141 }
137 142
138 fputs(out, stdout); 143 fputs(out, stdout);
139 free(out);
140 } 144 }
141 145
142 return EXIT_OK; 146 return EXIT_OK;
143 } 147 }