Replace all occurrences of `strtok' with `strtok_r'.
[collectd.git] / src / common.c
index 365b74f..922bd15 100644 (file)
@@ -152,10 +152,12 @@ int strsplit (char *string, char **fields, size_t size)
 {
        size_t i;
        char *ptr;
+       char *saveptr;
 
        i = 0;
        ptr = string;
-       while ((fields[i] = strtok (ptr, " \t")) != NULL)
+       saveptr = NULL;
+       while ((fields[i] = strtok_r (ptr, " \t", &saveptr)) != NULL)
        {
                ptr = NULL;
                i++;
@@ -289,7 +291,9 @@ int check_create_dir (const char *file_orig)
        char *fields[16];
        int   fields_num;
        char *ptr;
+       char *saveptr;
        int   last_is_file = 1;
+       int   path_is_absolute = 0;
        int   len;
        int   i;
 
@@ -310,9 +314,11 @@ int check_create_dir (const char *file_orig)
         */
        if (file_orig[len - 1] == '/')
                last_is_file = 0;
+       if (file_orig[0] == '/')
+               path_is_absolute = 1;
 
        /*
-        * Create a copy for `strtok' to destroy
+        * Create a copy for `strtok_r' to destroy
         */
        strncpy (file_copy, file_orig, 512);
        file_copy[511] = '\0';
@@ -322,8 +328,9 @@ int check_create_dir (const char *file_orig)
         * remove leading and trailing slashes..
         */
        ptr = file_copy;
+       saveptr = NULL;
        fields_num = 0;
-       while ((fields[fields_num] = strtok (ptr, "/")) != NULL)
+       while ((fields[fields_num] = strtok_r (ptr, "/", &saveptr)) != NULL)
        {
                ptr = NULL;
                fields_num++;
@@ -351,7 +358,9 @@ int check_create_dir (const char *file_orig)
                /*
                 * Join the components together again
                 */
-               if (strjoin (dir, dir_len, fields, i + 1, "/") < 0)
+               dir[0] = '/';
+               if (strjoin (dir + path_is_absolute, dir_len - path_is_absolute,
+                                       fields, i + 1, "/") < 0)
                {
                        syslog (LOG_ERR, "strjoin failed: `%s', component #%i", file_orig, i);
                        return (-1);