218178
diff -upr sysfsutils-2.1.0-old/lib/sysfs_utils.c sysfsutils-2.1.0/lib/sysfs_utils.c
218178
--- sysfsutils-2.1.0-old/lib/sysfs_utils.c	2006-08-07 07:08:01.000000000 +0200
218178
+++ sysfsutils-2.1.0/lib/sysfs_utils.c	2008-05-13 07:42:50.000000000 +0200
218178
@@ -117,84 +117,104 @@ int sysfs_get_link(const char *path, cha
218178
 {
218178
 	char devdir[SYSFS_PATH_MAX];
218178
 	char linkpath[SYSFS_PATH_MAX];
218178
-	char temp_path[SYSFS_PATH_MAX];
218178
-	char *d = NULL, *s = NULL;
218178
-	int slashes = 0, count = 0;
218178
+	char *d, *s;
218178
+	int count;
218178
 
218178
 	if (!path || !target || len == 0) {
218178
 		errno = EINVAL;
218178
 		return -1;
218178
 	}
218178
 
218178
-	memset(devdir, 0, SYSFS_PATH_MAX);
218178
-	memset(linkpath, 0, SYSFS_PATH_MAX);
218178
-	memset(temp_path, 0, SYSFS_PATH_MAX);
218178
-	safestrcpy(devdir, path);
218178
-
218178
-	if ((readlink(path, linkpath, SYSFS_PATH_MAX)) < 0) {
218178
+	count = readlink(path, linkpath, SYSFS_PATH_MAX);
218178
+	if (count < 0)
218178
 		return -1;
218178
-	}
218178
-	d = linkpath;
218178
+	else
218178
+		linkpath[count] = '\0';
218178
 	/*
218178
 	 * Three cases here:
218178
 	 * 1. relative path => format ../..
218178
 	 * 2. absolute path => format /abcd/efgh
218178
 	 * 3. relative path _from_ this dir => format abcd/efgh
218178
 	 */
218178
-	switch (*d) {
218178
-		case '.':
218178
+	if (*linkpath == '/') {
218178
+		/* absolute path - copy as is */
218178
+		safestrcpymax(target, linkpath, len);
218178
+		return 0;
218178
+	}
218178
+
218178
+	safestrcpy(devdir, path);
218178
+	s = strrchr(devdir, '/');
218178
+	if (s == NULL)
218178
+		s = devdir - 1;
218178
+	d = linkpath;
218178
+	while (*d == '.') {
218178
+		if (*(d+1) == '/') {
218178
 			/*
218178
 			 * handle the case where link is of type ./abcd/xxx
218178
 			 */
218178
-			safestrcpy(temp_path, devdir);
218178
-			if (*(d+1) == '/')
218178
-				d += 2;
218178
-			else if (*(d+1) == '.')
218178
-				goto parse_path;
218178
-			s = strrchr(temp_path, '/');
218178
-			if (s != NULL) {
218178
-				*(s+1) = '\0';
218178
-				safestrcat(temp_path, d);
218178
-			} else {
218178
-				safestrcpy(temp_path, d);
218178
-			}
218178
-			safestrcpymax(target, temp_path, len);
218178
-			break;
218178
+			d += 2;
218178
+			while (*d == '/')
218178
+				d++;
218178
+			continue;
218178
+		} else if (*(d+1) != '.' || *(d+2) != '/')
218178
 			/*
218178
-			 * relative path, getting rid of leading "../.."
218178
+			 * relative path from this directory, starting
218178
+			 * with a hidden directory
218178
 			 */
218178
-parse_path:
218178
-			while (*d == '/' || *d == '.') {
218178
-				if (*d == '/')
218178
-					slashes++;
218178
-				d++;
218178
-			}
218178
-			d--;
218178
-			s = &devdir[strlen(devdir)-1];
218178
-			while (s != NULL && count != (slashes+1)) {
218178
+			break;
218178
+
218178
+		/*
218178
+		 * relative path, getting rid of leading "../.."; must
218178
+		 * be careful here since any path component of devdir
218178
+		 * could be a symlink again
218178
+		 */
218178
+		for (;;) {
218178
+			while (s > devdir && *s == '/') {
218178
 				s--;
218178
-				if (*s == '/')
218178
-					count++;
218178
+				if (*s == '.'
218178
+				    && (s == devdir || *(s-1) == '/'))
218178
+					s--;
218178
 			}
218178
-			safestrcpymax(s, d, (SYSFS_PATH_MAX-strlen(devdir)));
218178
-			safestrcpymax(target, devdir, len);
218178
-			break;
218178
-		case '/':
218178
-			/* absolute path - copy as is */
218178
-			safestrcpymax(target, linkpath, len);
218178
-			break;
218178
-		default:
218178
-			/* relative path from this directory */
218178
-			safestrcpy(temp_path, devdir);
218178
-			s = strrchr(temp_path, '/');
218178
-			if (s != NULL) {
218178
-				*(s+1) = '\0';
218178
-				safestrcat(temp_path, linkpath);
218178
-			} else {
218178
-				safestrcpy(temp_path, linkpath);
218178
+			*(s+1) = '\0';
218178
+			if (*devdir == '\0' || sysfs_path_is_link(devdir))
218178
+				/*
218178
+				 * condition will be true eventually
218178
+				 * because we already know that all
218178
+				 * but the last component of path
218178
+				 * resolve to a directory
218178
+				 */
218178
+				break;
218178
+			if (sysfs_get_link(devdir, devdir, SYSFS_PATH_MAX))
218178
+				return -1;
218178
+			s = devdir + strlen(devdir) - 1;
218178
+		}
218178
+		while (s >= devdir) {
218178
+			if (*s == '/') {
218178
+				if (*(s+1) != '.' || *(s+2) != '.'
218178
+				    || *(s+3) != '\0') {
218178
+					d += 3;
218178
+					while (*d == '/')
218178
+						d++;
218178
+				} else
218178
+					s += 2;
218178
+				break;
218178
 			}
218178
-			safestrcpymax(target, temp_path, len);
218178
+			s--;
218178
+		}
218178
+		if (s < devdir || *(s+1) == '\0')
218178
+			break;
218178
 	}
218178
+
218178
+	/*
218178
+	 * appending to devdir a slash and the (possibly shortened)
218178
+	 * relative path to the link source
218178
+	 */
218178
+	s++;
218178
+	if (s > devdir && *s == '\0')
218178
+		*s++ = '/';
218178
+	*s = '\0';
218178
+	safestrcpymax(s, d, SYSFS_PATH_MAX-(s-devdir));
218178
+	safestrcpymax(target, devdir, len);
218178
 	return 0;
218178
 }
218178