Merge tag 'for-linus-20150909' of git://git.infradead.org/linux-mtd
[firefly-linux-kernel-4.4.55.git] / tools / iio / iio_utils.c
index 8fb3214c70f255ec21b76c71ce96b15fa4a3f67e..5eb6793f3972e64ed2c46533cd2e6aa5d65168fa 100644 (file)
@@ -6,9 +6,6 @@
  * under the terms of the GNU General Public License version 2 as published by
  * the Free Software Foundation.
  */
-#ifndef _IIO_UTILS_H
-#define _IIO_UTILS_H
-
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -39,7 +36,7 @@ int iioutils_break_up_name(const char *full_name, char **generic_name)
        char *working, *prefix = "";
        int i, ret;
 
-       for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++)
+       for (i = 0; i < ARRAY_SIZE(iio_direction); i++)
                if (!strncmp(full_name, iio_direction[i],
                             strlen(iio_direction[i]))) {
                        prefix = iio_direction[i];
@@ -117,13 +114,13 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
        }
 
        dp = opendir(scan_el_dir);
-       if (dp == NULL) {
+       if (!dp) {
                ret = -errno;
                goto error_free_builtname_generic;
        }
 
        ret = -ENOENT;
-       while (ent = readdir(dp), ent != NULL)
+       while (ent = readdir(dp), ent)
                /*
                 * Do we allow devices to override a generic name with
                 * a specific one?
@@ -138,9 +135,10 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
                        }
 
                        sysfsfp = fopen(filename, "r");
-                       if (sysfsfp == NULL) {
+                       if (!sysfsfp) {
                                ret = -errno;
-                               printf("failed to open %s\n", filename);
+                               fprintf(stderr, "failed to open %s\n",
+                                       filename);
                                goto error_free_filename;
                        }
 
@@ -152,11 +150,13 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
                                     &padint, shift);
                        if (ret < 0) {
                                ret = -errno;
-                               printf("failed to pass scan type description\n");
+                               fprintf(stderr,
+                                       "failed to pass scan type description\n");
                                goto error_close_sysfsfp;
                        } else if (ret != 5) {
                                ret = -EIO;
-                               printf("scan type description didn't match\n");
+                               fprintf(stderr,
+                                       "scan type description didn't match\n");
                                goto error_close_sysfsfp;
                        }
 
@@ -165,12 +165,13 @@ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used,
                        if (*bits_used == 64)
                                *mask = ~0;
                        else
-                               *mask = (1 << *bits_used) - 1;
+                               *mask = (1ULL << *bits_used) - 1;
 
                        *is_signed = (signchar == 's');
                        if (fclose(sysfsfp)) {
                                ret = -errno;
-                               printf("Failed to close %s\n", filename);
+                               fprintf(stderr, "Failed to close %s\n",
+                                       filename);
                                goto error_free_filename;
                        }
 
@@ -235,13 +236,13 @@ int iioutils_get_param_float(float *output, const char *param_name,
        }
 
        dp = opendir(device_dir);
-       if (dp == NULL) {
+       if (!dp) {
                ret = -errno;
                goto error_free_builtname_generic;
        }
 
        ret = -ENOENT;
-       while (ent = readdir(dp), ent != NULL)
+       while (ent = readdir(dp), ent)
                if ((strcmp(builtname, ent->d_name) == 0) ||
                    (strcmp(builtname_generic, ent->d_name) == 0)) {
                        ret = asprintf(&filename,
@@ -285,17 +286,17 @@ error_free_builtname:
  * @cnt: the amount of array elements
  **/
 
-void bsort_channel_array_by_index(struct iio_channel_info **ci_array, int cnt)
+void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt)
 {
        struct iio_channel_info temp;
        int x, y;
 
        for (x = 0; x < cnt; x++)
                for (y = 0; y < (cnt - 1); y++)
-                       if ((*ci_array)[y].index > (*ci_array)[y + 1].index) {
-                               temp = (*ci_array)[y + 1];
-                               (*ci_array)[y + 1] = (*ci_array)[y];
-                               (*ci_array)[y] = temp;
+                       if (ci_array[y].index > ci_array[y + 1].index) {
+                               temp = ci_array[y + 1];
+                               ci_array[y + 1] = ci_array[y];
+                               ci_array[y] = temp;
                        }
 }
 
@@ -325,12 +326,12 @@ int build_channel_array(const char *device_dir,
                return -ENOMEM;
 
        dp = opendir(scan_el_dir);
-       if (dp == NULL) {
+       if (!dp) {
                ret = -errno;
                goto error_free_name;
        }
 
-       while (ent = readdir(dp), ent != NULL)
+       while (ent = readdir(dp), ent)
                if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
                           "_en") == 0) {
                        ret = asprintf(&filename,
@@ -341,7 +342,7 @@ int build_channel_array(const char *device_dir,
                        }
 
                        sysfsfp = fopen(filename, "r");
-                       if (sysfsfp == NULL) {
+                       if (!sysfsfp) {
                                ret = -errno;
                                free(filename);
                                goto error_close_dir;
@@ -369,13 +370,13 @@ int build_channel_array(const char *device_dir,
                }
 
        *ci_array = malloc(sizeof(**ci_array) * (*counter));
-       if (*ci_array == NULL) {
+       if (!*ci_array) {
                ret = -ENOMEM;
                goto error_close_dir;
        }
 
        seekdir(dp, 0);
-       while (ent = readdir(dp), ent != NULL) {
+       while (ent = readdir(dp), ent) {
                if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
                           "_en") == 0) {
                        int current_enabled = 0;
@@ -391,7 +392,7 @@ int build_channel_array(const char *device_dir,
                        }
 
                        sysfsfp = fopen(filename, "r");
-                       if (sysfsfp == NULL) {
+                       if (!sysfsfp) {
                                ret = -errno;
                                free(filename);
                                count--;
@@ -424,7 +425,7 @@ int build_channel_array(const char *device_dir,
                        current->name = strndup(ent->d_name,
                                                strlen(ent->d_name) -
                                                strlen("_en"));
-                       if (current->name == NULL) {
+                       if (!current->name) {
                                free(filename);
                                ret = -ENOMEM;
                                count--;
@@ -452,9 +453,10 @@ int build_channel_array(const char *device_dir,
                        }
 
                        sysfsfp = fopen(filename, "r");
-                       if (sysfsfp == NULL) {
+                       if (!sysfsfp) {
                                ret = -errno;
-                               printf("failed to open %s\n", filename);
+                               fprintf(stderr, "failed to open %s\n",
+                                       filename);
                                free(filename);
                                goto error_cleanup_array;
                        }
@@ -482,7 +484,7 @@ int build_channel_array(const char *device_dir,
                                                       device_dir,
                                                       current->name,
                                                       current->generic_name);
-                       if (ret < 0)
+                       if ((ret < 0) && (ret != -ENOENT))
                                goto error_cleanup_array;
 
                        ret = iioutils_get_param_float(&current->offset,
@@ -490,7 +492,7 @@ int build_channel_array(const char *device_dir,
                                                       device_dir,
                                                       current->name,
                                                       current->generic_name);
-                       if (ret < 0)
+                       if ((ret < 0) && (ret != -ENOENT))
                                goto error_cleanup_array;
 
                        ret = iioutils_get_type(&current->is_signed,
@@ -514,7 +516,7 @@ int build_channel_array(const char *device_dir,
 
        free(scan_el_dir);
        /* reorder so that the array is in index order */
-       bsort_channel_array_by_index(ci_array, *counter);
+       bsort_channel_array_by_index(*ci_array, *counter);
 
        return 0;
 
@@ -524,6 +526,8 @@ error_cleanup_array:
                free((*ci_array)[i].generic_name);
        }
        free(*ci_array);
+       *ci_array = NULL;
+       *counter = 0;
 error_close_dir:
        if (dp)
                if (closedir(dp) == -1)
@@ -535,7 +539,7 @@ error_free_name:
        return ret;
 }
 
-int calc_digits(int num)
+static int calc_digits(int num)
 {
        int count = 0;
 
@@ -567,12 +571,12 @@ int find_type_by_name(const char *name, const char *type)
        char *filename;
 
        dp = opendir(iio_dir);
-       if (dp == NULL) {
-               printf("No industrialio devices available\n");
+       if (!dp) {
+               fprintf(stderr, "No industrialio devices available\n");
                return -ENODEV;
        }
 
-       while (ent = readdir(dp), ent != NULL) {
+       while (ent = readdir(dp), ent) {
                if (strcmp(ent->d_name, ".") != 0 &&
                    strcmp(ent->d_name, "..") != 0 &&
                    strlen(ent->d_name) > strlen(type) &&
@@ -581,11 +585,13 @@ int find_type_by_name(const char *name, const char *type)
                        ret = sscanf(ent->d_name + strlen(type), "%d", &number);
                        if (ret < 0) {
                                ret = -errno;
-                               printf("failed to read element number\n");
+                               fprintf(stderr,
+                                       "failed to read element number\n");
                                goto error_close_dir;
                        } else if (ret != 1) {
                                ret = -EIO;
-                               printf("failed to match element number\n");
+                               fprintf(stderr,
+                                       "failed to match element number\n");
                                goto error_close_dir;
                        }
 
@@ -595,7 +601,7 @@ int find_type_by_name(const char *name, const char *type)
                            ":", 1) != 0) {
                                filename = malloc(strlen(iio_dir) + strlen(type)
                                                  + numstrlen + 6);
-                               if (filename == NULL) {
+                               if (!filename) {
                                        ret = -ENOMEM;
                                        goto error_close_dir;
                                }
@@ -654,7 +660,7 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val,
        int test;
        char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
 
-       if (temp == NULL)
+       if (!temp)
                return -ENOMEM;
 
        ret = sprintf(temp, "%s/%s", basedir, filename);
@@ -662,9 +668,9 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val,
                goto error_free;
 
        sysfsfp = fopen(temp, "w");
-       if (sysfsfp == NULL) {
+       if (!sysfsfp) {
                ret = -errno;
-               printf("failed to open %s\n", temp);
+               fprintf(stderr, "failed to open %s\n", temp);
                goto error_free;
        }
 
@@ -683,9 +689,9 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val,
 
        if (verify) {
                sysfsfp = fopen(temp, "r");
-               if (sysfsfp == NULL) {
+               if (!sysfsfp) {
                        ret = -errno;
-                       printf("failed to open %s\n", temp);
+                       fprintf(stderr, "failed to open %s\n", temp);
                        goto error_free;
                }
 
@@ -703,8 +709,9 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val,
                }
 
                if (test != val) {
-                       printf("Possible failure in int write %d to %s/%s\n",
-                              val, basedir, filename);
+                       fprintf(stderr,
+                               "Possible failure in int write %d to %s/%s\n",
+                               val, basedir, filename);
                        ret = -1;
                }
        }
@@ -749,8 +756,8 @@ static int _write_sysfs_string(const char *filename, const char *basedir,
        FILE  *sysfsfp;
        char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
 
-       if (temp == NULL) {
-               printf("Memory allocation failed\n");
+       if (!temp) {
+               fprintf(stderr, "Memory allocation failed\n");
                return -ENOMEM;
        }
 
@@ -759,9 +766,9 @@ static int _write_sysfs_string(const char *filename, const char *basedir,
                goto error_free;
 
        sysfsfp = fopen(temp, "w");
-       if (sysfsfp == NULL) {
+       if (!sysfsfp) {
                ret = -errno;
-               printf("Could not open %s\n", temp);
+               fprintf(stderr, "Could not open %s\n", temp);
                goto error_free;
        }
 
@@ -780,9 +787,9 @@ static int _write_sysfs_string(const char *filename, const char *basedir,
 
        if (verify) {
                sysfsfp = fopen(temp, "r");
-               if (sysfsfp == NULL) {
+               if (!sysfsfp) {
                        ret = -errno;
-                       printf("Could not open file to verify\n");
+                       fprintf(stderr, "Could not open file to verify\n");
                        goto error_free;
                }
 
@@ -800,9 +807,10 @@ static int _write_sysfs_string(const char *filename, const char *basedir,
                }
 
                if (strcmp(temp, val) != 0) {
-                       printf("Possible failure in string write of %s "
-                              "Should be %s written to %s/%s\n", temp, val,
-                              basedir, filename);
+                       fprintf(stderr,
+                               "Possible failure in string write of %s "
+                               "Should be %s written to %s/%s\n", temp, val,
+                               basedir, filename);
                        ret = -1;
                }
        }
@@ -855,8 +863,8 @@ int read_sysfs_posint(const char *filename, const char *basedir)
        FILE  *sysfsfp;
        char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
 
-       if (temp == NULL) {
-               printf("Memory allocation failed");
+       if (!temp) {
+               fprintf(stderr, "Memory allocation failed");
                return -ENOMEM;
        }
 
@@ -865,7 +873,7 @@ int read_sysfs_posint(const char *filename, const char *basedir)
                goto error_free;
 
        sysfsfp = fopen(temp, "r");
-       if (sysfsfp == NULL) {
+       if (!sysfsfp) {
                ret = -errno;
                goto error_free;
        }
@@ -902,8 +910,8 @@ int read_sysfs_float(const char *filename, const char *basedir, float *val)
        FILE  *sysfsfp;
        char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
 
-       if (temp == NULL) {
-               printf("Memory allocation failed");
+       if (!temp) {
+               fprintf(stderr, "Memory allocation failed");
                return -ENOMEM;
        }
 
@@ -912,7 +920,7 @@ int read_sysfs_float(const char *filename, const char *basedir, float *val)
                goto error_free;
 
        sysfsfp = fopen(temp, "r");
-       if (sysfsfp == NULL) {
+       if (!sysfsfp) {
                ret = -errno;
                goto error_free;
        }
@@ -949,8 +957,8 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str)
        FILE  *sysfsfp;
        char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
 
-       if (temp == NULL) {
-               printf("Memory allocation failed");
+       if (!temp) {
+               fprintf(stderr, "Memory allocation failed");
                return -ENOMEM;
        }
 
@@ -959,7 +967,7 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str)
                goto error_free;
 
        sysfsfp = fopen(temp, "r");
-       if (sysfsfp == NULL) {
+       if (!sysfsfp) {
                ret = -errno;
                goto error_free;
        }
@@ -981,5 +989,3 @@ error_free:
 
        return ret;
 }
-
-#endif /* _IIO_UTILS_H */