diff --git a/tokenize.c b/tokenize.c index 71dc14698411b58e660ecaeb809aa149eb1addc4..552dbdc28629a177c99446f6ec8b51111fa32bb0 100644 --- a/tokenize.c +++ b/tokenize.c @@ -563,6 +563,25 @@ File *new_file(char *name, int file_no, char *contents) { return file; } +// Replaces \r or \r\n with \n. +static void canonicalize_newline(char *p) { + int i = 0, j = 0; + + while (p[i]) { + if (p[i] == '\r' && p[i + 1] == '\n') { + i += 2; + p[j++] = '\n'; + } else if (p[i] == '\r') { + i++; + p[j++] = '\n'; + } else { + p[j++] = p[i++]; + } + } + + p[j] = '\0'; +} + // Removes backslashes followed by a newline. static void remove_backslash_newline(char *p) { int i = 0, j = 0; @@ -593,6 +612,7 @@ Token *tokenize_file(char *path) { if (!p) return NULL; + canonicalize_newline(p); remove_backslash_newline(p); // Save the filename for assembler .file directive.