Merge branch 'collectd-5.5' into collectd-5.6
[collectd.git] / src / utils_mount.c
index 37b52d6..e527c25 100644 (file)
@@ -27,6 +27,7 @@
 #define _GNU_SOURCE
 
 #include "collectd.h"
+
 #include "utils_mount.h"
 
 #if HAVE_XFS_XQM_H
@@ -216,7 +217,6 @@ uuidcache_init(void)
        FILE *procpt;
        char uuid[16], *label = NULL;
        char device[110];
-       int firstPass;
        int handleOnFirst;
 
        if(uuidCache) {
@@ -228,7 +228,7 @@ uuidcache_init(void)
                return;
        }
 
-       for(firstPass = 1; firstPass >= 0; firstPass--) {
+       for(int firstPass = 1; firstPass >= 0; firstPass--) {
                fseek(procpt, 0, SEEK_SET);
                while(fgets(line, sizeof(line), procpt)) {
                        if(sscanf(line, " %d %d %d %[^\n ]",
@@ -318,14 +318,13 @@ static char *
 get_spec_by_uuid(const char *s)
 {
        char uuid[16];
-       int i;
 
        if(strlen(s) != 36
        || s[8] != '-' || s[13] != '-' || s[18] != '-' || s[23] != '-') {
                goto bad_uuid;
        }
 
-       for(i=0; i<16; i++) {
+       for(int i=0; i<16; i++) {
                if(*s == '-') {
                        s++;
                }
@@ -382,7 +381,6 @@ static char *get_device_name(const char *optstr)
 static cu_mount_t *cu_mount_listmntent (void)
 {
        cu_mount_t *last = *list;
-       struct tabmntent *p;
        struct mntent *mnt;
 
        struct tabmntent *mntlist;
@@ -394,7 +392,7 @@ static cu_mount_t *cu_mount_listmntent (void)
 #endif /* COLLECT_DEBUG */
        }
 
-       for(p = mntlist; p; p = p->next) {
+       for(struct tabmntent *p = mntlist; p; p = p->next) {
                char *loop = NULL, *device = NULL;
 
                mnt = p->ment;
@@ -451,7 +449,6 @@ static cu_mount_t *cu_mount_getfsstat (void)
        STRUCT_STATFS *buf;
 
        int num;
-       int i;
 
        cu_mount_t *first = NULL;
        cu_mount_t *last  = NULL;
@@ -468,10 +465,8 @@ static cu_mount_t *cu_mount_getfsstat (void)
                return (NULL);
        }
 
-       if ((buf = (STRUCT_STATFS *) malloc (bufsize * sizeof (STRUCT_STATFS)))
-                       == NULL)
+       if ((buf = calloc (bufsize, sizeof (*buf))) == NULL)
                return (NULL);
-       memset (buf, '\0', bufsize * sizeof (STRUCT_STATFS));
 
        /* The bufsize needs to be passed in bytes. Really. This is not in the
         * manpage.. -octo */
@@ -486,12 +481,11 @@ static cu_mount_t *cu_mount_getfsstat (void)
                return (NULL);
        }
 
-       for (i = 0; i < num; i++)
+       for (int i = 0; i < num; i++)
        {
-               if ((new = malloc (sizeof (cu_mount_t))) == NULL)
+               if ((new = calloc (1, sizeof (*new))) == NULL)
                        break;
-               memset (new, '\0', sizeof (cu_mount_t));
-               
+
                /* Copy values from `struct mnttab' */
                new->dir         = sstrdup (buf[i].f_mntonname);
                new->spec_device = sstrdup (buf[i].f_mntfromname);
@@ -542,10 +536,9 @@ static cu_mount_t *cu_mount_gen_getmntent (void)
 
        while (getmntent (fp, &mt) == 0)
        {
-               if ((new = malloc (sizeof (cu_mount_t))) == NULL)
+               if ((new = calloc (1, sizeof (*new))) == NULL)
                        break;
-               memset (new, '\0', sizeof (cu_mount_t));
-               
+
                /* Copy values from `struct mnttab' */
                new->dir         = sstrdup (mt.mnt_mountp);
                new->spec_device = sstrdup (mt.mnt_special);
@@ -600,9 +593,8 @@ static cu_mount_t *cu_mount_getmntent (void)
 
        while (getmntent_r (fp, &me, mntbuf, sizeof (mntbuf) ))
        {
-               if ((new = malloc (sizeof (cu_mount_t))) == NULL)
+               if ((new = calloc (1, sizeof (*new))) == NULL)
                        break;
-               memset (new, '\0', sizeof (cu_mount_t));
 
                /* Copy values from `struct mntent *' */
                new->dir         = sstrdup (me.mnt_dir);
@@ -657,10 +649,9 @@ static cu_mount_t *cu_mount_getmntent (void)
 
        while ((me = getmntent (fp)) != NULL)
        {
-               if ((new = malloc (sizeof (cu_mount_t))) == NULL)
+               if ((new = calloc (1, sizeof (*new))) == NULL)
                        break;
-               memset (new, '\0', sizeof (cu_mount_t));
-               
+
                /* Copy values from `struct mntent *' */
                new->dir         = sstrdup (me->mnt_dir);
                new->spec_device = sstrdup (me->mnt_fsname);
@@ -747,10 +738,9 @@ cu_mount_t *cu_mount_getlist(cu_mount_t **list)
 
 void cu_mount_freelist (cu_mount_t *list)
 {
-       cu_mount_t *this;
        cu_mount_t *next;
 
-       for (this = list; this != NULL; this = next)
+       for (cu_mount_t *this = list; this != NULL; this = next)
        {
                next = this->next;
 
@@ -764,7 +754,7 @@ void cu_mount_freelist (cu_mount_t *list)
 } /* void cu_mount_freelist(cu_mount_t *list) */
 
 char *
-cu_mount_checkoption(char *line, char *keyword, int full)
+cu_mount_checkoption(char *line, const char *keyword, int full)
 {
        char *line2, *l2, *p1, *p2;
        int l;
@@ -805,7 +795,7 @@ cu_mount_checkoption(char *line, char *keyword, int full)
 } /* char *cu_mount_checkoption(char *line, char *keyword, int full) */
 
 char *
-cu_mount_getoptionvalue(char *line, char *keyword)
+cu_mount_getoptionvalue(char *line, const char *keyword)
 {
        char *r;
 
@@ -821,13 +811,13 @@ cu_mount_getoptionvalue(char *line, char *keyword)
                        if((p-r) == 1) {
                                return NULL;
                        }
-                       m = (char *)smalloc(p-r+1);
+                       m = smalloc(p-r+1);
                        sstrncpy(m, r, p-r+1);
                        return m;
                }
        }
        return r;
-} /* char *cu_mount_getoptionvalue(char *line, char *keyword) */
+} /* char *cu_mount_getoptionvalue(char *line, const char *keyword) */
 
 int
 cu_mount_type(const char *type)