aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorJiri Olsa <jolsa@redhat.com>2013-12-03 08:09:22 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2013-12-04 13:20:12 -0500
commitcef82c9f5a3deab94c1f1f02cfc25213844852ed (patch)
tree72bd6003cdb4692b964a5a5b05feb877a5c116bc /tools
parent3d7c0144491bd8c21d53b43032274a85efdfe434 (diff)
perf tools: Add filename__read_str util function
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 v2 change: - better error handling suggested by Namhyung Kim. Signed-off-by: Jiri Olsa <jolsa@redhat.com> Cc: Corey Ashford <cjashfor@linux.vnet.ibm.com> Cc: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Paul Mackerras <paulus@samba.org> Cc: Peter Zijlstra <a.p.zijlstra@chello.nl> Cc: Steven Rostedt <rostedt@goodmis.org> Link: http://lkml.kernel.org/r/1386076182-14484-9-git-send-email-jolsa@redhat.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/perf/util/util.c49
-rw-r--r--tools/perf/util/util.h1
2 files changed, 50 insertions, 0 deletions
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c
index b1d5376b9dd9..bae8756a4eb1 100644
--- a/tools/perf/util/util.c
+++ b/tools/perf/util/util.c
@@ -6,6 +6,8 @@
6#endif 6#endif
7#include <stdio.h> 7#include <stdio.h>
8#include <stdlib.h> 8#include <stdlib.h>
9#include <string.h>
10#include <errno.h>
9#include <linux/kernel.h> 11#include <linux/kernel.h>
10 12
11/* 13/*
@@ -433,3 +435,50 @@ int filename__read_int(const char *filename, int *value)
433 close(fd); 435 close(fd);
434 return err; 436 return err;
435} 437}
438
439int filename__read_str(const char *filename, char **buf, size_t *sizep)
440{
441 size_t size = 0, alloc_size = 0;
442 void *bf = NULL, *nbf;
443 int fd, n, err = 0;
444
445 fd = open(filename, O_RDONLY);
446 if (fd < 0)
447 return -errno;
448
449 do {
450 if (size == alloc_size) {
451 alloc_size += BUFSIZ;
452 nbf = realloc(bf, alloc_size);
453 if (!nbf) {
454 err = -ENOMEM;
455 break;
456 }
457
458 bf = nbf;
459 }
460
461 n = read(fd, bf + size, alloc_size - size);
462 if (n < 0) {
463 if (size) {
464 pr_warning("read failed %d: %s\n",
465 errno, strerror(errno));
466 err = 0;
467 } else
468 err = -errno;
469
470 break;
471 }
472
473 size += n;
474 } while (n > 0);
475
476 if (!err) {
477 *sizep = size;
478 *buf = bf;
479 } else
480 free(bf);
481
482 close(fd);
483 return err;
484}
diff --git a/tools/perf/util/util.h b/tools/perf/util/util.h
index ce0f73d4d91f..adb39f251f90 100644
--- a/tools/perf/util/util.h
+++ b/tools/perf/util/util.h
@@ -308,4 +308,5 @@ char *get_srcline(struct dso *dso, unsigned long addr);
308void free_srcline(char *srcline); 308void free_srcline(char *srcline);
309 309
310int filename__read_int(const char *filename, int *value); 310int filename__read_int(const char *filename, int *value);
311int filename__read_str(const char *filename, char **buf, size_t *sizep);
311#endif /* GIT_COMPAT_UTIL_H */ 312#endif /* GIT_COMPAT_UTIL_H */