@@ -220,6 +220,11 @@ void print_line(const char *buf, size_t buf_pos, size_t prev_line_offset) {
220
220
if (opts .width > 0 && opts .width < write_chars ) {
221
221
write_chars = opts .width ;
222
222
}
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 ;
223
228
224
229
#if !SUPPORT_TWO_ENCODINGS
225
230
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) {
229
234
cp = CP_ACP ;
230
235
fwriteM (buf + prev_line_offset , 1 , write_chars , out_fd , cp );
231
236
#endif
237
+
238
+ if (hasLF ) fputc ('\n' , out_fd );
232
239
}
233
240
234
241
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,
393
400
}
394
401
print_context .printing_a_match = TRUE;
395
402
}
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' ) {
398
405
/* if only_matching is set, print only matches and newlines */
399
406
if (!opts .only_matching || print_context .printing_a_match ) {
400
407
if (opts .width == 0 || j - print_context .prev_line_offset < opts .width ) {
0 commit comments