coreutils
[Top][All Lists]
Advanced

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

[PATCH] md5sum: add an option to change directory


From: Bertrand Jacquin
Subject: [PATCH] md5sum: add an option to change directory
Date: Wed, 20 May 2020 22:15:52 +0100

In the fashion of make and git, add the ability for all sum tools to
change directory before reading a file.

  $ sha256sum /etc/fstab
  b5d6c0e5e6bc419b134478ad7b3e7c8cc628049876a7772cea469e81e4b0e0e5  /etc/fstab

  $ sha256sum -C /etc fstab
  b5d6c0e5e6bc419b134478ad7b3e7c8cc628049876a7772cea469e81e4b0e0e5  fstab

  $ sha256sum -C /tmp/do-not-exist fstab
  ./src/sha256sum: ‘/tmp/do-not-exist’: No such file or directory

  $ sha256sum -C /etc fstab > /tmp/fstab.sha256sum
  $ sha256sum -C /etc -c < /tmp/fstab.sha256sum
  fstab: OK

  $ sha256sum -c < /tmp/fstab.sha256sum
  sha256sum: fstab: No such file or directory
  fstab: FAILED open or read
  sha256sum: WARNING: 1 listed file could not be read

  $ sha256sum -C
  sha256sum: option requires an argument -- 'C'
  Try 'sha256sum --help' for more information.
---
 src/md5sum.c | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/src/md5sum.c b/src/md5sum.c
index 447a005a1137..1d0723e25900 100644
--- a/src/md5sum.c
+++ b/src/md5sum.c
@@ -204,6 +204,7 @@ static struct option const long_options[] =
 #if HASH_ALGO_BLAKE2
   { "length", required_argument, NULL, 'l'},
 #endif
+  { "directory", required_argument, NULL, 'C' },
   { "binary", no_argument, NULL, 'b' },
   { "check", no_argument, NULL, 'c' },
   { "ignore-missing", no_argument, NULL, IGNORE_MISSING_OPTION},
@@ -238,11 +239,19 @@ Print or check %s (%d-bit) checksums.\n\
       if (O_BINARY)
         fputs (_("\
 \n\
+  -C, --directory      change to directory instead of the current working 
directory\n\
+"), stdout);
+      else
+        fputs (_("\
+\n\
+  -C, --directory      change to directory instead of the current working 
directory\n\
+"), stdout);
+      if (O_BINARY)
+        fputs (_("\
   -b, --binary         read in binary mode (default unless reading tty 
stdin)\n\
 "), stdout);
       else
         fputs (_("\
-\n\
   -b, --binary         read in binary mode\n\
 "), stdout);
 
@@ -883,10 +892,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 = "lC:bctwz";
   const char* b2_length_str = "";
 #else
-  const char* short_opts = "bctwz";
+  const char* short_opts = "C:bctwz";
 #endif
 
   while ((opt = getopt_long (argc, argv, short_opts, long_options, NULL)) != 
-1)
@@ -904,6 +913,10 @@ main (int argc, char **argv)
           }
         break;
 #endif
+      case 'C':
+        if (chdir(optarg))
+          die(EXIT_FAILURE, errno, _("%s"), quote (optarg));
+        break;
       case 'b':
         binary = 1;
         break;



reply via email to

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