diff options
Diffstat (limited to 'tools/iio/iio_utils.c')
-rw-r--r-- | tools/iio/iio_utils.c | 243 |
1 files changed, 136 insertions, 107 deletions
diff --git a/tools/iio/iio_utils.c b/tools/iio/iio_utils.c index ec9ab7f9ae4c..5eb6793f3972 100644 --- a/tools/iio/iio_utils.c +++ b/tools/iio/iio_utils.c | |||
@@ -6,9 +6,6 @@ | |||
6 | * under the terms of the GNU General Public License version 2 as published by | 6 | * under the terms of the GNU General Public License version 2 as published by |
7 | * the Free Software Foundation. | 7 | * the Free Software Foundation. |
8 | */ | 8 | */ |
9 | #ifndef _IIO_UTILS_H | ||
10 | #define _IIO_UTILS_H | ||
11 | |||
12 | #include <string.h> | 9 | #include <string.h> |
13 | #include <stdlib.h> | 10 | #include <stdlib.h> |
14 | #include <stdio.h> | 11 | #include <stdio.h> |
@@ -32,15 +29,14 @@ static char * const iio_direction[] = { | |||
32 | * | 29 | * |
33 | * Returns 0 on success, or a negative error code if string extraction failed. | 30 | * Returns 0 on success, or a negative error code if string extraction failed. |
34 | **/ | 31 | **/ |
35 | int iioutils_break_up_name(const char *full_name, | 32 | int iioutils_break_up_name(const char *full_name, char **generic_name) |
36 | char **generic_name) | ||
37 | { | 33 | { |
38 | char *current; | 34 | char *current; |
39 | char *w, *r; | 35 | char *w, *r; |
40 | char *working, *prefix = ""; | 36 | char *working, *prefix = ""; |
41 | int i, ret; | 37 | int i, ret; |
42 | 38 | ||
43 | for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++) | 39 | for (i = 0; i < ARRAY_SIZE(iio_direction); i++) |
44 | if (!strncmp(full_name, iio_direction[i], | 40 | if (!strncmp(full_name, iio_direction[i], |
45 | strlen(iio_direction[i]))) { | 41 | strlen(iio_direction[i]))) { |
46 | prefix = iio_direction[i]; | 42 | prefix = iio_direction[i]; |
@@ -65,6 +61,7 @@ int iioutils_break_up_name(const char *full_name, | |||
65 | *w = *r; | 61 | *w = *r; |
66 | w++; | 62 | w++; |
67 | } | 63 | } |
64 | |||
68 | r++; | 65 | r++; |
69 | } | 66 | } |
70 | *w = '\0'; | 67 | *w = '\0'; |
@@ -88,15 +85,10 @@ int iioutils_break_up_name(const char *full_name, | |||
88 | * | 85 | * |
89 | * Returns a value >= 0 on success, otherwise a negative error code. | 86 | * Returns a value >= 0 on success, otherwise a negative error code. |
90 | **/ | 87 | **/ |
91 | int iioutils_get_type(unsigned *is_signed, | 88 | int iioutils_get_type(unsigned *is_signed, unsigned *bytes, unsigned *bits_used, |
92 | unsigned *bytes, | 89 | unsigned *shift, uint64_t *mask, unsigned *be, |
93 | unsigned *bits_used, | 90 | const char *device_dir, const char *name, |
94 | unsigned *shift, | 91 | const char *generic_name) |
95 | uint64_t *mask, | ||
96 | unsigned *be, | ||
97 | const char *device_dir, | ||
98 | const char *name, | ||
99 | const char *generic_name) | ||
100 | { | 92 | { |
101 | FILE *sysfsfp; | 93 | FILE *sysfsfp; |
102 | int ret; | 94 | int ret; |
@@ -122,12 +114,13 @@ int iioutils_get_type(unsigned *is_signed, | |||
122 | } | 114 | } |
123 | 115 | ||
124 | dp = opendir(scan_el_dir); | 116 | dp = opendir(scan_el_dir); |
125 | if (dp == NULL) { | 117 | if (!dp) { |
126 | ret = -errno; | 118 | ret = -errno; |
127 | goto error_free_builtname_generic; | 119 | goto error_free_builtname_generic; |
128 | } | 120 | } |
121 | |||
129 | ret = -ENOENT; | 122 | ret = -ENOENT; |
130 | while (ent = readdir(dp), ent != NULL) | 123 | while (ent = readdir(dp), ent) |
131 | /* | 124 | /* |
132 | * Do we allow devices to override a generic name with | 125 | * Do we allow devices to override a generic name with |
133 | * a specific one? | 126 | * a specific one? |
@@ -140,10 +133,12 @@ int iioutils_get_type(unsigned *is_signed, | |||
140 | ret = -ENOMEM; | 133 | ret = -ENOMEM; |
141 | goto error_closedir; | 134 | goto error_closedir; |
142 | } | 135 | } |
136 | |||
143 | sysfsfp = fopen(filename, "r"); | 137 | sysfsfp = fopen(filename, "r"); |
144 | if (sysfsfp == NULL) { | 138 | if (!sysfsfp) { |
145 | ret = -errno; | 139 | ret = -errno; |
146 | printf("failed to open %s\n", filename); | 140 | fprintf(stderr, "failed to open %s\n", |
141 | filename); | ||
147 | goto error_free_filename; | 142 | goto error_free_filename; |
148 | } | 143 | } |
149 | 144 | ||
@@ -155,31 +150,36 @@ int iioutils_get_type(unsigned *is_signed, | |||
155 | &padint, shift); | 150 | &padint, shift); |
156 | if (ret < 0) { | 151 | if (ret < 0) { |
157 | ret = -errno; | 152 | ret = -errno; |
158 | printf("failed to pass scan type description\n"); | 153 | fprintf(stderr, |
154 | "failed to pass scan type description\n"); | ||
159 | goto error_close_sysfsfp; | 155 | goto error_close_sysfsfp; |
160 | } else if (ret != 5) { | 156 | } else if (ret != 5) { |
161 | ret = -EIO; | 157 | ret = -EIO; |
162 | printf("scan type description didn't match\n"); | 158 | fprintf(stderr, |
159 | "scan type description didn't match\n"); | ||
163 | goto error_close_sysfsfp; | 160 | goto error_close_sysfsfp; |
164 | } | 161 | } |
162 | |||
165 | *be = (endianchar == 'b'); | 163 | *be = (endianchar == 'b'); |
166 | *bytes = padint / 8; | 164 | *bytes = padint / 8; |
167 | if (*bits_used == 64) | 165 | if (*bits_used == 64) |
168 | *mask = ~0; | 166 | *mask = ~0; |
169 | else | 167 | else |
170 | *mask = (1 << *bits_used) - 1; | 168 | *mask = (1ULL << *bits_used) - 1; |
169 | |||
171 | *is_signed = (signchar == 's'); | 170 | *is_signed = (signchar == 's'); |
172 | if (fclose(sysfsfp)) { | 171 | if (fclose(sysfsfp)) { |
173 | ret = -errno; | 172 | ret = -errno; |
174 | printf("Failed to close %s\n", filename); | 173 | fprintf(stderr, "Failed to close %s\n", |
174 | filename); | ||
175 | goto error_free_filename; | 175 | goto error_free_filename; |
176 | } | 176 | } |
177 | 177 | ||
178 | sysfsfp = 0; | 178 | sysfsfp = 0; |
179 | free(filename); | 179 | free(filename); |
180 | |||
181 | filename = 0; | 180 | filename = 0; |
182 | } | 181 | } |
182 | |||
183 | error_close_sysfsfp: | 183 | error_close_sysfsfp: |
184 | if (sysfsfp) | 184 | if (sysfsfp) |
185 | if (fclose(sysfsfp)) | 185 | if (fclose(sysfsfp)) |
@@ -188,6 +188,7 @@ error_close_sysfsfp: | |||
188 | error_free_filename: | 188 | error_free_filename: |
189 | if (filename) | 189 | if (filename) |
190 | free(filename); | 190 | free(filename); |
191 | |||
191 | error_closedir: | 192 | error_closedir: |
192 | if (closedir(dp) == -1) | 193 | if (closedir(dp) == -1) |
193 | perror("iioutils_get_type(): Failed to close directory"); | 194 | perror("iioutils_get_type(): Failed to close directory"); |
@@ -212,11 +213,9 @@ error_free_scan_el_dir: | |||
212 | * | 213 | * |
213 | * Returns a value >= 0 on success, otherwise a negative error code. | 214 | * Returns a value >= 0 on success, otherwise a negative error code. |
214 | **/ | 215 | **/ |
215 | int iioutils_get_param_float(float *output, | 216 | int iioutils_get_param_float(float *output, const char *param_name, |
216 | const char *param_name, | 217 | const char *device_dir, const char *name, |
217 | const char *device_dir, | 218 | const char *generic_name) |
218 | const char *name, | ||
219 | const char *generic_name) | ||
220 | { | 219 | { |
221 | FILE *sysfsfp; | 220 | FILE *sysfsfp; |
222 | int ret; | 221 | int ret; |
@@ -235,13 +234,15 @@ int iioutils_get_param_float(float *output, | |||
235 | ret = -ENOMEM; | 234 | ret = -ENOMEM; |
236 | goto error_free_builtname; | 235 | goto error_free_builtname; |
237 | } | 236 | } |
237 | |||
238 | dp = opendir(device_dir); | 238 | dp = opendir(device_dir); |
239 | if (dp == NULL) { | 239 | if (!dp) { |
240 | ret = -errno; | 240 | ret = -errno; |
241 | goto error_free_builtname_generic; | 241 | goto error_free_builtname_generic; |
242 | } | 242 | } |
243 | |||
243 | ret = -ENOENT; | 244 | ret = -ENOENT; |
244 | while (ent = readdir(dp), ent != NULL) | 245 | while (ent = readdir(dp), ent) |
245 | if ((strcmp(builtname, ent->d_name) == 0) || | 246 | if ((strcmp(builtname, ent->d_name) == 0) || |
246 | (strcmp(builtname_generic, ent->d_name) == 0)) { | 247 | (strcmp(builtname_generic, ent->d_name) == 0)) { |
247 | ret = asprintf(&filename, | 248 | ret = asprintf(&filename, |
@@ -250,11 +251,13 @@ int iioutils_get_param_float(float *output, | |||
250 | ret = -ENOMEM; | 251 | ret = -ENOMEM; |
251 | goto error_closedir; | 252 | goto error_closedir; |
252 | } | 253 | } |
254 | |||
253 | sysfsfp = fopen(filename, "r"); | 255 | sysfsfp = fopen(filename, "r"); |
254 | if (!sysfsfp) { | 256 | if (!sysfsfp) { |
255 | ret = -errno; | 257 | ret = -errno; |
256 | goto error_free_filename; | 258 | goto error_free_filename; |
257 | } | 259 | } |
260 | |||
258 | errno = 0; | 261 | errno = 0; |
259 | if (fscanf(sysfsfp, "%f", output) != 1) | 262 | if (fscanf(sysfsfp, "%f", output) != 1) |
260 | ret = errno ? -errno : -ENODATA; | 263 | ret = errno ? -errno : -ENODATA; |
@@ -264,6 +267,7 @@ int iioutils_get_param_float(float *output, | |||
264 | error_free_filename: | 267 | error_free_filename: |
265 | if (filename) | 268 | if (filename) |
266 | free(filename); | 269 | free(filename); |
270 | |||
267 | error_closedir: | 271 | error_closedir: |
268 | if (closedir(dp) == -1) | 272 | if (closedir(dp) == -1) |
269 | perror("iioutils_get_param_float(): Failed to close directory"); | 273 | perror("iioutils_get_param_float(): Failed to close directory"); |
@@ -282,19 +286,17 @@ error_free_builtname: | |||
282 | * @cnt: the amount of array elements | 286 | * @cnt: the amount of array elements |
283 | **/ | 287 | **/ |
284 | 288 | ||
285 | void bsort_channel_array_by_index(struct iio_channel_info **ci_array, | 289 | void bsort_channel_array_by_index(struct iio_channel_info *ci_array, int cnt) |
286 | int cnt) | ||
287 | { | 290 | { |
288 | |||
289 | struct iio_channel_info temp; | 291 | struct iio_channel_info temp; |
290 | int x, y; | 292 | int x, y; |
291 | 293 | ||
292 | for (x = 0; x < cnt; x++) | 294 | for (x = 0; x < cnt; x++) |
293 | for (y = 0; y < (cnt - 1); y++) | 295 | for (y = 0; y < (cnt - 1); y++) |
294 | if ((*ci_array)[y].index > (*ci_array)[y+1].index) { | 296 | if (ci_array[y].index > ci_array[y + 1].index) { |
295 | temp = (*ci_array)[y + 1]; | 297 | temp = ci_array[y + 1]; |
296 | (*ci_array)[y + 1] = (*ci_array)[y]; | 298 | ci_array[y + 1] = ci_array[y]; |
297 | (*ci_array)[y] = temp; | 299 | ci_array[y] = temp; |
298 | } | 300 | } |
299 | } | 301 | } |
300 | 302 | ||
@@ -307,8 +309,7 @@ void bsort_channel_array_by_index(struct iio_channel_info **ci_array, | |||
307 | * Returns 0 on success, otherwise a negative error code. | 309 | * Returns 0 on success, otherwise a negative error code. |
308 | **/ | 310 | **/ |
309 | int build_channel_array(const char *device_dir, | 311 | int build_channel_array(const char *device_dir, |
310 | struct iio_channel_info **ci_array, | 312 | struct iio_channel_info **ci_array, int *counter) |
311 | int *counter) | ||
312 | { | 313 | { |
313 | DIR *dp; | 314 | DIR *dp; |
314 | FILE *sysfsfp; | 315 | FILE *sysfsfp; |
@@ -325,11 +326,12 @@ int build_channel_array(const char *device_dir, | |||
325 | return -ENOMEM; | 326 | return -ENOMEM; |
326 | 327 | ||
327 | dp = opendir(scan_el_dir); | 328 | dp = opendir(scan_el_dir); |
328 | if (dp == NULL) { | 329 | if (!dp) { |
329 | ret = -errno; | 330 | ret = -errno; |
330 | goto error_free_name; | 331 | goto error_free_name; |
331 | } | 332 | } |
332 | while (ent = readdir(dp), ent != NULL) | 333 | |
334 | while (ent = readdir(dp), ent) | ||
333 | if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), | 335 | if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), |
334 | "_en") == 0) { | 336 | "_en") == 0) { |
335 | ret = asprintf(&filename, | 337 | ret = asprintf(&filename, |
@@ -338,12 +340,14 @@ int build_channel_array(const char *device_dir, | |||
338 | ret = -ENOMEM; | 340 | ret = -ENOMEM; |
339 | goto error_close_dir; | 341 | goto error_close_dir; |
340 | } | 342 | } |
343 | |||
341 | sysfsfp = fopen(filename, "r"); | 344 | sysfsfp = fopen(filename, "r"); |
342 | if (sysfsfp == NULL) { | 345 | if (!sysfsfp) { |
343 | ret = -errno; | 346 | ret = -errno; |
344 | free(filename); | 347 | free(filename); |
345 | goto error_close_dir; | 348 | goto error_close_dir; |
346 | } | 349 | } |
350 | |||
347 | errno = 0; | 351 | errno = 0; |
348 | if (fscanf(sysfsfp, "%i", &ret) != 1) { | 352 | if (fscanf(sysfsfp, "%i", &ret) != 1) { |
349 | ret = errno ? -errno : -ENODATA; | 353 | ret = errno ? -errno : -ENODATA; |
@@ -353,9 +357,9 @@ int build_channel_array(const char *device_dir, | |||
353 | free(filename); | 357 | free(filename); |
354 | goto error_close_dir; | 358 | goto error_close_dir; |
355 | } | 359 | } |
356 | |||
357 | if (ret == 1) | 360 | if (ret == 1) |
358 | (*counter)++; | 361 | (*counter)++; |
362 | |||
359 | if (fclose(sysfsfp)) { | 363 | if (fclose(sysfsfp)) { |
360 | ret = -errno; | 364 | ret = -errno; |
361 | free(filename); | 365 | free(filename); |
@@ -364,13 +368,15 @@ int build_channel_array(const char *device_dir, | |||
364 | 368 | ||
365 | free(filename); | 369 | free(filename); |
366 | } | 370 | } |
371 | |||
367 | *ci_array = malloc(sizeof(**ci_array) * (*counter)); | 372 | *ci_array = malloc(sizeof(**ci_array) * (*counter)); |
368 | if (*ci_array == NULL) { | 373 | if (!*ci_array) { |
369 | ret = -ENOMEM; | 374 | ret = -ENOMEM; |
370 | goto error_close_dir; | 375 | goto error_close_dir; |
371 | } | 376 | } |
377 | |||
372 | seekdir(dp, 0); | 378 | seekdir(dp, 0); |
373 | while (ent = readdir(dp), ent != NULL) { | 379 | while (ent = readdir(dp), ent) { |
374 | if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), | 380 | if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"), |
375 | "_en") == 0) { | 381 | "_en") == 0) { |
376 | int current_enabled = 0; | 382 | int current_enabled = 0; |
@@ -384,13 +390,15 @@ int build_channel_array(const char *device_dir, | |||
384 | count--; | 390 | count--; |
385 | goto error_cleanup_array; | 391 | goto error_cleanup_array; |
386 | } | 392 | } |
393 | |||
387 | sysfsfp = fopen(filename, "r"); | 394 | sysfsfp = fopen(filename, "r"); |
388 | if (sysfsfp == NULL) { | 395 | if (!sysfsfp) { |
389 | ret = -errno; | 396 | ret = -errno; |
390 | free(filename); | 397 | free(filename); |
391 | count--; | 398 | count--; |
392 | goto error_cleanup_array; | 399 | goto error_cleanup_array; |
393 | } | 400 | } |
401 | |||
394 | errno = 0; | 402 | errno = 0; |
395 | if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) { | 403 | if (fscanf(sysfsfp, "%i", ¤t_enabled) != 1) { |
396 | ret = errno ? -errno : -ENODATA; | 404 | ret = errno ? -errno : -ENODATA; |
@@ -417,12 +425,13 @@ int build_channel_array(const char *device_dir, | |||
417 | current->name = strndup(ent->d_name, | 425 | current->name = strndup(ent->d_name, |
418 | strlen(ent->d_name) - | 426 | strlen(ent->d_name) - |
419 | strlen("_en")); | 427 | strlen("_en")); |
420 | if (current->name == NULL) { | 428 | if (!current->name) { |
421 | free(filename); | 429 | free(filename); |
422 | ret = -ENOMEM; | 430 | ret = -ENOMEM; |
423 | count--; | 431 | count--; |
424 | goto error_cleanup_array; | 432 | goto error_cleanup_array; |
425 | } | 433 | } |
434 | |||
426 | /* Get the generic and specific name elements */ | 435 | /* Get the generic and specific name elements */ |
427 | ret = iioutils_break_up_name(current->name, | 436 | ret = iioutils_break_up_name(current->name, |
428 | ¤t->generic_name); | 437 | ¤t->generic_name); |
@@ -432,6 +441,7 @@ int build_channel_array(const char *device_dir, | |||
432 | count--; | 441 | count--; |
433 | goto error_cleanup_array; | 442 | goto error_cleanup_array; |
434 | } | 443 | } |
444 | |||
435 | ret = asprintf(&filename, | 445 | ret = asprintf(&filename, |
436 | "%s/%s_index", | 446 | "%s/%s_index", |
437 | scan_el_dir, | 447 | scan_el_dir, |
@@ -441,10 +451,12 @@ int build_channel_array(const char *device_dir, | |||
441 | ret = -ENOMEM; | 451 | ret = -ENOMEM; |
442 | goto error_cleanup_array; | 452 | goto error_cleanup_array; |
443 | } | 453 | } |
454 | |||
444 | sysfsfp = fopen(filename, "r"); | 455 | sysfsfp = fopen(filename, "r"); |
445 | if (sysfsfp == NULL) { | 456 | if (!sysfsfp) { |
446 | ret = -errno; | 457 | ret = -errno; |
447 | printf("failed to open %s\n", filename); | 458 | fprintf(stderr, "failed to open %s\n", |
459 | filename); | ||
448 | free(filename); | 460 | free(filename); |
449 | goto error_cleanup_array; | 461 | goto error_cleanup_array; |
450 | } | 462 | } |
@@ -472,15 +484,17 @@ int build_channel_array(const char *device_dir, | |||
472 | device_dir, | 484 | device_dir, |
473 | current->name, | 485 | current->name, |
474 | current->generic_name); | 486 | current->generic_name); |
475 | if (ret < 0) | 487 | if ((ret < 0) && (ret != -ENOENT)) |
476 | goto error_cleanup_array; | 488 | goto error_cleanup_array; |
489 | |||
477 | ret = iioutils_get_param_float(¤t->offset, | 490 | ret = iioutils_get_param_float(¤t->offset, |
478 | "offset", | 491 | "offset", |
479 | device_dir, | 492 | device_dir, |
480 | current->name, | 493 | current->name, |
481 | current->generic_name); | 494 | current->generic_name); |
482 | if (ret < 0) | 495 | if ((ret < 0) && (ret != -ENOENT)) |
483 | goto error_cleanup_array; | 496 | goto error_cleanup_array; |
497 | |||
484 | ret = iioutils_get_type(¤t->is_signed, | 498 | ret = iioutils_get_type(¤t->is_signed, |
485 | ¤t->bytes, | 499 | ¤t->bytes, |
486 | ¤t->bits_used, | 500 | ¤t->bits_used, |
@@ -502,7 +516,7 @@ int build_channel_array(const char *device_dir, | |||
502 | 516 | ||
503 | free(scan_el_dir); | 517 | free(scan_el_dir); |
504 | /* reorder so that the array is in index order */ | 518 | /* reorder so that the array is in index order */ |
505 | bsort_channel_array_by_index(ci_array, *counter); | 519 | bsort_channel_array_by_index(*ci_array, *counter); |
506 | 520 | ||
507 | return 0; | 521 | return 0; |
508 | 522 | ||
@@ -512,6 +526,8 @@ error_cleanup_array: | |||
512 | free((*ci_array)[i].generic_name); | 526 | free((*ci_array)[i].generic_name); |
513 | } | 527 | } |
514 | free(*ci_array); | 528 | free(*ci_array); |
529 | *ci_array = NULL; | ||
530 | *counter = 0; | ||
515 | error_close_dir: | 531 | error_close_dir: |
516 | if (dp) | 532 | if (dp) |
517 | if (closedir(dp) == -1) | 533 | if (closedir(dp) == -1) |
@@ -523,7 +539,7 @@ error_free_name: | |||
523 | return ret; | 539 | return ret; |
524 | } | 540 | } |
525 | 541 | ||
526 | int calc_digits(int num) | 542 | static int calc_digits(int num) |
527 | { | 543 | { |
528 | int count = 0; | 544 | int count = 0; |
529 | 545 | ||
@@ -549,44 +565,43 @@ int find_type_by_name(const char *name, const char *type) | |||
549 | const struct dirent *ent; | 565 | const struct dirent *ent; |
550 | int number, numstrlen, ret; | 566 | int number, numstrlen, ret; |
551 | 567 | ||
552 | FILE *nameFile; | 568 | FILE *namefp; |
553 | DIR *dp; | 569 | DIR *dp; |
554 | char thisname[IIO_MAX_NAME_LENGTH]; | 570 | char thisname[IIO_MAX_NAME_LENGTH]; |
555 | char *filename; | 571 | char *filename; |
556 | 572 | ||
557 | dp = opendir(iio_dir); | 573 | dp = opendir(iio_dir); |
558 | if (dp == NULL) { | 574 | if (!dp) { |
559 | printf("No industrialio devices available\n"); | 575 | fprintf(stderr, "No industrialio devices available\n"); |
560 | return -ENODEV; | 576 | return -ENODEV; |
561 | } | 577 | } |
562 | 578 | ||
563 | while (ent = readdir(dp), ent != NULL) { | 579 | while (ent = readdir(dp), ent) { |
564 | if (strcmp(ent->d_name, ".") != 0 && | 580 | if (strcmp(ent->d_name, ".") != 0 && |
565 | strcmp(ent->d_name, "..") != 0 && | 581 | strcmp(ent->d_name, "..") != 0 && |
566 | strlen(ent->d_name) > strlen(type) && | 582 | strlen(ent->d_name) > strlen(type) && |
567 | strncmp(ent->d_name, type, strlen(type)) == 0) { | 583 | strncmp(ent->d_name, type, strlen(type)) == 0) { |
568 | errno = 0; | 584 | errno = 0; |
569 | ret = sscanf(ent->d_name + strlen(type), "%d", &number); | 585 | ret = sscanf(ent->d_name + strlen(type), "%d", &number); |
570 | if (ret < 0) { | 586 | if (ret < 0) { |
571 | ret = -errno; | 587 | ret = -errno; |
572 | printf("failed to read element number\n"); | 588 | fprintf(stderr, |
589 | "failed to read element number\n"); | ||
573 | goto error_close_dir; | 590 | goto error_close_dir; |
574 | } else if (ret != 1) { | 591 | } else if (ret != 1) { |
575 | ret = -EIO; | 592 | ret = -EIO; |
576 | printf("failed to match element number\n"); | 593 | fprintf(stderr, |
594 | "failed to match element number\n"); | ||
577 | goto error_close_dir; | 595 | goto error_close_dir; |
578 | } | 596 | } |
579 | 597 | ||
580 | numstrlen = calc_digits(number); | 598 | numstrlen = calc_digits(number); |
581 | /* verify the next character is not a colon */ | 599 | /* verify the next character is not a colon */ |
582 | if (strncmp(ent->d_name + strlen(type) + numstrlen, | 600 | if (strncmp(ent->d_name + strlen(type) + numstrlen, |
583 | ":", | 601 | ":", 1) != 0) { |
584 | 1) != 0) { | 602 | filename = malloc(strlen(iio_dir) + strlen(type) |
585 | filename = malloc(strlen(iio_dir) | 603 | + numstrlen + 6); |
586 | + strlen(type) | 604 | if (!filename) { |
587 | + numstrlen | ||
588 | + 6); | ||
589 | if (filename == NULL) { | ||
590 | ret = -ENOMEM; | 605 | ret = -ENOMEM; |
591 | goto error_close_dir; | 606 | goto error_close_dir; |
592 | } | 607 | } |
@@ -598,19 +613,20 @@ int find_type_by_name(const char *name, const char *type) | |||
598 | goto error_close_dir; | 613 | goto error_close_dir; |
599 | } | 614 | } |
600 | 615 | ||
601 | nameFile = fopen(filename, "r"); | 616 | namefp = fopen(filename, "r"); |
602 | if (!nameFile) { | 617 | if (!namefp) { |
603 | free(filename); | 618 | free(filename); |
604 | continue; | 619 | continue; |
605 | } | 620 | } |
621 | |||
606 | free(filename); | 622 | free(filename); |
607 | errno = 0; | 623 | errno = 0; |
608 | if (fscanf(nameFile, "%s", thisname) != 1) { | 624 | if (fscanf(namefp, "%s", thisname) != 1) { |
609 | ret = errno ? -errno : -ENODATA; | 625 | ret = errno ? -errno : -ENODATA; |
610 | goto error_close_dir; | 626 | goto error_close_dir; |
611 | } | 627 | } |
612 | 628 | ||
613 | if (fclose(nameFile)) { | 629 | if (fclose(namefp)) { |
614 | ret = -errno; | 630 | ret = -errno; |
615 | goto error_close_dir; | 631 | goto error_close_dir; |
616 | } | 632 | } |
@@ -618,6 +634,7 @@ int find_type_by_name(const char *name, const char *type) | |||
618 | if (strcmp(name, thisname) == 0) { | 634 | if (strcmp(name, thisname) == 0) { |
619 | if (closedir(dp) == -1) | 635 | if (closedir(dp) == -1) |
620 | return -errno; | 636 | return -errno; |
637 | |||
621 | return number; | 638 | return number; |
622 | } | 639 | } |
623 | } | 640 | } |
@@ -631,6 +648,7 @@ int find_type_by_name(const char *name, const char *type) | |||
631 | error_close_dir: | 648 | error_close_dir: |
632 | if (closedir(dp) == -1) | 649 | if (closedir(dp) == -1) |
633 | perror("find_type_by_name(): Failed to close directory"); | 650 | perror("find_type_by_name(): Failed to close directory"); |
651 | |||
634 | return ret; | 652 | return ret; |
635 | } | 653 | } |
636 | 654 | ||
@@ -642,18 +660,20 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val, | |||
642 | int test; | 660 | int test; |
643 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); | 661 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
644 | 662 | ||
645 | if (temp == NULL) | 663 | if (!temp) |
646 | return -ENOMEM; | 664 | return -ENOMEM; |
665 | |||
647 | ret = sprintf(temp, "%s/%s", basedir, filename); | 666 | ret = sprintf(temp, "%s/%s", basedir, filename); |
648 | if (ret < 0) | 667 | if (ret < 0) |
649 | goto error_free; | 668 | goto error_free; |
650 | 669 | ||
651 | sysfsfp = fopen(temp, "w"); | 670 | sysfsfp = fopen(temp, "w"); |
652 | if (sysfsfp == NULL) { | 671 | if (!sysfsfp) { |
653 | ret = -errno; | 672 | ret = -errno; |
654 | printf("failed to open %s\n", temp); | 673 | fprintf(stderr, "failed to open %s\n", temp); |
655 | goto error_free; | 674 | goto error_free; |
656 | } | 675 | } |
676 | |||
657 | ret = fprintf(sysfsfp, "%d", val); | 677 | ret = fprintf(sysfsfp, "%d", val); |
658 | if (ret < 0) { | 678 | if (ret < 0) { |
659 | if (fclose(sysfsfp)) | 679 | if (fclose(sysfsfp)) |
@@ -669,11 +689,12 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val, | |||
669 | 689 | ||
670 | if (verify) { | 690 | if (verify) { |
671 | sysfsfp = fopen(temp, "r"); | 691 | sysfsfp = fopen(temp, "r"); |
672 | if (sysfsfp == NULL) { | 692 | if (!sysfsfp) { |
673 | ret = -errno; | 693 | ret = -errno; |
674 | printf("failed to open %s\n", temp); | 694 | fprintf(stderr, "failed to open %s\n", temp); |
675 | goto error_free; | 695 | goto error_free; |
676 | } | 696 | } |
697 | |||
677 | if (fscanf(sysfsfp, "%d", &test) != 1) { | 698 | if (fscanf(sysfsfp, "%d", &test) != 1) { |
678 | ret = errno ? -errno : -ENODATA; | 699 | ret = errno ? -errno : -ENODATA; |
679 | if (fclose(sysfsfp)) | 700 | if (fclose(sysfsfp)) |
@@ -688,13 +709,13 @@ static int _write_sysfs_int(const char *filename, const char *basedir, int val, | |||
688 | } | 709 | } |
689 | 710 | ||
690 | if (test != val) { | 711 | if (test != val) { |
691 | printf("Possible failure in int write %d to %s%s\n", | 712 | fprintf(stderr, |
692 | val, | 713 | "Possible failure in int write %d to %s/%s\n", |
693 | basedir, | 714 | val, basedir, filename); |
694 | filename); | ||
695 | ret = -1; | 715 | ret = -1; |
696 | } | 716 | } |
697 | } | 717 | } |
718 | |||
698 | error_free: | 719 | error_free: |
699 | free(temp); | 720 | free(temp); |
700 | return ret; | 721 | return ret; |
@@ -735,20 +756,22 @@ static int _write_sysfs_string(const char *filename, const char *basedir, | |||
735 | FILE *sysfsfp; | 756 | FILE *sysfsfp; |
736 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); | 757 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
737 | 758 | ||
738 | if (temp == NULL) { | 759 | if (!temp) { |
739 | printf("Memory allocation failed\n"); | 760 | fprintf(stderr, "Memory allocation failed\n"); |
740 | return -ENOMEM; | 761 | return -ENOMEM; |
741 | } | 762 | } |
763 | |||
742 | ret = sprintf(temp, "%s/%s", basedir, filename); | 764 | ret = sprintf(temp, "%s/%s", basedir, filename); |
743 | if (ret < 0) | 765 | if (ret < 0) |
744 | goto error_free; | 766 | goto error_free; |
745 | 767 | ||
746 | sysfsfp = fopen(temp, "w"); | 768 | sysfsfp = fopen(temp, "w"); |
747 | if (sysfsfp == NULL) { | 769 | if (!sysfsfp) { |
748 | ret = -errno; | 770 | ret = -errno; |
749 | printf("Could not open %s\n", temp); | 771 | fprintf(stderr, "Could not open %s\n", temp); |
750 | goto error_free; | 772 | goto error_free; |
751 | } | 773 | } |
774 | |||
752 | ret = fprintf(sysfsfp, "%s", val); | 775 | ret = fprintf(sysfsfp, "%s", val); |
753 | if (ret < 0) { | 776 | if (ret < 0) { |
754 | if (fclose(sysfsfp)) | 777 | if (fclose(sysfsfp)) |
@@ -764,11 +787,12 @@ static int _write_sysfs_string(const char *filename, const char *basedir, | |||
764 | 787 | ||
765 | if (verify) { | 788 | if (verify) { |
766 | sysfsfp = fopen(temp, "r"); | 789 | sysfsfp = fopen(temp, "r"); |
767 | if (sysfsfp == NULL) { | 790 | if (!sysfsfp) { |
768 | ret = -errno; | 791 | ret = -errno; |
769 | printf("could not open file to verify\n"); | 792 | fprintf(stderr, "Could not open file to verify\n"); |
770 | goto error_free; | 793 | goto error_free; |
771 | } | 794 | } |
795 | |||
772 | if (fscanf(sysfsfp, "%s", temp) != 1) { | 796 | if (fscanf(sysfsfp, "%s", temp) != 1) { |
773 | ret = errno ? -errno : -ENODATA; | 797 | ret = errno ? -errno : -ENODATA; |
774 | if (fclose(sysfsfp)) | 798 | if (fclose(sysfsfp)) |
@@ -783,16 +807,14 @@ static int _write_sysfs_string(const char *filename, const char *basedir, | |||
783 | } | 807 | } |
784 | 808 | ||
785 | if (strcmp(temp, val) != 0) { | 809 | if (strcmp(temp, val) != 0) { |
786 | printf("Possible failure in string write of %s " | 810 | fprintf(stderr, |
787 | "Should be %s " | 811 | "Possible failure in string write of %s " |
788 | "written to %s\%s\n", | 812 | "Should be %s written to %s/%s\n", temp, val, |
789 | temp, | 813 | basedir, filename); |
790 | val, | ||
791 | basedir, | ||
792 | filename); | ||
793 | ret = -1; | 814 | ret = -1; |
794 | } | 815 | } |
795 | } | 816 | } |
817 | |||
796 | error_free: | 818 | error_free: |
797 | free(temp); | 819 | free(temp); |
798 | 820 | ||
@@ -841,19 +863,21 @@ int read_sysfs_posint(const char *filename, const char *basedir) | |||
841 | FILE *sysfsfp; | 863 | FILE *sysfsfp; |
842 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); | 864 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
843 | 865 | ||
844 | if (temp == NULL) { | 866 | if (!temp) { |
845 | printf("Memory allocation failed"); | 867 | fprintf(stderr, "Memory allocation failed"); |
846 | return -ENOMEM; | 868 | return -ENOMEM; |
847 | } | 869 | } |
870 | |||
848 | ret = sprintf(temp, "%s/%s", basedir, filename); | 871 | ret = sprintf(temp, "%s/%s", basedir, filename); |
849 | if (ret < 0) | 872 | if (ret < 0) |
850 | goto error_free; | 873 | goto error_free; |
851 | 874 | ||
852 | sysfsfp = fopen(temp, "r"); | 875 | sysfsfp = fopen(temp, "r"); |
853 | if (sysfsfp == NULL) { | 876 | if (!sysfsfp) { |
854 | ret = -errno; | 877 | ret = -errno; |
855 | goto error_free; | 878 | goto error_free; |
856 | } | 879 | } |
880 | |||
857 | errno = 0; | 881 | errno = 0; |
858 | if (fscanf(sysfsfp, "%d\n", &ret) != 1) { | 882 | if (fscanf(sysfsfp, "%d\n", &ret) != 1) { |
859 | ret = errno ? -errno : -ENODATA; | 883 | ret = errno ? -errno : -ENODATA; |
@@ -868,6 +892,7 @@ int read_sysfs_posint(const char *filename, const char *basedir) | |||
868 | 892 | ||
869 | error_free: | 893 | error_free: |
870 | free(temp); | 894 | free(temp); |
895 | |||
871 | return ret; | 896 | return ret; |
872 | } | 897 | } |
873 | 898 | ||
@@ -885,19 +910,21 @@ int read_sysfs_float(const char *filename, const char *basedir, float *val) | |||
885 | FILE *sysfsfp; | 910 | FILE *sysfsfp; |
886 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); | 911 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
887 | 912 | ||
888 | if (temp == NULL) { | 913 | if (!temp) { |
889 | printf("Memory allocation failed"); | 914 | fprintf(stderr, "Memory allocation failed"); |
890 | return -ENOMEM; | 915 | return -ENOMEM; |
891 | } | 916 | } |
917 | |||
892 | ret = sprintf(temp, "%s/%s", basedir, filename); | 918 | ret = sprintf(temp, "%s/%s", basedir, filename); |
893 | if (ret < 0) | 919 | if (ret < 0) |
894 | goto error_free; | 920 | goto error_free; |
895 | 921 | ||
896 | sysfsfp = fopen(temp, "r"); | 922 | sysfsfp = fopen(temp, "r"); |
897 | if (sysfsfp == NULL) { | 923 | if (!sysfsfp) { |
898 | ret = -errno; | 924 | ret = -errno; |
899 | goto error_free; | 925 | goto error_free; |
900 | } | 926 | } |
927 | |||
901 | errno = 0; | 928 | errno = 0; |
902 | if (fscanf(sysfsfp, "%f\n", val) != 1) { | 929 | if (fscanf(sysfsfp, "%f\n", val) != 1) { |
903 | ret = errno ? -errno : -ENODATA; | 930 | ret = errno ? -errno : -ENODATA; |
@@ -912,6 +939,7 @@ int read_sysfs_float(const char *filename, const char *basedir, float *val) | |||
912 | 939 | ||
913 | error_free: | 940 | error_free: |
914 | free(temp); | 941 | free(temp); |
942 | |||
915 | return ret; | 943 | return ret; |
916 | } | 944 | } |
917 | 945 | ||
@@ -929,19 +957,21 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str) | |||
929 | FILE *sysfsfp; | 957 | FILE *sysfsfp; |
930 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); | 958 | char *temp = malloc(strlen(basedir) + strlen(filename) + 2); |
931 | 959 | ||
932 | if (temp == NULL) { | 960 | if (!temp) { |
933 | printf("Memory allocation failed"); | 961 | fprintf(stderr, "Memory allocation failed"); |
934 | return -ENOMEM; | 962 | return -ENOMEM; |
935 | } | 963 | } |
964 | |||
936 | ret = sprintf(temp, "%s/%s", basedir, filename); | 965 | ret = sprintf(temp, "%s/%s", basedir, filename); |
937 | if (ret < 0) | 966 | if (ret < 0) |
938 | goto error_free; | 967 | goto error_free; |
939 | 968 | ||
940 | sysfsfp = fopen(temp, "r"); | 969 | sysfsfp = fopen(temp, "r"); |
941 | if (sysfsfp == NULL) { | 970 | if (!sysfsfp) { |
942 | ret = -errno; | 971 | ret = -errno; |
943 | goto error_free; | 972 | goto error_free; |
944 | } | 973 | } |
974 | |||
945 | errno = 0; | 975 | errno = 0; |
946 | if (fscanf(sysfsfp, "%s\n", str) != 1) { | 976 | if (fscanf(sysfsfp, "%s\n", str) != 1) { |
947 | ret = errno ? -errno : -ENODATA; | 977 | ret = errno ? -errno : -ENODATA; |
@@ -956,7 +986,6 @@ int read_sysfs_string(const char *filename, const char *basedir, char *str) | |||
956 | 986 | ||
957 | error_free: | 987 | error_free: |
958 | free(temp); | 988 | free(temp); |
989 | |||
959 | return ret; | 990 | return ret; |
960 | } | 991 | } |
961 | |||
962 | #endif /* _IIO_UTILS_H */ | ||