X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=graph_list.c;h=eeab51a05190eef6f21ad2a36f9d21c174bcdd3f;hb=58d184eb1a59b534b57f3c1a13da9df1cb280ef4;hp=baa8849ea0f9c9985e40da8457263f7e1404d0f8;hpb=9e5770370bb085ccfb19f3dd14707542bf1475f1;p=collection4.git diff --git a/graph_list.c b/graph_list.c index baa8849..eeab51a 100644 --- a/graph_list.c +++ b/graph_list.c @@ -3,14 +3,87 @@ #include #include #include +#include #include "graph_list.h" #include "common.h" +#define UPDATE_INTERVAL 10 + static graph_list_t *graph_list = NULL; static size_t graph_list_length = 0; static time_t gl_last_update = 0; +/* "Safe" version of strcmp(3): Either or both pointers may be NULL. */ +static int strcmp_s (const char *s1, const char *s2) /* {{{ */ +{ + if ((s1 == NULL) && (s2 == NULL)) + return (0); + else if (s1 == NULL) + return (1); + else if (s2 == NULL) + return (-1); + assert ((s1 != NULL) && (s2 != NULL)); + + return (strcmp (s1, s2)); +} /* }}} int strcmp_s */ + +static int gl_compare (const void *p0, const void *p1) /* {{{ */ +{ + const graph_list_t *gl0 = p0; + const graph_list_t *gl1 = p1; + int status; + + status = strcmp (gl0->host, gl1->host); + if (status != 0) + return (status); + + status = strcmp (gl0->plugin, gl1->plugin); + if (status != 0) + return (status); + + status = strcmp_s (gl0->plugin_instance, gl1->plugin_instance); + if (status != 0) + return (status); + + status = strcmp (gl0->type, gl1->type); + if (status != 0) + return (status); + + return (strcmp_s (gl0->type_instance, gl1->type_instance)); +} /* }}} int gl_compare */ + +static void gl_clear_entry (graph_list_t *gl) /* {{{ */ +{ + if (gl == NULL) + return; + + free (gl->host); + free (gl->plugin); + free (gl->plugin_instance); + free (gl->type); + free (gl->type_instance); + + gl->host = NULL; + gl->plugin = NULL; + gl->plugin_instance = NULL; + gl->type = NULL; + gl->type_instance = NULL; +} /* }}} void gl_clear_entry */ + +static void gl_clear (void) /* {{{ */ +{ + size_t i; + + for (i = 0; i < graph_list_length; i++) + gl_clear_entry (graph_list + i); + + free (graph_list); + graph_list = NULL; + graph_list_length = 0; + gl_last_update = 0; +} /* }}} void gl_clear */ + static int gl_add_copy (graph_list_t *gl) /* {{{ */ { graph_list_t *ptr; @@ -166,9 +239,11 @@ int gl_update (void) /* {{{ */ now = time (NULL); - if ((gl_last_update + 2) >= now) + if ((gl_last_update + UPDATE_INTERVAL) >= now) return (0); + gl_clear (); + memset (&gl, 0, sizeof (gl)); gl.host = NULL; gl.plugin = NULL; @@ -176,9 +251,11 @@ int gl_update (void) /* {{{ */ gl.type = NULL; gl.type_instance = NULL; - /* TODO: Free old list */ - status = foreach_host (callback_host, &gl); + + if (graph_list_length > 1) + qsort (graph_list, graph_list_length, sizeof (*graph_list), gl_compare); + return (status); } /* }}} int gl_update */