]> git.ipfire.org Git - thirdparty/util-linux.git/blobdiff - misc-utils/uuidgen.c
scriptreplay: fix uninitialized value [coverity scan]
[thirdparty/util-linux.git] / misc-utils / uuidgen.c
index c1970695389cdf6c17eba99e57891c80b7069b4c..56dea3c711f1b99946b41d3902322623d8905ac7 100644 (file)
@@ -17,6 +17,9 @@
 #include "nls.h"
 #include "c.h"
 #include "closestream.h"
+#include "strutils.h"
+#include "optutils.h"
+#include "xalloc.h"
 
 static void __attribute__((__noreturn__)) usage(void)
 {
@@ -29,16 +32,20 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_("Create a new UUID value.\n"), out);
 
        fputs(USAGE_OPTIONS, out);
-       fputs(_(" -r, --random        generate random-based uuid\n"), out);
-       fputs(_(" -t, --time          generate time-based uuid\n"), out);
-       fputs(_(" -n, --namespace ns  generate hash-based uuid in this namespace\n"), out);
-       fputs(_(" -N, --name name     generate hash-based uuid from this name\n"), out);
-       fputs(_(" -m, --md5           generate md5 hash\n"), out);
-       fputs(_(" -s, --sha1          generate sha1 hash\n"), out);
-       fputs(_(" -x, --hex           interpret name as hex string\n"), out);
+       fputs(_(" -r, --random          generate random-based uuid\n"), out);
+       fputs(_(" -t, --time            generate time-based uuid\n"), out);
+       fputs(_(" -n, --namespace <ns>  generate hash-based uuid in this namespace\n"), out);
+       fprintf(out, _("                        available namespaces: %s\n"), "@dns @url @oid @x500");
+       fputs(_(" -N, --name <name>     generate hash-based uuid from this name\n"), out);
+       fputs(_(" -m, --md5             generate md5 hash\n"), out);
+       fputs(_(" -C, --count <num>     generate more uuids in loop\n"), out);
+       fputs(_(" -s, --sha1            generate sha1 hash\n"), out);
+       fputs(_(" -6, --time-v6         generate time-based v6 uuid\n"), out);
+       fputs(_(" -7, --time-v7         generate time-based v7 uuid\n"), out);
+       fputs(_(" -x, --hex             interpret name as hex string\n"), out);
        fputs(USAGE_SEPARATOR, out);
-       printf(USAGE_HELP_OPTIONS(18));
-       printf(USAGE_MAN_TAIL("uuidgen(1)"));
+       fprintf(out, USAGE_HELP_OPTIONS(21));
+       fprintf(out, USAGE_MAN_TAIL("uuidgen(1)"));
        exit(EXIT_SUCCESS);
 }
 
@@ -49,11 +56,11 @@ static char *unhex(const char *value, size_t *valuelen)
 
        if (*valuelen % 2 != 0) {
 badstring:
-               fprintf(stderr, "%s: not a valid hex string\n", program_invocation_short_name);
+               warnx(_("not a valid hex string"));
                errtryhelp(EXIT_FAILURE);
        }
 
-       value2 = malloc(*valuelen / 2 + 1);
+       value2 = xmalloc(*valuelen / 2 + 1);
 
        for (x = n = 0; n < *valuelen; n++) {
                c = value[n];
@@ -87,6 +94,7 @@ main (int argc, char *argv[])
        char   *namespace = NULL, *name = NULL;
        size_t namelen = 0;
        uuid_t ns, uu;
+       unsigned int count = 1, i;
 
        static const struct option longopts[] = {
                {"random", no_argument, NULL, 'r'},
@@ -96,17 +104,32 @@ main (int argc, char *argv[])
                {"namespace", required_argument, NULL, 'n'},
                {"name", required_argument, NULL, 'N'},
                {"md5", no_argument, NULL, 'm'},
+               {"count", required_argument, NULL, 'C'},
                {"sha1", no_argument, NULL, 's'},
+               {"time-v6", no_argument, NULL, '6'},
+               {"time-v7", no_argument, NULL, '7'},
                {"hex", no_argument, NULL, 'x'},
                {NULL, 0, NULL, 0}
        };
 
+       static const ul_excl_t excl[] = {
+               { '6', '7', 'm', 'r', 's', 't' },
+               { 'C', 'm', 's' },
+               { 'N', 'r', 't' },
+               { 'n', 'r', 't' },
+               { 0 }
+       };
+       int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
+
        setlocale(LC_ALL, "");
        bindtextdomain(PACKAGE, LOCALEDIR);
        textdomain(PACKAGE);
-       atexit(close_stdout);
+       close_stdout_atexit();
+
+       while ((c = getopt_long(argc, argv, "C:rtVhn:N:msx67", longopts, NULL)) != -1) {
+
+               err_exclusive_options(c, longopts, excl, excl_st);
 
-       while ((c = getopt_long(argc, argv, "rtVhn:N:msx", longopts, NULL)) != -1)
                switch (c) {
                case 't':
                        do_type = UUID_TYPE_DCE_TIME;
@@ -114,9 +137,6 @@ main (int argc, char *argv[])
                case 'r':
                        do_type = UUID_TYPE_DCE_RANDOM;
                        break;
-               case 'V':
-                       printf(UTIL_LINUX_VERSION);
-                       return EXIT_SUCCESS;
                case 'n':
                        namespace = optarg;
                        break;
@@ -126,34 +146,47 @@ main (int argc, char *argv[])
                case 'm':
                        do_type = UUID_TYPE_DCE_MD5;
                        break;
+               case 'C':
+                       count = strtou32_or_err(optarg, _("invalid count argument"));
+                       break;
                case 's':
                        do_type = UUID_TYPE_DCE_SHA1;
                        break;
                case 'x':
                        is_hex = 1;
                        break;
+               case '6':
+                       do_type = UUID_TYPE_DCE_TIME_V6;
+                       break;
+               case '7':
+                       do_type = UUID_TYPE_DCE_TIME_V7;
+                       break;
+
                case 'h':
                        usage();
+               case 'V':
+                       print_version(EXIT_SUCCESS);
                default:
                        errtryhelp(EXIT_FAILURE);
                }
+       }
 
        if (namespace) {
                if (!name) {
-                       fprintf(stderr, "%s: --namespace requires --name argument\n", program_invocation_short_name);
+                       warnx(_("--namespace requires --name argument"));
                        errtryhelp(EXIT_FAILURE);
                }
                if (do_type != UUID_TYPE_DCE_MD5 && do_type != UUID_TYPE_DCE_SHA1) {
-                       fprintf(stderr, "%s: --namespace requires --md5 or --sha1\n", program_invocation_short_name);
+                       warnx(_("--namespace requires --md5 or --sha1"));
                        errtryhelp(EXIT_FAILURE);
                }
        } else {
                if (name) {
-                       fprintf(stderr, "%s: --name requires --namespace argument\n", program_invocation_short_name);
+                       warnx(_("--name requires --namespace argument"));
                        errtryhelp(EXIT_FAILURE);
                }
                if (do_type == UUID_TYPE_DCE_MD5 || do_type == UUID_TYPE_DCE_SHA1) {
-                       fprintf(stderr, "%s: --md5 or --sha1 require --namespace\n", program_invocation_short_name);
+                       warnx(_("--md5 or --sha1 requires --namespace argument"));
                        errtryhelp(EXIT_FAILURE);
                }
        }
@@ -164,43 +197,51 @@ main (int argc, char *argv[])
                        name = unhex(name, &namelen);
        }
 
