X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=common.c;h=2b6b5eb9180efa2272695de031d0d3d4c6d49ac0;hb=fab918278f2919ebc74a2f7b1a9db2bfeab46593;hp=dab0b70f52c7038dc1abc82cef872d3546e3e375;hpb=b2db68ebb7acdb345c07985fcdb35dcfbb36d4aa;p=collection4.git diff --git a/common.c b/common.c index dab0b70..2b6b5eb 100644 --- a/common.c +++ b/common.c @@ -16,138 +16,8 @@ #include "common.h" #include "graph_list.h" -static int foreach_rrd_file (const char *dir, /* {{{ */ - int (*callback) (const char *, void *), - void *user_data) -{ - DIR *dh; - struct dirent *entry; - int status; - - if (callback == NULL) - return (EINVAL); - - dh = opendir (dir); - if (dh == NULL) - return (errno); - - while ((entry = readdir (dh)) != NULL) - { - struct stat statbuf; - char abspath[PATH_MAX + 1]; - size_t d_name_len; - - if (entry->d_name[0] == '.') - continue; - - d_name_len = strlen (entry->d_name); - if (d_name_len <= 4) - continue; - - if (strcasecmp (".rrd", entry->d_name + (d_name_len - 4)) != 0) - continue; - - snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name); - abspath[sizeof (abspath) - 1] = 0; - - memset (&statbuf, 0, sizeof (statbuf)); - - status = stat (abspath, &statbuf); - if (status != 0) - continue; - - if (!S_ISREG (statbuf.st_mode)) - continue; - - entry->d_name[d_name_len - 4] = 0; - - status = (*callback) (entry->d_name, user_data); - if (status != 0) - break; - } /* while (readdir) */ - - closedir (dh); - return (status); -} /* }}} int foreach_rrd_file */ - -static int foreach_dir (const char *dir, /* {{{ */ - int (*callback) (const char *, void *), - void *user_data) -{ - DIR *dh; - struct dirent *entry; - int status; - - if (callback == NULL) - return (EINVAL); - - dh = opendir (dir); - if (dh == NULL) - return (errno); - - while ((entry = readdir (dh)) != NULL) - { - struct stat statbuf; - char abspath[PATH_MAX + 1]; - - if (entry->d_name[0] == '.') - continue; - - snprintf (abspath, sizeof (abspath), "%s/%s", dir, entry->d_name); - abspath[sizeof (abspath) - 1] = 0; - - memset (&statbuf, 0, sizeof (statbuf)); - - status = stat (abspath, &statbuf); - if (status != 0) - continue; - - if (!S_ISDIR (statbuf.st_mode)) - continue; - - status = (*callback) (entry->d_name, user_data); - if (status != 0) - break; - } /* while (readdir) */ - - closedir (dh); - return (status); -} /* }}} int foreach_dir */ - -int foreach_type (const char *host, const char *plugin, /* {{{ */ - callback_type_t callback, void *user_data) -{ - char abspath[PATH_MAX + 1]; - - if ((host == NULL) || (plugin == NULL)) - return (EINVAL); - - snprintf (abspath, sizeof (abspath), "%s/%s/%s", DATA_DIR, host, plugin); - abspath[sizeof (abspath) - 1] = 0; - - return (foreach_rrd_file (abspath, callback, user_data)); -} /* }}} int foreach_type */ - -int foreach_plugin (const char *host, /* {{{ */ - callback_plugin_t callback, - void *user_data) -{ - char abspath[PATH_MAX + 1]; - - if (host == NULL) - return (EINVAL); - - snprintf (abspath, sizeof (abspath), "%s/%s", DATA_DIR, host); - abspath[sizeof (abspath) - 1] = 0; - - return (foreach_dir (abspath, callback, user_data)); -} /* }}} int foreach_plugin */ - -int foreach_host (callback_host_t callback, /* {{{ */ - void *user_data) -{ - return (foreach_dir (DATA_DIR, callback, user_data)); -} /* }}} int foreach_host */ +#include +#include size_t c_strlcat (char *dst, const char *src, size_t size) /* {{{ */ { @@ -300,4 +170,24 @@ uint32_t get_random_color (void) /* {{{ */ return (rgb_to_uint32 (rgb)); } /* }}} uint32_t get_random_color */ +int print_debug (const char *format, ...) /* {{{ */ +{ + static _Bool have_header = 0; + + va_list ap; + int status; + + if (!have_header) + { + printf ("Content-Type: text/plain\n\n"); + have_header = 1; + } + + va_start (ap, format); + status = vprintf (format, ap); + va_end (ap); + + return (status); +} /* }}} int print_debug */ + /* vim: set sw=2 sts=2 et fdm=marker : */