]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/mcookie.c
dmesg: add --follow-new
[thirdparty/util-linux.git] / misc-utils / mcookie.c
index 7b60dd825eb61fc167f89c931f277e0e3b98581a..549531454b51456fa0bb50f3d9b397734b81e288 100644 (file)
@@ -3,7 +3,7 @@
  * Revised: Fri Mar 19 07:48:01 1999 by faith@acm.org
  * Public Domain 1995, 1999 Rickard E. Faith (faith@acm.org)
  * This program comes with ABSOLUTELY NO WARRANTY.
- * 
+ *
  * This program gathers some random bits of data and used the MD5
  * message-digest algorithm to generate a 128-bit hexadecimal number for
  * use with xauth(1).
@@ -41,7 +41,7 @@ enum {
 };
 
 struct mcookie_control {
-       struct  MD5Context ctx;
+       struct  UL_MD5Context ctx;
        char    **files;
        size_t  nfiles;
        uint64_t maxsz;
@@ -67,31 +67,38 @@ static uint64_t hash_file(struct mcookie_control *ctl, int fd)
                r = read_all(fd, (char *) buf, rdsz);
                if (r < 0)
                        break;
-               MD5Update(&ctl->ctx, buf, r);
+               ul_MD5Update(&ctl->ctx, buf, r);
                count += r;
        }
        /* Separate files with a null byte */
        buf[0] = '\0';
-       MD5Update(&ctl->ctx, buf, 1);
+       ul_MD5Update(&ctl->ctx, buf, 1);
        return count;
 }
 
-static void __attribute__ ((__noreturn__)) usage(FILE * out)
+static void __attribute__((__noreturn__)) usage(void)
 {
+       FILE *out = stdout;
        fputs(USAGE_HEADER, out);
        fprintf(out, _(" %s [options]\n"), program_invocation_short_name);
 
+       fputs(USAGE_SEPARATOR, out);
+       fputs(_("Generate magic cookies for xauth.\n"), out);
+
        fputs(USAGE_OPTIONS, out);
        fputs(_(" -f, --file <file>     use file as a cookie seed\n"), out);
        fputs(_(" -m, --max-size <num>  limit how much is read from seed files\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)"));
+       printf(USAGE_HELP_OPTIONS(23));
+
+       fputs(USAGE_ARGUMENTS, out);
+       printf(USAGE_ARG_SIZE(_("<num>")));
 
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       printf(USAGE_MAN_TAIL("mcookie(1)"));
+
+       exit(EXIT_SUCCESS);
 }
 
 static void randomness_from_files(struct mcookie_control *ctl)
@@ -118,10 +125,8 @@ static void randomness_from_files(struct mcookie_control *ctl)
                                           "Got %zu bytes from %s\n", count),
                                        count, fname);
 
-                       if (fd != STDIN_FILENO)
-                               if (close(fd))
-                                       err(EXIT_FAILURE,
-                                           _("closing %s failed"), fname);
+                       if (fd != STDIN_FILENO && close(fd))
+                               err(EXIT_FAILURE, _("closing %s failed"), fname);
                }
        }
 }
@@ -130,7 +135,7 @@ int main(int argc, char **argv)
 {
        struct mcookie_control ctl = { .verbose = 0 };
        size_t i;
-       unsigned char digest[MD5LENGTH];
+       unsigned char digest[UL_MD5LENGTH];
        unsigned char buf[RAND_BYTES];
        int c;
 
@@ -146,10 +151,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
-
-       if (2 < argc)
-               ctl.files = xmalloc(sizeof(char *) * argc);
+       close_stdout_atexit();
 
        while ((c = getopt_long(argc, argv, "f:m:vVh", longopts, NULL)) != -1) {
                switch (c) {
@@ -157,36 +159,40 @@ int main(int argc, char **argv)
                        ctl.verbose = 1;
                        break;
                case 'f':
+                       if (!ctl.files)
+                               ctl.files = xmalloc(sizeof(char *) * argc);
                        ctl.files[ctl.nfiles++] = optarg;
                        break;
                case 'm':
                        ctl.maxsz = strtosize_or_err(optarg,
                                                     _("failed to parse length"));
                        break;
+
                case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
+                       print_version(EXIT_SUCCESS);
                case 'h':
-                       usage(stdout);
+                       usage();
                default:
-                       usage(stderr);
+                       errtryhelp(EXIT_FAILURE);
                }
        }
 
        if (ctl.maxsz && ctl.nfiles == 0)
-               warnx(_("--max-size ignored when used without --file."));
+               warnx(_("--max-size ignored when used without --file"));
 
+       ul_MD5Init(&ctl.ctx);
        randomness_from_files(&ctl);
        free(ctl.files);
 
        random_get_bytes(&buf, RAND_BYTES);
-       MD5Update(&ctl.ctx, buf, RAND_BYTES);
+       ul_MD5Update(&ctl.ctx, buf, RAND_BYTES);
        if (ctl.verbose)
-               fprintf(stderr, _("Got %d bytes from %s\n"), RAND_BYTES,
-                               random_tell_source());
+               fprintf(stderr, P_("Got %d byte from %s\n",
+                                  "Got %d bytes from %s\n", RAND_BYTES),
+                               RAND_BYTES, random_tell_source());
 
-       MD5Final(digest, &ctl.ctx);
-       for (i = 0; i < MD5LENGTH; i++)
+       ul_MD5Final(digest, &ctl.ctx);
+       for (i = 0; i < UL_MD5LENGTH; i++)
                printf("%02x", digest[i]);
        putchar('\n');