aboutsummaryrefslogtreecommitdiff
path: root/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils.c')
-rw-r--r--utils.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index e47c58e..4d4b780 100644
--- a/utils.c
+++ b/utils.c
@@ -53,3 +53,39 @@ out_free:
free(rpath);
return ret;
}
+
+/*
+ * This functions is a helper to write a specific file content and store
+ * the content inside a variable pointer passed as parameter, the format
+ * parameter gives the variable type to be write to the file.
+ *
+ * @path : directory path containing the file
+ * @name : name of the file to be read
+ * @format : the format of the format
+ * @value : a pointer to a variable to store the content of the file
+ * Returns 0 on success, -1 otherwise
+ */
+int file_write_value(const char *path, const char *name,
+ const char *format, void *value)
+{
+ FILE *file;
+ char *rpath;
+ int ret;
+
+ ret = asprintf(&rpath, "%s/%s", path, name);
+ if (ret < 0)
+ return ret;
+
+ file = fopen(rpath, "w");
+ if (!file) {
+ ret = -1;
+ goto out_free;
+ }
+
+ ret = fprintf(file, format, value) < 0 ? -1 : 0;
+
+ fclose(file);
+out_free:
+ free(rpath);
+ return ret;
+}