Skip to content

Commit

Permalink
Bug fix: Lines were output with two CR then one LF.
Browse files Browse the repository at this point in the history
  • Loading branch information
JFLarvoire committed Nov 16, 2018
1 parent f584ba9 commit 1fa55a8
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ void print_line(const char *buf, size_t buf_pos, size_t prev_line_offset) {
if (opts.width > 0 && opts.width < write_chars) {
write_chars = opts.width;
}

/* Remove trailing \r, if any. Another one will be generated along with the \n */
int hasLF = FALSE;
if (buf[prev_line_offset + write_chars - 1] == '\n') { hasLF = TRUE; write_chars -= 1; }
if (buf[prev_line_offset + write_chars - 1] == '\r') write_chars -= 1;

#if !SUPPORT_TWO_ENCODINGS
fwrite(buf + prev_line_offset, 1, write_chars, out_fd);
Expand All @@ -229,6 +234,8 @@ void print_line(const char *buf, size_t buf_pos, size_t prev_line_offset) {
cp = CP_ACP;
fwriteM(buf + prev_line_offset, 1, write_chars, out_fd, cp);
#endif

if (hasLF) fputc('\n', out_fd);
}

void print_binary_file_matches(const char *path) {
Expand Down Expand Up @@ -393,8 +400,8 @@ void print_file_matches(const char *path, const char *buf, const size_t buf_len,
}
print_context.printing_a_match = TRUE;
}
/* Don't print the null terminator */
if (j < buf_len) {
/* Don't print the null terminator, nor \r characters which will be regenerated along with the \n */
if (j < buf_len && buf[j] != '\r') {
/* if only_matching is set, print only matches and newlines */
if (!opts.only_matching || print_context.printing_a_match) {
if (opts.width == 0 || j - print_context.prev_line_offset < opts.width) {
Expand Down

0 comments on commit 1fa55a8

Please sign in to comment.