[PATCH 19/22] perf tools: Add filename__read_str util function

From: Jiri Olsa
Date: Thu Nov 21 2013 - 06:04:02 EST


Adding filename__read_str util function to read
text file and return it in the char array.

The interface is:
int filename__read_str(const char *filename, char **buf, size_t *sizep)

Returns 0/-1 if the read suceeded/fail respectively.

buf - place to store the data pointer
size - place to store data size

Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
Cc: Corey Ashford <cjashfor@xxxxxxxxxxxxxxxxxx>
Cc: Frederic Weisbecker <fweisbec@xxxxxxxxx>
Cc: Ingo Molnar <mingo@xxxxxxx>
Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
Cc: Paul Mackerras <paulus@xxxxxxxxx>
Cc: Peter Zijlstra <a.p.zijlstra@xxxxxxxxx>
Cc: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
Cc: Steven Rostedt <rostedt@xxxxxxxxxxx>
Cc: David Ahern <dsahern@xxxxxxxxx>
---
tools/perf/util/util.c | 41 +++++++++++++++++++++++++++++++++++++++++
tools/perf/util/util.h | 1 +
2 files changed, 42 insertions(+)

diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index 28a0a89..e96cc90 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -413,3 +413,44 @@ int filename__read_int(const char *filename, int *value)
close(fd);
return err;
}
+
+int filename__read_str(const char *filename, char **buf, size_t *sizep)
+{
+ size_t size = 0, alloc_size = 0;
+ void *bf = NULL, *nbf;
+ int fd, n, err = 0;
+
+ fd = open(filename, O_RDONLY);
+ if (fd < 0)
+ return -errno;
+
+ do {
+ if (size == alloc_size) {
+ alloc_size += BUFSIZ;
+ nbf = realloc(bf, alloc_size);
+ if (!nbf) {
+ err = -ENOMEM;
+ break;
+ }
+
+ bf = nbf;
+ }
+
+ n = read(fd, bf + size, BUFSIZ);
+ if (n < 0) {
+ err = 0;
+ break;
+ }
+
+ size += n;
+ } while (n > 0);
+
+ if (!err) {
+ *sizep = size;
+ *buf = bf;
+ } else
+ free(bf);
+
+ close(fd);
+ return err;
+}
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index c8f362d..e54bb10 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -307,4 +307,5 @@ char *get_srcline(struct dso *dso, unsigned long addr);
void free_srcline(char *srcline);

int filename__read_int(const char *filename, int *value);
+int filename__read_str(const char *filename, char **buf, size_t *sizep);
#endif /* GIT_COMPAT_UTIL_H */
--
1.8.3.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/