aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThara Gopinath <thara.gopinath@linaro.org>2017-07-14 13:25:17 -0400
committerLisa Nguyen <lisa.nguyen@linaro.org>2017-09-19 13:04:22 -0700
commit56f6178da2825ff6b9f894c0e2812591817718e7 (patch)
treee71e8794de1c373d0b4c630850c9e00b76fb5873
parente48aec79e8b75266562b9f0b6a8d5940f52765ae (diff)
Define file operations.
This patch adds functions for file open, file read line and file close. Signed-off-by: Thara Gopinath <thara.gopinath@linaro.org>
-rw-r--r--utils.c38
-rw-r--r--utils.h5
2 files changed, 41 insertions, 2 deletions
diff --git a/utils.c b/utils.c
index 5976f74..bf0c148 100644
--- a/utils.c
+++ b/utils.c
@@ -95,3 +95,41 @@ out_free:
free(rpath);
return ret;
}
+
+int file_open(FILE **fp, const char *path, const char *name, const char *format)
+{
+ int ret;
+ char *rpath;
+
+ ret = asprintf(&rpath, "%s/%s", path, name);
+ if (ret < 0)
+ return ret;
+
+ ret = 0;
+ *fp = fopen(rpath, format);
+ if (!(*fp))
+ ret = -1;
+
+ free(rpath);
+ return ret;
+}
+
+int file_read_line(FILE **fp, char *line, int size)
+{
+ if (!(*fp))
+ return -1;
+
+ if (fgets(line, size, *fp) != NULL)
+ return 0;
+ else
+ return -1;
+}
+
+int file_close(FILE **fp)
+{
+ if (!(*fp))
+ return -1;
+
+ fclose(*fp);
+ return 0;
+}
diff --git a/utils.h b/utils.h
index bd3f9bb..61be653 100644
--- a/utils.h
+++ b/utils.h
@@ -25,6 +25,7 @@ extern int file_read_value(const char *path, const char *name,
const char *format, void *value);
extern int file_write_value(const char *path, const char *name,
const char *format, void *value);
-
-
+extern int file_open(FILE **fp, const char *path, const char *name, const char *format);
+extern int file_read_line(FILE **fp, char *line, int size);
+extern int file_close(FILE **fp);
#endif