bind plugin: Reworked Bruno's patch to remove repeating code.
[collectd.git] / src / common.c
index 119d284..aeea28d 100644 (file)
@@ -77,15 +77,21 @@ int ssnprintf (char *dest, size_t n, const char *format, ...)
 char *sstrdup (const char *s)
 {
        char *r;
+       size_t sz;
 
        if (s == NULL)
                return (NULL);
 
-       if((r = strdup (s)) == NULL)
+       /* Do not use `strdup' here, because it's not specified in POSIX. It's
+        * ``only'' an XSI extension. */
+       sz = strlen (s) + 1;
+       r = (char *) malloc (sizeof (char) * sz);
+       if (r == NULL)
        {
-               ERROR ("Not enough memory.");
+               ERROR ("sstrdup: Out of memory.");
                exit (3);
        }
+       memcpy (r, s, sizeof (char) * sz);
 
        return (r);
 } /* char *sstrdup */
@@ -919,4 +925,22 @@ int read_file_contents (const char *filename, char *buf, int bufsize)
        return n;
 }
 
+counter_t counter_diff (counter_t old_value, counter_t new_value)
+{
+       counter_t diff;
+
+       if (old_value > new_value)
+       {
+               if (old_value <= 4294967295U)
+                       diff = (4294967295U - old_value) + new_value;
+               else
+                       diff = (18446744073709551615ULL - old_value)
+                               + new_value;
+       }
+       else
+       {
+               diff = new_value - old_value;
+       }
 
+       return (diff);
+} /* counter_t counter_to_gauge */