Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Power debug tool (powerdebug) |
Daniel Lezcano | 88b38e3 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 3 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 4 | * Copyright (C) 2016, Linaro Limited. |
Daniel Lezcano | 88b38e3 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 5 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 6 | * 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 Lezcano | 88b38e3 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 10 | * |
Daniel Lezcano | 7c15fad | 2016-02-18 15:57:43 +0000 | [diff] [blame] | 11 | * 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 Lezcano | 88b38e3 | 2011-06-15 15:45:12 +0200 | [diff] [blame] | 21 | |
| 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 | */ |
| 38 | int 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); |
| 58 | out_free: |
| 59 | free(rpath); |
| 60 | return ret; |
| 61 | } |
Shaojie Sun | 8ac4a2e | 2013-07-29 20:11:46 +0800 | [diff] [blame] | 62 | |
| 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 | */ |
| 74 | int 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); |
| 94 | out_free: |
| 95 | free(rpath); |
| 96 | return ret; |
| 97 | } |