[Nagiosplug-checkins] SF.net SVN: nagiosplug:[2036] nagiosplug/trunk/plugins/check_ntp_peer.c
dermoth at users.sourceforge.net
dermoth at users.sourceforge.net
Thu Aug 21 06:17:36 CEST 2008
Revision: 2036
http://nagiosplug.svn.sourceforge.net/nagiosplug/?rev=2036&view=rev
Author: dermoth
Date: 2008-08-21 04:17:35 +0000 (Thu, 21 Aug 2008)
Log Message:
-----------
Complete rewrite of the extract_value function
The original one was flawed (easy to trigger segfaults) and did not allow some whitespaces as permitted by rfc1305. This one has been troughfully tested with tap (the testing code might get included later)
Modified Paths:
--------------
nagiosplug/trunk/plugins/check_ntp_peer.c
Modified: nagiosplug/trunk/plugins/check_ntp_peer.c
===================================================================
--- nagiosplug/trunk/plugins/check_ntp_peer.c 2008-08-21 04:17:25 UTC (rev 2035)
+++ nagiosplug/trunk/plugins/check_ntp_peer.c 2008-08-21 04:17:35 UTC (rev 2036)
@@ -175,24 +175,57 @@
}
}
+/*
+ * Extract the value from NTP key/value pairs, or return NULL.
+ * The value returned can be free()ed.
+ */
char *extract_value(const char *varlist, const char *name){
- char *tmpvarlist=NULL, *tmpkey=NULL, *value=NULL;
- int last=0;
+ char *tmp=NULL, *value=NULL;
+ int i;
- /* The following code require a non-empty varlist */
- if(strlen(varlist) == 0)
- return NULL;
+ /* Strip any leading space */
- tmpvarlist = strdup(varlist);
- tmpkey = strtok(tmpvarlist, "=");
+ while (1) {
+ for (varlist; isspace(varlist[0]); varlist++);
+ if (strncmp(name, varlist, strlen(name)) == 0) {
+ varlist += strlen(name);
+ /* strip trailing spaces */
+ for (varlist; isspace(varlist[0]); varlist++);
- do {
- if(strstr(tmpkey, name) != NULL) {
- value = strtok(NULL, ",");
- last = 1;
+ if (varlist[0] == '=') {
+ /* We matched the key, go past the = sign */
+ varlist++;
+ /* strip leading spaces */
+ for (varlist; isspace(varlist[0]); varlist++);
+
+ if (tmp = index(varlist, ',')) {
+ /* Value is delimited by a comma */
+ if (tmp-varlist == 0) continue;
+ value = (char *)malloc(tmp-varlist+1);
+ strncpy(value, varlist, tmp-varlist);
+ value[tmp-varlist] = '\0';
+ } else {
+ /* Value is delimited by a \0 */
+ if (strlen(varlist) == 0) continue;
+ value = (char *)malloc(strlen(varlist) + 1);
+ strncpy(value, varlist, strlen(varlist));
+ value[strlen(varlist)] = '\0';
+ }
+ break;
+ }
}
- } while (last == 0 && (tmpkey = strtok(NULL, "=")));
+ if (tmp = index(varlist, ',')) {
+ /* More keys, keep going... */
+ varlist = tmp + 1;
+ } else {
+ /* We're done */
+ break;
+ }
+ }
+ /* Clean-up trailing spaces/newlines */
+ if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0';
+
return value;
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
More information about the Commits
mailing list