aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorHartmut Knaack <knaack.h@gmx.de>2015-05-31 08:40:15 -0400
committerJonathan Cameron <jic23@kernel.org>2015-05-31 14:45:00 -0400
commitacf50b3586f8d8a7530b905e111dda41876d38f4 (patch)
treedcc9ac4a39397c5ef274c021e67f4c41a59eaab7 /tools
parent53118557b6a9c263e4a80825da367b2116529541 (diff)
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: Hartmut Knaack <knaack.h@gmx.de> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'tools')
-rw-r--r--tools/iio/lsiio.c44
1 files changed, 32 insertions, 12 deletions
diff --git a/tools/iio/lsiio.c b/tools/iio/lsiio.c
index daa6c5312d66..b1089adb7d3a 100644
--- a/tools/iio/lsiio.c
+++ b/tools/iio/lsiio.c
@@ -69,7 +69,10 @@ static int dump_one_device(const char *dev_dir_name)
69 "%i", &dev_idx); 69 "%i", &dev_idx);
70 if (retval != 1) 70 if (retval != 1)
71 return -EINVAL; 71 return -EINVAL;
72 read_sysfs_string("name", dev_dir_name, name); 72 retval = read_sysfs_string("name", dev_dir_name, name);
73 if (retval)
74 return retval;
75
73 printf("Device %03d: %s\n", dev_idx, name); 76 printf("Device %03d: %s\n", dev_idx, name);
74 77
75 if (verblevel >= VERBLEVEL_SENSORS) 78 if (verblevel >= VERBLEVEL_SENSORS)
@@ -87,20 +90,24 @@ static int dump_one_trigger(const char *dev_dir_name)
87 "%i", &dev_idx); 90 "%i", &dev_idx);
88 if (retval != 1) 91 if (retval != 1)
89 return -EINVAL; 92 return -EINVAL;
90 read_sysfs_string("name", dev_dir_name, name); 93 retval = read_sysfs_string("name", dev_dir_name, name);
94 if (retval)
95 return retval;
96
91 printf("Trigger %03d: %s\n", dev_idx, name); 97 printf("Trigger %03d: %s\n", dev_idx, name);
92 return 0; 98 return 0;
93} 99}
94 100
95static void dump_devices(void) 101static int dump_devices(void)
96{ 102{
97 const struct dirent *ent; 103 const struct dirent *ent;
104 int ret;
98 DIR *dp; 105 DIR *dp;
99 106
100 dp = opendir(iio_dir); 107 dp = opendir(iio_dir);
101 if (dp == NULL) { 108 if (dp == NULL) {
102 printf("No industrial I/O devices available\n"); 109 printf("No industrial I/O devices available\n");
103 return; 110 return -ENODEV;
104 } 111 }
105 112
106 while (ent = readdir(dp), ent != NULL) { 113 while (ent = readdir(dp), ent != NULL) {
@@ -109,11 +116,16 @@ static void dump_devices(void)
109 116
110 if (asprintf(&dev_dir_name, "%s%s", iio_dir, 117 if (asprintf(&dev_dir_name, "%s%s", iio_dir,
111 ent->d_name) < 0) { 118 ent->d_name) < 0) {
112 printf("Memory allocation failed\n"); 119 ret = -ENOMEM;
120 goto error_close_dir;
121 }
122
123 ret = dump_one_device(dev_dir_name);
124 if (ret) {
125 free(dev_dir_name);
113 goto error_close_dir; 126 goto error_close_dir;
114 } 127 }
115 128
116 dump_one_device(dev_dir_name);
117 free(dev_dir_name); 129 free(dev_dir_name);
118 if (verblevel >= VERBLEVEL_SENSORS) 130 if (verblevel >= VERBLEVEL_SENSORS)
119 printf("\n"); 131 printf("\n");
@@ -126,16 +138,26 @@ static void dump_devices(void)
126 138
127 if (asprintf(&dev_dir_name, "%s%s", iio_dir, 139 if (asprintf(&dev_dir_name, "%s%s", iio_dir,
128 ent->d_name) < 0) { 140 ent->d_name) < 0) {
129 printf("Memory allocation failed\n"); 141 ret = -ENOMEM;
142 goto error_close_dir;
143 }
144
145 ret = dump_one_trigger(dev_dir_name);
146 if (ret) {
147 free(dev_dir_name);
130 goto error_close_dir; 148 goto error_close_dir;
131 } 149 }
132 150
133 dump_one_trigger(dev_dir_name);
134 free(dev_dir_name); 151 free(dev_dir_name);
135 } 152 }
136 } 153 }
154 return (closedir(dp) == -1) ? -errno : 0;
155
137error_close_dir: 156error_close_dir:
138 closedir(dp); 157 if (closedir(dp) == -1)
158 perror("dump_devices(): Failed to close directory");
159
160 return ret;
139} 161}
140 162
141int main(int argc, char **argv) 163int main(int argc, char **argv)
@@ -163,7 +185,5 @@ int main(int argc, char **argv)
163 exit(1); 185 exit(1);
164 } 186 }
165 187
166 dump_devices(); 188 return dump_devices();
167
168 return 0;
169} 189}