From 4d155cbf37ca909daff06dd8c7119274d0861f6c Mon Sep 17 00:00:00 2001 From: Bob Gilligan Date: Sun, 16 Jan 2011 22:44:12 -0800 Subject: Merge upstream version 0.3.4. (cherry picked from commit 21eb1c8d6fefa5dcd3a9a800b4add06d5c30c01f) --- src/sysfs.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/sysfs.c (limited to 'src/sysfs.c') diff --git a/src/sysfs.c b/src/sysfs.c new file mode 100644 index 0000000..f2c616e --- /dev/null +++ b/src/sysfs.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include + +/** + * sysfs_path_is_file: Check if the path supplied points to a file + * @path: path to validate + * Returns 0 if path points to file, 1 otherwise + * Copied from sysfsutils-2.1.0 (which is LGPL2.1 or later), relicensed GPLv2 for use here. + */ +int sysfs_path_is_file(const char * path) +{ + struct stat astats; + + if (!path) { + errno = EINVAL; + return 1; + } + if ((lstat(path, &astats)) != 0) { + return 1; + } + if (S_ISREG(astats.st_mode)) + return 0; + + return 1; +} + +int sysfs_read_file(const char * path, char **output) +{ + int ret; + char *result = NULL; + int fd; + unsigned long resultsize = 0; + ssize_t length = 0; + + resultsize = getpagesize(); + result = malloc(resultsize); + if (!result) + return -ENOMEM; + memset(result, 0, resultsize); + + fd = open(path, O_RDONLY); + if (fd < 0) { + ret = fd; + goto free_out; + } + + length = read(fd, result, resultsize-1); + if (length < 0) { + close(fd); + ret = -1; + goto free_out; + } + result[length] = '\0'; + if (result[length-1] == '\n') + result[length-1] = '\0'; + *output = result; + ret = 0; + goto out; +free_out: + free(result); +out: + return ret; +} -- cgit v1.2.3