From 56f6178da2825ff6b9f894c0e2812591817718e7 Mon Sep 17 00:00:00 2001 From: Thara Gopinath Date: Fri, 14 Jul 2017 13:25:17 -0400 Subject: Define file operations. This patch adds functions for file open, file read line and file close. Signed-off-by: Thara Gopinath --- utils.c | 38 ++++++++++++++++++++++++++++++++++++++ utils.h | 5 +++-- 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 -- cgit v1.2.3