Selaa lähdekoodia

write some error-handling for UUID creation and string conversion

George C. Privon 6 kuukautta sitten
vanhempi
commit
bf63bca6df
1 muutettua tiedostoa jossa 19 lisäystä ja 0 poistoa
  1. 19 0
      uuid-generator.c

+ 19 - 0
uuid-generator.c

@@ -3,6 +3,8 @@
 #include <uuid.h>
 #include <unistd.h>
 
+void check_uuid_status(uint32_t uuidstatus);
+
 int main() {
 
     /* restrict ourselves to standard IO */
@@ -18,7 +20,9 @@ int main() {
     /* create a UUID (Version 4), convert it to a string representation,
      * and print it out to the screen. */
     uuid_create(myuuid, &uuidstatus);
+    check_uuid_status(uuidstatus);
     uuid_to_string(myuuid, &myuuidstr, &uuidstatus);
+    check_uuid_status(uuidstatus);
     printf("%s\n", myuuidstr);
 
     /* free malloc()'ed memory. */
@@ -27,3 +31,18 @@ int main() {
 
 return 0;
 }
+
+/* process and check uuid status. Returns zero if program should continue,
+ * otherwise returns -1 if program should exit.
+ * If an issue is identified, it prints a message to STDERR.
+ */
+void check_uuid_status(uint32_t uuidstatus) {
+    switch (uuidstatus) {
+        case uuid_s_bad_version:
+            fprintf(stderr, "Unknown UUID version. Exiting.\n");
+            exit(-1);
+        case uuid_s_no_memory:
+            fprintf(stderr, "Unable to allocate memory. Exiting.\n");
+            exit(-1);
+    }
+}