blob: 5976f74030de6632063fdfc95d920e86774e7ac9 [file] [log] [blame]
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00001/*
2 * Power debug tool (powerdebug)
Daniel Lezcano88b38e32011-06-15 15:45:12 +02003 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00004 * Copyright (C) 2016, Linaro Limited.
Daniel Lezcano88b38e32011-06-15 15:45:12 +02005 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +00006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
Daniel Lezcano88b38e32011-06-15 15:45:12 +020010 *
Daniel Lezcano7c15fad2016-02-18 15:57:43 +000011 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 */
Daniel Lezcano88b38e32011-06-15 15:45:12 +020021
22#define _GNU_SOURCE
23#include <stdio.h>
24#undef _GNU_SOURCE
25#include <stdlib.h>
26
27/*
28 * This functions is a helper to read a specific file content and store
29 * the content inside a variable pointer passed as parameter, the format
30 * parameter gives the variable type to be read from the file.
31 *
32 * @path : directory path containing the file
33 * @name : name of the file to be read
34 * @format : the format of the format
35 * @value : a pointer to a variable to store the content of the file
36 * Returns 0 on success, -1 otherwise
37 */
38int file_read_value(const char *path, const char *name,
39 const char *format, void *value)
40{
41 FILE *file;
42 char *rpath;
43 int ret;
44
45 ret = asprintf(&rpath, "%s/%s", path, name);
46 if (ret < 0)
47 return ret;
48
49 file = fopen(rpath, "r");
50 if (!file) {
51 ret = -1;
52 goto out_free;
53 }
54
55 ret = fscanf(file, format, value) == EOF ? -1 : 0;
56
57 fclose(file);
58out_free:
59 free(rpath);
60 return ret;
61}
Shaojie Sun8ac4a2e2013-07-29 20:11:46 +080062
63/*
64 * This functions is a helper to write a specific file content and store
65 * the content inside a variable pointer passed as parameter, the format
66 * parameter gives the variable type to be write to the file.
67 *
68 * @path : directory path containing the file
69 * @name : name of the file to be read
70 * @format : the format of the format
71 * @value : a pointer to a variable to store the content of the file
72 * Returns 0 on success, -1 otherwise
73 */
74int file_write_value(const char *path, const char *name,
75 const char *format, void *value)
76{
77 FILE *file;
78 char *rpath;
79 int ret;
80
81 ret = asprintf(&rpath, "%s/%s", path, name);
82 if (ret < 0)
83 return ret;
84
85 file = fopen(rpath, "w");
86 if (!file) {
87 ret = -1;
88 goto out_free;
89 }
90
91 ret = fprintf(file, format, value) < 0 ? -1 : 0;
92
93 fclose(file);
94out_free:
95 free(rpath);
96 return ret;
97}