aboutsummaryrefslogtreecommitdiff
path: root/util/uuid.c
blob: 82e310b943dbcf902701b0d611c494457765d407 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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 <assert.h>
#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, 37);
	free(str);
	return true;
}
#endif