diff options
author | Mattias Andrée <maandree@kth.se> | 2024-08-25 23:28:25 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@kth.se> | 2024-08-25 23:28:25 +0200 |
commit | b35cea4b8d82a28994351f3b550e8191ee1d94bf (patch) | |
tree | 24ce0d8728711e55d5532a57db477b53521f8bc4 /anysum.c | |
download | anysum-b35cea4b8d82a28994351f3b550e8191ee1d94bf.tar.gz anysum-b35cea4b8d82a28994351f3b550e8191ee1d94bf.tar.bz2 anysum-b35cea4b8d82a28994351f3b550e8191ee1d94bf.tar.xz |
First commit
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'anysum.c')
-rw-r--r-- | anysum.c | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/anysum.c b/anysum.c new file mode 100644 index 0000000..a478a7a --- /dev/null +++ b/anysum.c @@ -0,0 +1,75 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" +#include <libsimple-arg.h> + +NUSAGE(2, "[file] ..."); + + +static int +calculate_and_print(const char *file, struct barrier_group *group, struct global_data *global) +{ + size_t i; + if (calculate(file, group, global)) + return -1; + for (i = 0; i < global->nalgorithms; i++) + writeall(STDOUT_FILENO, global->algorithms[i].result, global->algorithms[i].result_length, "<stdout>"); + return 0; +} + + +static int +calculate_each_and_print(char **files, struct algorithm *algorithms, size_t nalgorithms, enum format format) +{ + size_t wanted_nalgorithms = nalgorithms; + struct buffer buffer = {0}; + int r, ret = 0; + struct barrier_group group; + struct global_data global; + + global.format = format; + global.buffer = &buffer; + global.algorithms = algorithms; + global.nalgorithms = nalgorithms; + + createbarriergroup(&group, nalgorithms, &global); + + if (!*files) { + global.file = "-"; + ret = calculate_and_print("-", &group, &global) ? 2 : 0; + } + while (*files) { + global.file = *files; + r = calculate_and_print(*files++, &group, &global) ? 2 : 0; + ret = MAX(r, ret); + } + + if (nalgorithms != wanted_nalgorithms) + ret = 2; + + killbarriergroup(&group, &global); + + free(buffer.buf); + return ret; +} + + +int +main(int argc, char *argv[]) +{ + enum format format = LOWERCASE_HEX | WITH_ALGOSTR | WITH_FILENAME | WITH_LF; + struct algorithm algorithms[] = {{"sha1"}, {"sha2-512/224"}, {"sha1"}}; + size_t i, nalgorithms = ELEMSOF(algorithms); + int ret; + + libsimple_default_failure_exit = 2; + + ARGBEGIN { + default: + usage(); + } ARGEND; + + ret = calculate_each_and_print(argv, algorithms, nalgorithms, format); + for (i = 0; i < nalgorithms; i++) + free(algorithms[i].result); + return ret; +} |