From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 2/5] mcookie: allow --file option be defined multiple times
Date: Sat, 8 Mar 2014 15:43:27 -0600 [thread overview]
Message-ID: <1394315010-22481-2-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1394315010-22481-1-git-send-email-kerolasa@iki.fi>
Earlier only the last option argument took effect.
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/mcookie.c | 73 +++++++++++++++++++++++++++++++---------------------
1 file changed, 44 insertions(+), 29 deletions(-)
diff --git a/misc-utils/mcookie.c b/misc-utils/mcookie.c
index 63eeb5b..41f5a0c 100644
--- a/misc-utils/mcookie.c
+++ b/misc-utils/mcookie.c
@@ -23,6 +23,7 @@
#include "nls.h"
#include "closestream.h"
#include "randutils.h"
+#include "xalloc.h"
#include <fcntl.h>
#include <getopt.h>
@@ -69,15 +70,45 @@ static void __attribute__((__noreturn__)) usage(FILE * out)
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[] = {
@@ -93,6 +124,13 @@ int main(int argc, char **argv)
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) {
@@ -100,7 +138,8 @@ int main(int argc, char **argv)
verbose = 1;
break;
case 'f':
- file = optarg;
+ files[nfiles] = optarg;
+ nfiles++;
break;
case 'V':
printf(UTIL_LINUX_VERSION);
@@ -111,32 +150,8 @@ int main(int argc, char **argv)
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);
--
1.9.0
next prev parent reply other threads:[~2014-03-08 21:43 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-03-08 21:43 [PATCH 1/5] mcookie: use lib/randutils Sami Kerola
2014-03-08 21:43 ` Sami Kerola [this message]
2014-03-08 21:43 ` [PATCH 3/5] mcookie: use same variable type consistently Sami Kerola
2014-03-08 21:43 ` [PATCH 4/5] mcookie: use control structure, and fix usage() Sami Kerola
2014-03-08 21:43 ` [PATCH 5/5] mcookie: add --max-size option Sami Kerola
2014-03-26 11:12 ` [PATCH 1/5] mcookie: use lib/randutils Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1394315010-22481-2-git-send-email-kerolasa@iki.fi \
--to=kerolasa@iki.fi \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox