diff options
author | mananth <mananth> | 2003-08-29 11:21:10 +0000 |
---|---|---|
committer | mananth <mananth> | 2003-08-29 11:21:10 +0000 |
commit | 2d58b36c6d6a421c8a9975e43e01557b62d28833 (patch) | |
tree | aa0013e322183c5e764a8e77d7243f47ebd31ae6 | |
parent | b3f3a30f9553e0400660191dbb772438aa849a1d (diff) | |
download | sysfsutils-2d58b36c6d6a421c8a9975e43e01557b62d28833.tar.gz |
Added Guo Min's sysfs_write_attribute patch
-rw-r--r-- | CREDITS | 4 | ||||
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | include/libsysfs.h | 3 | ||||
-rw-r--r-- | lib/sysfs_dir.c | 73 |
4 files changed, 82 insertions, 1 deletions
@@ -1,5 +1,5 @@ -The authors of sysutils would like to thank the following people who +The authors of sysfsutils would like to thank the following people who have made contributions: o Lev Makhlis <mlev@despammed.com>: @@ -9,3 +9,5 @@ have made contributions: o Eric J Bohm <bohm@gate.csgeeks.org>: - Supplied dlist generic linked list implementation. - Added dlist_for_each* functionality. + o Guo Min <min.guo@intel.com>: + - Supplied sysfs_write_attribute patch @@ -1,4 +1,7 @@ +08/28/2003 - Guo Min <min.guo@intel.com> + * Added "write" attribute support to sysfsutils + 08/28/2003 - Ananth Mavinakayanahalli <ananth@in.ibm.com> * Changed sysfs_driver to contain a dlist of devices diff --git a/include/libsysfs.h b/include/libsysfs.h index 5f8659b..6314805 100644 --- a/include/libsysfs.h +++ b/include/libsysfs.h @@ -138,6 +138,9 @@ extern struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path); extern int sysfs_read_attribute(struct sysfs_attribute *sysattr); extern int sysfs_read_attribute_value(const unsigned char *attrpath, unsigned char *value, size_t vsize); +extern int sysfs_write_attribute(struct sysfs_attribute *sysattr); +extern int sysfs_write_attribute_value(const unsigned char *attrpath, + unsigned char *value); extern unsigned char *sysfs_get_value_from_attributes(struct dlist *attr, const unsigned char * name); extern void sysfs_close_directory(struct sysfs_directory *sysdir); diff --git a/lib/sysfs_dir.c b/lib/sysfs_dir.c index 1b365d7..c75193e 100644 --- a/lib/sysfs_dir.c +++ b/lib/sysfs_dir.c @@ -160,6 +160,43 @@ struct sysfs_attribute *sysfs_open_attribute(const unsigned char *path) return sysattr; } +/** + * sysfs_write_attribute: write value to the attribute + * @sysattr: attribute to write + * returns 0 with success and -1 with error. + */ +int sysfs_write_attribute(struct sysfs_attribute *sysattr) +{ + int fd; + int length; + + if (sysattr == NULL) { + errno = EINVAL; + return -1; + } + + if (!(sysattr->method & SYSFS_METHOD_STORE)) { + dprintf ("Store method not supported for attribute %s\n", + sysattr->path); + return -1; + } + + if ((fd = open(sysattr->path, O_RDWR)) < 0) { + dprintf ("Error reading attribute %s\n", sysattr->path); + return -1; + } + + length = write(fd, sysattr->value, sizeof(sysattr->value)); + if (length < 0) { + dprintf("Error write to the attribute %s\n", + sysattr->path); + close(fd); + return -1; + } + close(fd); + return 0; +} + /** * sysfs_read_attribute: reads value from attribute @@ -215,6 +252,42 @@ int sysfs_read_attribute(struct sysfs_attribute *sysattr) } /** + * sysfs_write_attribute_value: given path to attribute, + * value will be saved to the attribute. + * @attrpath: sysfs path to attribute + * @value: value to give to attribute + * returns 0 with success and -1 with error. + */ +int sysfs_write_attribute_value(const unsigned char *attrpath, + unsigned char *value) +{ + struct sysfs_attribute *attr = NULL; + + if (attrpath == NULL || value == NULL) { + errno = EINVAL; + return -1; + } + + attr = sysfs_open_attribute(attrpath); + if (attr == NULL) { + dprintf("Invalid attribute path %s\n", attrpath); + errno = EINVAL; + return -1; + } + strncpy(attr->value,value,sizeof(value)); + if ((sysfs_write_attribute(attr) != 0 )) { + dprintf("Error write to attribute %s\n", attrpath); + sysfs_close_attribute(attr); + return -1; + } + + sysfs_close_attribute(attr); + + return 0; +} + + +/** * sysfs_read_attribute_value: given path to attribute, return its value. * values can be up to a pagesize, if buffer is smaller the value will * be truncated. |