From 6df2b6e22e0720173cec23669ecf3f99ee9f21eb Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sun, 27 May 2018 00:01:08 +0300 Subject: configfs: Restructure attribute read to support binary attributes Modify the attribute_read() function to not add a terminating 0 to the buffer, and to return the number of bytes read, in order to support binary attributes. The attribute_read_uint() and attribute_read_str() functions are modified accordingly to use the new attribute_read() semantics. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- configfs.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'configfs.c') diff --git a/configfs.c b/configfs.c index 38954d5..4c83dca 100644 --- a/configfs.c +++ b/configfs.c @@ -53,7 +53,7 @@ static char *path_glob_first_match(const char *g) * Attribute handling */ -static int attribute_read(const char *path, const char *file, char *buf, +static int attribute_read(const char *path, const char *file, void *buf, unsigned int len) { char *f; @@ -72,7 +72,7 @@ static int attribute_read(const char *path, const char *file, char *buf, return -ENOENT; } - ret = read(fd, buf, len - 1); + ret = read(fd, buf, len); close(fd); if (ret < 0) { @@ -81,9 +81,7 @@ static int attribute_read(const char *path, const char *file, char *buf, return -ENODATA; } - buf[ret] = '\0'; - - return 0; + return len; } static int attribute_read_uint(const char *path, const char *file, @@ -94,10 +92,12 @@ static int attribute_read_uint(const char *path, const char *file, char *endptr; int ret; - ret = attribute_read(path, file, buf, sizeof(buf)); - if (ret) + ret = attribute_read(path, file, buf, sizeof(buf) - 1); + if (ret < 0) return ret; + buf[ret] = '\0'; + errno = 0; /* base 0: Autodetect hex, octal, decimal. */ @@ -117,10 +117,12 @@ static char *attribute_read_str(const char *path, const char *file) char *p; int ret; - ret = attribute_read(path, file, buf, sizeof(buf)); - if (ret) + ret = attribute_read(path, file, buf, sizeof(buf) - 1); + if (ret < 0) return NULL; + buf[ret] = '\0'; + p = strrchr(buf, '\n'); if (p != buf) *p = '\0'; -- cgit v1.2.3