X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_fbhash.c;h=7c342e53c3715295078f58a36a8a9a6d1d067251;hb=48efd3deb4c9139fd060ff3d289896e9031bcc7c;hp=7513729c679b29f4278395654a5f465c07e7ffe9;hpb=c1219a1c9db2e8400e2ee94b87f86ccd441485d5;p=collectd.git diff --git a/src/utils_fbhash.c b/src/utils_fbhash.c index 7513729c..7c342e53 100644 --- a/src/utils_fbhash.c +++ b/src/utils_fbhash.c @@ -1,34 +1,37 @@ /** * collectd - src/utils_fbhash.c - * Copyright (C) 2009 Florian octo Forster + * Copyright (C) 2009 Florian octo Forster * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; only version 2 of the License is applicable. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * Authors: - * Florian octo Forster + * Florian octo Forster **/ #include "collectd.h" -#include "plugin.h" -#include +#include "plugin.h" +#include "utils/avltree/avltree.h" #include "utils_fbhash.h" -#include "utils_avltree.h" -struct fbhash_s -{ +struct fbhash_s { char *filename; time_t mtime; @@ -39,64 +42,58 @@ struct fbhash_s /* * Private functions */ -static void fbh_free_tree (c_avl_tree_t *tree) /* {{{ */ +static void fbh_free_tree(c_avl_tree_t *tree) /* {{{ */ { int status; if (tree == NULL) return; - while (42) - { + while (42) { char *key = NULL; char *value = NULL; - status = c_avl_pick (tree, (void *) &key, (void *) &value); + status = c_avl_pick(tree, (void *)&key, (void *)&value); if (status != 0) break; - free (key); - free (value); + free(key); + free(value); } - c_avl_destroy (tree); + c_avl_destroy(tree); } /* }}} void fbh_free_tree */ -static int fbh_read_file (fbhash_t *h) /* {{{ */ +static int fbh_read_file(fbhash_t *h) /* {{{ */ { FILE *fh; char buffer[4096]; - struct flock fl; + struct flock fl = {0}; c_avl_tree_t *tree; int status; - fh = fopen (h->filename, "r"); + fh = fopen(h->filename, "r"); if (fh == NULL) - return (-1); + return -1; - memset (&fl, 0, sizeof (fl)); fl.l_type = F_RDLCK; fl.l_whence = SEEK_SET; - fl.l_start = 0; - fl.l_len = 0; /* == entire file */ /* TODO: Lock file? -> fcntl */ - status = fcntl (fileno (fh), F_SETLK, &fl); - if (status != 0) - { - fclose (fh); - return (-1); + status = fcntl(fileno(fh), F_SETLK, &fl); + if (status != 0) { + fclose(fh); + return -1; } - tree = c_avl_create ((void *) strcmp); - if (tree == NULL) - { - fclose (fh); - return (-1); + tree = c_avl_create((int (*)(const void *, const void *))strcmp); + if (tree == NULL) { + fclose(fh); + return -1; } /* Read `fh' into `tree' */ - while (fgets (buffer, sizeof (buffer), fh) != NULL) /* {{{ */ + while (fgets(buffer, sizeof(buffer), fh) != NULL) /* {{{ */ { size_t len; char *key; @@ -105,20 +102,19 @@ static int fbh_read_file (fbhash_t *h) /* {{{ */ char *key_copy; char *value_copy; - buffer[sizeof (buffer) - 1] = 0; - len = strlen (buffer); + buffer[sizeof(buffer) - 1] = '\0'; + len = strlen(buffer); /* Remove trailing newline characters. */ - while ((len > 0) - && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r'))) - { + while ((len > 0) && + ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r'))) { len--; buffer[len] = 0; } /* Seek first non-space character */ key = buffer; - while ((*key != 0) && isspace ((int) *key)) + while ((*key != 0) && isspace((int)*key)) key++; /* Skip empty lines and comments */ @@ -126,7 +122,7 @@ static int fbh_read_file (fbhash_t *h) /* {{{ */ continue; /* Seek first colon */ - value = strchr (key, ':'); + value = strchr(key, ':'); if (value == NULL) continue; @@ -135,138 +131,127 @@ static int fbh_read_file (fbhash_t *h) /* {{{ */ value++; /* Skip leading whitespace */ - while ((*value != 0) && isspace ((int) *value)) + while ((*value != 0) && isspace((int)*value)) value++; /* Skip lines without value */ if (value[0] == 0) continue; - key_copy = strdup (key); - value_copy = strdup (value); + key_copy = strdup(key); + value_copy = strdup(value); - if ((key_copy == NULL) || (value_copy == NULL)) - { - free (key_copy); - free (value_copy); + if ((key_copy == NULL) || (value_copy == NULL)) { + free(key_copy); + free(value_copy); continue; } - status = c_avl_insert (tree, key_copy, value_copy); - if (status != 0) - { - free (key_copy); - free (value_copy); + status = c_avl_insert(tree, key_copy, value_copy); + if (status != 0) { + free(key_copy); + free(value_copy); continue; } - DEBUG ("utils_fbhash: fbh_read_file: key = %s; value = %s;", - key, value); + DEBUG("utils_fbhash: fbh_read_file: key = %s; value = %s;", key, value); } /* }}} while (fgets) */ - fclose (fh); + fclose(fh); - fbh_free_tree (h->tree); + fbh_free_tree(h->tree); h->tree = tree; - return (0); + return 0; } /* }}} int fbh_read_file */ -static int fbh_check_file (fbhash_t *h) /* {{{ */ +static int fbh_check_file(fbhash_t *h) /* {{{ */ { - struct stat statbuf; + struct stat statbuf = {0}; int status; - memset (&statbuf, 0, sizeof (statbuf)); - - status = stat (h->filename, &statbuf); + status = stat(h->filename, &statbuf); if (status != 0) - return (-1); + return -1; if (h->mtime >= statbuf.st_mtime) - return (0); + return 0; - status = fbh_read_file (h); + status = fbh_read_file(h); if (status == 0) h->mtime = statbuf.st_mtime; - return (status); + return status; } /* }}} int fbh_check_file */ /* * Public functions */ -fbhash_t *fbh_create (const char *file) /* {{{ */ +fbhash_t *fbh_create(const char *file) /* {{{ */ { fbhash_t *h; int status; if (file == NULL) - return (NULL); + return NULL; - h = malloc (sizeof (*h)); + h = calloc(1, sizeof(*h)); if (h == NULL) - return (NULL); - memset (h, 0, sizeof (*h)); + return NULL; - h->filename = strdup (file); - if (h->filename == NULL) - { - free (h); - return (NULL); + h->filename = strdup(file); + if (h->filename == NULL) { + free(h); + return NULL; } h->mtime = 0; - pthread_mutex_init (&h->lock, /* attr = */ NULL); + pthread_mutex_init(&h->lock, /* attr = */ NULL); - status = fbh_check_file (h); - if (status != 0) - { - fbh_destroy (h); - free (h); - return (NULL); + status = fbh_check_file(h); + if (status != 0) { + fbh_destroy(h); + free(h); + return NULL; } - return (h); + return h; } /* }}} fbhash_t *fbh_create */ -void fbh_destroy (fbhash_t *h) /* {{{ */ +void fbh_destroy(fbhash_t *h) /* {{{ */ { if (h == NULL) return; - pthread_mutex_destroy (&h->lock); - free (h->filename); - fbh_free_tree (h->tree); + pthread_mutex_destroy(&h->lock); + free(h->filename); + fbh_free_tree(h->tree); } /* }}} void fbh_destroy */ -char *fbh_get (fbhash_t *h, const char *key) /* {{{ */ +char *fbh_get(fbhash_t *h, const char *key) /* {{{ */ { char *value; char *value_copy; int status; if ((h == NULL) || (key == NULL)) - return (NULL); + return NULL; value = NULL; value_copy = NULL; - pthread_mutex_lock (&h->lock); + pthread_mutex_lock(&h->lock); - /* TODO: Checking this everytime may be a bit much..? */ - fbh_check_file (h); + /* TODO: Checking this every time may be a bit much..? */ + fbh_check_file(h); - status = c_avl_get (h->tree, key, (void *) &value); - if (status == 0) - { - assert (value != NULL); - value_copy = strdup (value); + status = c_avl_get(h->tree, key, (void *)&value); + if (status == 0) { + assert(value != NULL); + value_copy = strdup(value); } - pthread_mutex_unlock (&h->lock); + pthread_mutex_unlock(&h->lock); - return (value_copy); + return value_copy; } /* }}} char *fbh_get */ - -/* vim: set sw=2 sts=2 et fdm=marker : */