From: Florian Forster Date: Wed, 10 Sep 2014 15:15:24 +0000 (+0200) Subject: Merge branch 'collectd-5.3' into collectd-5.4 X-Git-Tag: collectd-5.4.2~13 X-Git-Url: https://git.octo.it/?a=commitdiff_plain;h=336ea1e244e6c5d1d8bf8b42774b08c03a932039;hp=-c;p=collectd.git Merge branch 'collectd-5.3' into collectd-5.4 --- 336ea1e244e6c5d1d8bf8b42774b08c03a932039 diff --combined src/common.c index dc77e02f,b771f62e..8a40792a --- a/src/common.c +++ b/src/common.c @@@ -86,47 -86,6 +86,47 @@@ int ssnprintf (char *dest, size_t n, co 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; @@@ -411,8 -370,8 +411,8 @@@ size_t strstripnewline (char *buffer if ((buffer[buffer_len - 1] != '\n') && (buffer[buffer_len - 1] != '\r')) break; - buffer[buffer_len] = 0; buffer_len--; + buffer[buffer_len] = 0; } return (buffer_len); @@@ -1261,25 -1220,18 +1261,25 @@@ int walk_directory (const char *dir, di 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)