Add a simple uuidgen utility that generates RFC 4122 compliant
UUIDs (version 4, random). Uses the existing generate_uuid()
function from libbb and volume_id_set_uuid() for formatting.

Implementation suggested by Ulli.

Features:
- Generates standard format UUIDs: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
- RFC 4122 version 4 compliant
- Minimal implementation (~1.1 kb)
- NOFORK applet for efficiency
- Uses existing volume_id infrastructure for UUID formatting

Signed-off-by: Osama Abdelkader <[email protected]>
---
v2:
- Use struct volume_id and volume_id_set_uuid() for formatting
- Return 0 instead of fflush_all()
---
 util-linux/uuidgen.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 util-linux/uuidgen.c

diff --git a/util-linux/uuidgen.c b/util-linux/uuidgen.c
new file mode 100644
index 000000000..d75a25940
--- /dev/null
+++ b/util-linux/uuidgen.c
@@ -0,0 +1,43 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini uuidgen implementation for busybox
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
+//config:config UUIDGEN
+//config:      bool "uuidgen (1.1 kb)"
+//config:      default y
+//config:      help
+//config:      Generate a UUID (Universally Unique Identifier) in RFC 4122 
format.
+
+//applet:IF_UUIDGEN(APPLET_NOFORK(uuidgen, uuidgen, BB_DIR_USR_BIN, 
BB_SUID_DROP, uuidgen))
+
+//kbuild:lib-$(CONFIG_UUIDGEN) += uuidgen.o
+
+//usage:#define uuidgen_trivial_usage
+//usage:       ""
+//usage:#define uuidgen_full_usage "\n\n"
+//usage:       "Generate a UUID (Universally Unique Identifier)"
+
+#include "libbb.h"
+#include "volume_id/volume_id_internal.h"
+
+/* This is a NOFORK applet. Be very careful! */
+
+int uuidgen_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int uuidgen_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
+{
+       struct volume_id id;
+       uint8_t uuid[16];
+
+       if (argv[1]) {
+               bb_show_usage();
+       }
+
+       generate_uuid(uuid);
+       volume_id_set_uuid(&id, uuid, UUID_DCE);
+       printf("%s\n", id.uuid);
+
+       return 0;
+}
+
-- 
2.43.0

_______________________________________________
busybox mailing list
[email protected]
https://lists.busybox.net/mailman/listinfo/busybox

Reply via email to