diff options
Diffstat (limited to 'sed.c')
-rw-r--r-- | sed.c | 10 |
1 files changed, 6 insertions, 4 deletions
@@ -490,7 +490,7 @@ static void load_cmd_file(char *filename) | |||
490 | } | 490 | } |
491 | } | 491 | } |
492 | 492 | ||
493 | static void print_subst_w_backrefs(const char *line, const char *replace, regmatch_t *regmatch) | 493 | static void print_subst_w_backrefs(const char *line, const char *replace, regmatch_t *regmatch, int matches) |
494 | { | 494 | { |
495 | int i; | 495 | int i; |
496 | 496 | ||
@@ -506,8 +506,9 @@ static void print_subst_w_backrefs(const char *line, const char *replace, regmat | |||
506 | tmpstr[1] = 0; | 506 | tmpstr[1] = 0; |
507 | backref = atoi(tmpstr); | 507 | backref = atoi(tmpstr); |
508 | /* print out the text held in regmatch[backref] */ | 508 | /* print out the text held in regmatch[backref] */ |
509 | for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++) | 509 | if (backref <= matches && regmatch[backref].rm_so != -1) |
510 | fputc(line[j], stdout); | 510 | for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++) |
511 | fputc(line[j], stdout); | ||
511 | } | 512 | } |
512 | 513 | ||
513 | /* if we find a backslash escaped character, print the character */ | 514 | /* if we find a backslash escaped character, print the character */ |
@@ -555,7 +556,8 @@ static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line) | |||
555 | fputc(hackline[i], stdout); | 556 | fputc(hackline[i], stdout); |
556 | 557 | ||
557 | /* then print the substitution string */ | 558 | /* then print the substitution string */ |
558 | print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch); | 559 | print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch, |
560 | sed_cmd->num_backrefs); | ||
559 | 561 | ||
560 | /* advance past the match */ | 562 | /* advance past the match */ |
561 | hackline += regmatch[0].rm_eo; | 563 | hackline += regmatch[0].rm_eo; |