Unified string handling.
[collectd.git] / src / memcached.c
index 39c599c..ca93102 100644 (file)
@@ -1,6 +1,7 @@
 /**
- * collectd - src/memcached.c
- * Copyright (C) 2007  Antony Dovgal, heavily based on hddtemp.c
+ * collectd - src/memcached.c, based on src/hddtemp.c
+ * Copyright (C) 2007  Antony Dovgal
+ * Copyright (C) 2005,2006  Vincent StehlĂ©
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -18,7 +19,8 @@
  *
  * Authors:
  *   Antony Dovgal <tony at daylessday dot org>
- *
+ *   Vincent StehlĂ© <vincent.stehle at free.fr>
+ *   Florian octo Forster <octo at verplant.org>
  **/
 
 #include "collectd.h"
 # include <sys/socket.h>
 # include <netinet/in.h>
 # include <netinet/tcp.h>
-# include <libgen.h> /* for basename */
-
-#if HAVE_LINUX_MAJOR_H
-# include <linux/major.h>
-#endif
 
 #define MEMCACHED_DEF_HOST "127.0.0.1"
 #define MEMCACHED_DEF_PORT "11211"
 static const char *config_keys[] =
 {
        "Host",
-       "Port",
-       NULL
+       "Port"
 };
-static int config_keys_num = 2;
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
 
 static char *memcached_host = NULL;
 static char memcached_port[16];
@@ -204,11 +200,10 @@ static int memcached_config (const char *key, const char *value) /* {{{ */
        } else if (strcasecmp (key, "Port") == 0) {
                int port = (int) (atof (value));
                if ((port > 0) && (port <= 65535)) {
-                       snprintf (memcached_port, sizeof (memcached_port), "%i", port);
+                       ssnprintf (memcached_port, sizeof (memcached_port), "%i", port);
                } else {
-                       strncpy (memcached_port, value, sizeof (memcached_port));
+                       sstrncpy (memcached_port, value, sizeof (memcached_port));
                }
-               memcached_port[sizeof (memcached_port) - 1] = '\0';
        } else {
                return -1;
        }
@@ -230,13 +225,11 @@ static void submit_counter (const char *type, const char *type_inst,
        vl.time = time (NULL);
        strcpy (vl.host, hostname_g);
        strcpy (vl.plugin, "memcached");
+       sstrncpy (vl.type, type, sizeof (vl.type));
        if (type_inst != NULL)
-       {
-               strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
-               vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-       }
+               sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void memcached_submit_cmd */
 /* }}} */
 
@@ -254,13 +247,11 @@ static void submit_counter2 (const char *type, const char *type_inst,
        vl.time = time (NULL);
        strcpy (vl.host, hostname_g);
        strcpy (vl.plugin, "memcached");
+       sstrncpy (vl.type, type, sizeof (vl.type));
        if (type_inst != NULL)
-       {
-               strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
-               vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-       }
+               sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 } /* void memcached_submit_cmd */
 /* }}} */
 
@@ -277,13 +268,11 @@ static void submit_gauge (const char *type, const char *type_inst,
        vl.time = time (NULL);
        strcpy (vl.host, hostname_g);
        strcpy (vl.plugin, "memcached");
+       sstrncpy (vl.type, type, sizeof (vl.type));
        if (type_inst != NULL)
-       {
-               strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
-               vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-       }
+               sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 }
 /* }}} */
 
@@ -301,13 +290,11 @@ static void submit_gauge2 (const char *type, const char *type_inst,
        vl.time = time (NULL);
        strcpy (vl.host, hostname_g);
        strcpy (vl.plugin, "memcached");
+       sstrncpy (vl.type, type, sizeof (vl.type));
        if (type_inst != NULL)
-       {
-               strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
-               vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
-       }
+               sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       plugin_dispatch_values (&vl);
 }
 /* }}} */