X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcommon.c;h=81142fd0084bb4d9c89ea7061c62aca72cd12b7d;hb=72ec1d0ed6cde4aa78f17d031e7a48e37cbf7a57;hp=c41c4fe629dd14685e543964d9a7c05b7d8f399b;hpb=affac33e83584e7538c358e3bd0a587d0c692bc3;p=collectd.git diff --git a/src/common.c b/src/common.c index c41c4fe6..81142fd0 100644 --- a/src/common.c +++ b/src/common.c @@ -1,6 +1,6 @@ /** * collectd - src/common.c - * Copyright (C) 2005-2010 Florian octo Forster + * Copyright (C) 2005-2014 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 @@ -86,6 +86,47 @@ int ssnprintf (char *dest, size_t n, const char *format, ...) return (ret); } /* int ssnprintf */ +char *ssnprintf_alloc (char const *format, ...) /* {{{ */ +{ + char static_buffer[1024] = ""; + char *alloc_buffer; + size_t alloc_buffer_size; + int status; + va_list ap; + + /* Try printing into the static buffer. In many cases it will be + * sufficiently large and we can simply return a strdup() of this + * buffer. */ + va_start (ap, format); + status = vsnprintf (static_buffer, sizeof (static_buffer), format, ap); + va_end (ap); + if (status < 0) + return (NULL); + + /* "status" does not include the null byte. */ + alloc_buffer_size = (size_t) (status + 1); + if (alloc_buffer_size <= sizeof (static_buffer)) + return (strdup (static_buffer)); + + /* Allocate a buffer large enough to hold the string. */ + alloc_buffer = malloc (alloc_buffer_size); + if (alloc_buffer == NULL) + return (NULL); + memset (alloc_buffer, 0, alloc_buffer_size); + + /* Print again into this new buffer. */ + va_start (ap, format); + status = vsnprintf (alloc_buffer, alloc_buffer_size, format, ap); + va_end (ap); + if (status < 0) + { + sfree (alloc_buffer); + return (NULL); + } + + return (alloc_buffer); +} /* }}} char *ssnprintf_alloc */ + char *sstrdup (const char *s) { char *r; @@ -377,34 +418,36 @@ size_t strstripnewline (char *buffer) return (buffer_len); } /* size_t strstripnewline */ -int escape_slashes (char *buf, int buf_len) +int escape_slashes (char *buffer, size_t buffer_size) { int i; + size_t buffer_len; - if (strcmp (buf, "/") == 0) - { - if (buf_len < 5) - return (-1); + buffer_len = strlen (buffer); - strncpy (buf, "root", buf_len); + if (buffer_len <= 1) + { + if (strcmp ("/", buffer) == 0) + { + if (buffer_size < 5) + return (-1); + sstrncpy (buffer, "root", buffer_size); + } return (0); } - if (buf_len <= 1) - return (0); - /* Move one to the left */ - if (buf[0] == '/') - memmove (buf, buf + 1, buf_len - 1); + if (buffer[0] == '/') + { + memmove (buffer, buffer + 1, buffer_len); + buffer_len--; + } - for (i = 0; i < buf_len - 1; i++) + for (i = 0; i < buffer_len - 1; i++) { - if (buf[i] == '\0') - break; - else if (buf[i] == '/') - buf[i] = '_'; + if (buffer[i] == '/') + buffer[i] = '_'; } - buf[i] = '\0'; return (0); } /* int escape_slashes */ @@ -1220,18 +1263,25 @@ int walk_directory (const char *dir, dirwalk_callback_f callback, return (0); } -int read_file_contents (const char *filename, char *buf, int bufsize) +ssize_t read_file_contents (const char *filename, char *buf, size_t bufsize) { FILE *fh; - int n; + ssize_t ret; - if ((fh = fopen (filename, "r")) == NULL) - return -1; + fh = fopen (filename, "r"); + if (fh == NULL) + return (-1); - n = fread(buf, 1, bufsize, fh); - fclose(fh); + ret = (ssize_t) fread (buf, 1, bufsize, fh); + if ((ret == 0) && (ferror (fh) != 0)) + { + ERROR ("read_file_contents: Reading file \"%s\" failed.", + filename); + ret = -1; + } - return n; + fclose(fh); + return (ret); } counter_t counter_diff (counter_t old_value, counter_t new_value)