summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorHartmut Knaack <knaack.h@gmx.de>2015-05-31 14:40:00 +0200
committerJonathan Cameron <jic23@kernel.org>2015-05-31 19:18:55 +0100
commit096f9b862e605fe08bb30e4f7a381684a8ff82ed (patch)
treec8c8c7cc31ea17fce97564b2768e1a847dc19485 /tools
parentdc8b5d6e633f8e54f70594d0be87aaf401ea0559 (diff)
downloadlinux-096f9b862e605fe08bb30e4f7a381684a8ff82ed.tar.gz
linux-096f9b862e605fe08bb30e4f7a381684a8ff82ed.tar.bz2
linux-096f9b862e605fe08bb30e4f7a381684a8ff82ed.zip
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 <knaack.h@gmx.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'tools')
-rw-r--r--tools/iio/iio_utils.c35
1 files changed, 31 insertions, 4 deletions
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)