#include "nls.h"
#include "closestream.h"
#include "randutils.h"
+#include "xalloc.h"
#include <fcntl.h>
#include <getopt.h>
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
+static void randomness_from_files(char **files, int nfiles,
+ struct MD5Context *ctx, int verbose)
+{
+ int fd, i;
+ int count = 0;
+
+ for (i = 0; i < nfiles; i++) {
+ if (files[i][0] == '-' && !files[i][1])
+ fd = STDIN_FILENO;
+ else
+ fd = open(files[i], O_RDONLY);
+
+ if (fd < 0) {
+ warn(_("cannot open %s"), files[i]);
+ } else {
+ count = hash_file(ctx, fd);
+ if (verbose)
+ fprintf(stderr,
+ P_("Got %d byte from %s\n",
+ "Got %d bytes from %s\n", count),
+ count, files[i]);
+
+ if (fd != STDIN_FILENO)
+ if (close(fd))
+ err(EXIT_FAILURE,
+ _("closing %s failed"), files[i]);
+ }
+ }
+}
+
int main(int argc, char **argv)
{
size_t i;
struct MD5Context ctx;
unsigned char digest[MD5LENGTH];
unsigned char buf[RAND_BYTES];
- int fd;
+ char **files = NULL;
+ int nfiles;
int c;
- char *file = NULL;
int verbose = 0;
static const struct option longopts[] = {
textdomain(PACKAGE);
atexit(close_stdout);
+ MD5Init(&ctx);
+
+ if (2 < argc) {
+ files = xmalloc(sizeof(char *) * argc);
+ nfiles = 0;
+ }
+
while ((c =
getopt_long(argc, argv, "f:vVh", longopts, NULL)) != -1)
switch (c) {
verbose = 1;
break;
case 'f':
- file = optarg;
+ files[nfiles] = optarg;
+ nfiles++;
break;
case 'V':
printf(UTIL_LINUX_VERSION);
usage(stderr);
}
- MD5Init(&ctx);
-
- if (file) {
- int count = 0;
-
- if (file[0] == '-' && !file[1])
- fd = STDIN_FILENO;
- else
- fd = open(file, O_RDONLY);
-
- if (fd < 0) {
- warn(_("cannot open %s"), file);
- } else {
- count = hash_file(&ctx, fd);
- if (verbose)
- fprintf(stderr,
- P_("Got %d byte from %s\n",
- "Got %d bytes from %s\n", count),
- count, file);
-
- if (fd != STDIN_FILENO)
- if (close(fd))
- err(EXIT_FAILURE,
- _("closing %s failed"), file);
- }
- }
+ randomness_from_files(files, nfiles, &ctx, verbose);
+ free(files);
random_get_bytes(&buf, RAND_BYTES);
MD5Update(&ctx, buf, RAND_BYTES);