Commit 34e0664c authored by Christian Kellner's avatar Christian Kellner
Browse files

Add utility function go generate UUIDs

parent c7c29283
/* -*- mode: C; c-file-style: "stroustrup"; indent-tabs-mode: nil; -*- */
#include "util.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
void
die(const char *msg, ...)
......@@ -40,3 +43,28 @@ break_time(double span,
*m = span_seconds / 60 - *h * 60;
*s = span_seconds - *h *3600 - *m * 60;
}
gchar *
uuid_gen_new(void)
{
guint8 bytes[32];
int i;
for (i = 0; i < 4; i++) {
guint32 n = g_random_int();
memcpy(bytes + i * sizeof(guint32), &n, sizeof(n));
}
bytes[6] &= 0x0f;
bytes[6] |= 4 << 4;
bytes[8] &= 0x3f;
bytes[8] |= 0x80;
return g_strdup_printf("%02x%02x%02x%02x-%02x%02x"
"-%02x%02x-%02x%02x"
"-%02x%02x%02x%02x%02x%02x",
bytes[0], bytes[1], bytes[2], bytes[3],
bytes[4], bytes[5], bytes[6], bytes[7],
bytes[8], bytes[9], bytes[10], bytes[11],
bytes[12], bytes[13], bytes[14], bytes[15]);
}
#ifndef __UTIL_H__
#define __UTIL_H__
#include <glib.h>
void die(const char *msg, ...)
__attribute__ ((noreturn))
__attribute__ ((format (printf, 1, 2)));
......@@ -13,5 +15,6 @@ void break_time(double span,
int *h, int *m, int *s);
gchar *uuid_gen_new(void);
#endif /* __UTIL_H__ */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment