From: Florian Forster Date: Sun, 20 Jun 2010 16:02:39 +0000 (+0200) Subject: Merge remote branch 'trenkel/collectd-4.10' into collectd-4.10 X-Git-Tag: collectd-4.10.1~3 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=96c474f576c721c6d993025b99c55de375552259;hp=23308534a393a9186e1d64a9c0a6a82086cb0a57;p=collectd.git Merge remote branch 'trenkel/collectd-4.10' into collectd-4.10 --- diff --git a/src/common.h b/src/common.h index c0bea36e..229f7098 100644 --- a/src/common.h +++ b/src/common.h @@ -291,6 +291,8 @@ counter_t counter_diff (counter_t old_value, counter_t new_value); * (in the range [1-65535]). Returns less than zero on error. */ int service_name_to_port_number (const char *service_name); +/** Parse a string to a derive_t value. Returns zero on success or non-zero on + * failure. If failure is returned, ret_value is not touched. */ int strtoderive (const char *string, derive_t *ret_value); #endif /* COMMON_H */ diff --git a/src/swap.c b/src/swap.c index e467818d..68a5b71c 100644 --- a/src/swap.c +++ b/src/swap.c @@ -210,25 +210,18 @@ static int swap_read (void) return (-1); } - while (fgets (buffer, 1024, fh) != NULL) + while (fgets (buffer, sizeof (buffer), fh) != NULL) { - derive_t *val = NULL; - - if (strncasecmp (buffer, "SwapTotal:", 10) == 0) - val = &swap_total; - else if (strncasecmp (buffer, "SwapFree:", 9) == 0) - val = &swap_free; - else if (strncasecmp (buffer, "SwapCached:", 11) == 0) - val = &swap_cached; - else - continue; - - numfields = strsplit (buffer, fields, 8); - + numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields)); if (numfields < 2) continue; - *val = (derive_t) atoll (fields[1]) * 1024LL; + if (strcasecmp (fields[0], "SwapTotal:") == 0) + strtoderive (fields[1], &swap_total); + else if (strcasecmp (fields[0], "SwapFree:") == 0) + strtoderive (fields[1], &swap_free); + else if (strcasecmp (fields[0], "SwapCached:") == 0) + strtoderive (fields[1], &swap_cached); } if (fclose (fh)) @@ -257,7 +250,7 @@ static int swap_read (void) old_kernel = 1; } - while (fgets (buffer, 1024, fh) != NULL) + while (fgets (buffer, sizeof (buffer), fh) != NULL) { numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));