aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorIlia Bozhinov <ammen99@gmail.com>2021-01-05 20:11:23 +0100
committerSimon Ser <contact@emersion.fr>2021-01-05 20:32:56 +0100
commitbf4e2e0eac4b336747979dbda30d4f328ab0bf3d (patch)
tree6c373b31e2ddb97dbea3a5a5c6bff4b1a09b93c0 /util
parent3721dbfddb0e21c29688bccf11725d337f45a05c (diff)
util: add support for generating UUIDs
Co-authored-by: Jason Francis <cycl0ps@tuta.io>
Diffstat (limited to 'util')
-rw-r--r--util/meson.build8
-rw-r--r--util/uuid.c33
2 files changed, 41 insertions, 0 deletions
diff --git a/util/meson.build b/util/meson.build
index 14cd59c5..06996a4e 100644
--- a/util/meson.build
+++ b/util/meson.build
@@ -7,3 +7,11 @@ wlr_files += files(
'signal.c',
'time.c',
)
+
+if uuid.found()
+ wlr_deps += uuid
+ add_project_arguments('-DHAS_LIBUUID=1', language: 'c')
+else
+ add_project_arguments('-DHAS_LIBUUID=0', language: 'c')
+endif
+wlr_files += files('uuid.c')
diff --git a/util/uuid.c b/util/uuid.c
new file mode 100644
index 00000000..a4523216
--- /dev/null
+++ b/util/uuid.c
@@ -0,0 +1,33 @@
+#include <uuid.h>
+#include "util/uuid.h"
+
+#if HAS_LIBUUID
+bool generate_uuid(char out[static 37]) {
+ uuid_t uuid;
+ uuid_generate_random(uuid);
+ uuid_unparse(uuid, out);
+ return true;
+}
+#else
+#include <string.h>
+#include <stdlib.h>
+
+bool generate_uuid(char out[static 37]) {
+ uuid_t uuid;
+ uint32_t status;
+ uuid_create(&uuid, &status);
+ if (status != uuid_s_ok) {
+ return false;
+ }
+ char *str;
+ uuid_to_string(&uuid, &str, &status);
+ if (status != uuid_s_ok) {
+ return false;
+ }
+
+ assert(strlen(str) + 1 == 37);
+ memcpy(out, str, sizeof(out));
+ free(str);
+ return true;
+}
+#endif