uuid-generator.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <uuid.h>
  4. #include <unistd.h>
  5. void check_uuid_status(uint32_t uuidstatus);
  6. int main() {
  7. /* restrict ourselves to standard IO */
  8. pledge("stdio", NULL);
  9. /* allocate space to store a generated UUID, a string representation
  10. * and status/error information. */
  11. void *myuuid;
  12. char *myuuidstr;
  13. uint32_t uuidstatus;
  14. myuuid = malloc(sizeof(uuid_t));
  15. /* create a UUID (Version 4), convert it to a string representation,
  16. * and print it out to the screen. */
  17. uuid_create(myuuid, &uuidstatus);
  18. check_uuid_status(uuidstatus);
  19. uuid_to_string(myuuid, &myuuidstr, &uuidstatus);
  20. check_uuid_status(uuidstatus);
  21. printf("%s\n", myuuidstr);
  22. /* free malloc()'ed memory. */
  23. free(myuuid);
  24. free(myuuidstr);
  25. return 0;
  26. }
  27. /* process and check uuid status. Returns zero if program should continue,
  28. * otherwise returns -1 if program should exit.
  29. * If an issue is identified, it prints a message to STDERR.
  30. */
  31. void check_uuid_status(uint32_t uuidstatus) {
  32. switch (uuidstatus) {
  33. case uuid_s_bad_version:
  34. fprintf(stderr, "Unknown UUID version. Exiting.\n");
  35. exit(-1);
  36. case uuid_s_no_memory:
  37. fprintf(stderr, "Unable to allocate memory. Exiting.\n");
  38. exit(-1);
  39. }
  40. }