uuidgen: add -c for compact uuid

It generates the uuid string but without the hyphen

MFC After:		3 days
Reviews by:		tcberner
Differential Revision:	https://reviews.freebsd.org/D38820
This commit is contained in:
Baptiste Daroussin 2023-02-28 11:29:51 +01:00
parent 71a21ad489
commit b2b294f27c
2 changed files with 35 additions and 3 deletions

View file

@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd May 23, 2012
.Dd March 1, 2024
.Dt UUIDGEN 1
.Os
.Sh NAME
@ -34,6 +34,7 @@
.Nm
.Op Fl 1
.Op Fl r
.Op Fl c
.Op Fl n Ar count
.Op Fl o Ar filename
.Sh DESCRIPTION
@ -53,6 +54,8 @@ instructs
to not generate them in batch, but one at a time.
.It Fl r
This option controls creation of random UUID (version 4).
.It Fl c
This options contols creation of compact UUID (without hyphen).
.It Fl n
This option controls the number of identifiers generated.
By default, multiple identifiers are generated in batch.

View file

@ -46,6 +46,31 @@ usage(void)
exit(1);
}
static void
uuid_to_compact_string(const uuid_t *u, char **s, uint32_t *status)
{
uuid_t nil;
if (status != NULL)
*status = uuid_s_ok;
if (s == NULL)
return;
if (u == NULL) {
u = &nil;
uuid_create_nil(&nil, NULL);
}
asprintf(s, "%08x%04x%04x%02x%02x%02x%02x%02x%02x%02x%02x",
u->time_low, u->time_mid, u->time_hi_and_version,
u->clock_seq_hi_and_reserved, u->clock_seq_low, u->node[0],
u->node[1], u->node[2], u->node[3], u->node[4], u->node[5]);
if (*s == NULL && status != NULL)
*status = uuid_s_no_memory;
}
static int
uuidgen_v4(struct uuid *store, int count)
{
@ -85,16 +110,20 @@ main(int argc, char *argv[])
uuid_t *store, *uuid;
char *p;
int ch, count, i, iterate, status, version;
void (*tostring)(const uuid_t *, char **, uint32_t *) = uuid_to_string;
count = -1; /* no count yet */
fp = stdout; /* default output file */
iterate = 0; /* not one at a time */
version = 1; /* create uuid v1 by default */
while ((ch = getopt(argc, argv, "1rn:o:")) != -1)
while ((ch = getopt(argc, argv, "1crn:o:")) != -1)
switch (ch) {
case '1':
iterate = 1;
break;
case 'c':
tostring = uuid_to_compact_string;
break;
case 'r':
version = 4;
break;
@ -162,7 +191,7 @@ main(int argc, char *argv[])
uuid = store;
while (count--) {
uuid_to_string(uuid++, &p, &status);
tostring(uuid++, &p, &status);
if (status != uuid_s_ok)
err(1, "cannot stringify a UUID");
fprintf(fp, "%s\n", p);