summaryrefslogtreecommitdiffstats
path: root/tools/iio
diff options
context:
space:
mode:
authorHartmut Knaack <knaack.h@gmx.de>2015-05-31 08:40:00 -0400
committerJonathan Cameron <jic23@kernel.org>2015-05-31 14:18:55 -0400
commit096f9b862e605fe08bb30e4f7a381684a8ff82ed (patch)
treec8c8c7cc31ea17fce97564b2768e1a847dc19485 /tools/iio
parentdc8b5d6e633f8e54f70594d0be87aaf401ea0559 (diff)
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/iio')
-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:
431 return ret; 431 return ret;
432} 432}
433 433
434int calc_digits(int num)
435{
436 int count = 0;
437
438 while (num != 0) {
439 num /= 10;
440 count++;
441 }
442
443 return count;
444}
445
434/** 446/**
435 * find_type_by_name() - function to match top level types by name 447 * find_type_by_name() - function to match top level types by name
436 * @name: top level type instance name 448 * @name: top level type instance name
@@ -441,7 +453,7 @@ error_ret:
441int find_type_by_name(const char *name, const char *type) 453int find_type_by_name(const char *name, const char *type)
442{ 454{
443 const struct dirent *ent; 455 const struct dirent *ent;
444 int number, numstrlen; 456 int number, numstrlen, ret;
445 457
446 FILE *nameFile; 458 FILE *nameFile;
447 DIR *dp; 459 DIR *dp;
@@ -459,9 +471,19 @@ int find_type_by_name(const char *name, const char *type)
459 strcmp(ent->d_name, "..") != 0 && 471 strcmp(ent->d_name, "..") != 0 &&
460 strlen(ent->d_name) > strlen(type) && 472 strlen(ent->d_name) > strlen(type) &&
461 strncmp(ent->d_name, type, strlen(type)) == 0) { 473 strncmp(ent->d_name, type, strlen(type)) == 0) {
462 numstrlen = sscanf(ent->d_name + strlen(type), 474 errno = 0;
463 "%d", 475 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
464 &number); 476 if (ret < 0) {
477 ret = -errno;
478 printf("failed to read element number\n");
479 goto error_close_dir;
480 } else if (ret != 1) {
481 ret = -EIO;
482 printf("failed to match element number\n");
483 goto error_close_dir;
484 }
485
486 numstrlen = calc_digits(number);
465 /* verify the next character is not a colon */ 487 /* verify the next character is not a colon */
466 if (strncmp(ent->d_name + strlen(type) + numstrlen, 488 if (strncmp(ent->d_name + strlen(type) + numstrlen,
467 ":", 489 ":",
@@ -495,6 +517,11 @@ int find_type_by_name(const char *name, const char *type)
495 } 517 }
496 closedir(dp); 518 closedir(dp);
497 return -ENODEV; 519 return -ENODEV;
520
521error_close_dir:
522 if (closedir(dp) == -1)
523 perror("find_type_by_name(): Failed to close directory");
524 return ret;
498} 525}
499 526
500int _write_sysfs_int(char *filename, char *basedir, int val, int verify) 527int _write_sysfs_int(char *filename, char *basedir, int val, int verify)