From b2b294f27cf4618d6f7510007b41882860a080b9 Mon Sep 17 00:00:00 2001 From: Baptiste Daroussin Date: Tue, 28 Feb 2023 11:29:51 +0100 Subject: [PATCH] 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 --- bin/uuidgen/uuidgen.1 | 5 ++++- bin/uuidgen/uuidgen.c | 33 +++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/bin/uuidgen/uuidgen.1 b/bin/uuidgen/uuidgen.1 index 83326e3a189..75fd0270317 100644 --- a/bin/uuidgen/uuidgen.1 +++ b/bin/uuidgen/uuidgen.1 @@ -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. diff --git a/bin/uuidgen/uuidgen.c b/bin/uuidgen/uuidgen.c index 061907f5710..6dcfdde92a5 100644 --- a/bin/uuidgen/uuidgen.c +++ b/bin/uuidgen/uuidgen.c @@ -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); -- 2.45.0