-       switch (do_type) {
-       case UUID_TYPE_DCE_TIME:
-               uuid_generate_time(uu);
-               break;
-       case UUID_TYPE_DCE_RANDOM:
-               uuid_generate_random(uu);
-               break;
-       case UUID_TYPE_DCE_MD5:
-       case UUID_TYPE_DCE_SHA1:
-               if (namespace[0] == '@' && namespace[1] != '\0') {
-                       const uuid_t *uuidptr;
-
-                       uuidptr = uuid_get_template(&namespace[1]);
-                       if (uuidptr == NULL) {
-                               fprintf(stderr, "%s: unknown namespace alias '%s'\n", program_invocation_short_name, namespace);
-                               errtryhelp(EXIT_FAILURE);
-                       }
-                       memcpy(ns, *uuidptr, sizeof(ns));
-               } else {
-                       if (uuid_parse(namespace, ns) != 0) {
-                               fprintf(stderr, "%s: invalid uuid for namespace '%s'\n", program_invocation_short_name, namespace);
-                               errtryhelp(EXIT_FAILURE);
+       for (i = 0; i < count; i++) {
+               switch (do_type) {
+               case UUID_TYPE_DCE_TIME:
+                       uuid_generate_time(uu);
+                       break;
+               case UUID_TYPE_DCE_TIME_V6:
+                       uuid_generate_time_v6(uu);
+                       break;
+               case UUID_TYPE_DCE_TIME_V7:
+                       uuid_generate_time_v7(uu);
+                       break;
+               case UUID_TYPE_DCE_RANDOM:
+                       uuid_generate_random(uu);
+                       break;
+               case UUID_TYPE_DCE_MD5:
+               case UUID_TYPE_DCE_SHA1:
+                       if (namespace[0] == '@' && namespace[1] != '\0') {
+                               const uuid_t *uuidptr;
+
+                               uuidptr = uuid_get_template(&namespace[1]);
+                               if (uuidptr == NULL) {
+                                       warnx(_("unknown namespace alias: '%s'"), namespace);
+                                       errtryhelp(EXIT_FAILURE);
+                               }
+                               memcpy(ns, *uuidptr, sizeof(ns));
+                       } else {
+                               if (uuid_parse(namespace, ns) != 0) {
+                                       warnx(_("invalid uuid for namespace: '%s'"), namespace);
+                                       errtryhelp(EXIT_FAILURE);
+                               }
                        }
+                       if (do_type == UUID_TYPE_DCE_MD5)
+                               uuid_generate_md5(uu, ns, name, namelen);
+                       else
+                               uuid_generate_sha1(uu, ns, name, namelen);
+                       break;
+               default:
+                       uuid_generate(uu);
+                       break;
                }
-               if (do_type == UUID_TYPE_DCE_MD5)
-                       uuid_generate_md5(uu, ns, name, namelen);
-               else
-                       uuid_generate_sha1(uu, ns, name, namelen);
-               break;
-       default:
-               uuid_generate(uu);
-               break;
-       }
 
-       uuid_unparse(uu, str);
+               uuid_unparse(uu, str);
 
-       printf("%s\n", str);
+               printf("%s\n", str);
+       }
 
        if (is_hex)
                free(name);