Skip to content

Commit 1fa55a8

Browse files
committed
Bug fix: Lines were output with two CR then one LF.
1 parent f584ba9 commit 1fa55a8

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/print.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,11 @@ void print_line(const char *buf, size_t buf_pos, size_t prev_line_offset) {
220220
if (opts.width > 0 && opts.width < write_chars) {
221221
write_chars = opts.width;
222222
}
223+
224+
/* Remove trailing \r, if any. Another one will be generated along with the \n */
225+
int hasLF = FALSE;
226+
if (buf[prev_line_offset + write_chars - 1] == '\n') { hasLF = TRUE; write_chars -= 1; }
227+
if (buf[prev_line_offset + write_chars - 1] == '\r') write_chars -= 1;
223228

224229
#if !SUPPORT_TWO_ENCODINGS
225230
fwrite(buf + prev_line_offset, 1, write_chars, out_fd);
@@ -229,6 +234,8 @@ void print_line(const char *buf, size_t buf_pos, size_t prev_line_offset) {
229234
cp = CP_ACP;
230235
fwriteM(buf + prev_line_offset, 1, write_chars, out_fd, cp);
231236
#endif
237+
238+
if (hasLF) fputc('\n', out_fd);
232239
}
233240

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

0 commit comments

Comments
 (0)