Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // random_uuid.c
- //
- //
- // Created by Jeffrey Ryan on 6/30/15.
- //
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main(int argc, char* argv[]) {
- const int hexDigitsLen = 16;
- const char hexDigits[] = "0123456789abcdef";
- const int bufferLen = 37 * 8;
- char buffer[bufferLen + 1];
- unsigned int currentIndex = 0;
- long int numUUIDsToGenerate = -1;
- if (argc >= 2) {
- numUUIDsToGenerate = strtol(argv[1], NULL, 10);
- }
- else {
- return 1;
- }
- if (numUUIDsToGenerate <= 0) {
- return 1;
- }
- //char help[] = "01234567-9012-4567-9012-456789012345"; // REMOVE WHEN DONE
- srand(time(NULL));
- buffer[bufferLen] = '\0';
- for (int i = 0; i < numUUIDsToGenerate; i++) {
- if (currentIndex + 37 < bufferLen) {
- for (int j = 0; j < 36; j++) {
- buffer[currentIndex + j] = hexDigits[rand() % hexDigitsLen];
- }
- buffer[currentIndex + 8] = '-';
- buffer[currentIndex + 13] = '-';
- buffer[currentIndex + 18] = '-';
- buffer[currentIndex + 23] = '-';
- buffer[currentIndex + 36] = '\n';
- currentIndex += 37;
- }
- else {
- buffer[currentIndex] = '\0';
- printf("%s", buffer);
- currentIndex = 0;
- i--;
- }
- }
- if (currentIndex != 0) {
- buffer[currentIndex] = '\0';
- printf("%s", buffer);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement