Skip to content

Commit

Permalink
tools lib api fs: Add filename__read_xll function
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Jiri Olsa authored and Arnaldo Carvalho de Melo committed Feb 16, 2018
1 parent 3233b37 commit 6baddfc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
29 changes: 22 additions & 7 deletions tools/lib/api/fs/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,8 @@ int filename__read_int(const char *filename, int *value)
return err;
}

/*
* Parses @value out of @filename with strtoull.
* By using 0 for base, the strtoull detects the
* base automatically (see man strtoull).
*/
int filename__read_ull(const char *filename, unsigned long long *value)
static int filename__read_ull_base(const char *filename,
unsigned long long *value, int base)
{
char line[64];
int fd = open(filename, O_RDONLY), err = -1;
Expand All @@ -329,7 +325,7 @@ int filename__read_ull(const char *filename, unsigned long long *value)
return -1;

if (read(fd, line, sizeof(line)) > 0) {
*value = strtoull(line, NULL, 0);
*value = strtoull(line, NULL, base);
if (*value != ULLONG_MAX)
err = 0;
}
Expand All @@ -338,6 +334,25 @@ int filename__read_ull(const char *filename, unsigned long long *value)
return err;
}

/*
* Parses @value out of @filename with strtoull.
* By using 16 for base to treat the number as hex.
*/
int filename__read_xll(const char *filename, unsigned long long *value)
{
return filename__read_ull_base(filename, value, 16);
}

/*
* Parses @value out of @filename with strtoull.
* By using 0 for base, the strtoull detects the
* base automatically (see man strtoull).
*/
int filename__read_ull(const char *filename, unsigned long long *value)
{
return filename__read_ull_base(filename, value, 0);
}

#define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */

int filename__read_str(const char *filename, char **buf, size_t *sizep)
Expand Down
1 change: 1 addition & 0 deletions tools/lib/api/fs/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ FS(bpf_fs)

int filename__read_int(const char *filename, int *value);
int filename__read_ull(const char *filename, unsigned long long *value);
int filename__read_xll(const char *filename, unsigned long long *value);
int filename__read_str(const char *filename, char **buf, size_t *sizep);

int filename__write_int(const char *filename, int value);
Expand Down

0 comments on commit 6baddfc

Please sign in to comment.