Advertisement
Guest User

Random UUID Generator

a guest
Jul 10th, 2015
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.51 KB | None | 0 0
  1. //
  2. //  random_uuid.c
  3. //  
  4. //
  5. //  Created by Jeffrey Ryan on 6/30/15.
  6. //
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12.  
  13. int main(int argc, char* argv[]) {
  14.     const int hexDigitsLen = 16;
  15.     const char hexDigits[] = "0123456789abcdef";
  16.  
  17.     const int bufferLen = 37 * 8;
  18.     char buffer[bufferLen + 1];
  19.     unsigned int currentIndex = 0;
  20.  
  21.     long int numUUIDsToGenerate = -1;
  22.  
  23.     if (argc >= 2) {
  24.         numUUIDsToGenerate = strtol(argv[1], NULL, 10);
  25.     }
  26.     else {
  27.         return 1;
  28.     }
  29.  
  30.     if (numUUIDsToGenerate <= 0) {
  31.         return 1;
  32.     }
  33.  
  34.     //char help[] = "01234567-9012-4567-9012-456789012345"; // REMOVE WHEN DONE
  35.  
  36.     srand(time(NULL));
  37.     buffer[bufferLen] = '\0';
  38.  
  39.     for (int i = 0; i < numUUIDsToGenerate; i++) {
  40.         if (currentIndex + 37 < bufferLen) {
  41.             for (int j = 0; j < 36; j++) {
  42.                 buffer[currentIndex + j] = hexDigits[rand() % hexDigitsLen];
  43.             }
  44.  
  45.             buffer[currentIndex +  8] = '-';
  46.             buffer[currentIndex + 13] = '-';
  47.             buffer[currentIndex + 18] = '-';
  48.             buffer[currentIndex + 23] = '-';
  49.             buffer[currentIndex + 36] = '\n';
  50.  
  51.             currentIndex += 37;
  52.         }
  53.         else {
  54.             buffer[currentIndex] = '\0';
  55.             printf("%s", buffer);
  56.             currentIndex = 0;
  57.             i--;
  58.         }
  59.     }
  60.  
  61.     if (currentIndex != 0) {
  62.         buffer[currentIndex] = '\0';
  63.         printf("%s", buffer);
  64.     }
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement