[systemd-devel] [RFC 1/2] fileio: additional read one line function which contains specifed string
WaLyong Cho
walyong.cho at samsung.com
Fri Mar 28 01:07:15 PDT 2014
---
src/shared/fileio.c | 61 +++++++++++++++++++++++++++++++++++++++++++--------
src/shared/fileio.h | 3 +++
2 files changed, 55 insertions(+), 9 deletions(-)
diff --git a/src/shared/fileio.c b/src/shared/fileio.c
index f101269..8916970 100644
--- a/src/shared/fileio.c
+++ b/src/shared/fileio.c
@@ -90,17 +90,9 @@ int write_string_file_atomic(const char *fn, const char *line) {
return r;
}
-int read_one_line_file(const char *fn, char **line) {
- _cleanup_fclose_ FILE *f = NULL;
+int read_one_line_from_file(FILE *f, char **line) {
char t[LINE_MAX], *c;
- assert(fn);
- assert(line);
-
- f = fopen(fn, "re");
- if (!f)
- return -errno;
-
if (!fgets(t, sizeof(t), f)) {
if (ferror(f))
@@ -118,6 +110,57 @@ int read_one_line_file(const char *fn, char **line) {
return 0;
}
+int read_one_line_file(const char *fn, char **line) {
+ _cleanup_fclose_ FILE *f = NULL;
+
+ assert(fn);
+ assert(line);
+
+ f = fopen(fn, "re");
+ if (!f)
+ return -errno;
+
+ return read_one_line_from_file(f, line);
+}
+
+int read_one_line_from_file_if_has(FILE *f, char **line, const char *str) {
+ char *l, *p;
+ int r;
+
+ while (!feof(f)) {
+ r = read_one_line_from_file(f, &l);
+ if (r < 0)
+ return r;
+ p = l;
+ while ((int *)p != NULL && *p != '\n' &&
+ strlen(p) >= strlen(str)) {
+ if (startswith(p, str)) {
+ *line = l;
+ return 0;
+ }
+ p++;
+ }
+ free(l);
+ }
+
+ return -ENOENT;
+}
+
+int read_one_line_file_if_has(const char *fn, char **line, const char *str) {
+ _cleanup_fclose_ FILE *f = NULL;
+
+
+ assert(fn);
+ assert(line);
+ assert(str);
+
+ f = fopen(fn, "re");
+ if (!f)
+ return -errno;
+
+ return read_one_line_from_file_if_has(f, line, str);
+}
+
ssize_t sendfile_full(int out_fd, const char *fn) {
_cleanup_fclose_ FILE *f;
struct stat st;
diff --git a/src/shared/fileio.h b/src/shared/fileio.h
index 06c2887..3f799ce 100644
--- a/src/shared/fileio.h
+++ b/src/shared/fileio.h
@@ -29,7 +29,10 @@ int write_string_to_file(FILE *f, const char *line);
int write_string_file(const char *fn, const char *line);
int write_string_file_atomic(const char *fn, const char *line);
+int read_one_line_from_file(FILE *f, char **line);
int read_one_line_file(const char *fn, char **line);
+int read_one_line_from_file_if_has(FILE *f, char **line, const char *str);
+int read_one_line_file_if_has(const char *fn, char **line, const char *str);
int read_full_file(const char *fn, char **contents, size_t *size);
ssize_t sendfile_full(int out_fd, const char *fn);
--
1.7.9.5
More information about the systemd-devel
mailing list