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