+static int resolve_sys_link(struct udev *udev, char *path, size_t size)
+{
+ char link_path[UTIL_PATH_SIZE];
+ char link_target[UTIL_PATH_SIZE];
+
+ int len;
+ int i;
+ int back;
+
+ util_strlcpy(link_path, udev_get_sys_path(udev), sizeof(link_path));
+ util_strlcat(link_path, path, sizeof(link_path));
+ len = readlink(link_path, link_target, sizeof(link_target));
+ if (len <= 0)
+ return -1;
+ link_target[len] = '\0';
+ dbg(udev, "path link '%s' points to '%s'\n", path, link_target);
+
+ for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
+ ;
+ dbg(udev, "base '%s', tail '%s', back %i\n", path, &link_target[back * 3], back);
+ for (i = 0; i <= back; i++) {
+ char *pos = strrchr(path, '/');
+
+ if (pos == NULL)
+ return -1;
+ pos[0] = '\0';
+ }
+ dbg(udev, "after moving back '%s'\n", path);
+ util_strlcat(path, "/", size);
+ util_strlcat(path, &link_target[back * 3], size);
+ return 0;
+}
+