From 37348ef662a7052dae798e500ca8c9b769fff3e6 Mon Sep 17 00:00:00 2001 From: Sven Schnelle Date: Thu, 13 Aug 2020 13:09:35 +0200 Subject: [PATCH] libutil: add util_file_read_i()/util_file_read_ui() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These functions parse a sysfs file and return either an unsigned integer or signed integer. Signed-off-by: Sven Schnelle Reviewed-by: Niklas Schnelle Acked-by: Jan Höppner Signed-off-by: Jan Höppner --- include/lib/util_file.h | 2 ++ libutil/util_file.c | 76 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/include/lib/util_file.h b/include/lib/util_file.h index d4891f98..53c000ea 100644 --- a/include/lib/util_file.h +++ b/include/lib/util_file.h @@ -12,8 +12,10 @@ #define LIB_UTIL_FILE_H int util_file_read_line(char *str, size_t size, const char *fmt, ...); +int util_file_read_i(int *val, int base, const char *fmt, ...); int util_file_read_l(long *val, int base, const char *fmt, ...); int util_file_read_ll(long long *val, int base, const char *fmt, ...); +int util_file_read_ui(unsigned int *val, int base, const char *fmt, ...); int util_file_read_ul(unsigned long *val, int base, const char *fmt, ...); int util_file_read_ull(unsigned long long *val, int base, const char *fmt, ...); diff --git a/libutil/util_file.c b/libutil/util_file.c index 6c947606..994a065b 100644 --- a/libutil/util_file.c +++ b/libutil/util_file.c @@ -281,6 +281,44 @@ int util_file_write_ull(unsigned long long val, int base, const char *fmt, ...) return rc; } +/** + * Read a file and convert it to signed int according to given base + * + * @param[out] val Buffer for value + * @param[in] base Base for conversion, either 8, 10, or 16 + * @param[in] fmt Format string for generation of the path name + * @param[in] ... Parameters for format string + * + * @retval 0 Integer has been read correctly + * @retval -1 Error while reading file + */ +int util_file_read_i(int *val, int base, const char *fmt, ...) +{ + char path[PATH_MAX], buf[512]; + va_list ap; + int count; + + /* Construct the file name */ + UTIL_VSPRINTF(path, fmt, ap); + + if (file_gets(buf, sizeof(buf), path)) + return -1; + switch (base) { + case 8: + count = sscanf(buf, "%do", val); + break; + case 10: + count = sscanf(buf, "%dd", val); + break; + case 16: + count = sscanf(buf, "%dx", val); + break; + default: + util_panic("Invalid base: %d\n", base); + } + return (count == 1) ? 0 : -1; +} + /** * Read a file and convert it to signed long according to given base * @@ -357,6 +395,44 @@ int util_file_read_ll(long long *val, int base, const char *fmt, ...) return (count == 1) ? 0 : -1; } +/** + * Read a file and convert it to unsigned int according to given base + * + * @param[out] val Buffer for value + * @param[in] base Base for conversion, either 8, 10, or 16 + * @param[in] fmt Format string for generation of the path name + * @param[in] ... Parameters for format string + * + * @retval 0 Integer has been read correctly + * @retval -1 Error while reading file + */ +int util_file_read_ui(unsigned int *val, int base, const char *fmt, ...) +{ + char path[PATH_MAX], buf[512]; + va_list ap; + int count; + + /* Construct the file name */ + UTIL_VSPRINTF(path, fmt, ap); + + if (file_gets(buf, sizeof(buf), path)) + return -1; + switch (base) { + case 8: + count = sscanf(buf, "%uo", val); + break; + case 10: + count = sscanf(buf, "%uu", val); + break; + case 16: + count = sscanf(buf, "%ux", val); + break; + default: + util_panic("Invalid base: %d\n", base); + } + return (count == 1) ? 0 : -1; +} + /** * Read a file and convert it to unsigned long according to given base *