summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormananth <mananth>2003-10-27 08:32:57 +0000
committermananth <mananth>2003-10-27 08:32:57 +0000
commitd874adee536019b3565f5b6620c30bc27c2b799b (patch)
treeef603911574b592ed64ddd853ca26ae7e0f8d55e
parent18170e71126615eaf49be5add2915d8fa6edb4a5 (diff)
downloadsysfsutils-d874adee536019b3565f5b6620c30bc27c2b799b.tar.gz
Added Dan's patches to sysfs_utils.c functions and classname to
struct sysfs_class_device.
-rw-r--r--ChangeLog7
-rw-r--r--include/libsysfs.h2
-rw-r--r--lib/sysfs_class.c34
-rw-r--r--lib/sysfs_utils.c45
4 files changed, 75 insertions, 13 deletions
diff --git a/ChangeLog b/ChangeLog
index 9cb2000..785b5a2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,11 @@
+10/27/2003 - Daniel Stekloff <dsteklof@us.ibm.com>
+ * Fixed bug in sysfs_get_name_from_path()
+ * Added code to check if SYSFS_PATH environment variable
+ is set
+ * Modified sysfs_get_link()
+ * Added "classname" to struct sysfs_class_device
+
10/22/2003 - Ananth Mavinakayanahalli <ananth@in.ibm.com>
* Removed "block" subsystem support
diff --git a/include/libsysfs.h b/include/libsysfs.h
index 6b3055b..d9403ea 100644
--- a/include/libsysfs.h
+++ b/include/libsysfs.h
@@ -40,6 +40,7 @@
#define SYSFS_DRIVERS_NAME "drivers"
#define SYSFS_NAME_ATTRIBUTE "name"
#define SYSFS_UNKNOWN "unknown"
+#define SYSFS_PATH_ENV "SYSFS_PATH"
#define SYSFS_PATH_MAX 255
#define SYSFS_NAME_LEN 50
@@ -114,6 +115,7 @@ struct sysfs_class_device {
struct sysfs_device *sysdevice; /* NULL if virtual */
struct sysfs_driver *driver; /* NULL if not implemented */
unsigned char name[SYSFS_NAME_LEN];
+ unsigned char classname[SYSFS_NAME_LEN];
unsigned char path[SYSFS_PATH_MAX];
/* for internal use only */
diff --git a/lib/sysfs_class.c b/lib/sysfs_class.c
index cb6ca9d..f44c7aa 100644
--- a/lib/sysfs_class.c
+++ b/lib/sysfs_class.c
@@ -134,6 +134,39 @@ static struct sysfs_directory *open_class_dir(const unsigned char *name)
return classdir;
}
+/**
+ * set_classdev_classname: Grabs classname from path
+ * @cdev: class device to set
+ * Returns nothing
+ */
+static void set_classdev_classname(struct sysfs_class_device *cdev)
+{
+ unsigned char *c = NULL, *e = NULL;
+ int count = 0;
+
+ c = strstr(cdev->path, SYSFS_CLASS_DIR);
+ if (c == NULL)
+ c = strstr(cdev->path, SYSFS_BLOCK_DIR);
+ else {
+ c++;
+ while (c != NULL && *c != '/')
+ c++;
+ }
+
+ if (c == NULL)
+ strcpy(cdev->classname, SYSFS_UNKNOWN);
+
+ else {
+ c++;
+ e = c;
+ while (e != NULL && *e != '/') {
+ e++;
+ count++;
+ }
+ strncpy(cdev->classname, c, count);
+ }
+}
+
/**
* sysfs_open_class_device: Opens and populates class device
* @path: path to class device.
@@ -178,6 +211,7 @@ struct sysfs_class_device *sysfs_open_class_device(const unsigned char *path)
sysfs_read_all_subdirs(dir);
cdev->directory = dir;
strcpy(cdev->path, dir->path);
+ set_classdev_classname(cdev);
/* get driver and device, if implemented */
if (cdev->directory->links != NULL) {
diff --git a/lib/sysfs_utils.c b/lib/sysfs_utils.c
index 4475342..2465eb0 100644
--- a/lib/sysfs_utils.c
+++ b/lib/sysfs_utils.c
@@ -76,12 +76,18 @@ static int sysfs_get_fs_mnt_path(const unsigned char *fs_type,
*/
int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len)
{
- int ret = -1;
+ char *sysfs_path = NULL;
+ int ret = 0;
- if (mnt_path != NULL)
- ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len);
- else
+ if (mnt_path == NULL) {
errno = EINVAL;
+ return -1;
+ }
+ sysfs_path = getenv(SYSFS_PATH_ENV);
+ if (sysfs_path != NULL)
+ strncpy(mnt_path, sysfs_path, len);
+ else
+ ret = sysfs_get_fs_mnt_path(SYSFS_FSTYPE_NAME, mnt_path, len);
return ret;
}
@@ -95,13 +101,19 @@ int sysfs_get_mnt_path(unsigned char *mnt_path, size_t len)
int sysfs_get_name_from_path(const unsigned char *path, unsigned char *name,
size_t len)
{
+ unsigned char tmp[SYSFS_PATH_MAX];
unsigned char *n = NULL;
if (path == NULL || name == NULL) {
errno = EINVAL;
return -1;
}
- n = strrchr(path, '/');
+ memset(tmp, 0, SYSFS_PATH_MAX);
+ strcpy(tmp, path);
+ n = &tmp[strlen(tmp)-1];
+ if (strncmp(n, "/", 1) == 0)
+ *n = '\0';
+ n = strrchr(tmp, '/');
if (n == NULL) {
errno = EINVAL;
return -1;
@@ -122,7 +134,8 @@ int sysfs_get_link(const unsigned char *path, unsigned char *target, size_t len)
{
unsigned char devdir[SYSFS_PATH_MAX];
unsigned char linkpath[SYSFS_PATH_MAX];
- unsigned char *d = NULL;
+ unsigned char *d = NULL, *s = NULL;
+ int slashes = 0, count = 0;
if (path == NULL || target == NULL) {
errno = EINVAL;
@@ -131,12 +144,8 @@ int sysfs_get_link(const unsigned char *path, unsigned char *target, size_t len)
memset(devdir, 0, SYSFS_PATH_MAX);
memset(linkpath, 0, SYSFS_PATH_MAX);
+ strncpy(devdir, path, SYSFS_PATH_MAX);
- if ((sysfs_get_mnt_path(devdir, SYSFS_PATH_MAX)) != 0) {
- dprintf("Sysfs not supported on this system\n");
- return -1;
- }
-
if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
return -1;
}
@@ -144,12 +153,22 @@ int sysfs_get_link(const unsigned char *path, unsigned char *target, size_t len)
d = linkpath;
/* getting rid of leading "../.." */
- while (*d == '/' || *d == '.')
+ while (*d == '/' || *d == '.') {
+ if (*d == '/')
+ slashes++;
d++;
+ }
d--;
+
+ s = &devdir[strlen(devdir)-1];
+ while (s != NULL && count != (slashes+1)) {
+ s--;
+ if (*s == '/')
+ count++;
+ }
- strcat(devdir, d);
+ strncpy(s, d, (SYSFS_PATH_MAX-strlen(devdir)));
strncpy(target, devdir, len);
return 0;