diff options
Diffstat (limited to 'common/utils.c')
| -rw-r--r-- | common/utils.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/common/utils.c b/common/utils.c index 1b207ea..68b23b9 100644 --- a/common/utils.c +++ b/common/utils.c | |||
| @@ -26,6 +26,7 @@ | |||
| 26 | #include <stdarg.h> | 26 | #include <stdarg.h> |
| 27 | #include <stdlib.h> | 27 | #include <stdlib.h> |
| 28 | #include <string.h> | 28 | #include <string.h> |
| 29 | #include <time.h> | ||
| 29 | 30 | ||
| 30 | #include "utils.h" | 31 | #include "utils.h" |
| 31 | 32 | ||
| @@ -107,6 +108,35 @@ char *string_concat(const char *str, ...) | |||
| 107 | return result; | 108 | return result; |
| 108 | } | 109 | } |
| 109 | 110 | ||
| 111 | static int get_rand(int min, int max) | ||
| 112 | { | ||
| 113 | int retval = (rand() % (max - min)) + min; | ||
| 114 | return retval; | ||
| 115 | } | ||
| 116 | |||
| 117 | char *generate_uuid() | ||
| 118 | { | ||
| 119 | const char *chars = "ABCDEF0123456789"; | ||
| 120 | int i = 0; | ||
| 121 | char *uuid = (char *) malloc(sizeof(char) * 37); | ||
| 122 | |||
| 123 | srand(time(NULL)); | ||
| 124 | |||
| 125 | for (i = 0; i < 36; i++) { | ||
| 126 | if (i == 8 || i == 13 || i == 18 || i == 23) { | ||
| 127 | uuid[i] = '-'; | ||
| 128 | continue; | ||
| 129 | } else { | ||
| 130 | uuid[i] = chars[get_rand(0, 16)]; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | /* make it a real string */ | ||
| 135 | uuid[36] = '\0'; | ||
| 136 | |||
| 137 | return uuid; | ||
| 138 | } | ||
| 139 | |||
| 110 | void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length) | 140 | void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length) |
| 111 | { | 141 | { |
| 112 | FILE *f; | 142 | FILE *f; |
