summaryrefslogtreecommitdiffstats
path: root/misc-utils/mcookie.c
diff options
context:
space:
mode:
authorSami Kerola2011-06-11 19:14:59 +0200
committerSami Kerola2011-06-25 12:37:43 +0200
commite6ebab66d4c7056d98bd67d3c8f7bc52d86b470d (patch)
treef0792bd4b8587634d6e481bea252ed431c6a0681 /misc-utils/mcookie.c
parentdocs: inform about mcookie long options (diff)
downloadkernel-qcow2-util-linux-e6ebab66d4c7056d98bd67d3c8f7bc52d86b470d.tar.gz
kernel-qcow2-util-linux-e6ebab66d4c7056d98bd67d3c8f7bc52d86b470d.tar.xz
kernel-qcow2-util-linux-e6ebab66d4c7056d98bd67d3c8f7bc52d86b470d.zip
mcookie: change coding style
Now in align with README.devel. Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Diffstat (limited to 'misc-utils/mcookie.c')
-rw-r--r--misc-utils/mcookie.c294
1 files changed, 152 insertions, 142 deletions
diff --git a/misc-utils/mcookie.c b/misc-utils/mcookie.c
index 35fd8ab6b..61896e17d 100644
--- a/misc-utils/mcookie.c
+++ b/misc-utils/mcookie.c
@@ -18,167 +18,177 @@
*
*/
+#include "c.h"
+#include "md5.h"
+#include "nls.h"
+#include <fcntl.h>
+#include <getopt.h>
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
-#include <fcntl.h>
-#include "md5.h"
#include <sys/time.h>
#include <unistd.h>
-#include "nls.h"
-#include <getopt.h>
-#include "c.h"
-#define BUFFERSIZE 4096
+#define BUFFERSIZE 4096
struct rngs {
- const char *path;
- int minlength, maxlength;
+ const char *path;
+ int minlength, maxlength;
} rngs[] = {
- { "/dev/random", 16, 16 }, /* 16 bytes = 128 bits suffice */
- { "/proc/interrupts", 0, 0 },
- { "/proc/slabinfo", 0, 0 },
- { "/proc/stat", 0, 0 },
- { "/dev/urandom", 32, 64 },
+ {"/dev/random", 16, 16}, /* 16 bytes = 128 bits suffice */
+ {"/proc/interrupts", 0, 0},
+ {"/proc/slabinfo", 0, 0},
+ {"/proc/stat", 0, 0},
+ {"/dev/urandom", 32, 64},
};
-#define RNGS (sizeof(rngs)/sizeof(struct rngs))
-int Verbose = 0;
+#define RNGS (sizeof(rngs)/sizeof(struct rngs))
/* The basic function to hash a file */
-static off_t
-hash_file(struct MD5Context *ctx, int fd)
+static off_t hash_file(struct MD5Context *ctx, int fd)
{
- off_t count = 0;
- ssize_t r;
- unsigned char buf[BUFFERSIZE];
-
- while ((r = read(fd, buf, sizeof(buf))) > 0) {
- MD5Update(ctx, buf, r);
- count += r;
- }
- /* Separate files with a null byte */
- buf[0] = 0;
- MD5Update(ctx, buf, 1);
- return count;
+ off_t count = 0;
+ ssize_t r;
+ unsigned char buf[BUFFERSIZE];
+
+ while ((r = read(fd, buf, sizeof(buf))) > 0) {
+ MD5Update(ctx, buf, r);
+ count += r;
+ }
+ /* Separate files with a null byte */
+ buf[0] = '\0';
+ MD5Update(ctx, buf, 1);
+ return count;
}
static void __attribute__ ((__noreturn__)) usage(FILE * out)
{
- fprintf(out, _("Usage: %s [options]\n"),
- program_invocation_short_name);
+ fprintf(out, _("Usage: %s [options]\n"),
+ program_invocation_short_name);
- fprintf(out, _("\nOptions:\n"
- " -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"));
+ fprintf(out, _("\nOptions:\n"
+ " -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"));
- exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+ exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
-int main( int argc, char **argv )
+
+int main(int argc, char **argv)
{
- size_t i;
- struct MD5Context ctx;
- unsigned char digest[16];
- unsigned char buf[BUFFERSIZE];
- int fd;
- int c;
- pid_t pid;
- char *file = NULL;
- int r;
- struct timeval tv;
- struct timezone tz;
-
- static const struct option longopts[] = {
- {"file", required_argument, NULL, 'f'},
- {"verbose", no_argument, NULL, 'v'},
- {"version", no_argument, NULL, 'V'},
- {"help", no_argument, NULL, 'h'},
- {NULL, 0, NULL, 0}
- };
-
- setlocale(LC_ALL, "");
- bindtextdomain(PACKAGE, LOCALEDIR);
- textdomain(PACKAGE);
-
- while ((c = getopt_long(argc, argv, "f:vVh", longopts, NULL)) != -1)
- switch (c) {
- case 'v':
- Verbose = 1;
- break;
- case 'f':
- file = optarg;
- break;
- case 'V':
- printf(_("%s from %s\n"), program_invocation_short_name,
- PACKAGE_STRING);
- return EXIT_SUCCESS;
- case 'h':
- usage(stdout);
- default:
- usage(stderr);
- }
-
- MD5Init( &ctx );
- gettimeofday( &tv, &tz );
- MD5Update( &ctx, (unsigned char *)&tv, sizeof( tv ) );
-
- pid = getppid();
- MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
- pid = getpid();
- MD5Update( &ctx, (unsigned char *)&pid, sizeof( pid ));
-
- if (file) {
- int count = 0;
-
- if (file[0] == '-' && !file[1])
- fd = STDIN_FILENO;
- else
- fd = open( file, O_RDONLY );
-
- if (fd < 0) {
- warn( _("Could not open %s"), file );
- } else {
- count = hash_file( &ctx, fd );
- if (Verbose)
- fprintf( stderr, _("Got %d bytes from %s\n"), count, file );
-
- if (fd != STDIN_FILENO)
- if(close( fd ))
- err(EXIT_FAILURE, _("closing %s failed"), file);
- }
- }
-
- for (i = 0; i < RNGS; i++) {
- if ((fd = open( rngs[i].path, O_RDONLY|O_NONBLOCK )) >= 0) {
- int count = sizeof(buf);
-
- if (rngs[i].maxlength && count > rngs[i].maxlength)
- count = rngs[i].maxlength;
- r = read( fd, buf, count );
- if (r > 0)
- MD5Update( &ctx, buf, r );
- else
- r = 0;
- close( fd );
- if (Verbose)
- fprintf( stderr, _("Got %d bytes from %s\n"), r, rngs[i].path );
- if (rngs[i].minlength && r >= rngs[i].minlength)
- break;
- } else if (Verbose)
- warn( _("Could not open %s"), rngs[i].path );
- }
-
- MD5Final( digest, &ctx );
- for (i = 0; i < 16; i++) printf( "%02x", digest[i] );
- putchar ( '\n' );
-
- /*
- * The following is important for cases like disk full, so shell scripts
- * can bomb out properly rather than think they succeeded.
- */
- if (fflush(stdout) < 0 || fclose(stdout) < 0)
- return EXIT_FAILURE;
-
- return EXIT_SUCCESS;
+ size_t i;
+ struct MD5Context ctx;
+ unsigned char digest[16];
+ unsigned char buf[BUFFERSIZE];
+ int fd;
+ int c;
+ pid_t pid;
+ char *file = NULL;
+ int verbose = 0;
+ int r;
+ struct timeval tv;
+ struct timezone tz;
+
+ static const struct option longopts[] = {
+ {"file", required_argument, NULL, 'f'},
+ {"verbose", no_argument, NULL, 'v'},
+ {"version", no_argument, NULL, 'V'},
+ {"help", no_argument, NULL, 'h'},
+ {NULL, 0, NULL, 0}
+ };
+
+ setlocale(LC_ALL, "");
+ bindtextdomain(PACKAGE, LOCALEDIR);
+ textdomain(PACKAGE);
+
+ while ((c =
+ getopt_long(argc, argv, "f:vVh", longopts, NULL)) != -1)
+ switch (c) {
+ case 'v':
+ verbose = 1;
+ break;
+ case 'f':
+ file = optarg;
+ break;
+ case 'V':
+ printf(_("%s from %s\n"),
+ program_invocation_short_name,
+ PACKAGE_STRING);
+ return EXIT_SUCCESS;
+ case 'h':
+ usage(stdout);
+ default:
+ usage(stderr);
+ }
+
+ MD5Init(&ctx);
+ gettimeofday(&tv, &tz);
+ MD5Update(&ctx, (unsigned char *) &tv, sizeof(tv));
+
+ pid = getppid();
+ MD5Update(&ctx, (unsigned char *) &pid, sizeof(pid));
+ pid = getpid();
+ MD5Update(&ctx, (unsigned char *) &pid, sizeof(pid));
+
+ if (file) {
+ int count = 0;
+
+ if (file[0] == '-' && !file[1])
+ fd = STDIN_FILENO;
+ else
+ fd = open(file, O_RDONLY);
+
+ if (fd < 0) {
+ warn(_("Could not open %s"), file);
+ } else {
+ count = hash_file(&ctx, fd);
+ if (verbose)
+ fprintf(stderr,
+ _("Got %d bytes from %s\n"), count,
+ file);
+
+ if (fd != STDIN_FILENO)
+ if (close(fd))
+ err(EXIT_FAILURE,
+ _("closing %s failed"), file);
+ }
+ }
+
+ for (i = 0; i < RNGS; i++) {
+ if ((fd = open(rngs[i].path, O_RDONLY | O_NONBLOCK)) >= 0) {
+ int count = sizeof(buf);
+
+ if (rngs[i].maxlength && count > rngs[i].maxlength)
+ count = rngs[i].maxlength;
+ r = read(fd, buf, count);
+ if (r > 0)
+ MD5Update(&ctx, buf, r);
+ else
+ r = 0;
+ close(fd);
+ if (verbose)
+ fprintf(stderr,
+ _("Got %d bytes from %s\n"), r,
+ rngs[i].path);
+ if (rngs[i].minlength && r >= rngs[i].minlength)
+ break;
+ } else if (verbose)
+ warn(_("Could not open %s"), rngs[i].path);
+ }
+
+ MD5Final(digest, &ctx);
+ for (i = 0; i < 16; i++)
+ printf("%02x", digest[i]);
+ putchar('\n');
+
+ /*
+ * The following is important for cases like disk full,
+ * so shell scripts can bomb out properly rather than
+ * think they succeeded.
+ */
+ if (fflush(stdout) < 0 || fclose(stdout) < 0)
+ return EXIT_FAILURE;
+
+ return EXIT_SUCCESS;
}