Beautifying many debug messages..
[collectd.git] / src / common.c
index 9ebfe27..974a29e 100644 (file)
@@ -70,12 +70,35 @@ char *sstrdup (const char *s)
        return (r);
 }
 
-/* Don't use the return value of `strerror_r', because the GNU-people got
- * inventive there.. -octo */
+/* Even though Posix requires "strerror_r" to return an "int",
+ * some systems (e.g. the GNU libc) return a "char *" _and_
+ * ignore the second argument ... -tokkee */
 char *sstrerror (int errnum, char *buf, size_t buflen)
 {
        buf[0] = '\0';
-       strerror_r (errnum, buf, buflen);
+#ifdef STRERROR_R_CHAR_P
+       {
+               char *temp;
+               temp = strerror_r (errnum, buf, buflen);
+               if (buf[0] == '\0')
+               {
+                       if ((temp != NULL) && (temp != buf) && (temp[0] != '\0'))
+                               strncpy (buf, temp, buflen);
+                       else
+                               strncpy (buf, "strerror_r did not return "
+                                               "an error message", buflen);
+                       buf[buflen - 1] = '\0';
+               }
+       }
+#else
+       if (strerror_r (errnum, buf, buflen) != 0)
+       {
+               snprintf (buf, buflen, "Error #%i; "
+                               "Additionally, strerror_r failed.",
+                               errnum);
+       }
+#endif /* STRERROR_R_CHAR_P */
+       buf[buflen - 1] = '\0';
        return (buf);
 } /* char *sstrerror */
 
@@ -398,7 +421,7 @@ int check_create_dir (const char *file_orig)
                                if (mkdir (dir, 0755) == -1)
                                {
                                        char errbuf[1024];
-                                       ERROR ("mkdir (%s): %s", dir,
+                                       ERROR ("check_create_dir: mkdir (%s): %s", dir,
                                                        sstrerror (errno,
                                                                errbuf, sizeof (errbuf)));
                                        return (-1);
@@ -421,7 +444,7 @@ int check_create_dir (const char *file_orig)
        }
 
        return (0);
-}
+} /* check_create_dir */
 
 #ifdef HAVE_LIBKSTAT
 int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name)