diff --git a/CMakeLists.txt b/CMakeLists.txt index 73192d8..3896d0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,4 +25,5 @@ add_executable(${PROJECT_NAME} tiger.c main.d tiger.d + sums.d ) diff --git a/main.d b/main.d index 775f68f..5e88514 100644 --- a/main.d +++ b/main.d @@ -17,31 +17,12 @@ import std.stdio; import std.file; -import tiger; import std.array; import std.getopt; +import sums; mixin(import("tigersumConfig.d")); -void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode, bool parBsdStyle) { - foreach (file_name; parFiles) { - const auto curr_path = parCWD ~ file_name; - File curr_file = File(curr_path, parFileMode); - if (!isDir(curr_path)) { - const string hash = tigerToString(getTiger(curr_file, parTiger2)); - if (parBsdStyle) { - writefln("tiger (%s) = %s", file_name, hash); - } - else { - writefln("%s %s", hash, file_name); - } - } - else { - writefln("tigersum: %s: is a directory", file_name); - } - } -} - int main (string[] parArgs) { if (parArgs.length == 1) return 0; @@ -77,9 +58,13 @@ int main (string[] parArgs) { "binary|b", &fileModeHandler, "text|t", &fileModeHandler, "tag", &bsd_style + //"quiet", &switch_quiet, + //"status", &switch_status, + //"warn|w", &switch_warn ); if (check_mode) { + checkSums(args[1 .. $], working_dir, tiger_version == 2, file_mode); } else if (show_help) { writefln("Usage: %s [OPTION]... [FILE]...", PROGRAM_NAME); @@ -89,6 +74,10 @@ int main (string[] parArgs) { writeln(" -t, --text read in text mode (default)"); writefln(" -i n, --tiger=n set the tiger version that should be used; valid values are 1 or 2, %d is default", TIGER_VERSION_DEF); writeln(); + //writeln("The following three options are useful only when verifying checksums:"); + //writeln(" --quiet don't print OK for each successfully verified file"); + //writeln(" --status don't output anything, status code shows success"); + //writeln(" -w, --warn warn about improperly formatted checksum lines"); writeln(); writeln(" -h, --help display this help and exit"); writeln(" -v, --version display version informations and exit"); diff --git a/sums.d b/sums.d new file mode 100644 index 0000000..48101a1 --- /dev/null +++ b/sums.d @@ -0,0 +1,87 @@ +/* tigersum - a command line utility to calculate the tiger hash from input. + Copyright (C) 2014 Michele "King_DuckZ" Santullo + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +import std.regex; +import std.stdio; +import std.file : isDir; +import std.conv; +import std.string; +import tiger; + +void calculateSums (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode, bool parBsdStyle) { + foreach (file_name; parFiles) { + const auto curr_path = parCWD ~ file_name; + File curr_file = File(curr_path, parFileMode); + if (!isDir(curr_path)) { + const string hash = tigerToString(getTiger(curr_file, parTiger2)); + if (parBsdStyle) { + writefln("tiger (%s) = %s", file_name, hash); + } + else { + writefln("%s %s", hash, file_name); + } + } + else { + writefln("tigersum: %s: is a directory", file_name); + } + } +} + +bool checkSums (in string parFile, in string parCWD, bool parTiger2, string parFileMode) { + File curr_list = File(parFile, "r"); + bool ret_val = true; + const auto reg_bsd = r"^tiger \((.+)\)\s*=\s*([a-fA-F0-9]{48})$"; + const auto reg_normal = r"^([a-fA-F0-9]{48})\s+(.+)$"; + + foreach (line; curr_list.byLine()) { + auto m = match(line, reg_bsd); + int index_hash, index_file; + if (m) { + index_hash = 2; + index_file = 1; + } + else { + m = match(line, reg_normal); + index_hash = 1; + index_file = 2; + } + + if (m) { + const string capt_filename = to!string(m.captures[index_file]); + writef("%s: ", capt_filename); + + const string curr_path = parCWD ~ capt_filename; + File curr_file = File(curr_path, parFileMode); + const string curr_tiger = tigerToString(getTiger(curr_file, parTiger2)); + const string check_tiger = to!string(m.captures[index_hash]).toLower; + if (check_tiger == curr_tiger) + writeln("OK"); + else + writeln("FAILED"); + } + } + return ret_val; +} + +bool checkSums (in string[] parFiles, in string parCWD, bool parTiger2, string parFileMode) { + bool ret_val = true; + foreach (file_name; parFiles) { + const auto curr_path = parCWD ~ file_name; + ret_val &= checkSums(curr_path, parCWD, parTiger2, parFileMode); + } + return ret_val; +}