]> git.ipfire.org Git - thirdparty/util-linux.git/blame - misc-utils/uuidgen.c
Merge branch 'meson-more-build-options' of https://github.com/jwillikers/util-linux
[thirdparty/util-linux.git] / misc-utils / uuidgen.c
CommitLineData
0140c397
KZ
1/*
2 * gen_uuid.c --- generate a DCE-compatible uuid
3 *
4 * Copyright (C) 1999, Andreas Dilger and Theodore Ts'o
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include <stdio.h>
0140c397 13#include <stdlib.h>
0140c397 14#include <getopt.h>
0140c397
KZ
15
16#include "uuid.h"
17#include "nls.h"
0379a55d 18#include "c.h"
c05a80ca 19#include "closestream.h"
06d3a851 20#include "strutils.h"
28387664 21#include "optutils.h"
0b14dcd7 22#include "xalloc.h"
0140c397 23
86be6a32 24static void __attribute__((__noreturn__)) usage(void)
0140c397 25{
86be6a32 26 FILE *out = stdout;
db433bf7 27 fputs(USAGE_HEADER, out);
44697763
KZ
28 fprintf(out,
29 _(" %s [options]\n"), program_invocation_short_name);
0379a55d 30
451dbcfa
BS
31 fputs(USAGE_SEPARATOR, out);
32 fputs(_("Create a new UUID value.\n"), out);
33
db433bf7 34 fputs(USAGE_OPTIONS, out);
06d3a851
KZ
35 fputs(_(" -r, --random generate random-based uuid\n"), out);
36 fputs(_(" -t, --time generate time-based uuid\n"), out);
37 fputs(_(" -n, --namespace <ns> generate hash-based uuid in this namespace\n"), out);
bad4c729 38 fprintf(out, _(" available namespaces: %s\n"), "@dns @url @oid @x500");
06d3a851
KZ
39 fputs(_(" -N, --name <name> generate hash-based uuid from this name\n"), out);
40 fputs(_(" -m, --md5 generate md5 hash\n"), out);
41 fputs(_(" -C, --count <num> generate more uuids in loop\n"), out);
42 fputs(_(" -s, --sha1 generate sha1 hash\n"), out);
43 fputs(_(" -x, --hex interpret name as hex string\n"), out);
504c03ec 44 fputs(USAGE_SEPARATOR, out);
bad4c729
MY
45 fprintf(out, USAGE_HELP_OPTIONS(21));
46 fprintf(out, USAGE_MAN_TAIL("uuidgen(1)"));
86be6a32 47 exit(EXIT_SUCCESS);
0140c397
KZ
48}
49
c6f1ec68
PP
50static char *unhex(const char *value, size_t *valuelen)
51{
52 char c, *value2;
53 unsigned n, x;
54
55 if (*valuelen % 2 != 0) {
56badstring:
451d0e9e 57 warnx(_("not a valid hex string"));
c6f1ec68
PP
58 errtryhelp(EXIT_FAILURE);
59 }
60
0b14dcd7 61 value2 = xmalloc(*valuelen / 2 + 1);
c6f1ec68
PP
62
63 for (x = n = 0; n < *valuelen; n++) {
64 c = value[n];
65 if ('0' <= c && c <= '9')
66 x += c - '0';
67 else if (('a' <= c && c <= 'f') || ('A' <= c && c <= 'F'))
68 x += (c - 'A' + 10) & 0xf;
69 else
70 goto badstring;
71
72 if (n % 2 == 0)
73 x *= 16;
74 else {
75 value2[n / 2] = x;
76 x = 0;
77 }
78 }
79 value2[n / 2] = '\0';
80
81 *valuelen = (n / 2);
82
83 return value2;
84}
85
0140c397
KZ
86int
87main (int argc, char *argv[])
88{
89 int c;
c6f1ec68 90 int do_type = 0, is_hex = 0;
b443c177 91 char str[UUID_STR_LEN];
c6f1ec68
PP
92 char *namespace = NULL, *name = NULL;
93 size_t namelen = 0;
94 uuid_t ns, uu;
06d3a851 95 unsigned int count = 1, i;
0140c397 96
0379a55d 97 static const struct option longopts[] = {
71383195 98 {"random", no_argument, NULL, 'r'},
0379a55d
SK
99 {"time", no_argument, NULL, 't'},
100 {"version", no_argument, NULL, 'V'},
101 {"help", no_argument, NULL, 'h'},
c6f1ec68
PP
102 {"namespace", required_argument, NULL, 'n'},
103 {"name", required_argument, NULL, 'N'},
104 {"md5", no_argument, NULL, 'm'},
06d3a851 105 {"count", required_argument, NULL, 'C'},
c6f1ec68
PP
106 {"sha1", no_argument, NULL, 's'},
107 {"hex", no_argument, NULL, 'x'},
0379a55d
SK
108 {NULL, 0, NULL, 0}
109 };
110
28387664
KZ
111 static const ul_excl_t excl[] = {
112 { 'C', 'm', 's' },
113 { 'N', 'r', 't' },
114 { 'm', 'r', 's', 't' },
115 { 'n', 'r', 't' },
116 { 0 }
117 };
118 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
119
0140c397
KZ
120 setlocale(LC_ALL, "");
121 bindtextdomain(PACKAGE, LOCALEDIR);
122 textdomain(PACKAGE);
2c308875 123 close_stdout_atexit();
0140c397 124
06d3a851 125 while ((c = getopt_long(argc, argv, "C:rtVhn:N:msx", longopts, NULL)) != -1) {
28387664
KZ
126
127 err_exclusive_options(c, longopts, excl, excl_st);
128
0140c397
KZ
129 switch (c) {
130 case 't':
6328799d 131 do_type = UUID_TYPE_DCE_TIME;
0140c397
KZ
132 break;
133 case 'r':
6328799d 134 do_type = UUID_TYPE_DCE_RANDOM;
0140c397 135 break;
c6f1ec68
PP
136 case 'n':
137 namespace = optarg;
138 break;
139 case 'N':
140 name = optarg;
141 break;
142 case 'm':
143 do_type = UUID_TYPE_DCE_MD5;
144 break;
06d3a851
KZ
145 case 'C':
146 count = strtou32_or_err(optarg, _("invalid count argument"));
147 break;
c6f1ec68
PP
148 case 's':
149 do_type = UUID_TYPE_DCE_SHA1;
150 break;
151 case 'x':
152 is_hex = 1;
153 break;
2c308875 154
0379a55d 155 case 'h':
86be6a32 156 usage();
2c308875
KZ
157 case 'V':
158 print_version(EXIT_SUCCESS);
0140c397 159 default:
677ec86c 160 errtryhelp(EXIT_FAILURE);
0140c397 161 }
06d3a851 162 }
0140c397 163
c6f1ec68
PP
164 if (namespace) {
165 if (!name) {
451d0e9e 166 warnx(_("--namespace requires --name argument"));
c6f1ec68
PP
167 errtryhelp(EXIT_FAILURE);
168 }
169 if (do_type != UUID_TYPE_DCE_MD5 && do_type != UUID_TYPE_DCE_SHA1) {
451d0e9e 170 warnx(_("--namespace requires --md5 or --sha1"));
c6f1ec68
PP
171 errtryhelp(EXIT_FAILURE);
172 }
173 } else {
174 if (name) {
451d0e9e 175 warnx(_("--name requires --namespace argument"));
c6f1ec68
PP
176 errtryhelp(EXIT_FAILURE);
177 }
178 if (do_type == UUID_TYPE_DCE_MD5 || do_type == UUID_TYPE_DCE_SHA1) {
451d0e9e 179 warnx(_("--md5 or --sha1 requires --namespace argument"));
c6f1ec68
PP
180 errtryhelp(EXIT_FAILURE);
181 }
182 }
183
184 if (name) {
185 namelen = strlen(name);
186 if (is_hex)
187 name = unhex(name, &namelen);
188 }
189
06d3a851
KZ
190 for (i = 0; i < count; i++) {
191 switch (do_type) {
192 case UUID_TYPE_DCE_TIME:
193 uuid_generate_time(uu);
194 break;
195 case UUID_TYPE_DCE_RANDOM:
196 uuid_generate_random(uu);
197 break;
198 case UUID_TYPE_DCE_MD5:
199 case UUID_TYPE_DCE_SHA1:
200 if (namespace[0] == '@' && namespace[1] != '\0') {
201 const uuid_t *uuidptr;
202
203 uuidptr = uuid_get_template(&namespace[1]);
204 if (uuidptr == NULL) {
205 warnx(_("unknown namespace alias: '%s'"), namespace);
206 errtryhelp(EXIT_FAILURE);
207 }
208 memcpy(ns, *uuidptr, sizeof(ns));
209 } else {
210 if (uuid_parse(namespace, ns) != 0) {
211 warnx(_("invalid uuid for namespace: '%s'"), namespace);
212 errtryhelp(EXIT_FAILURE);
213 }
c6f1ec68 214 }
06d3a851
KZ
215 if (do_type == UUID_TYPE_DCE_MD5)
216 uuid_generate_md5(uu, ns, name, namelen);
217 else
218 uuid_generate_sha1(uu, ns, name, namelen);
219 break;
220 default:
221 uuid_generate(uu);
222 break;
c6f1ec68 223 }
0140c397 224
06d3a851 225 uuid_unparse(uu, str);
0140c397 226
06d3a851
KZ
227 printf("%s\n", str);
228 }
0140c397 229
c6f1ec68
PP
230 if (is_hex)
231 free(name);
232
0379a55d 233 return EXIT_SUCCESS;
0140c397 234}