Added Makefile.am for `liboping'.
[collectd.git] / src / common.c
index db8fce1..50a563a 100644 (file)
@@ -46,35 +46,54 @@ static char *rra_def[] =
 static int rra_num = 9;
 #endif /* HAVE_LIBRRD */
 
-void
-sstrncpy(char *d, const char *s, int len)
+void sstrncpy (char *d, const char *s, int len)
 {
-       strncpy(d, s, len);
-       d[len - 1] = 0;
+       strncpy (d, s, len);
+       d[len - 1] = '\0';
 }
 
-char *
-sstrdup(const char *s)
+char *sstrdup (const char *s)
 {
-       char *r = strdup(s);
-       if(r == NULL) {
-               DBG("Not enough memory.");
+       char *r;
+
+       if (s == NULL)
+               return (NULL);
+
+       if((r = strdup (s)) == NULL)
+       {
+               DBG ("Not enough memory.");
                exit(3);
        }
-       return r;
+
+       return (r);
 }
 
-void *
-smalloc(size_t size)
+void *smalloc (size_t size)
 {
-       void *r = malloc(size);
-       if(r == NULL) {
+       void *r;
+
+       if ((r = malloc (size)) == NULL)
+       {
                DBG("Not enough memory.");
                exit(3);
        }
+
        return r;
 }
 
+#if 0
+void sfree (void **ptr)
+{
+       if (ptr == NULL)
+               return;
+
+       if (*ptr != NULL)
+               free (*ptr);
+
+       *ptr = NULL;
+}
+#endif
+
 int strsplit (char *string, char **fields, size_t size)
 {
        size_t i;
@@ -222,22 +241,25 @@ int check_create_dir (const char *file_orig)
        for (i = 0; i < (fields_num - last_is_file); i++)
        {
                /*
-                * Join the components together again
-                */
-               if (strjoin (dir, dir_len, fields, i + 1, "/") < 0)
-                       return (-1);
-
-               /*
                 * Do not create directories that start with a dot. This
                 * prevents `../../' attacks and other likely malicious
                 * behavior.
                 */
                if (fields[i][0] == '.')
                {
-                       syslog (LOG_ERR, "Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", dir);
+                       syslog (LOG_ERR, "Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig);
                        return (-2);
                }
 
+               /*
+                * Join the components together again
+                */
+               if (strjoin (dir, dir_len, fields, i + 1, "/") < 0)
+               {
+                       syslog (LOG_ERR, "strjoin failed: `%s', component #%i", file_orig, i);
+                       return (-1);
+               }
+
                if (stat (dir, &statbuf) == -1)
                {
                        if (errno == ENOENT)
@@ -271,6 +293,9 @@ int rrd_create_file (char *filename, char **ds_def, int ds_num)
        int i, j;
        int status = 0;
 
+       if (check_create_dir (filename))
+               return (-1);
+
        argc = ds_num + rra_num + 4;
 
        if ((argv = (char **) malloc (sizeof (char *) * (argc + 1))) == NULL)
@@ -316,9 +341,6 @@ int rrd_update_file (char *host, char *file, char *values,
        /* host == NULL => local mode */
        if (host != NULL)
        {
-               if (check_create_dir (host))
-                       return (-1);
-
                if (snprintf (full_file, 1024, "%s/%s", host, file) >= 1024)
                        return (-1);
        }