Code

sensors plugin: Improved the documentation in the sample configfile and the `collectd...
[collectd.git] / src / common.c
index 7690d01949175869928ad95f986af9409150270f..5487b326fd34ac31eb8adfe20fa6971ecd0c4fec 100644 (file)
@@ -125,6 +125,70 @@ void sfree (void **ptr)
 }
 #endif
 
+ssize_t sread (int fd, void *buf, size_t count)
+{
+       char    *ptr;
+       size_t   nleft;
+       ssize_t  status;
+
+       ptr   = (char *) buf;
+       nleft = count;
+
+       while (nleft > 0)
+       {
+               status = read (fd, (void *) ptr, nleft);
+
+               if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
+                       continue;
+
+               if (status < 0)
+                       return (status);
+
+               if (status == 0)
+               {
+                       DBG ("Received EOF from fd %i. "
+                                       "Closing fd and returning error.",
+                                       fd);
+                       close (fd);
+                       return (-1);
+               }
+
+               assert (nleft >= status);
+
+               nleft = nleft - status;
+               ptr   = ptr   + status;
+       }
+
+       return (0);
+}
+
+
+ssize_t swrite (int fd, const void *buf, size_t count)
+{
+       const char *ptr;
+       size_t      nleft;
+       ssize_t     status;
+
+       ptr   = (const char *) buf;
+       nleft = count;
+
+       while (nleft > 0)
+       {
+               status = write (fd, (const void *) ptr, nleft);
+
+               if ((status < 0) && ((errno == EAGAIN) || (errno == EINTR)))
+                       continue;
+
+               if (status < 0)
+                       return (status);
+
+               nleft = nleft - status;
+               ptr   = ptr   + status;
+       }
+
+       return (0);
+}
+
 int strsplit (char *string, char **fields, size_t size)
 {
        size_t i;
@@ -363,7 +427,8 @@ static int check_create_dir (const char *file_orig)
 /* * * * *
  * Magic *
  * * * * */
-int rra_get (char ***ret)
+#if HAVE_LIBRRD
+static int rra_get (char ***ret)
 {
        static char **rra_def = NULL;
        static int rra_num = 0;
@@ -440,12 +505,16 @@ int rra_get (char ***ret)
        *ret = rra_def;
        return (rra_num);
 }
+#endif /* HAVE_LIBRRD */
 
 static int log_create_file (char *filename, char **ds_def, int ds_num)
 {
        FILE *log;
        int i;
 
+       if (check_create_dir (filename))
+               return (-1);
+
        log = fopen (filename, "w");
        if (log == NULL)
        {
@@ -483,7 +552,7 @@ static int log_create_file (char *filename, char **ds_def, int ds_num)
 
                /* The `%.*s' is needed because there is no null-byte behind
                 * the name. */
-               fprintf(log, ",%.*s", (tmp - name), name);
+               fprintf(log, ",%.*s", (int) (tmp - name), name);
        }
        fprintf(log, "\n");
        fclose(log);
@@ -517,7 +586,7 @@ static int log_update_file (char *host, char *file, char *values,
        strncpy (full_file, file, 1024);
 
        tmp = full_file + strlen (full_file) - 4;
-       assert (tmp > 0);
+       assert ((tmp != NULL) && (tmp > full_file));
 
        /* Change the filename for logfiles. */
        if (strncmp (tmp, ".rrd", 4) == 0)