From 4d94e65d06772c41b8ac11e8b55f749c57c18f5b Mon Sep 17 00:00:00 2001 From: Pádraig Brady Date: Fri, 1 Nov 2013 16:14:44 +0000 Subject: maint: simplify printing of md5sum file names * src/md5sum.c (main): Add a comment as to why we continue to escape names that do not have '\n' but do have '\\' chars. (print_filename): Use the predetermined boolean to decide whether to escape or not, so that in the common case we can output the file name directly, rather than inspecting each char. * tests/misc/md5sum.pl: Add case to show '\\' chars cause escaping. * tests/misc/sha1sum.pl: Likewise. --- src/md5sum.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) (limited to 'src/md5sum.c') diff --git a/src/md5sum.c b/src/md5sum.c index b43781150..bf7eb81a6 100644 --- a/src/md5sum.c +++ b/src/md5sum.c @@ -657,11 +657,17 @@ digest_check (const char *checkfile_name) && (!strict || n_improperly_formatted_lines == 0)); } +/* If ESCAPE is true, then translate each NEWLINE byte to the string, "\\n", + and each backslash to "\\\\". */ static void -print_filename (char const *file) +print_filename (char const *file, bool escape) { - /* Translate each NEWLINE byte to the string, "\\n", - and each backslash to "\\\\". */ + if (! escape) + { + fputs (file, stdout); + return; + } + while (*file) { switch (*file) @@ -823,14 +829,23 @@ main (int argc, char **argv) ok = false; else { + /* We don't really need to escape, and hence detect, the '\\' + char, and not doing so should be both forwards and backwards + compatible, since only escaped lines would have a '\\' char at + the start. However just in case users are directly comparing + against old (hashed) outputs, in the presence of files + containing '\\' characters, we decided to not simplify the + output in this case. */ + bool needs_escape = strchr (file, '\\') || strchr (file, '\n'); + if (prefix_tag) { - if (strchr (file, '\n') || strchr (file, '\\')) + if (needs_escape) putchar ('\\'); fputs (DIGEST_TYPE_STRING, stdout); fputs (" (", stdout); - print_filename (file); + print_filename (file, needs_escape); fputs (") = ", stdout); } @@ -838,7 +853,7 @@ main (int argc, char **argv) /* Output a leading backslash if the file name contains a newline or backslash. */ - if (!prefix_tag && (strchr (file, '\n') || strchr (file, '\\'))) + if (!prefix_tag && needs_escape) putchar ('\\'); for (i = 0; i < (digest_hex_bytes / 2); ++i) @@ -850,7 +865,7 @@ main (int argc, char **argv) putchar (file_is_binary ? '*' : ' '); - print_filename (file); + print_filename (file, needs_escape); } putchar ('\n'); -- cgit v1.2.3-54-g00ecf