coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] md5sum: add an option to strip directory from filename


From: Bertrand Jacquin
Subject: [PATCH] md5sum: add an option to strip directory from filename
Date: Sat, 30 May 2020 22:57:16 +0100

The can be useful to create checksum of a file without having to change
to a directory first if the target is not expected to have the same
directory structure as locally.

  $ md5sum /etc/fstab
  cd884f87a3ab7bd4bfa9f79bb1c0f0a6  /etc/fstab

  $ md5sum -B /etc/fstab
  cd884f87a3ab7bd4bfa9f79bb1c0f0a6  fstab
---
 src/md5sum.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)

diff --git a/src/md5sum.c b/src/md5sum.c
index 447a005a1137..7366d01397d6 100644
--- a/src/md5sum.c
+++ b/src/md5sum.c
@@ -160,6 +160,9 @@ static int bsd_reversed = -1;
 /* line delimiter.  */
 static unsigned char delim = '\n';
 
+/* Set to strip directory from filename */
+static unsigned char print_basename = false;
+
 #if HASH_ALGO_BLAKE2
 static char const *const algorithm_in_string[] =
 {
@@ -214,6 +217,7 @@ static struct option const long_options[] =
   { "strict", no_argument, NULL, STRICT_OPTION },
   { "tag", no_argument, NULL, TAG_OPTION },
   { "zero", no_argument, NULL, 'z' },
+  { "basename", no_argument, NULL, 'B' },
   { GETOPT_HELP_OPTION_DECL },
   { GETOPT_VERSION_OPTION_DECL },
   { NULL, 0, NULL, 0 }
@@ -237,12 +241,10 @@ Print or check %s (%d-bit) checksums.\n\
       emit_stdin_note ();
       if (O_BINARY)
         fputs (_("\
-\n\
   -b, --binary         read in binary mode (default unless reading tty 
stdin)\n\
 "), stdout);
       else
         fputs (_("\
-\n\
   -b, --binary         read in binary mode\n\
 "), stdout);
 
@@ -269,6 +271,14 @@ Print or check %s (%d-bit) checksums.\n\
       fputs (_("\
   -z, --zero           end each output line with NUL, not newline,\n\
                        and disable file name escaping\n\
+"), stdout);
+      if (O_BINARY)
+        fputs (_("\
+  -B, --basename       strip directory from filename\n\
+"), stdout);
+      else
+        fputs (_("\
+  -B, --basename       strip directory from filename\n\
 "), stdout);
       fputs (_("\
 \n\
@@ -547,8 +557,13 @@ split_3 (char *s, size_t s_len,
 /* If ESCAPE is true, then translate each NEWLINE byte to the string, "\\n",
    and each backslash to "\\\\".  */
 static void
-print_filename (char const *file, bool escape)
+print_filename (char const *file, bool basename, bool escape)
 {
+  if (basename)
+    {
+      file = base_name(file);
+    }
+
   if (! escape)
     {
       fputs (file, stdout);
@@ -748,7 +763,7 @@ digest_check (const char *checkfile_name)
                 {
                   if (needs_escape)
                     putchar ('\\');
-                  print_filename (filename, needs_escape);
+                  print_filename (filename, false, needs_escape);
                   printf (": %s\n", _("FAILED open or read"));
                 }
             }
@@ -783,7 +798,7 @@ digest_check (const char *checkfile_name)
                     {
                       if (needs_escape)
                         putchar ('\\');
-                      print_filename (filename, needs_escape);
+                      print_filename (filename, false, needs_escape);
                     }
 
                   if (cnt != digest_bin_bytes)
@@ -883,10 +898,10 @@ main (int argc, char **argv)
   setvbuf (stdout, NULL, _IOLBF, 0);
 
 #if HASH_ALGO_BLAKE2
-  const char* short_opts = "l:bctwz";
+  const char* short_opts = "l:bctwzB";
   const char* b2_length_str = "";
 #else
-  const char* short_opts = "bctwz";
+  const char* short_opts = "bctwzB";
 #endif
 
   while ((opt = getopt_long (argc, argv, short_opts, long_options, NULL)) != 
-1)
@@ -941,6 +956,9 @@ main (int argc, char **argv)
       case 'z':
         delim = '\0';
         break;
+      case 'B':
+        print_basename = true;
+        break;
       case_GETOPT_HELP_CHAR;
       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
       default:
@@ -1077,7 +1095,7 @@ main (int argc, char **argv)
                   fputs (DIGEST_TYPE_STRING, stdout);
 #endif
                   fputs (" (", stdout);
-                  print_filename (file, needs_escape);
+                  print_filename (file, print_basename, needs_escape);
                   fputs (") = ", stdout);
                 }
 
@@ -1095,7 +1113,7 @@ main (int argc, char **argv)
 
                   putchar (file_is_binary ? '*' : ' ');
 
-                  print_filename (file, needs_escape);
+                  print_filename (file, print_basename, needs_escape);
                 }
 
               putchar (delim);



reply via email to

[Prev in Thread] Current Thread [Next in Thread]