>From 55c1749bed8092d27fdd3938b220e9c22425871f Mon Sep 17 00:00:00 2001 From: Luther Thompson Date: Wed, 8 Dec 2010 19:56:32 -0500 Subject: [PATCH 1/3] md5sum: Added option '--ignore-missing' With '--check', suppresses error messages for nonexistant files. * src/md5sum.c (ignore_missing, long_options, usage, main): Add option -i/--ignore-missing. (digest_file): If fopen fails, ignore_missing suppresses the error message. (digest_check): ignore_missing suppresses the 'FAILED' message. --- src/md5sum.c | 24 ++++++++++++++++++++---- 1 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/md5sum.c b/src/md5sum.c index a660e3b..9635692 100644 --- a/src/md5sum.c +++ b/src/md5sum.c @@ -122,6 +122,9 @@ static bool warn = false; /* With --check, suppress the "OK" printed for each verified file. */ static bool quiet = false; +/* With --check, suppress the error message for each non-existant file. */ +static bool ignore_missing = false; + /* For long options that have no equivalent short option, use a non-character as a pseudo short option, starting with CHAR_MAX + 1. */ enum @@ -138,6 +141,7 @@ static struct option const long_options[] = { "status", no_argument, NULL, STATUS_OPTION }, { "text", no_argument, NULL, 't' }, { "warn", no_argument, NULL, 'w' }, + { "ignore-missing", no_argument, NULL, 'i' }, { GETOPT_HELP_OPTION_DECL }, { GETOPT_VERSION_OPTION_DECL }, { NULL, 0, NULL, 0 } @@ -181,10 +185,11 @@ With no FILE, or when FILE is -, read standard input.\n\ "), stdout); fputs (_("\ \n\ -The following three options are useful only when verifying checksums:\n\ +The following four options are useful only when verifying checksums:\n\ --quiet don't print OK for each successfully verified file\n\ --status don't output anything, status code shows success\n\ -w, --warn warn about improperly formatted checksum lines\n\ + -i, --ignore-missing don't report errors for missing files\n\ \n\ "), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); @@ -402,7 +407,8 @@ digest_file (const char *filename, int *binary, unsigned char *bin_result) fp = fopen (filename, (O_BINARY && *binary ? "rb" : "r")); if (fp == NULL) { - error (0, errno, "%s", filename); + if (!ignore_missing) + error (0, errno, "%s", filename); return false; } } @@ -516,7 +522,7 @@ digest_check (const char *checkfile_name) if (!ok) { ++n_open_or_read_failures; - if (!status_only) + if (!status_only && !ignore_missing) { printf (_("%s: FAILED open or read\n"), filename); } @@ -626,7 +632,7 @@ main (int argc, char **argv) so that processes running in parallel do not intersperse their output. */ setvbuf (stdout, NULL, _IOLBF, 0); - while ((opt = getopt_long (argc, argv, "bctw", long_options, NULL)) != -1) + while ((opt = getopt_long (argc, argv, "bcitw", long_options, NULL)) != -1) switch (opt) { case 'b': @@ -653,6 +659,9 @@ main (int argc, char **argv) warn = false; quiet = true; break; + case 'i': + ignore_missing = true; + break; case_GETOPT_HELP_CHAR; case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); default: @@ -690,6 +699,13 @@ main (int argc, char **argv) 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 (!O_BINARY && binary < 0) binary = 0; -- 1.7.0.4