diff options
Diffstat (limited to 'lib/randstring.c')
-rw-r--r-- | lib/randstring.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/randstring.c b/lib/randstring.c new file mode 100644 index 0000000..2eeed8f --- /dev/null +++ b/lib/randstring.c @@ -0,0 +1,21 @@ +#pragma once +char *randstring(size_t length) { + + static char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + char *randomString = NULL; + + if (length) { + randomString = malloc(sizeof(char) * (length +1)); + + if (randomString) { + for (int n = 0;n < length;n++) { + int key = rand() % (int)(sizeof(charset) -1); + randomString[n] = charset[key]; + } + + randomString[length] = '\0'; + } + } + + return randomString; +} |