From da724d1a59236efa7d6845465a0fa4dfba57d8ab Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Sat, 10 Mar 2007 09:48:54 +0100 Subject: [PATCH] Replace all occurrences of `strtok' with `strtok_r'. --- src/apache.c | 4 +++- src/apcups.c | 8 +++++--- src/common.c | 10 +++++++--- src/hddtemp.c | 6 ++++-- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/apache.c b/src/apache.c index 973ad865..167ead1f 100644 --- a/src/apache.c +++ b/src/apache.c @@ -294,6 +294,7 @@ static int apache_read (void) int i; char *ptr; + char *saveptr; char *lines[16]; int lines_num = 0; @@ -314,7 +315,8 @@ static int apache_read (void) } ptr = apache_buffer; - while ((lines[lines_num] = strtok (ptr, "\n\r")) != NULL) + saveptr = NULL; + while ((lines[lines_num] = strtok_r (ptr, "\n\r", &saveptr)) != NULL) { ptr = NULL; lines_num++; diff --git a/src/apcups.c b/src/apcups.c index e23bc9e1..9fab7858 100644 --- a/src/apcups.c +++ b/src/apcups.c @@ -304,6 +304,7 @@ static int apc_query_server (char *host, int port, int n; char recvline[1024]; char *tokptr; + char *toksaveptr; char *key; double value; @@ -344,11 +345,12 @@ static int apc_query_server (char *host, int port, printf ("net_recv = `%s';\n", recvline); #endif /* if APCMAIN */ - tokptr = strtok (recvline, " :\t"); + toksaveptr = NULL; + tokptr = strtok_r (recvline, " :\t", &toksaveptr); while (tokptr != NULL) { key = tokptr; - if ((tokptr = strtok (NULL, " :\t")) == NULL) + if ((tokptr = strtok_r (NULL, " :\t", &toksaveptr)) == NULL) continue; value = atof (tokptr); @@ -371,7 +373,7 @@ static int apc_query_server (char *host, int port, else if (strcmp ("TIMELEFT", key) == 0) apcups_detail->timeleft = value; - tokptr = strtok (NULL, ":"); + tokptr = strtok_r (NULL, ":", &toksaveptr); } /* while (tokptr != NULL) */ } diff --git a/src/common.c b/src/common.c index fc34afe7..922bd15e 100644 --- a/src/common.c +++ b/src/common.c @@ -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,6 +291,7 @@ 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; @@ -315,7 +318,7 @@ int check_create_dir (const char *file_orig) 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'; @@ -325,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++; diff --git a/src/hddtemp.c b/src/hddtemp.c index 03990f22..353fbe3c 100644 --- a/src/hddtemp.c +++ b/src/hddtemp.c @@ -443,6 +443,7 @@ static int hddtemp_read (void) char buf[1024]; char *fields[128]; char *ptr; + char *saveptr; int num_fields; int num_disks; int i; @@ -451,10 +452,11 @@ static int hddtemp_read (void) if (hddtemp_query_daemon (buf, sizeof (buf)) < 0) return (-1); - /* NB: strtok will eat up "||" and leading "|"'s */ + /* NB: strtok_r will eat up "||" and leading "|"'s */ num_fields = 0; ptr = buf; - while ((fields[num_fields] = strtok (ptr, "|")) != NULL) + saveptr = NULL; + while ((fields[num_fields] = strtok_r (ptr, "|", &saveptr)) != NULL) { ptr = NULL; num_fields++; -- 2.11.0