Changes to md5sum

Jean Delvare <[email protected]>
Newsgroups gmane.comp.gnu.textutils.bugs
Organization http://www.linux-fr.org/
Message-ID <[email protected]>
Hello.

I have been adding a feature to md5sum:
  -m, --ignore-missing    don't complain about missing or unreadable
files

I needed it and I suppose that other people may need it too, so it may
be worth integrating it.

The attached patch also fixes the --string option not being listed by
--help. The man page would require the same fix.

Thanks for your attention.

-- 
Jean "Khali" Delvare
mail: [email protected]
http://www.ensicaen.ismra.fr/~delvare/
md5sum-ignore-missing-RC2.diff (application/octet-stream, 4.5 KB)
--- md5sum.c.orig	Mon May  3 19:55:37 1999
+++ md5sum.c	Sun Mar 31 22:16:35 2002
@@ -84,6 +84,7 @@
 {
   { "binary", no_argument, 0, 'b' },
   { "check", no_argument, 0, 'c' },
+  { "ignore-missing", no_argument, 0, 'm' },
   { "status", no_argument, 0, 2 },
   { "string", required_argument, 0, 1 },
   { "text", no_argument, 0, 't' },
@@ -104,14 +105,17 @@
       printf (_("\
 Usage: %s [OPTION] [FILE]...\n\
   or:  %s [OPTION] --check [FILE]\n\
+  or:  %s [OPTION] --string STRING\n\
 Print or check MD5 checksums.\n\
 With no FILE, or when FILE is -, read standard input.\n\
 \n\
   -b, --binary            read files in binary mode (default on DOS/Windows)\n\
   -c, --check             check MD5 sums against given list\n\
+      --string            read data from command line, not files\n\
   -t, --text              read files in text mode (default)\n\
 \n\
-The following two options are useful only when verifying checksums:\n\
+The following three options are useful only when verifying checksums:\n\
+  -m, --ignore-missing    don't complain about missing or unreadable files\n\
       --status            don't output anything, status code shows success\n\
   -w, --warn              warn about improperly formated MD5 checksum lines\n\
 \n\
@@ -122,7 +126,7 @@
 should be a former output of this program.  The default mode is to print\n\
 a line with checksum, a character indicating type (`*' for binary, ` ' for\n\
 text), and name for each FILE.\n"),
-	      program_name, program_name);
+	      program_name, program_name, program_name);
       puts (_("\nReport bugs to <[email protected]>."));
     }
 
@@ -236,7 +240,7 @@
    to indicate success.  */
 
 static int
-md5_file (const char *filename, int binary, unsigned char *md5_result)
+md5_file (const char *filename, int binary, unsigned char *md5_result, int ignore_missing)
 {
   FILE *fp;
   int err;
@@ -261,7 +265,8 @@
       fp = fopen (filename, OPENOPTS (binary));
       if (fp == NULL)
 	{
-	  error (0, errno, "%s", filename);
+	  if(!ignore_missing)
+	    error (0, errno, "%s", filename);
 	  return 1;
 	}
     }
@@ -285,7 +290,7 @@
 }
 
 static int
-md5_check (const char *checkfile_name)
+md5_check (const char *checkfile_name, int ignore_missing)
 {
   FILE *checkfile_stream;
   int n_properly_formated_lines = 0;
@@ -357,12 +362,12 @@
 
 	  ++n_properly_formated_lines;
 
-	  fail = md5_file (filename, binary, md5buffer);
+	  fail = md5_file (filename, binary, md5buffer, ignore_missing);
 
 	  if (fail)
 	    {
 	      ++n_open_or_read_failures;
-	      if (!status_only)
+	      if (!(status_only || ignore_missing))
 		{
 		  printf (_("%s: FAILED open or read\n"), filename);
 		  fflush (stdout);
@@ -422,7 +427,7 @@
 	  int n_computed_checkums = (n_properly_formated_lines
 				     - n_open_or_read_failures);
 
-	  if (n_open_or_read_failures > 0)
+	  if (n_open_or_read_failures > 0 && !ignore_missing)
 	    {
 	      error (0, 0,
 		   _("WARNING: %d of %d listed %s could not be read\n"),
@@ -443,7 +448,7 @@
     }
 
   return ((n_properly_formated_lines > 0 && n_mismatched_checksums == 0
-	   && n_open_or_read_failures == 0) ? 0 : 1);
+	   && (n_open_or_read_failures == 0 || ignore_missing)) ? 0 : 1);
 }
 
 int
@@ -451,6 +456,7 @@
 {
   unsigned char md5buffer[16];
   int do_check = 0;
+  int ignore_missing = 0;
   int opt;
   char **string = NULL;
   size_t n_strings = 0;
@@ -472,7 +478,7 @@
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
-  while ((opt = getopt_long (argc, argv, "bctw", long_options, NULL)) != -1)
+  while ((opt = getopt_long (argc, argv, "bcmtw", long_options, NULL)) != -1)
     switch (opt)
       {
       case 0:			/* long option */
@@ -496,8 +502,13 @@
 	break;
       case 2:
 	status_only = 1;
+	ignore_missing = 0;
 	warn = 0;
 	break;
+      case 'm':
+	status_only = 0;
+	ignore_missing = 1;
+	break;
       case 't':
 	file_type_specified = 1;
 	binary = 0;
@@ -533,6 +544,13 @@
       usage (EXIT_FAILURE);
     }
 
+  if (ignore_missing && !do_check)
+    {
+      error (0, 0,
+       _("the --ignore-missing option is meaningful only when verifying checksums"));
+      usage (EXIT_FAILURE);
+    }
+
   if (warn && !do_check)
     {
       error (0, 0,
@@ -569,7 +587,7 @@
 	  usage (EXIT_FAILURE);
 	}
 
-      err = md5_check ((optind == argc) ? "-" : argv[optind]);
+      err = md5_check ((optind == argc) ? "-" : argv[optind], ignore_missing);
     }
   else
     {
@@ -581,7 +599,7 @@
 	  int fail;
 	  char *file = argv[optind];
 
-	  fail = md5_file (file, binary, md5buffer);
+	  fail = md5_file (file, binary, md5buffer, 0);
 	  err |= fail;
 	  if (!fail)
 	    {
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.