PM / devfreq: event: Remove incorrect property in exynos-ppmu DT binding
[linux-2.6/btrfs-unstable.git] / tools / iio / iio_utils.c
blobec9ab7f9ae4c50e5ac77bedbdff5e6a144eafe78
1 /* IIO - useful set of util functionality
3 * Copyright (c) 2008 Jonathan Cameron
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.
8 */
9 #ifndef _IIO_UTILS_H
10 #define _IIO_UTILS_H
12 #include <string.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <stdint.h>
16 #include <dirent.h>
17 #include <errno.h>
18 #include <ctype.h>
19 #include "iio_utils.h"
21 const char *iio_dir = "/sys/bus/iio/devices/";
23 static char * const iio_direction[] = {
24 "in",
25 "out",
28 /**
29 * iioutils_break_up_name() - extract generic name from full channel name
30 * @full_name: the full channel name
31 * @generic_name: the output generic channel name
33 * Returns 0 on success, or a negative error code if string extraction failed.
34 **/
35 int iioutils_break_up_name(const char *full_name,
36 char **generic_name)
38 char *current;
39 char *w, *r;
40 char *working, *prefix = "";
41 int i, ret;
43 for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++)
44 if (!strncmp(full_name, iio_direction[i],
45 strlen(iio_direction[i]))) {
46 prefix = iio_direction[i];
47 break;
50 current = strdup(full_name + strlen(prefix) + 1);
51 if (!current)
52 return -ENOMEM;
54 working = strtok(current, "_\0");
55 if (!working) {
56 free(current);
57 return -EINVAL;
60 w = working;
61 r = working;
63 while (*r != '\0') {
64 if (!isdigit(*r)) {
65 *w = *r;
66 w++;
68 r++;
70 *w = '\0';
71 ret = asprintf(generic_name, "%s_%s", prefix, working);
72 free(current);
74 return (ret == -1) ? -ENOMEM : 0;
77 /**
78 * iioutils_get_type() - find and process _type attribute data
79 * @is_signed: output whether channel is signed
80 * @bytes: output how many bytes the channel storage occupies
81 * @bits_used: output number of valid bits of data
82 * @shift: output amount of bits to shift right data before applying bit mask
83 * @mask: output a bit mask for the raw data
84 * @be: output if data in big endian
85 * @device_dir: the IIO device directory
86 * @name: the channel name
87 * @generic_name: the channel type name
89 * Returns a value >= 0 on success, otherwise a negative error code.
90 **/
91 int iioutils_get_type(unsigned *is_signed,
92 unsigned *bytes,
93 unsigned *bits_used,
94 unsigned *shift,
95 uint64_t *mask,
96 unsigned *be,
97 const char *device_dir,
98 const char *name,
99 const char *generic_name)
101 FILE *sysfsfp;
102 int ret;
103 DIR *dp;
104 char *scan_el_dir, *builtname, *builtname_generic, *filename = 0;
105 char signchar, endianchar;
106 unsigned padint;
107 const struct dirent *ent;
109 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
110 if (ret < 0)
111 return -ENOMEM;
113 ret = asprintf(&builtname, FORMAT_TYPE_FILE, name);
114 if (ret < 0) {
115 ret = -ENOMEM;
116 goto error_free_scan_el_dir;
118 ret = asprintf(&builtname_generic, FORMAT_TYPE_FILE, generic_name);
119 if (ret < 0) {
120 ret = -ENOMEM;
121 goto error_free_builtname;
124 dp = opendir(scan_el_dir);
125 if (dp == NULL) {
126 ret = -errno;
127 goto error_free_builtname_generic;
129 ret = -ENOENT;
130 while (ent = readdir(dp), ent != NULL)
132 * Do we allow devices to override a generic name with
133 * a specific one?
135 if ((strcmp(builtname, ent->d_name) == 0) ||
136 (strcmp(builtname_generic, ent->d_name) == 0)) {
137 ret = asprintf(&filename,
138 "%s/%s", scan_el_dir, ent->d_name);
139 if (ret < 0) {
140 ret = -ENOMEM;
141 goto error_closedir;
143 sysfsfp = fopen(filename, "r");
144 if (sysfsfp == NULL) {
145 ret = -errno;
146 printf("failed to open %s\n", filename);
147 goto error_free_filename;
150 ret = fscanf(sysfsfp,
151 "%ce:%c%u/%u>>%u",
152 &endianchar,
153 &signchar,
154 bits_used,
155 &padint, shift);
156 if (ret < 0) {
157 ret = -errno;
158 printf("failed to pass scan type description\n");
159 goto error_close_sysfsfp;
160 } else if (ret != 5) {
161 ret = -EIO;
162 printf("scan type description didn't match\n");
163 goto error_close_sysfsfp;
165 *be = (endianchar == 'b');
166 *bytes = padint / 8;
167 if (*bits_used == 64)
168 *mask = ~0;
169 else
170 *mask = (1 << *bits_used) - 1;
171 *is_signed = (signchar == 's');
172 if (fclose(sysfsfp)) {
173 ret = -errno;
174 printf("Failed to close %s\n", filename);
175 goto error_free_filename;
178 sysfsfp = 0;
179 free(filename);
181 filename = 0;
183 error_close_sysfsfp:
184 if (sysfsfp)
185 if (fclose(sysfsfp))
186 perror("iioutils_get_type(): Failed to close file");
188 error_free_filename:
189 if (filename)
190 free(filename);
191 error_closedir:
192 if (closedir(dp) == -1)
193 perror("iioutils_get_type(): Failed to close directory");
195 error_free_builtname_generic:
196 free(builtname_generic);
197 error_free_builtname:
198 free(builtname);
199 error_free_scan_el_dir:
200 free(scan_el_dir);
202 return ret;
206 * iioutils_get_param_float() - read a float value from a channel parameter
207 * @output: output the float value
208 * @param_name: the parameter name to read
209 * @device_dir: the IIO device directory in sysfs
210 * @name: the channel name
211 * @generic_name: the channel type name
213 * Returns a value >= 0 on success, otherwise a negative error code.
215 int iioutils_get_param_float(float *output,
216 const char *param_name,
217 const char *device_dir,
218 const char *name,
219 const char *generic_name)
221 FILE *sysfsfp;
222 int ret;
223 DIR *dp;
224 char *builtname, *builtname_generic;
225 char *filename = NULL;
226 const struct dirent *ent;
228 ret = asprintf(&builtname, "%s_%s", name, param_name);
229 if (ret < 0)
230 return -ENOMEM;
232 ret = asprintf(&builtname_generic,
233 "%s_%s", generic_name, param_name);
234 if (ret < 0) {
235 ret = -ENOMEM;
236 goto error_free_builtname;
238 dp = opendir(device_dir);
239 if (dp == NULL) {
240 ret = -errno;
241 goto error_free_builtname_generic;
243 ret = -ENOENT;
244 while (ent = readdir(dp), ent != NULL)
245 if ((strcmp(builtname, ent->d_name) == 0) ||
246 (strcmp(builtname_generic, ent->d_name) == 0)) {
247 ret = asprintf(&filename,
248 "%s/%s", device_dir, ent->d_name);
249 if (ret < 0) {
250 ret = -ENOMEM;
251 goto error_closedir;
253 sysfsfp = fopen(filename, "r");
254 if (!sysfsfp) {
255 ret = -errno;
256 goto error_free_filename;
258 errno = 0;
259 if (fscanf(sysfsfp, "%f", output) != 1)
260 ret = errno ? -errno : -ENODATA;
262 break;
264 error_free_filename:
265 if (filename)
266 free(filename);
267 error_closedir:
268 if (closedir(dp) == -1)
269 perror("iioutils_get_param_float(): Failed to close directory");
271 error_free_builtname_generic:
272 free(builtname_generic);
273 error_free_builtname:
274 free(builtname);
276 return ret;
280 * bsort_channel_array_by_index() - sort the array in index order
281 * @ci_array: the iio_channel_info array to be sorted
282 * @cnt: the amount of array elements
285 void bsort_channel_array_by_index(struct iio_channel_info **ci_array,
286 int cnt)
289 struct iio_channel_info temp;
290 int x, y;
292 for (x = 0; x < cnt; x++)
293 for (y = 0; y < (cnt - 1); y++)
294 if ((*ci_array)[y].index > (*ci_array)[y+1].index) {
295 temp = (*ci_array)[y + 1];
296 (*ci_array)[y + 1] = (*ci_array)[y];
297 (*ci_array)[y] = temp;
302 * build_channel_array() - function to figure out what channels are present
303 * @device_dir: the IIO device directory in sysfs
304 * @ci_array: output the resulting array of iio_channel_info
305 * @counter: output the amount of array elements
307 * Returns 0 on success, otherwise a negative error code.
309 int build_channel_array(const char *device_dir,
310 struct iio_channel_info **ci_array,
311 int *counter)
313 DIR *dp;
314 FILE *sysfsfp;
315 int count = 0, i;
316 struct iio_channel_info *current;
317 int ret;
318 const struct dirent *ent;
319 char *scan_el_dir;
320 char *filename;
322 *counter = 0;
323 ret = asprintf(&scan_el_dir, FORMAT_SCAN_ELEMENTS_DIR, device_dir);
324 if (ret < 0)
325 return -ENOMEM;
327 dp = opendir(scan_el_dir);
328 if (dp == NULL) {
329 ret = -errno;
330 goto error_free_name;
332 while (ent = readdir(dp), ent != NULL)
333 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
334 "_en") == 0) {
335 ret = asprintf(&filename,
336 "%s/%s", scan_el_dir, ent->d_name);
337 if (ret < 0) {
338 ret = -ENOMEM;
339 goto error_close_dir;
341 sysfsfp = fopen(filename, "r");
342 if (sysfsfp == NULL) {
343 ret = -errno;
344 free(filename);
345 goto error_close_dir;
347 errno = 0;
348 if (fscanf(sysfsfp, "%i", &ret) != 1) {
349 ret = errno ? -errno : -ENODATA;
350 if (fclose(sysfsfp))
351 perror("build_channel_array(): Failed to close file");
353 free(filename);
354 goto error_close_dir;
357 if (ret == 1)
358 (*counter)++;
359 if (fclose(sysfsfp)) {
360 ret = -errno;
361 free(filename);
362 goto error_close_dir;
365 free(filename);
367 *ci_array = malloc(sizeof(**ci_array) * (*counter));
368 if (*ci_array == NULL) {
369 ret = -ENOMEM;
370 goto error_close_dir;
372 seekdir(dp, 0);
373 while (ent = readdir(dp), ent != NULL) {
374 if (strcmp(ent->d_name + strlen(ent->d_name) - strlen("_en"),
375 "_en") == 0) {
376 int current_enabled = 0;
378 current = &(*ci_array)[count++];
379 ret = asprintf(&filename,
380 "%s/%s", scan_el_dir, ent->d_name);
381 if (ret < 0) {
382 ret = -ENOMEM;
383 /* decrement count to avoid freeing name */
384 count--;
385 goto error_cleanup_array;
387 sysfsfp = fopen(filename, "r");
388 if (sysfsfp == NULL) {
389 ret = -errno;
390 free(filename);
391 count--;
392 goto error_cleanup_array;
394 errno = 0;
395 if (fscanf(sysfsfp, "%i", &current_enabled) != 1) {
396 ret = errno ? -errno : -ENODATA;
397 free(filename);
398 count--;
399 goto error_cleanup_array;
402 if (fclose(sysfsfp)) {
403 ret = -errno;
404 free(filename);
405 count--;
406 goto error_cleanup_array;
409 if (!current_enabled) {
410 free(filename);
411 count--;
412 continue;
415 current->scale = 1.0;
416 current->offset = 0;
417 current->name = strndup(ent->d_name,
418 strlen(ent->d_name) -
419 strlen("_en"));
420 if (current->name == NULL) {
421 free(filename);
422 ret = -ENOMEM;
423 count--;
424 goto error_cleanup_array;
426 /* Get the generic and specific name elements */
427 ret = iioutils_break_up_name(current->name,
428 &current->generic_name);
429 if (ret) {
430 free(filename);
431 free(current->name);
432 count--;
433 goto error_cleanup_array;
435 ret = asprintf(&filename,
436 "%s/%s_index",
437 scan_el_dir,
438 current->name);
439 if (ret < 0) {
440 free(filename);
441 ret = -ENOMEM;
442 goto error_cleanup_array;
444 sysfsfp = fopen(filename, "r");
445 if (sysfsfp == NULL) {
446 ret = -errno;
447 printf("failed to open %s\n", filename);
448 free(filename);
449 goto error_cleanup_array;
452 errno = 0;
453 if (fscanf(sysfsfp, "%u", &current->index) != 1) {
454 ret = errno ? -errno : -ENODATA;
455 if (fclose(sysfsfp))
456 perror("build_channel_array(): Failed to close file");
458 free(filename);
459 goto error_cleanup_array;
462 if (fclose(sysfsfp)) {
463 ret = -errno;
464 free(filename);
465 goto error_cleanup_array;
468 free(filename);
469 /* Find the scale */
470 ret = iioutils_get_param_float(&current->scale,
471 "scale",
472 device_dir,
473 current->name,
474 current->generic_name);
475 if (ret < 0)
476 goto error_cleanup_array;
477 ret = iioutils_get_param_float(&current->offset,
478 "offset",
479 device_dir,
480 current->name,
481 current->generic_name);
482 if (ret < 0)
483 goto error_cleanup_array;
484 ret = iioutils_get_type(&current->is_signed,
485 &current->bytes,
486 &current->bits_used,
487 &current->shift,
488 &current->mask,
489 &current->be,
490 device_dir,
491 current->name,
492 current->generic_name);
493 if (ret < 0)
494 goto error_cleanup_array;
498 if (closedir(dp) == -1) {
499 ret = -errno;
500 goto error_cleanup_array;
503 free(scan_el_dir);
504 /* reorder so that the array is in index order */
505 bsort_channel_array_by_index(ci_array, *counter);
507 return 0;
509 error_cleanup_array:
510 for (i = count - 1; i >= 0; i--) {
511 free((*ci_array)[i].name);
512 free((*ci_array)[i].generic_name);
514 free(*ci_array);
515 error_close_dir:
516 if (dp)
517 if (closedir(dp) == -1)
518 perror("build_channel_array(): Failed to close dir");
520 error_free_name:
521 free(scan_el_dir);
523 return ret;
526 int calc_digits(int num)
528 int count = 0;
530 while (num != 0) {
531 num /= 10;
532 count++;
535 return count;
539 * find_type_by_name() - function to match top level types by name
540 * @name: top level type instance name
541 * @type: the type of top level instance being searched
543 * Returns the device number of a matched IIO device on success, otherwise a
544 * negative error code.
545 * Typical types this is used for are device and trigger.
547 int find_type_by_name(const char *name, const char *type)
549 const struct dirent *ent;
550 int number, numstrlen, ret;
552 FILE *nameFile;
553 DIR *dp;
554 char thisname[IIO_MAX_NAME_LENGTH];
555 char *filename;
557 dp = opendir(iio_dir);
558 if (dp == NULL) {
559 printf("No industrialio devices available\n");
560 return -ENODEV;
563 while (ent = readdir(dp), ent != NULL) {
564 if (strcmp(ent->d_name, ".") != 0 &&
565 strcmp(ent->d_name, "..") != 0 &&
566 strlen(ent->d_name) > strlen(type) &&
567 strncmp(ent->d_name, type, strlen(type)) == 0) {
568 errno = 0;
569 ret = sscanf(ent->d_name + strlen(type), "%d", &number);
570 if (ret < 0) {
571 ret = -errno;
572 printf("failed to read element number\n");
573 goto error_close_dir;
574 } else if (ret != 1) {
575 ret = -EIO;
576 printf("failed to match element number\n");
577 goto error_close_dir;
580 numstrlen = calc_digits(number);
581 /* verify the next character is not a colon */
582 if (strncmp(ent->d_name + strlen(type) + numstrlen,
583 ":",
584 1) != 0) {
585 filename = malloc(strlen(iio_dir)
586 + strlen(type)
587 + numstrlen
588 + 6);
589 if (filename == NULL) {
590 ret = -ENOMEM;
591 goto error_close_dir;
594 ret = sprintf(filename, "%s%s%d/name", iio_dir,
595 type, number);
596 if (ret < 0) {
597 free(filename);
598 goto error_close_dir;
601 nameFile = fopen(filename, "r");
602 if (!nameFile) {
603 free(filename);
604 continue;
606 free(filename);
607 errno = 0;
608 if (fscanf(nameFile, "%s", thisname) != 1) {
609 ret = errno ? -errno : -ENODATA;
610 goto error_close_dir;
613 if (fclose(nameFile)) {
614 ret = -errno;
615 goto error_close_dir;
618 if (strcmp(name, thisname) == 0) {
619 if (closedir(dp) == -1)
620 return -errno;
621 return number;
626 if (closedir(dp) == -1)
627 return -errno;
629 return -ENODEV;
631 error_close_dir:
632 if (closedir(dp) == -1)
633 perror("find_type_by_name(): Failed to close directory");
634 return ret;
637 static int _write_sysfs_int(const char *filename, const char *basedir, int val,
638 int verify)
640 int ret = 0;
641 FILE *sysfsfp;
642 int test;
643 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
645 if (temp == NULL)
646 return -ENOMEM;
647 ret = sprintf(temp, "%s/%s", basedir, filename);
648 if (ret < 0)
649 goto error_free;
651 sysfsfp = fopen(temp, "w");
652 if (sysfsfp == NULL) {
653 ret = -errno;
654 printf("failed to open %s\n", temp);
655 goto error_free;
657 ret = fprintf(sysfsfp, "%d", val);
658 if (ret < 0) {
659 if (fclose(sysfsfp))
660 perror("_write_sysfs_int(): Failed to close dir");
662 goto error_free;
665 if (fclose(sysfsfp)) {
666 ret = -errno;
667 goto error_free;
670 if (verify) {
671 sysfsfp = fopen(temp, "r");
672 if (sysfsfp == NULL) {
673 ret = -errno;
674 printf("failed to open %s\n", temp);
675 goto error_free;
677 if (fscanf(sysfsfp, "%d", &test) != 1) {
678 ret = errno ? -errno : -ENODATA;
679 if (fclose(sysfsfp))
680 perror("_write_sysfs_int(): Failed to close dir");
682 goto error_free;
685 if (fclose(sysfsfp)) {
686 ret = -errno;
687 goto error_free;
690 if (test != val) {
691 printf("Possible failure in int write %d to %s%s\n",
692 val,
693 basedir,
694 filename);
695 ret = -1;
698 error_free:
699 free(temp);
700 return ret;
704 * write_sysfs_int() - write an integer value to a sysfs file
705 * @filename: name of the file to write to
706 * @basedir: the sysfs directory in which the file is to be found
707 * @val: integer value to write to file
709 * Returns a value >= 0 on success, otherwise a negative error code.
711 int write_sysfs_int(const char *filename, const char *basedir, int val)
713 return _write_sysfs_int(filename, basedir, val, 0);
717 * write_sysfs_int_and_verify() - write an integer value to a sysfs file
718 * and verify
719 * @filename: name of the file to write to
720 * @basedir: the sysfs directory in which the file is to be found
721 * @val: integer value to write to file
723 * Returns a value >= 0 on success, otherwise a negative error code.
725 int write_sysfs_int_and_verify(const char *filename, const char *basedir,
726 int val)
728 return _write_sysfs_int(filename, basedir, val, 1);
731 static int _write_sysfs_string(const char *filename, const char *basedir,
732 const char *val, int verify)
734 int ret = 0;
735 FILE *sysfsfp;
736 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
738 if (temp == NULL) {
739 printf("Memory allocation failed\n");
740 return -ENOMEM;
742 ret = sprintf(temp, "%s/%s", basedir, filename);
743 if (ret < 0)
744 goto error_free;
746 sysfsfp = fopen(temp, "w");
747 if (sysfsfp == NULL) {
748 ret = -errno;
749 printf("Could not open %s\n", temp);
750 goto error_free;
752 ret = fprintf(sysfsfp, "%s", val);
753 if (ret < 0) {
754 if (fclose(sysfsfp))
755 perror("_write_sysfs_string(): Failed to close dir");
757 goto error_free;
760 if (fclose(sysfsfp)) {
761 ret = -errno;
762 goto error_free;
765 if (verify) {
766 sysfsfp = fopen(temp, "r");
767 if (sysfsfp == NULL) {
768 ret = -errno;
769 printf("could not open file to verify\n");
770 goto error_free;
772 if (fscanf(sysfsfp, "%s", temp) != 1) {
773 ret = errno ? -errno : -ENODATA;
774 if (fclose(sysfsfp))
775 perror("_write_sysfs_string(): Failed to close dir");
777 goto error_free;
780 if (fclose(sysfsfp)) {
781 ret = -errno;
782 goto error_free;
785 if (strcmp(temp, val) != 0) {
786 printf("Possible failure in string write of %s "
787 "Should be %s "
788 "written to %s\%s\n",
789 temp,
790 val,
791 basedir,
792 filename);
793 ret = -1;
796 error_free:
797 free(temp);
799 return ret;
803 * write_sysfs_string_and_verify() - string write, readback and verify
804 * @filename: name of file to write to
805 * @basedir: the sysfs directory in which the file is to be found
806 * @val: the string to write
808 * Returns a value >= 0 on success, otherwise a negative error code.
810 int write_sysfs_string_and_verify(const char *filename, const char *basedir,
811 const char *val)
813 return _write_sysfs_string(filename, basedir, val, 1);
817 * write_sysfs_string() - write string to a sysfs file
818 * @filename: name of file to write to
819 * @basedir: the sysfs directory in which the file is to be found
820 * @val: the string to write
822 * Returns a value >= 0 on success, otherwise a negative error code.
824 int write_sysfs_string(const char *filename, const char *basedir,
825 const char *val)
827 return _write_sysfs_string(filename, basedir, val, 0);
831 * read_sysfs_posint() - read an integer value from file
832 * @filename: name of file to read from
833 * @basedir: the sysfs directory in which the file is to be found
835 * Returns the read integer value >= 0 on success, otherwise a negative error
836 * code.
838 int read_sysfs_posint(const char *filename, const char *basedir)
840 int ret;
841 FILE *sysfsfp;
842 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
844 if (temp == NULL) {
845 printf("Memory allocation failed");
846 return -ENOMEM;
848 ret = sprintf(temp, "%s/%s", basedir, filename);
849 if (ret < 0)
850 goto error_free;
852 sysfsfp = fopen(temp, "r");
853 if (sysfsfp == NULL) {
854 ret = -errno;
855 goto error_free;
857 errno = 0;
858 if (fscanf(sysfsfp, "%d\n", &ret) != 1) {
859 ret = errno ? -errno : -ENODATA;
860 if (fclose(sysfsfp))
861 perror("read_sysfs_posint(): Failed to close dir");
863 goto error_free;
866 if (fclose(sysfsfp))
867 ret = -errno;
869 error_free:
870 free(temp);
871 return ret;
875 * read_sysfs_float() - read a float value from file
876 * @filename: name of file to read from
877 * @basedir: the sysfs directory in which the file is to be found
878 * @val: output the read float value
880 * Returns a value >= 0 on success, otherwise a negative error code.
882 int read_sysfs_float(const char *filename, const char *basedir, float *val)
884 int ret = 0;
885 FILE *sysfsfp;
886 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
888 if (temp == NULL) {
889 printf("Memory allocation failed");
890 return -ENOMEM;
892 ret = sprintf(temp, "%s/%s", basedir, filename);
893 if (ret < 0)
894 goto error_free;
896 sysfsfp = fopen(temp, "r");
897 if (sysfsfp == NULL) {
898 ret = -errno;
899 goto error_free;
901 errno = 0;
902 if (fscanf(sysfsfp, "%f\n", val) != 1) {
903 ret = errno ? -errno : -ENODATA;
904 if (fclose(sysfsfp))
905 perror("read_sysfs_float(): Failed to close dir");
907 goto error_free;
910 if (fclose(sysfsfp))
911 ret = -errno;
913 error_free:
914 free(temp);
915 return ret;
919 * read_sysfs_string() - read a string from file
920 * @filename: name of file to read from
921 * @basedir: the sysfs directory in which the file is to be found
922 * @str: output the read string
924 * Returns a value >= 0 on success, otherwise a negative error code.
926 int read_sysfs_string(const char *filename, const char *basedir, char *str)
928 int ret = 0;
929 FILE *sysfsfp;
930 char *temp = malloc(strlen(basedir) + strlen(filename) + 2);
932 if (temp == NULL) {
933 printf("Memory allocation failed");
934 return -ENOMEM;
936 ret = sprintf(temp, "%s/%s", basedir, filename);
937 if (ret < 0)
938 goto error_free;
940 sysfsfp = fopen(temp, "r");
941 if (sysfsfp == NULL) {
942 ret = -errno;
943 goto error_free;
945 errno = 0;
946 if (fscanf(sysfsfp, "%s\n", str) != 1) {
947 ret = errno ? -errno : -ENODATA;
948 if (fclose(sysfsfp))
949 perror("read_sysfs_string(): Failed to close dir");
951 goto error_free;
954 if (fclose(sysfsfp))
955 ret = -errno;
957 error_free:
958 free(temp);
959 return ret;
962 #endif /* _IIO_UTILS_H */