summaryrefslogtreecommitdiffstats
path: root/tools/lib/api
diff options
context:
space:
mode:
authorJiri Olsa <jolsa@kernel.org>2018-02-06 13:18:00 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2018-02-16 08:09:23 -0500
commit6baddfc6900eca7f6b360c91ff737890ab4f1d55 (patch)
tree6d604e38804e7594c9220f8c30125ff99ea4ca5d /tools/lib/api
parent3233b37a71c794e25a0a794185df8d6abd9f277e (diff)
tools lib api fs: Add filename__read_xll function
Adding filename__read_xll function to be able to read files with hex numbers in, which do not have 0x prefix. Signed-off-by: Jiri Olsa <jolsa@kernel.org> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/20180206181813.10943-5-jolsa@kernel.org Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/lib/api')
-rw-r--r--tools/lib/api/fs/fs.c29
-rw-r--r--tools/lib/api/fs/fs.h1
2 files changed, 23 insertions, 7 deletions
diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c
index b24afc0e6e81..8b0e4a4315bd 100644
--- a/tools/lib/api/fs/fs.c
+++ b/tools/lib/api/fs/fs.c
@@ -315,12 +315,8 @@ int filename__read_int(const char *filename, int *value)
315 return err; 315 return err;
316} 316}
317 317
318/* 318static int filename__read_ull_base(const char *filename,
319 * Parses @value out of @filename with strtoull. 319 unsigned long long *value, int base)
320 * By using 0 for base, the strtoull detects the
321 * base automatically (see man strtoull).
322 */
323int filename__read_ull(const char *filename, unsigned long long *value)
324{ 320{
325 char line[64]; 321 char line[64];
326 int fd = open(filename, O_RDONLY), err = -1; 322 int fd = open(filename, O_RDONLY), err = -1;
@@ -329,7 +325,7 @@ int filename__read_ull(const char *filename, unsigned long long *value)
329 return -1; 325 return -1;
330 326
331 if (read(fd, line, sizeof(line)) > 0) { 327 if (read(fd, line, sizeof(line)) > 0) {
332 *value = strtoull(line, NULL, 0); 328 *value = strtoull(line, NULL, base);
333 if (*value != ULLONG_MAX) 329 if (*value != ULLONG_MAX)
334 err = 0; 330 err = 0;
335 } 331 }
@@ -338,6 +334,25 @@ int filename__read_ull(const char *filename, unsigned long long *value)
338 return err; 334 return err;
339} 335}
340 336
337/*
338 * Parses @value out of @filename with strtoull.
339 * By using 16 for base to treat the number as hex.
340 */
341int filename__read_xll(const char *filename, unsigned long long *value)
342{
343 return filename__read_ull_base(filename, value, 16);
344}
345
346/*
347 * Parses @value out of @filename with strtoull.
348 * By using 0 for base, the strtoull detects the
349 * base automatically (see man strtoull).
350 */
351int filename__read_ull(const char *filename, unsigned long long *value)
352{
353 return filename__read_ull_base(filename, value, 0);
354}
355
341#define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */ 356#define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */
342 357
343int filename__read_str(const char *filename, char **buf, size_t *sizep) 358int filename__read_str(const char *filename, char **buf, size_t *sizep)
diff --git a/tools/lib/api/fs/fs.h b/tools/lib/api/fs/fs.h
index dda49deefb52..8ebee35a6395 100644
--- a/tools/lib/api/fs/fs.h
+++ b/tools/lib/api/fs/fs.h
@@ -30,6 +30,7 @@ FS(bpf_fs)
30 30
31int filename__read_int(const char *filename, int *value); 31int filename__read_int(const char *filename, int *value);
32int filename__read_ull(const char *filename, unsigned long long *value); 32int filename__read_ull(const char *filename, unsigned long long *value);
33int filename__read_xll(const char *filename, unsigned long long *value);
33int filename__read_str(const char *filename, char **buf, size_t *sizep); 34int filename__read_str(const char *filename, char **buf, size_t *sizep);
34 35
35int filename__write_int(const char *filename, int value); 36int filename__write_int(const char *filename, int value);