summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStephen Hemminger <stephen.hemminger@vyatta.com>2011-01-27 15:05:10 +1000
committerStephen Hemminger <stephen.hemminger@vyatta.com>2011-01-27 15:05:10 +1000
commite19a941f0c48fa4a14e746086d46c043ffb9d279 (patch)
treef059965ab3c7e8abc0e0dd32a498149aadddd956 /src
parentcf56fac03017304a9201532cf4ecfa55f4ee487c (diff)
downloadvyatta-biosdevname-e19a941f0c48fa4a14e746086d46c043ffb9d279.tar.gz
vyatta-biosdevname-e19a941f0c48fa4a14e746086d46c043ffb9d279.zip
Add missing files from upstream versionupstream
Missing bits from upstream copy
Diffstat (limited to 'src')
-rw-r--r--src/sysfs.c68
-rw-r--r--src/sysfs.h2
2 files changed, 70 insertions, 0 deletions
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 <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+
+/**
+ * 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;
+}
diff --git a/src/sysfs.h b/src/sysfs.h
new file mode 100644
index 0000000..91bfb15
--- /dev/null
+++ b/src/sysfs.h
@@ -0,0 +1,2 @@
+extern int sysfs_path_is_file(const char *path);
+extern int sysfs_read_file(const char *path, char **output);