]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/mcookie.c
include/pidfd-utils: remove hardcoded syscall fallback
[thirdparty/util-linux.git] / misc-utils / mcookie.c
index cc2d7ce6a38b18e059b96e5d088b96392108856f..99df4f4a6d973d8dad5b102553fe3da3088b8693 100644 (file)
@@ -1,9 +1,13 @@
-/* mcookie.c -- Generates random numbers for xauth
+/*
+ * No copyright is claimed.  This code is in the public domain; do with
+ * it what you wish.
+ *
+ * mcookie.c -- Generates random numbers for xauth
  * Created: Fri Feb  3 10:42:48 1995 by faith@cs.unc.edu
  * 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 +45,7 @@ enum {
 };
 
 struct mcookie_control {
-       struct  MD5Context ctx;
+       struct  UL_MD5Context ctx;
        char    **files;
        size_t  nfiles;
        uint64_t maxsz;
@@ -65,19 +69,20 @@ static uint64_t hash_file(struct mcookie_control *ctl, int fd)
                        rdsz = wanted - count;
 
                r = read_all(fd, (char *) buf, rdsz);
-               if (r < 0)
+               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);
 
@@ -90,11 +95,14 @@ static void __attribute__ ((__noreturn__)) usage(FILE * 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_HELP_OPTIONS(23));
+
+       fputs(USAGE_ARGUMENTS, out);
+       fprintf(out, USAGE_ARG_SIZE(_("<num>")));
+
        fprintf(out, USAGE_MAN_TAIL("mcookie(1)"));
 
-       exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
+       exit(EXIT_SUCCESS);
 }
 
 static void randomness_from_files(struct mcookie_control *ctl)
@@ -131,7 +139,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;
 
@@ -147,7 +155,7 @@ int main(int argc, char **argv)
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
 
        while ((c = getopt_long(argc, argv, "f:m:vVh", longopts, NULL)) != -1) {
                switch (c) {
@@ -163,11 +171,11 @@ int main(int argc, char **argv)
                        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:
                        errtryhelp(EXIT_FAILURE);
                }
@@ -176,18 +184,19 @@ int main(int argc, char **argv)
        if (ctl.maxsz && ctl.nfiles == 0)
                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_random_get_bytes(&buf, RAND_BYTES);
+       ul_MD5Update(&ctl.ctx, buf, RAND_BYTES);
        if (ctl.verbose)
                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');