summaryrefslogtreecommitdiff
path: root/src/md5sum.c
diff options
context:
space:
mode:
authorPatrick Schoenfeld <schoenfeld@debian.org>2011-07-07 08:57:39 +0200
committerJim Meyering <meyering@redhat.com>2011-07-07 16:42:47 +0200
commit33171d049a162ca5648c7e213e9c988ea835a19b (patch)
tree77022f7d0ceff372e08a61248a97ed7d94441b74 /src/md5sum.c
parent88bdce982a99238b5320eca4e19d86fd364419ee (diff)
downloadcoreutils-33171d049a162ca5648c7e213e9c988ea835a19b.tar.xz
md5sum, sha1sum, etc: accept new option: --strict
Use this new option with --check when the input is expected to consist solely of checksum lines. With only --check, an invalid line evokes a warning, but the program can still exit successfully. With --strict, any invalid line makes the program exit non-zero. * src/md5sum.c (strict, STRICT_OPTION): Declare/define. (long_options): Add "strict". (usage): Describe --strict. (digest_check): Count improperly_formatted lines, too, and use that number and the global "strict" to determine the return value. (main): Handle STRICT_OPTION. Reject --strict without --check. * doc/coreutils.texi: Describe it. * NEWS (New features): Mention it.
Diffstat (limited to 'src/md5sum.c')
-rw-r--r--src/md5sum.c27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/md5sum.c b/src/md5sum.c
index 9bbdc605f..ff9538a28 100644
--- a/src/md5sum.c
+++ b/src/md5sum.c
@@ -122,12 +122,17 @@ static bool warn = false;
/* With --check, suppress the "OK" printed for each verified file. */
static bool quiet = false;
+/* With --check, exit with a non-zero return code if any line is
+ improperly formatted. */
+static bool strict = 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
{
STATUS_OPTION = CHAR_MAX + 1,
- QUIET_OPTION
+ QUIET_OPTION,
+ STRICT_OPTION
};
static struct option const long_options[] =
@@ -138,6 +143,7 @@ static struct option const long_options[] =
{ "status", no_argument, NULL, STATUS_OPTION },
{ "text", no_argument, NULL, 't' },
{ "warn", no_argument, NULL, 'w' },
+ { "strict", no_argument, NULL, STRICT_OPTION },
{ GETOPT_HELP_OPTION_DECL },
{ GETOPT_VERSION_OPTION_DECL },
{ NULL, 0, NULL, 0 }
@@ -187,6 +193,9 @@ The following three options are useful only when verifying checksums:\n\
-w, --warn warn about improperly formatted checksum lines\n\
\n\
"), stdout);
+ fputs (_("\
+ --strict with --check, exit non-zero for any invalid input\n\
+"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
printf (_("\
@@ -434,6 +443,7 @@ digest_check (const char *checkfile_name)
FILE *checkfile_stream;
uintmax_t n_misformatted_lines = 0;
uintmax_t n_properly_formatted_lines = 0;
+ uintmax_t n_improperly_formatted_lines = 0;
uintmax_t n_mismatched_checksums = 0;
uintmax_t n_open_or_read_failures = 0;
unsigned char bin_buffer_unaligned[DIGEST_BIN_BYTES + DIGEST_ALIGN];
@@ -501,6 +511,8 @@ digest_check (const char *checkfile_name)
checkfile_name, line_number,
DIGEST_TYPE_STRING);
}
+
+ ++n_improperly_formatted_lines;
}
else
{
@@ -603,7 +615,8 @@ digest_check (const char *checkfile_name)
return (n_properly_formatted_lines != 0
&& n_mismatched_checksums == 0
- && n_open_or_read_failures == 0);
+ && n_open_or_read_failures == 0
+ && (!strict || n_improperly_formatted_lines == 0));
}
int
@@ -657,6 +670,9 @@ main (int argc, char **argv)
warn = false;
quiet = true;
break;
+ case STRICT_OPTION:
+ strict = true;
+ break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
@@ -694,6 +710,13 @@ main (int argc, char **argv)
usage (EXIT_FAILURE);
}
+ if (strict & !do_check)
+ {
+ error (0, 0,
+ _("the --strict option is meaningful only when verifying checksums"));
+ usage (EXIT_FAILURE);
+ }
+
if (!O_BINARY && binary < 0)
binary = 0;