PERFORCE change 143448 for review
Gabor Kovesdan
gabor at FreeBSD.org
Sat Jun 14 17:33:11 UTC 2008
http://perforce.freebsd.org/chv.cgi?CH=143448
Change 143448 by gabor at gabor_server on 2008/06/14 17:32:44
- Slight changes to the --color code, -o --color works now,
--color in itself is still broken.
- Use strlcpy instead of strncpy and use asprintf instead of
sprintf. [1]
Pointed out by: fjoe (soc-mentor)
Affected files ...
.. //depot/projects/soc2008/gabor_textproc/grep/util.c#15 edit
Differences ...
==== //depot/projects/soc2008/gabor_textproc/grep/util.c#15 (text+ko) ====
@@ -224,36 +224,37 @@
if (pmatch.rm_so != 0 || pmatch.rm_eo != l->len)
r = REG_NOMATCH;
}
- if ((r == 0) && (color != NULL)) {
+ if ((r == 0) && (color != NULL) && !oflag) {
/* XXX: this color stuff does not work yet :( */
- if (oflag) {
- char *matched, *tmp;
- matched = malloc((pmatch.rm_eo - pmatch.rm_so + 2) * sizeof(char));
- strncpy(matched, &(l->dat[pmatch.rm_so]), pmatch.rm_eo - pmatch.rm_so + 1);
- tmp = malloc((strlen(matched) + 50) * sizeof(char));
- sprintf(tmp, "\33[%sm%s\33[00m", color, matched);
- } else {
- char *tmp, *begin, *matched, *end;
- begin = malloc(strlen(l->dat) - pmatch.rm_so + 1);
- matched = malloc((pmatch.rm_eo - pmatch.rm_so + 2) * sizeof(char));
- end = malloc(strlen(l->dat) - pmatch.rm_eo);
- strncpy(begin, l->dat, pmatch.rm_so);
- strncpy(matched, &(l->dat[pmatch.rm_so]), pmatch.rm_eo - pmatch.rm_so + 1);
- strncpy(end, &(l->dat[pmatch.rm_eo]), strlen(l->dat) - strlen(begin) - strlen(matched));
- tmp = malloc((strlen(l->dat) + 50) * sizeof(char));
- sprintf(tmp, "%s\33[%sm%s\33[00m%s", begin, color, matched, end);
- free(begin);
- free(matched);
- free(end);
- l->dat = tmp;
- }
+ char *tmp, *begin, *matched, *end;
+
+ begin = malloc(strlen(l->dat) - pmatch.rm_so + 2);
+ matched = malloc((pmatch.rm_eo - pmatch.rm_so + 1) * sizeof(char));
+ end = malloc(strlen(l->dat) - pmatch.rm_eo + 1);
+
+ strlcpy(begin, l->dat, pmatch.rm_so + 1);
+ strlcpy(matched, &(l->dat[pmatch.rm_so]), pmatch.rm_eo - pmatch.rm_so + 1);
+ strlcpy(end, &(l->dat[pmatch.rm_eo]), strlen(l->dat) - strlen(begin) - strlen(matched) + 1);
+ asprintf(&tmp, "%s\33[%sm%s\33[00m%s", begin, color, matched, end);
+
+ free(begin);
+ free(matched);
+ free(end);
+
+ l->dat = tmp;
}
- if (r == 0 && oflag && (color == NULL)) {
- char *tmp;
- if ((tmp = malloc((pmatch.rm_eo - pmatch.rm_so + 2) * sizeof(char))) == NULL)
+ if (r == 0 && oflag) {
+ char *tmp, *matched;
+
+ if ((matched = malloc((pmatch.rm_eo - pmatch.rm_so + 2) * sizeof(char))) == NULL)
errx(2, NULL);
- strncpy(tmp, &(l->dat[pmatch.rm_so]), pmatch.rm_eo - pmatch.rm_so + 1);
- tmp[pmatch.rm_eo - pmatch.rm_so] = '\0';
+ strlcpy(matched, &(l->dat[pmatch.rm_so]), pmatch.rm_eo - pmatch.rm_so + 1);
+
+ if (color != NULL)
+ asprintf(&tmp, "\33[%sm%s\33[00m", color, matched);
+ else
+ tmp = matched;
+
l->dat = tmp;
l->len = strlen(l->dat);
}
More information about the p4-projects
mailing list