Commit acf50b35 authored by Hartmut Knaack's avatar Hartmut Knaack Committed by Jonathan Cameron

tools:iio:lsiio: add error handling

Add error handling to calls which can indicate a major problem by
returning an error code.
This also involves to change the type of dump_devices() from void to int.
Signed-off-by: default avatarHartmut Knaack <knaack.h@gmx.de>
Signed-off-by: default avatarJonathan Cameron <jic23@kernel.org>
parent 53118557
...@@ -69,7 +69,10 @@ static int dump_one_device(const char *dev_dir_name) ...@@ -69,7 +69,10 @@ static int dump_one_device(const char *dev_dir_name)
"%i", &dev_idx); "%i", &dev_idx);
if (retval != 1) if (retval != 1)
return -EINVAL; return -EINVAL;
read_sysfs_string("name", dev_dir_name, name); retval = read_sysfs_string("name", dev_dir_name, name);
if (retval)
return retval;
printf("Device %03d: %s\n", dev_idx, name); printf("Device %03d: %s\n", dev_idx, name);
if (verblevel >= VERBLEVEL_SENSORS) if (verblevel >= VERBLEVEL_SENSORS)
...@@ -87,20 +90,24 @@ static int dump_one_trigger(const char *dev_dir_name) ...@@ -87,20 +90,24 @@ static int dump_one_trigger(const char *dev_dir_name)
"%i", &dev_idx); "%i", &dev_idx);
if (retval != 1) if (retval != 1)
return -EINVAL; return -EINVAL;
read_sysfs_string("name", dev_dir_name, name); retval = read_sysfs_string("name", dev_dir_name, name);
if (retval)
return retval;
printf("Trigger %03d: %s\n", dev_idx, name); printf("Trigger %03d: %s\n", dev_idx, name);
return 0; return 0;
} }
static void dump_devices(void) static int dump_devices(void)
{ {
const struct dirent *ent; const struct dirent *ent;
int ret;
DIR *dp; DIR *dp;
dp = opendir(iio_dir); dp = opendir(iio_dir);
if (dp == NULL) { if (dp == NULL) {
printf("No industrial I/O devices available\n"); printf("No industrial I/O devices available\n");
return; return -ENODEV;
} }
while (ent = readdir(dp), ent != NULL) { while (ent = readdir(dp), ent != NULL) {
...@@ -109,11 +116,16 @@ static void dump_devices(void) ...@@ -109,11 +116,16 @@ static void dump_devices(void)
if (asprintf(&dev_dir_name, "%s%s", iio_dir, if (asprintf(&dev_dir_name, "%s%s", iio_dir,
ent->d_name) < 0) { ent->d_name) < 0) {
printf("Memory allocation failed\n"); ret = -ENOMEM;
goto error_close_dir;
}
ret = dump_one_device(dev_dir_name);
if (ret) {
free(dev_dir_name);
goto error_close_dir; goto error_close_dir;
} }
dump_one_device(dev_dir_name);
free(dev_dir_name); free(dev_dir_name);
if (verblevel >= VERBLEVEL_SENSORS) if (verblevel >= VERBLEVEL_SENSORS)
printf("\n"); printf("\n");
...@@ -126,16 +138,26 @@ static void dump_devices(void) ...@@ -126,16 +138,26 @@ static void dump_devices(void)
if (asprintf(&dev_dir_name, "%s%s", iio_dir, if (asprintf(&dev_dir_name, "%s%s", iio_dir,
ent->d_name) < 0) { ent->d_name) < 0) {
printf("Memory allocation failed\n"); ret = -ENOMEM;
goto error_close_dir;
}
ret = dump_one_trigger(dev_dir_name);
if (ret) {
free(dev_dir_name);
goto error_close_dir; goto error_close_dir;
} }
dump_one_trigger(dev_dir_name);
free(dev_dir_name); free(dev_dir_name);
} }
} }
return (closedir(dp) == -1) ? -errno : 0;
error_close_dir: error_close_dir:
closedir(dp); if (closedir(dp) == -1)
perror("dump_devices(): Failed to close directory");
return ret;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
...@@ -163,7 +185,5 @@ int main(int argc, char **argv) ...@@ -163,7 +185,5 @@ int main(int argc, char **argv)
exit(1); exit(1);
} }
dump_devices(); return dump_devices();
return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment