src/configfile.c: s/TypesDS/TypesDB/;
[collectd.git] / src / common.c
index ede2ea5..6521a58 100644 (file)
@@ -58,6 +58,15 @@ char *sstrdup (const char *s)
        return (r);
 }
 
+/* Don't use the return value of `strerror_r', because the GNU-people got
+ * inventive there.. -octo */
+char *sstrerror (int errnum, char *buf, size_t buflen)
+{
+       buf[0] = '\0';
+       strerror_r (errnum, buf, buflen);
+       return (buf);
+} /* char *sstrerror */
+
 void *smalloc (size_t size)
 {
        void *r;
@@ -372,13 +381,19 @@ int check_create_dir (const char *file_orig)
                        {
                                if (mkdir (dir, 0755) == -1)
                                {
-                                       ERROR ("mkdir (%s): %s", dir, strerror (errno));
+                                       char errbuf[1024];
+                                       ERROR ("mkdir (%s): %s", dir,
+                                                       sstrerror (errno,
+                                                               errbuf, sizeof (errbuf)));
                                        return (-1);
                                }
                        }
                        else
                        {
-                               ERROR ("stat (%s): %s", dir, strerror (errno));
+                               char errbuf[1024];
+                               ERROR ("stat (%s): %s", dir,
+                                               sstrerror (errno, errbuf,
+                                                       sizeof (errbuf)));
                                return (-1);
                        }
                }
@@ -485,7 +500,7 @@ unsigned long long ntohll (unsigned long long n)
 #else
        return (((unsigned long long) ntohl (n)) << 32) + ntohl (n >> 32);
 #endif
-}
+} /* unsigned long long ntohll */
 
 unsigned long long htonll (unsigned long long n)
 {
@@ -494,4 +509,41 @@ unsigned long long htonll (unsigned long long n)
 #else
        return (((unsigned long long) htonl (n)) << 32) + htonl (n >> 32);
 #endif
-}
+} /* unsigned long long htonll */
+
+int format_name (char *ret, int ret_len,
+               const char *hostname,
+               const char *plugin, const char *plugin_instance,
+               const char *type, const char *type_instance)
+{
+       int  status;
+
+       assert (plugin != NULL);
+       assert (type != NULL);
+
+       if ((plugin_instance == NULL) || (strlen (plugin_instance) == 0))
+       {
+               if ((type_instance == NULL) || (strlen (type_instance) == 0))
+                       status = snprintf (ret, ret_len, "%s/%s/%s",
+                                       hostname, plugin, type);
+               else
+                       status = snprintf (ret, ret_len, "%s/%s/%s-%s",
+                                       hostname, plugin, type,
+                                       type_instance);
+       }
+       else
+       {
+               if ((type_instance == NULL) || (strlen (type_instance) == 0))
+                       status = snprintf (ret, ret_len, "%s/%s-%s/%s",
+                                       hostname, plugin, plugin_instance,
+                                       type);
+               else
+                       status = snprintf (ret, ret_len, "%s/%s-%s/%s-%s",
+                                       hostname, plugin, plugin_instance,
+                                       type, type_instance);
+       }
+
+       if ((status < 1) || (status >= ret_len))
+               return (-1);
+       return (0);
+} /* int format_name */