From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 4/5] mcookie: use control structure, and fix usage()
Date: Sat, 8 Mar 2014 15:43:29 -0600 [thread overview]
Message-ID: <1394315010-22481-4-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1394315010-22481-1-git-send-email-kerolasa@iki.fi>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
misc-utils/mcookie.c | 95 +++++++++++++++++++++++++++-------------------------
1 file changed, 49 insertions(+), 46 deletions(-)
diff --git a/misc-utils/mcookie.c b/misc-utils/mcookie.c
index 9167440..31f33e4 100644
--- a/misc-utils/mcookie.c
+++ b/misc-utils/mcookie.c
@@ -38,78 +38,82 @@ enum {
RAND_BYTES = 128
};
+struct mcookie_control {
+ struct MD5Context ctx;
+ char **files;
+ int nfiles;
+ int fd;
+ unsigned int
+ verbose:1;
+};
+
/* The basic function to hash a file */
-static size_t hash_file(struct MD5Context *ctx, int fd)
+static size_t hash_file(struct mcookie_control *ctl)
{
size_t count = 0;
ssize_t r;
unsigned char buf[BUFFERSIZE];
- while ((r = read(fd, buf, sizeof(buf))) > 0) {
- MD5Update(ctx, buf, r);
+ while ((r = read(ctl->fd, buf, sizeof(buf))) > 0) {
+ MD5Update(&(ctl->ctx), buf, r);
count += r;
}
/* Separate files with a null byte */
buf[0] = '\0';
- MD5Update(ctx, buf, 1);
+ MD5Update(&(ctl->ctx), buf, 1);
return count;
}
static void __attribute__((__noreturn__)) usage(FILE * out)
{
- fputs(_("\nUsage:\n"), out);
- fprintf(out,
- _(" %s [options]\n"), program_invocation_short_name);
-
- fputs(_("\nOptions:\n"), out);
- fputs(_(" -f, --file <file> use file as a cookie seed\n"
- " -v, --verbose explain what is being done\n"
- " -V, --version output version information and exit\n"
- " -h, --help display this help and exit\n\n"), out);
-
+ fputs(USAGE_HEADER, out);
+ fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
+ fputs(USAGE_OPTIONS, out);
+ fputs(_(" -f, --file <file> use file as a cookie seed\n"), out);
+ fputs(_(" -v, --verbose explain what is being done\n"), out);
+ fputs(USAGE_SEPARATOR, out);
+ fputs(USAGE_HELP, out);
+ fputs(USAGE_VERSION, out);
+ fprintf(out, USAGE_MAN_TAIL("mcookie(1)"));
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
-static void randomness_from_files(char **files, int nfiles,
- struct MD5Context *ctx, int verbose)
+static void randomness_from_files(struct mcookie_control *ctl)
{
- int fd, i;
+ int i;
size_t count = 0;
- for (i = 0; i < nfiles; i++) {
- if (files[i][0] == '-' && !files[i][1])
- fd = STDIN_FILENO;
+ for (i = 0; i < ctl->nfiles; i++) {
+ if (ctl->files[i][0] == '-' && !ctl->files[i][1])
+ ctl->fd = STDIN_FILENO;
else
- fd = open(files[i], O_RDONLY);
+ ctl->fd = open(ctl->files[i], O_RDONLY);
- if (fd < 0) {
- warn(_("cannot open %s"), files[i]);
+ if (ctl->fd < 0) {
+ warn(_("cannot open %s"), ctl->files[i]);
} else {
- count = hash_file(ctx, fd);
- if (verbose)
+ count = hash_file(ctl);
+ if (ctl->verbose)
fprintf(stderr,
P_("Got %zu byte from %s\n",
"Got %zu bytes from %s\n", count),
- count, files[i]);
+ count, ctl->files[i]);
- if (fd != STDIN_FILENO)
- if (close(fd))
+ if (ctl->fd != STDIN_FILENO)
+ if (close(ctl->fd))
err(EXIT_FAILURE,
- _("closing %s failed"), files[i]);
+ _("closing %s failed"), ctl->files[i]);
}
}
}
int main(int argc, char **argv)
{
+ struct mcookie_control ctl;
size_t i;
- struct MD5Context ctx;
unsigned char digest[MD5LENGTH];
unsigned char buf[RAND_BYTES];
- char **files = NULL;
- int nfiles;
int c;
- int verbose = 0;
static const struct option longopts[] = {
{"file", required_argument, NULL, 'f'},
@@ -124,22 +128,21 @@ int main(int argc, char **argv)
textdomain(PACKAGE);
atexit(close_stdout);
- MD5Init(&ctx);
+ memset(&ctl, 0, sizeof(ctl));
+ MD5Init(&(ctl.ctx));
- if (2 < argc) {
- files = xmalloc(sizeof(char *) * argc);
- nfiles = 0;
- }
+ if (2 < argc)
+ ctl.files = xmalloc(sizeof(char *) * argc);
while ((c =
getopt_long(argc, argv, "f:vVh", longopts, NULL)) != -1)
switch (c) {
case 'v':
- verbose = 1;
+ ctl.verbose = 1;
break;
case 'f':
- files[nfiles] = optarg;
- nfiles++;
+ ctl.files[ctl.nfiles] = optarg;
+ ctl.nfiles++;
break;
case 'V':
printf(UTIL_LINUX_VERSION);
@@ -150,16 +153,16 @@ int main(int argc, char **argv)
usage(stderr);
}
- randomness_from_files(files, nfiles, &ctx, verbose);
- free(files);
+ randomness_from_files(&ctl);
+ free(ctl.files);
random_get_bytes(&buf, RAND_BYTES);
- MD5Update(&ctx, buf, RAND_BYTES);
- if (verbose)
+ MD5Update(&(ctl.ctx), buf, RAND_BYTES);
+ if (ctl.verbose)
fprintf(stderr,
_("Got %d bytes from %s\n"), RAND_BYTES, random_tell_source());
- MD5Final(digest, &ctx);
+ MD5Final(digest, &(ctl.ctx));
for (i = 0; i < MD5LENGTH; i++)
printf("%02x", digest[i]);
putchar('\n');
--
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 ` [PATCH 2/5] mcookie: allow --file option be defined multiple times Sami Kerola
2014-03-08 21:43 ` [PATCH 3/5] mcookie: use same variable type consistently Sami Kerola
2014-03-08 21:43 ` Sami Kerola [this message]
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-4-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