home · contact · privacy
Server: Remove log_help(), this should be serverd by the client.
[plomrogue] / src / common / rexit.c
index 7b837e946987bc59b658ba441f5129b1c22a1c7a..7eeaccb52d1c92d91a3c9760771888de70b16bde 100644 (file)
@@ -1,11 +1,17 @@
-/* src/common/rexit.c */
+/* src/common/rexit.c
+ *
+ * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
+ * or any later version. For details on its copyright, license, and warranties,
+ * see the file NOTICE in the root directory of the PlomRogue source package.
+ */
 
 #include "rexit.h"
 #include <errno.h> /* global errno */
 #include <stdint.h> /* uint16_t */
 #include <stdio.h> /* printf(), perror(), sprintf() */
-#include <stdlib.h> /* exit(), EXIT_FAILURE */
+#include <stdlib.h> /* exit(), free(), EXIT_FAILURE */
 #include <string.h> /* strlen() */
+#include "try_malloc.h" /* try_malloc() */
 
 
 
@@ -20,20 +26,18 @@ extern void set_cleanup_func(void (* f)())
 
 
 
-extern void exit_err(int err, char * msg)
+extern void exit_err(int err, const char * msg)
 {
     if (0 == err)
     {
         return;
     }
     cleanup_func();
-    if (NULL == msg)
+    if (!msg)
     {
         msg = "Details unknown.";
     }
-    printf("Aborted program due to error. %s\n"
-            "Internal error code: %d\n",
-            msg, err);
+    printf("Aborted program due to error. %s\n", msg);
     if (0 != errno)
     {
         perror("errno states");
@@ -43,14 +47,16 @@ extern void exit_err(int err, char * msg)
 
 
 
-extern void exit_trouble(int err, char * parent, char * child)
+extern void exit_trouble(int err, const char * parent, const char * child)
 {
     char * p1 = "Trouble in ";
     char * p2 = " with ";
     char * p3 = ".";
     uint16_t size = strlen(p1) + strlen(parent) + strlen(p2) + strlen(child)
                     + strlen(p3) + 1;
-    char msg[size];
-    sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3);
+    char * msg = try_malloc(size, __func__);
+    int test = sprintf(msg, "%s%s%s%s%s", p1, parent, p2, child, p3);
+    exit_err(test < 0, "Trouble in exit_trouble with sprintf.");
     exit_err(err, msg);
+    free(msg);
 }