From 66dd08fde06e5ad6f0f86c7a780d60973e9d9cf0 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:39:43 +0200 Subject: tools:iio:iio_utils: free scan_el_dir on exit In the error path, the string scan_el_dir got freed, while it was missing when build_channel_array() finished without errors. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 1 + 1 file changed, 1 insertion(+) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 6f6452167b67..f879ad7b88bc 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -403,6 +403,7 @@ int build_channel_array(const char *device_dir, } closedir(dp); + free(scan_el_dir); /* reorder so that the array is in index order */ bsort_channel_array_by_index(ci_array, *counter); -- cgit v1.2.3-70-g09d2 From 63f05c855f3825b89b92cd21df0415e6e32af3dd Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:39:44 +0200 Subject: tools:iio: free channel-array completely In iio_utils.c build_channel_array() dynamically allocates the string generic_name in the current iio_channel_info, which doesn't got freed in case of an error. This dynamically allocated channel-array is used by generic_buffer, and needs to be freed on the error/exit path. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/generic_buffer.c | 8 +++++++- tools/iio/iio_utils.c | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c index 7859ee9a46b2..0410948e23c2 100644 --- a/tools/iio/generic_buffer.c +++ b/tools/iio/generic_buffer.c @@ -255,7 +255,7 @@ int main(int argc, char **argv) "%siio:device%d/buffer", iio_dir, dev_num); if (ret < 0) { ret = -ENOMEM; - goto error_free_triggername; + goto error_free_channels; } if (!notrigger) { @@ -351,6 +351,12 @@ error_free_data: free(data); error_free_buf_dir_name: free(buf_dir_name); +error_free_channels: + for (i = num_channels - 1; i >= 0; i--) { + free(channels[i].name); + free(channels[i].generic_name); + } + free(channels); error_free_triggername: if (datardytrigger) free(trigger_name); diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index f879ad7b88bc..6daf98fdde1a 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -410,8 +410,10 @@ int build_channel_array(const char *device_dir, return 0; error_cleanup_array: - for (i = count - 1; i >= 0; i--) + for (i = count - 1; i >= 0; i--) { free((*ci_array)[i].name); + free((*ci_array)[i].generic_name); + } free(*ci_array); error_close_dir: closedir(dp); -- cgit v1.2.3-70-g09d2 From 121b5e505fe86ea52603c946865d658a5fe8130b Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:39:45 +0200 Subject: tools:iio:iio_utils: fix allocation handling In build_channel_array(), count needs to be decreased in more places since current->name and current->generic_name would be freed on the error path, although they have not been allocated, yet. This also requires to free current->name, when it is allocated, but current->generic_name is not yet allocated. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 6daf98fdde1a..1c0ca2f89e18 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -334,6 +334,7 @@ int build_channel_array(const char *device_dir, if (sysfsfp == NULL) { free(filename); ret = -errno; + count--; goto error_cleanup_array; } fscanf(sysfsfp, "%i", ¤t_enabled); @@ -353,6 +354,7 @@ int build_channel_array(const char *device_dir, if (current->name == NULL) { free(filename); ret = -ENOMEM; + count--; goto error_cleanup_array; } /* Get the generic and specific name elements */ @@ -360,6 +362,8 @@ int build_channel_array(const char *device_dir, ¤t->generic_name); if (ret) { free(filename); + free(current->name); + count--; goto error_cleanup_array; } ret = asprintf(&filename, -- cgit v1.2.3-70-g09d2 From 2b6a6e67af6f8f644a48f75efc1f44544c0d74f6 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:39:48 +0200 Subject: tools:iio: save errno first The man-page of errno states, that errno should be saved before doing any library call, as that call may have changed the value of errno. So, when encountering any error, save errno first. This patch affects generic_buffer.c, iio_event_monitor.c and iio_utils.c. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/generic_buffer.c | 2 +- tools/iio/iio_event_monitor.c | 6 +++--- tools/iio/iio_utils.c | 14 +++++++------- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c index 0410948e23c2..7635aeb93dc8 100644 --- a/tools/iio/generic_buffer.c +++ b/tools/iio/generic_buffer.c @@ -296,8 +296,8 @@ int main(int argc, char **argv) /* Attempt to open non blocking the access dev */ fp = open(buffer_access, O_RDONLY | O_NONBLOCK); if (fp == -1) { /* If it isn't there make the node */ - printf("Failed to open %s\n", buffer_access); ret = -errno; + printf("Failed to open %s\n", buffer_access); goto error_free_buffer_access; } diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c index 427c271ac0d6..f6fdab48d340 100644 --- a/tools/iio/iio_event_monitor.c +++ b/tools/iio/iio_event_monitor.c @@ -269,8 +269,8 @@ int main(int argc, char **argv) fd = open(chrdev_name, 0); if (fd == -1) { - fprintf(stdout, "Failed to open %s\n", chrdev_name); ret = -errno; + fprintf(stdout, "Failed to open %s\n", chrdev_name); goto error_free_chrdev_name; } @@ -279,8 +279,8 @@ int main(int argc, char **argv) close(fd); if (ret == -1 || event_fd == -1) { - fprintf(stdout, "Failed to retrieve event fd\n"); ret = -errno; + fprintf(stdout, "Failed to retrieve event fd\n"); goto error_free_chrdev_name; } @@ -291,8 +291,8 @@ int main(int argc, char **argv) printf("nothing available\n"); continue; } else { - perror("Failed to read event from device"); ret = -errno; + perror("Failed to read event from device"); break; } } diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 1c0ca2f89e18..f12bc2e81c80 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -129,8 +129,8 @@ int iioutils_get_type(unsigned *is_signed, } sysfsfp = fopen(filename, "r"); if (sysfsfp == NULL) { - printf("failed to open %s\n", filename); ret = -errno; + printf("failed to open %s\n", filename); goto error_free_filename; } @@ -141,8 +141,8 @@ int iioutils_get_type(unsigned *is_signed, bits_used, &padint, shift); if (ret < 0) { - printf("failed to pass scan type description\n"); ret = -errno; + printf("failed to pass scan type description\n"); goto error_close_sysfsfp; } *be = (endianchar == 'b'); @@ -332,8 +332,8 @@ int build_channel_array(const char *device_dir, } sysfsfp = fopen(filename, "r"); if (sysfsfp == NULL) { - free(filename); ret = -errno; + free(filename); count--; goto error_cleanup_array; } @@ -505,8 +505,8 @@ int _write_sysfs_int(char *filename, char *basedir, int val, int verify) sprintf(temp, "%s/%s", basedir, filename); sysfsfp = fopen(temp, "w"); if (sysfsfp == NULL) { - printf("failed to open %s\n", temp); ret = -errno; + printf("failed to open %s\n", temp); goto error_free; } fprintf(sysfsfp, "%d", val); @@ -514,8 +514,8 @@ int _write_sysfs_int(char *filename, char *basedir, int val, int verify) if (verify) { sysfsfp = fopen(temp, "r"); if (sysfsfp == NULL) { - printf("failed to open %s\n", temp); ret = -errno; + printf("failed to open %s\n", temp); goto error_free; } fscanf(sysfsfp, "%d", &test); @@ -556,8 +556,8 @@ int _write_sysfs_string(char *filename, char *basedir, char *val, int verify) sprintf(temp, "%s/%s", basedir, filename); sysfsfp = fopen(temp, "w"); if (sysfsfp == NULL) { - printf("Could not open %s\n", temp); ret = -errno; + printf("Could not open %s\n", temp); goto error_free; } fprintf(sysfsfp, "%s", val); @@ -565,8 +565,8 @@ int _write_sysfs_string(char *filename, char *basedir, char *val, int verify) if (verify) { sysfsfp = fopen(temp, "r"); if (sysfsfp == NULL) { - printf("could not open file to verify\n"); ret = -errno; + printf("could not open file to verify\n"); goto error_free; } fscanf(sysfsfp, "%s", temp); -- cgit v1.2.3-70-g09d2 From dc8b5d6e633f8e54f70594d0be87aaf401ea0559 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:39:59 +0200 Subject: tools:iio:iio_utils: check amount of matches fscanf() usually returns the number of input items successfully matched and assigned, which can be fewer than provided (or even zero). Add a check in iioutils_get_type() to make sure all items are matched. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index f12bc2e81c80..c5b4136e648a 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -144,6 +144,10 @@ int iioutils_get_type(unsigned *is_signed, ret = -errno; printf("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"); + goto error_close_sysfsfp; } *be = (endianchar == 'b'); *bytes = padint / 8; -- cgit v1.2.3-70-g09d2 From 096f9b862e605fe08bb30e4f7a381684a8ff82ed Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:00 +0200 Subject: tools:iio:iio_utils: implement digit calculation Previously, the return value of sscanf() was treated as an indication of the digits it would have read. Yet, sscanf() only returns the amount of valid matches. Therefore, introduce a function to calculate the decimal digits of the read number and use this one to commence a colon search, as originally intended. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index c5b4136e648a..60e5ec4165b7 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -431,6 +431,18 @@ error_ret: return ret; } +int calc_digits(int num) +{ + int count = 0; + + while (num != 0) { + num /= 10; + count++; + } + + return count; +} + /** * find_type_by_name() - function to match top level types by name * @name: top level type instance name @@ -441,7 +453,7 @@ error_ret: int find_type_by_name(const char *name, const char *type) { const struct dirent *ent; - int number, numstrlen; + int number, numstrlen, ret; FILE *nameFile; DIR *dp; @@ -459,9 +471,19 @@ int find_type_by_name(const char *name, const char *type) strcmp(ent->d_name, "..") != 0 && strlen(ent->d_name) > strlen(type) && strncmp(ent->d_name, type, strlen(type)) == 0) { - numstrlen = sscanf(ent->d_name + strlen(type), - "%d", - &number); + errno = 0; + ret = sscanf(ent->d_name + strlen(type), "%d", &number); + if (ret < 0) { + ret = -errno; + printf("failed to read element number\n"); + goto error_close_dir; + } else if (ret != 1) { + ret = -EIO; + printf("failed to match element number\n"); + goto error_close_dir; + } + + numstrlen = calc_digits(number); /* verify the next character is not a colon */ if (strncmp(ent->d_name + strlen(type) + numstrlen, ":", @@ -495,6 +517,11 @@ int find_type_by_name(const char *name, const char *type) } closedir(dp); return -ENODEV; + +error_close_dir: + if (closedir(dp) == -1) + perror("find_type_by_name(): Failed to close directory"); + return ret; } int _write_sysfs_int(char *filename, char *basedir, int val, int verify) -- cgit v1.2.3-70-g09d2 From 2156b179993e3d5b422976181ba17d91153313e1 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:01 +0200 Subject: tools:iio:iio_utils: mark private function static Functions _write_sysfs_int() and _write_sysfs_string() are supposed to be called only by public wrappers, so make them static. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 60e5ec4165b7..812153ff18c3 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -524,7 +524,7 @@ error_close_dir: return ret; } -int _write_sysfs_int(char *filename, char *basedir, int val, int verify) +static int _write_sysfs_int(char *filename, char *basedir, int val, int verify) { int ret = 0; FILE *sysfsfp; @@ -574,7 +574,8 @@ int write_sysfs_int_and_verify(char *filename, char *basedir, int val) return _write_sysfs_int(filename, basedir, val, 1); } -int _write_sysfs_string(char *filename, char *basedir, char *val, int verify) +static int _write_sysfs_string(char *filename, char *basedir, char *val, + int verify) { int ret = 0; FILE *sysfsfp; -- cgit v1.2.3-70-g09d2 From e9e45b43b8f06273d9b78f187042dff0bf5be0a5 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:02 +0200 Subject: tools:iio: catch errors in string allocation This patch catches errors in string allocation in generic_buffer.c, iio_event_monitor.c, iio_utils.c and lsiio.c. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/generic_buffer.c | 4 +++- tools/iio/iio_event_monitor.c | 2 ++ tools/iio/iio_utils.c | 9 ++++++--- tools/iio/lsiio.c | 15 +++++++++++++-- 4 files changed, 24 insertions(+), 6 deletions(-) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c index 4cd246426b57..eb89bc2ca5d0 100644 --- a/tools/iio/generic_buffer.c +++ b/tools/iio/generic_buffer.c @@ -234,7 +234,9 @@ int main(int argc, char **argv) } printf("iio device number being used is %d\n", dev_num); - asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); + ret = asprintf(&dev_dir_name, "%siio:device%d", iio_dir, dev_num); + if (ret < 0) + return -ENOMEM; if (!notrigger) { if (trigger_name == NULL) { diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c index 578390db1bec..1374374208ee 100644 --- a/tools/iio/iio_event_monitor.c +++ b/tools/iio/iio_event_monitor.c @@ -265,6 +265,8 @@ int main(int argc, char **argv) /* If we can't find a IIO device by name assume device_name is a IIO chrdev */ chrdev_name = strdup(device_name); + if (!chrdev_name) + return -ENOMEM; } fd = open(chrdev_name, 0); diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 812153ff18c3..f0896f46847f 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -36,7 +36,7 @@ int iioutils_break_up_name(const char *full_name, char *current; char *w, *r; char *working, *prefix = ""; - int i; + int i, ret; for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++) if (!strncmp(full_name, iio_direction[i], @@ -46,6 +46,9 @@ int iioutils_break_up_name(const char *full_name, } current = strdup(full_name + strlen(prefix) + 1); + if (!current) + return -ENOMEM; + working = strtok(current, "_\0"); w = working; @@ -59,10 +62,10 @@ int iioutils_break_up_name(const char *full_name, r++; } *w = '\0'; - asprintf(generic_name, "%s_%s", prefix, working); + ret = asprintf(generic_name, "%s_%s", prefix, working); free(current); - return 0; + return (ret == -1) ? -ENOMEM : 0; } /** diff --git a/tools/iio/lsiio.c b/tools/iio/lsiio.c index 65a2385e2a78..daa6c5312d66 100644 --- a/tools/iio/lsiio.c +++ b/tools/iio/lsiio.c @@ -107,7 +107,12 @@ static void dump_devices(void) if (check_prefix(ent->d_name, type_device)) { char *dev_dir_name; - asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name); + if (asprintf(&dev_dir_name, "%s%s", iio_dir, + ent->d_name) < 0) { + printf("Memory allocation failed\n"); + goto error_close_dir; + } + dump_one_device(dev_dir_name); free(dev_dir_name); if (verblevel >= VERBLEVEL_SENSORS) @@ -119,11 +124,17 @@ static void dump_devices(void) if (check_prefix(ent->d_name, type_trigger)) { char *dev_dir_name; - asprintf(&dev_dir_name, "%s%s", iio_dir, ent->d_name); + if (asprintf(&dev_dir_name, "%s%s", iio_dir, + ent->d_name) < 0) { + printf("Memory allocation failed\n"); + goto error_close_dir; + } + dump_one_trigger(dev_dir_name); free(dev_dir_name); } } +error_close_dir: closedir(dp); } -- cgit v1.2.3-70-g09d2 From 53118557b6a9c263e4a80825da367b2116529541 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:14 +0200 Subject: tools:iio:iio_utils: add error handling Add error handling to calls which can indicate a major problem by returning an error code. This also sets ret to -ENOENT in iioutils_get_type() and iioutils_get_param_float() to indicate if no matching directory entry was found. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 265 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 223 insertions(+), 42 deletions(-) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index f0896f46847f..e1828d0d8a38 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -50,6 +50,10 @@ int iioutils_break_up_name(const char *full_name, return -ENOMEM; working = strtok(current, "_\0"); + if (!working) { + free(current); + return -EINVAL; + } w = working; r = working; @@ -117,6 +121,7 @@ int iioutils_get_type(unsigned *is_signed, ret = -errno; goto error_free_builtname_generic; } + ret = -ENOENT; while (ent = readdir(dp), ent != NULL) /* * Do we allow devices to override a generic name with @@ -162,7 +167,12 @@ int iioutils_get_type(unsigned *is_signed, *is_signed = 1; else *is_signed = 0; - fclose(sysfsfp); + if (fclose(sysfsfp)) { + ret = -errno; + printf("Failed to close %s\n", filename); + goto error_free_filename; + } + free(filename); filename = 0; @@ -170,12 +180,16 @@ int iioutils_get_type(unsigned *is_signed, } error_close_sysfsfp: if (sysfsfp) - fclose(sysfsfp); + if (fclose(sysfsfp)) + perror("iioutils_get_type(): Failed to close file"); + error_free_filename: if (filename) free(filename); error_closedir: - closedir(dp); + if (closedir(dp) == -1) + perror("iioutils_get_type(): Failed to close directory"); + error_free_builtname_generic: free(builtname_generic); error_free_builtname: @@ -215,6 +229,7 @@ int iioutils_get_param_float(float *output, ret = -errno; goto error_free_builtname_generic; } + ret = -ENOENT; while (ent = readdir(dp), ent != NULL) if ((strcmp(builtname, ent->d_name) == 0) || (strcmp(builtname_generic, ent->d_name) == 0)) { @@ -229,14 +244,19 @@ int iioutils_get_param_float(float *output, ret = -errno; goto error_free_filename; } - fscanf(sysfsfp, "%f", output); + errno = 0; + if (fscanf(sysfsfp, "%f", output) != 1) + ret = errno ? -errno : -ENODATA; + break; } error_free_filename: if (filename) free(filename); error_closedir: - closedir(dp); + if (closedir(dp) == -1) + perror("iioutils_get_param_float(): Failed to close directory"); + error_free_builtname_generic: free(builtname_generic); error_free_builtname: @@ -310,10 +330,24 @@ int build_channel_array(const char *device_dir, free(filename); goto error_close_dir; } - fscanf(sysfsfp, "%i", &ret); + errno = 0; + if (fscanf(sysfsfp, "%i", &ret) != 1) { + ret = errno ? -errno : -ENODATA; + if (fclose(sysfsfp)) + perror("build_channel_array(): Failed to close file"); + + free(filename); + goto error_close_dir; + } + if (ret == 1) (*counter)++; - fclose(sysfsfp); + if (fclose(sysfsfp)) { + ret = -errno; + free(filename); + goto error_close_dir; + } + free(filename); } *ci_array = malloc(sizeof(**ci_array) * (*counter)); @@ -344,8 +378,20 @@ int build_channel_array(const char *device_dir, count--; goto error_cleanup_array; } - fscanf(sysfsfp, "%i", ¤t_enabled); - fclose(sysfsfp); + errno = 0; + if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) { + ret = errno ? -errno : -ENODATA; + free(filename); + count--; + goto error_cleanup_array; + } + + if (fclose(sysfsfp)) { + ret = -errno; + free(filename); + count--; + goto error_cleanup_array; + } if (!current_enabled) { free(filename); @@ -383,8 +429,29 @@ int build_channel_array(const char *device_dir, goto error_cleanup_array; } sysfsfp = fopen(filename, "r"); - fscanf(sysfsfp, "%u", ¤t->index); - fclose(sysfsfp); + if (sysfsfp == NULL) { + ret = -errno; + printf("failed to open %s\n", filename); + free(filename); + goto error_cleanup_array; + } + + errno = 0; + if (fscanf(sysfsfp, "%u", ¤t->index) != 1) { + ret = errno ? -errno : -ENODATA; + if (fclose(sysfsfp)) + perror("build_channel_array(): Failed to close file"); + + free(filename); + goto error_cleanup_array; + } + + if (fclose(sysfsfp)) { + ret = -errno; + free(filename); + goto error_cleanup_array; + } + free(filename); /* Find the scale */ ret = iioutils_get_param_float(¤t->scale, @@ -410,10 +477,16 @@ int build_channel_array(const char *device_dir, device_dir, current->name, current->generic_name); + if (ret < 0) + goto error_cleanup_array; } } - closedir(dp); + if (closedir(dp) == -1) { + ret = -errno; + goto error_cleanup_array; + } + free(scan_el_dir); /* reorder so that the array is in index order */ bsort_channel_array_by_index(ci_array, *counter); @@ -427,7 +500,10 @@ error_cleanup_array: } free(*ci_array); error_close_dir: - closedir(dp); + if (dp) + if (closedir(dp) == -1) + perror("build_channel_array(): Failed to close dir"); + error_free_name: free(scan_el_dir); error_ret: @@ -496,29 +572,45 @@ int find_type_by_name(const char *name, const char *type) + numstrlen + 6); if (filename == NULL) { - closedir(dp); - return -ENOMEM; + ret = -ENOMEM; + goto error_close_dir; + } + + ret = sprintf(filename, "%s%s%d/name", iio_dir, + type, number); + if (ret < 0) { + free(filename); + goto error_close_dir; } - sprintf(filename, "%s%s%d/name", - iio_dir, - type, - number); + nameFile = fopen(filename, "r"); if (!nameFile) { free(filename); continue; } free(filename); - fscanf(nameFile, "%s", thisname); - fclose(nameFile); + errno = 0; + if (fscanf(nameFile, "%s", thisname) != 1) { + ret = errno ? -errno : -ENODATA; + goto error_close_dir; + } + + if (fclose(nameFile)) { + ret = -errno; + goto error_close_dir; + } + if (strcmp(name, thisname) == 0) { - closedir(dp); + if (closedir(dp) == -1) + return -errno; return number; } } } } - closedir(dp); + if (closedir(dp) == -1) + return -errno; + return -ENODEV; error_close_dir: @@ -536,15 +628,29 @@ static int _write_sysfs_int(char *filename, char *basedir, int val, int verify) if (temp == NULL) return -ENOMEM; - sprintf(temp, "%s/%s", basedir, filename); + ret = sprintf(temp, "%s/%s", basedir, filename); + if (ret < 0) + goto error_free; + sysfsfp = fopen(temp, "w"); if (sysfsfp == NULL) { ret = -errno; printf("failed to open %s\n", temp); goto error_free; } - fprintf(sysfsfp, "%d", val); - fclose(sysfsfp); + ret = fprintf(sysfsfp, "%d", val); + if (ret < 0) { + if (fclose(sysfsfp)) + perror("_write_sysfs_int(): Failed to close dir"); + + goto error_free; + } + + if (fclose(sysfsfp)) { + ret = -errno; + goto error_free; + } + if (verify) { sysfsfp = fopen(temp, "r"); if (sysfsfp == NULL) { @@ -552,8 +658,19 @@ static int _write_sysfs_int(char *filename, char *basedir, int val, int verify) printf("failed to open %s\n", temp); goto error_free; } - fscanf(sysfsfp, "%d", &test); - fclose(sysfsfp); + if (fscanf(sysfsfp, "%d", &test) != 1) { + ret = errno ? -errno : -ENODATA; + if (fclose(sysfsfp)) + perror("_write_sysfs_int(): Failed to close dir"); + + goto error_free; + } + + if (fclose(sysfsfp)) { + ret = -errno; + goto error_free; + } + if (test != val) { printf("Possible failure in int write %d to %s%s\n", val, @@ -588,15 +705,29 @@ static int _write_sysfs_string(char *filename, char *basedir, char *val, printf("Memory allocation failed\n"); return -ENOMEM; } - sprintf(temp, "%s/%s", basedir, filename); + ret = sprintf(temp, "%s/%s", basedir, filename); + if (ret < 0) + goto error_free; + sysfsfp = fopen(temp, "w"); if (sysfsfp == NULL) { ret = -errno; printf("Could not open %s\n", temp); goto error_free; } - fprintf(sysfsfp, "%s", val); - fclose(sysfsfp); + ret = fprintf(sysfsfp, "%s", val); + if (ret < 0) { + if (fclose(sysfsfp)) + perror("_write_sysfs_string(): Failed to close dir"); + + goto error_free; + } + + if (fclose(sysfsfp)) { + ret = -errno; + goto error_free; + } + if (verify) { sysfsfp = fopen(temp, "r"); if (sysfsfp == NULL) { @@ -604,8 +735,19 @@ static int _write_sysfs_string(char *filename, char *basedir, char *val, printf("could not open file to verify\n"); goto error_free; } - fscanf(sysfsfp, "%s", temp); - fclose(sysfsfp); + if (fscanf(sysfsfp, "%s", temp) != 1) { + ret = errno ? -errno : -ENODATA; + if (fclose(sysfsfp)) + perror("_write_sysfs_string(): Failed to close dir"); + + goto error_free; + } + + if (fclose(sysfsfp)) { + ret = -errno; + goto error_free; + } + if (strcmp(temp, val) != 0) { printf("Possible failure in string write of %s " "Should be %s " @@ -649,14 +791,27 @@ int read_sysfs_posint(char *filename, char *basedir) printf("Memory allocation failed"); return -ENOMEM; } - sprintf(temp, "%s/%s", basedir, filename); + ret = sprintf(temp, "%s/%s", basedir, filename); + if (ret < 0) + goto error_free; + sysfsfp = fopen(temp, "r"); if (sysfsfp == NULL) { ret = -errno; goto error_free; } - fscanf(sysfsfp, "%d\n", &ret); - fclose(sysfsfp); + errno = 0; + if (fscanf(sysfsfp, "%d\n", &ret) != 1) { + ret = errno ? -errno : -ENODATA; + if (fclose(sysfsfp)) + perror("read_sysfs_posint(): Failed to close dir"); + + goto error_free; + } + + if (fclose(sysfsfp)) + ret = -errno; + error_free: free(temp); return ret; @@ -672,14 +827,27 @@ int read_sysfs_float(char *filename, char *basedir, float *val) printf("Memory allocation failed"); return -ENOMEM; } - sprintf(temp, "%s/%s", basedir, filename); + ret = sprintf(temp, "%s/%s", basedir, filename); + if (ret < 0) + goto error_free; + sysfsfp = fopen(temp, "r"); if (sysfsfp == NULL) { ret = -errno; goto error_free; } - fscanf(sysfsfp, "%f\n", val); - fclose(sysfsfp); + errno = 0; + if (fscanf(sysfsfp, "%f\n", val) != 1) { + ret = errno ? -errno : -ENODATA; + if (fclose(sysfsfp)) + perror("read_sysfs_float(): Failed to close dir"); + + goto error_free; + } + + if (fclose(sysfsfp)) + ret = -errno; + error_free: free(temp); return ret; @@ -695,14 +863,27 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str) printf("Memory allocation failed"); return -ENOMEM; } - sprintf(temp, "%s/%s", basedir, filename); + ret = sprintf(temp, "%s/%s", basedir, filename); + if (ret < 0) + goto error_free; + sysfsfp = fopen(temp, "r"); if (sysfsfp == NULL) { ret = -errno; goto error_free; } - fscanf(sysfsfp, "%s\n", str); - fclose(sysfsfp); + errno = 0; + if (fscanf(sysfsfp, "%s\n", str) != 1) { + ret = errno ? -errno : -ENODATA; + if (fclose(sysfsfp)) + perror("read_sysfs_string(): Failed to close dir"); + + goto error_free; + } + + if (fclose(sysfsfp)) + ret = -errno; + error_free: free(temp); return ret; -- cgit v1.2.3-70-g09d2 From 5dc65d791ddafc9f6d944391e379708b6056fc48 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:16 +0200 Subject: tools:iio:iio_utils: add missing documentation Fully document public functions and elements. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++---- tools/iio/iio_utils.h | 5 ++- 2 files changed, 83 insertions(+), 7 deletions(-) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index e1828d0d8a38..5d5831d7dfd0 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -29,6 +29,8 @@ static char * const iio_direction[] = { * iioutils_break_up_name() - extract generic name from full channel name * @full_name: the full channel name * @generic_name: the output generic channel name + * + * Returns 0 on success, or a negative error code if string extraction failed. **/ int iioutils_break_up_name(const char *full_name, char **generic_name) @@ -76,11 +78,15 @@ int iioutils_break_up_name(const char *full_name, * iioutils_get_type() - find and process _type attribute data * @is_signed: output whether channel is signed * @bytes: output how many bytes the channel storage occupies + * @bits_used: output number of valid bits of data + * @shift: output amount of bits to shift right data before applying bit mask * @mask: output a bit mask for the raw data - * @be: big endian - * @device_dir: the iio device directory + * @be: output if data in big endian + * @device_dir: the IIO device directory * @name: the channel name * @generic_name: the channel type name + * + * Returns a value >= 0 on success, otherwise a negative error code. **/ int iioutils_get_type(unsigned *is_signed, unsigned *bytes, @@ -200,6 +206,16 @@ error_ret: return ret; } +/** + * iioutils_get_param_float() - read a float value from a channel parameter + * @output: output the float value + * @param_name: the parameter name to read + * @device_dir: the IIO device directory in sysfs + * @name: the channel name + * @generic_name: the channel type name + * + * Returns a value >= 0 on success, otherwise a negative error code. + **/ int iioutils_get_param_float(float *output, const char *param_name, const char *device_dir, @@ -266,8 +282,9 @@ error_ret: } /** - * bsort_channel_array_by_index() - reorder so that the array is in index order - * + * bsort_channel_array_by_index() - sort the array in index order + * @ci_array: the iio_channel_info array to be sorted + * @cnt: the amount of array elements **/ void bsort_channel_array_by_index(struct iio_channel_info **ci_array, @@ -289,7 +306,10 @@ void bsort_channel_array_by_index(struct iio_channel_info **ci_array, /** * build_channel_array() - function to figure out what channels are present * @device_dir: the IIO device directory in sysfs - * @ + * @ci_array: output the resulting array of iio_channel_info + * @counter: output the amount of array elements + * + * Returns 0 on success, otherwise a negative error code. **/ int build_channel_array(const char *device_dir, struct iio_channel_info **ci_array, @@ -525,8 +545,10 @@ int calc_digits(int num) /** * find_type_by_name() - function to match top level types by name * @name: top level type instance name - * @type: the type of top level instance being sort + * @type: the type of top level instance being searched * + * Returns the device number of a matched IIO device on success, otherwise a + * negative error code. * Typical types this is used for are device and trigger. **/ int find_type_by_name(const char *name, const char *type) @@ -684,11 +706,28 @@ error_free: return ret; } +/** + * write_sysfs_int() - write an integer value to a sysfs file + * @filename: name of the file to write to + * @basedir: the sysfs directory in which the file is to be found + * @val: integer value to write to file + * + * Returns a value >= 0 on success, otherwise a negative error code. + **/ int write_sysfs_int(char *filename, char *basedir, int val) { return _write_sysfs_int(filename, basedir, val, 0); } +/** + * write_sysfs_int_and_verify() - write an integer value to a sysfs file + * and verify + * @filename: name of the file to write to + * @basedir: the sysfs directory in which the file is to be found + * @val: integer value to write to file + * + * Returns a value >= 0 on success, otherwise a negative error code. + **/ int write_sysfs_int_and_verify(char *filename, char *basedir, int val) { return _write_sysfs_int(filename, basedir, val, 1); @@ -770,17 +809,35 @@ error_free: * @filename: name of file to write to * @basedir: the sysfs directory in which the file is to be found * @val: the string to write + * + * Returns a value >= 0 on success, otherwise a negative error code. **/ int write_sysfs_string_and_verify(char *filename, char *basedir, char *val) { return _write_sysfs_string(filename, basedir, val, 1); } +/** + * write_sysfs_string() - write string to a sysfs file + * @filename: name of file to write to + * @basedir: the sysfs directory in which the file is to be found + * @val: the string to write + * + * Returns a value >= 0 on success, otherwise a negative error code. + **/ int write_sysfs_string(char *filename, char *basedir, char *val) { return _write_sysfs_string(filename, basedir, val, 0); } +/** + * read_sysfs_posint() - read an integer value from file + * @filename: name of file to read from + * @basedir: the sysfs directory in which the file is to be found + * + * Returns the read integer value >= 0 on success, otherwise a negative error + * code. + **/ int read_sysfs_posint(char *filename, char *basedir) { int ret; @@ -817,6 +874,14 @@ error_free: return ret; } +/** + * read_sysfs_float() - read a float value from file + * @filename: name of file to read from + * @basedir: the sysfs directory in which the file is to be found + * @val: output the read float value + * + * Returns a value >= 0 on success, otherwise a negative error code. + **/ int read_sysfs_float(char *filename, char *basedir, float *val) { int ret = 0; @@ -853,6 +918,14 @@ error_free: return ret; } +/** + * read_sysfs_string() - read a string from file + * @filename: name of file to read from + * @basedir: the sysfs directory in which the file is to be found + * @str: output the read string + * + * Returns a value >= 0 on success, otherwise a negative error code. + **/ int read_sysfs_string(const char *filename, const char *basedir, char *str) { int ret = 0; diff --git a/tools/iio/iio_utils.h b/tools/iio/iio_utils.h index 1bc837b2d769..e5eb46ade0a3 100644 --- a/tools/iio/iio_utils.h +++ b/tools/iio/iio_utils.h @@ -28,9 +28,12 @@ extern const char *iio_dir; * @offset: offset to be applied for conversion to si units * @index: the channel index in the buffer output * @bytes: number of bytes occupied in buffer output + * @bits_used: number of valid bits of data + * @shift: amount of bits to shift right data before applying bit mask * @mask: a bit mask for the raw output + * @be: flag if data is big endian * @is_signed: is the raw value stored signed - * @enabled: is this channel enabled + * @location: data offset for this channel inside the buffer (in bytes) **/ struct iio_channel_info { char *name; -- cgit v1.2.3-70-g09d2 From 0e799878175aa7d08f5882b6a391de4724c52e9e Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:17 +0200 Subject: tools:iio: return values directly Return directly, if no common cleanup is required. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/generic_buffer.c | 5 ++--- tools/iio/iio_event_monitor.c | 5 ++--- tools/iio/iio_utils.c | 27 ++++++++++++--------------- 3 files changed, 16 insertions(+), 21 deletions(-) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/generic_buffer.c b/tools/iio/generic_buffer.c index cf9a4120204f..62ec398922a0 100644 --- a/tools/iio/generic_buffer.c +++ b/tools/iio/generic_buffer.c @@ -238,8 +238,7 @@ int main(int argc, char **argv) dev_num = find_type_by_name(device_name, "iio:device"); if (dev_num < 0) { printf("Failed to find the %s\n", device_name); - ret = dev_num; - goto error_ret; + return dev_num; } printf("iio device number being used is %d\n", dev_num); @@ -410,6 +409,6 @@ error_free_triggername: free(trigger_name); error_free_dev_dir_name: free(dev_dir_name); -error_ret: + return ret; } diff --git a/tools/iio/iio_event_monitor.c b/tools/iio/iio_event_monitor.c index 1316527f3c3c..7f56238b1d66 100644 --- a/tools/iio/iio_event_monitor.c +++ b/tools/iio/iio_event_monitor.c @@ -258,8 +258,7 @@ int main(int argc, char **argv) device_name, dev_num); ret = asprintf(&chrdev_name, "/dev/iio:device%d", dev_num); if (ret < 0) { - ret = -ENOMEM; - goto error_ret; + return -ENOMEM; } } else { /* If we can't find a IIO device by name assume device_name is a @@ -312,6 +311,6 @@ int main(int argc, char **argv) error_free_chrdev_name: free(chrdev_name); -error_ret: + return ret; } diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 5d5831d7dfd0..0524725279bb 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -107,10 +107,9 @@ int iioutils_get_type(unsigned *is_signed, const struct dirent *ent; ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir); - if (ret < 0) { - ret = -ENOMEM; - goto error_ret; - } + if (ret < 0) + return -ENOMEM; + ret = asprintf(&builtname, FORMAT_TYPE_FILE, name); if (ret < 0) { ret = -ENOMEM; @@ -202,7 +201,7 @@ error_free_builtname: free(builtname); error_free_scan_el_dir: free(scan_el_dir); -error_ret: + return ret; } @@ -230,10 +229,9 @@ int iioutils_get_param_float(float *output, const struct dirent *ent; ret = asprintf(&builtname, "%s_%s", name, param_name); - if (ret < 0) { - ret = -ENOMEM; - goto error_ret; - } + if (ret < 0) + return -ENOMEM; + ret = asprintf(&builtname_generic, "%s_%s", generic_name, param_name); if (ret < 0) { @@ -277,7 +275,7 @@ error_free_builtname_generic: free(builtname_generic); error_free_builtname: free(builtname); -error_ret: + return ret; } @@ -326,10 +324,9 @@ int build_channel_array(const char *device_dir, *counter = 0; ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir); - if (ret < 0) { - ret = -ENOMEM; - goto error_ret; - } + if (ret < 0) + return -ENOMEM; + dp = opendir(scan_el_dir); if (dp == NULL) { ret = -errno; @@ -526,7 +523,7 @@ error_close_dir: error_free_name: free(scan_el_dir); -error_ret: + return ret; } -- cgit v1.2.3-70-g09d2 From 33ebcb21a67f257faf1128f08a6d7c0299cb0da2 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:19 +0200 Subject: tools:iio:iio_utils: refactor assignment of is_signed Change the assignment of *is_signed in iioutils_get_type() to a one-liner, as already done with *be. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 0524725279bb..c83f4dfe71d6 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -168,10 +168,7 @@ int iioutils_get_type(unsigned *is_signed, *mask = ~0; else *mask = (1 << *bits_used) - 1; - if (signchar == 's') - *is_signed = 1; - else - *is_signed = 0; + *is_signed = (signchar == 's'); if (fclose(sysfsfp)) { ret = -errno; printf("Failed to close %s\n", filename); -- cgit v1.2.3-70-g09d2 From ace76e42bcd5d67e5be303997a4dc325d44366ce Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:20 +0200 Subject: tools:iio:iio_utils: move up reset of sysfsfp In iioutils_get_type() it is logically better fitting to have sysfsfp assigned zero right after closing it. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index c83f4dfe71d6..7c0abb306f5a 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -175,10 +175,10 @@ int iioutils_get_type(unsigned *is_signed, goto error_free_filename; } + sysfsfp = 0; free(filename); filename = 0; - sysfsfp = 0; } error_close_sysfsfp: if (sysfsfp) -- cgit v1.2.3-70-g09d2 From 1e7c34788de3c0e5b18b6f27c42c191da5811c74 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:21 +0200 Subject: tools:iio:iio_utils: initialize count during declaration In build_channel_array(), count can be initialized already during variable declaration. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index 7c0abb306f5a..dfee1a3f3f63 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -312,7 +312,7 @@ int build_channel_array(const char *device_dir, { DIR *dp; FILE *sysfsfp; - int count, i; + int count = 0, i; struct iio_channel_info *current; int ret; const struct dirent *ent; @@ -370,7 +370,6 @@ int build_channel_array(const char *device_dir, goto error_close_dir; } seekdir(dp, 0); - count = 0; while (ent = readdir(dp), ent != NULL) { if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), "_en") == 0) { -- cgit v1.2.3-70-g09d2 From 9d4752544d17a10a40be5fc5ef68edcae5363599 Mon Sep 17 00:00:00 2001 From: Hartmut Knaack Date: Sun, 31 May 2015 14:40:36 +0200 Subject: tools:iio:iio_utils: pass strings as const Mark strings, which are not supposed to be changed (basedir, filename, value), as const in function parameters. Signed-off-by: Hartmut Knaack Signed-off-by: Jonathan Cameron --- tools/iio/iio_utils.c | 22 +++++++++++++--------- tools/iio/iio_utils.h | 15 +++++++++------ 2 files changed, 22 insertions(+), 15 deletions(-) (limited to 'tools/iio/iio_utils.c') diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index dfee1a3f3f63..ec9ab7f9ae4c 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c @@ -634,7 +634,8 @@ error_close_dir: return ret; } -static int _write_sysfs_int(char *filename, char *basedir, int val, int verify) +static int _write_sysfs_int(const char *filename, const char *basedir, int val, + int verify) { int ret = 0; FILE *sysfsfp; @@ -707,7 +708,7 @@ error_free: * * Returns a value >= 0 on success, otherwise a negative error code. **/ -int write_sysfs_int(char *filename, char *basedir, int val) +int write_sysfs_int(const char *filename, const char *basedir, int val) { return _write_sysfs_int(filename, basedir, val, 0); } @@ -721,13 +722,14 @@ int write_sysfs_int(char *filename, char *basedir, int val) * * Returns a value >= 0 on success, otherwise a negative error code. **/ -int write_sysfs_int_and_verify(char *filename, char *basedir, int val) +int write_sysfs_int_and_verify(const char *filename, const char *basedir, + int val) { return _write_sysfs_int(filename, basedir, val, 1); } -static int _write_sysfs_string(char *filename, char *basedir, char *val, - int verify) +static int _write_sysfs_string(const char *filename, const char *basedir, + const char *val, int verify) { int ret = 0; FILE *sysfsfp; @@ -805,7 +807,8 @@ error_free: * * Returns a value >= 0 on success, otherwise a negative error code. **/ -int write_sysfs_string_and_verify(char *filename, char *basedir, char *val) +int write_sysfs_string_and_verify(const char *filename, const char *basedir, + const char *val) { return _write_sysfs_string(filename, basedir, val, 1); } @@ -818,7 +821,8 @@ int write_sysfs_string_and_verify(char *filename, char *basedir, char *val) * * Returns a value >= 0 on success, otherwise a negative error code. **/ -int write_sysfs_string(char *filename, char *basedir, char *val) +int write_sysfs_string(const char *filename, const char *basedir, + const char *val) { return _write_sysfs_string(filename, basedir, val, 0); } @@ -831,7 +835,7 @@ int write_sysfs_string(char *filename, char *basedir, char *val) * Returns the read integer value >= 0 on success, otherwise a negative error * code. **/ -int read_sysfs_posint(char *filename, char *basedir) +int read_sysfs_posint(const char *filename, const char *basedir) { int ret; FILE *sysfsfp; @@ -875,7 +879,7 @@ error_free: * * Returns a value >= 0 on success, otherwise a negative error code. **/ -int read_sysfs_float(char *filename, char *basedir, float *val) +int read_sysfs_float(const char *filename, const char *basedir, float *val) { int ret = 0; FILE *sysfsfp; diff --git a/tools/iio/iio_utils.h b/tools/iio/iio_utils.h index e5eb46ade0a3..379eed9deaea 100644 --- a/tools/iio/iio_utils.h +++ b/tools/iio/iio_utils.h @@ -63,12 +63,15 @@ void bsort_channel_array_by_index(struct iio_channel_info **ci_array, int cnt); int build_channel_array(const char *device_dir, struct iio_channel_info **ci_array, int *counter); int find_type_by_name(const char *name, const char *type); -int write_sysfs_int(char *filename, char *basedir, int val); -int write_sysfs_int_and_verify(char *filename, char *basedir, int val); -int write_sysfs_string_and_verify(char *filename, char *basedir, char *val); -int write_sysfs_string(char *filename, char *basedir, char *val); -int read_sysfs_posint(char *filename, char *basedir); -int read_sysfs_float(char *filename, char *basedir, float *val); +int write_sysfs_int(const char *filename, const char *basedir, int val); +int write_sysfs_int_and_verify(const char *filename, const char *basedir, + int val); +int write_sysfs_string_and_verify(const char *filename, const char *basedir, + const char *val); +int write_sysfs_string(const char *filename, const char *basedir, + const char *val); +int read_sysfs_posint(const char *filename, const char *basedir); +int read_sysfs_float(const char *filename, const char *basedir, float *val); int read_sysfs_string(const char *filename, const char *basedir, char *str); #endif /* _IIO_UTILS_H_ */ -- cgit v1.2.3-70-g09d2