hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/tools/iio/iio_utils.c
....@@ -1,10 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /* IIO - useful set of util functionality
23 *
34 * Copyright (c) 2008 Jonathan Cameron
4
- *
5
- * This program is free software; you can redistribute it and/or modify it
6
- * under the terms of the GNU General Public License version 2 as published by
7
- * the Free Software Foundation.
85 */
96 #include <string.h>
107 #include <stdlib.h>
....@@ -265,6 +262,7 @@
265262 if (fscanf(sysfsfp, "%f", output) != 1)
266263 ret = errno ? -errno : -ENODATA;
267264
265
+ fclose(sysfsfp);
268266 break;
269267 }
270268 error_free_filename:
....@@ -345,9 +343,9 @@
345343 }
346344
347345 sysfsfp = fopen(filename, "r");
346
+ free(filename);
348347 if (!sysfsfp) {
349348 ret = -errno;
350
- free(filename);
351349 goto error_close_dir;
352350 }
353351
....@@ -357,7 +355,6 @@
357355 if (fclose(sysfsfp))
358356 perror("build_channel_array(): Failed to close file");
359357
360
- free(filename);
361358 goto error_close_dir;
362359 }
363360 if (ret == 1)
....@@ -365,11 +362,9 @@
365362
366363 if (fclose(sysfsfp)) {
367364 ret = -errno;
368
- free(filename);
369365 goto error_close_dir;
370366 }
371367
372
- free(filename);
373368 }
374369
375370 *ci_array = malloc(sizeof(**ci_array) * (*counter));
....@@ -395,9 +390,9 @@
395390 }
396391
397392 sysfsfp = fopen(filename, "r");
393
+ free(filename);
398394 if (!sysfsfp) {
399395 ret = -errno;
400
- free(filename);
401396 count--;
402397 goto error_cleanup_array;
403398 }
....@@ -405,20 +400,17 @@
405400 errno = 0;
406401 if (fscanf(sysfsfp, "%i", &current_enabled) != 1) {
407402 ret = errno ? -errno : -ENODATA;
408
- free(filename);
409403 count--;
410404 goto error_cleanup_array;
411405 }
412406
413407 if (fclose(sysfsfp)) {
414408 ret = -errno;
415
- free(filename);
416409 count--;
417410 goto error_cleanup_array;
418411 }
419412
420413 if (!current_enabled) {
421
- free(filename);
422414 count--;
423415 continue;
424416 }
....@@ -429,7 +421,6 @@
429421 strlen(ent->d_name) -
430422 strlen("_en"));
431423 if (!current->name) {
432
- free(filename);
433424 ret = -ENOMEM;
434425 count--;
435426 goto error_cleanup_array;
....@@ -439,7 +430,6 @@
439430 ret = iioutils_break_up_name(current->name,
440431 &current->generic_name);
441432 if (ret) {
442
- free(filename);
443433 free(current->name);
444434 count--;
445435 goto error_cleanup_array;
....@@ -450,17 +440,16 @@
450440 scan_el_dir,
451441 current->name);
452442 if (ret < 0) {
453
- free(filename);
454443 ret = -ENOMEM;
455444 goto error_cleanup_array;
456445 }
457446
458447 sysfsfp = fopen(filename, "r");
448
+ free(filename);
459449 if (!sysfsfp) {
460450 ret = -errno;
461
- fprintf(stderr, "failed to open %s\n",
462
- filename);
463
- free(filename);
451
+ fprintf(stderr, "failed to open %s/%s_index\n",
452
+ scan_el_dir, current->name);
464453 goto error_cleanup_array;
465454 }
466455
....@@ -470,17 +459,14 @@
470459 if (fclose(sysfsfp))
471460 perror("build_channel_array(): Failed to close file");
472461
473
- free(filename);
474462 goto error_cleanup_array;
475463 }
476464
477465 if (fclose(sysfsfp)) {
478466 ret = -errno;
479
- free(filename);
480467 goto error_cleanup_array;
481468 }
482469
483
- free(filename);
484470 /* Find the scale */
485471 ret = iioutils_get_param_float(&current->scale,
486472 "scale",
....@@ -546,6 +532,10 @@
546532 {
547533 int count = 0;
548534
535
+ /* It takes a digit to represent zero */
536
+ if (!num)
537
+ return 1;
538
+
549539 while (num != 0) {
550540 num /= 10;
551541 count++;