summaryrefslogtreecommitdiff
path: root/lib
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 /lib
parent18170e71126615eaf49be5add2915d8fa6edb4a5 (diff)
downloadsysfsutils-d874adee536019b3565f5b6620c30bc27c2b799b.tar.gz
Added Dan's patches to sysfs_utils.c functions and classname to
struct sysfs_class_device.
Diffstat (limited to 'lib')
-rw-r--r--lib/sysfs_class.c34
-rw-r--r--lib/sysfs_utils.c45
2 files changed, 66 insertions, 13 deletions
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;