+/* return canonicalized absolute pathname, even if the target file does not
+ * exist, unlike realpath */
+static char *absolute_path(char *devname)
+{
+ char buf[PATH_MAX + 1];
+ char *path;
+ char *ptr;
+
+ path = malloc(PATH_MAX + 1);
+ if (path == NULL)
+ return NULL;
+
+ if (devname[0] != '/') {
+ if (getcwd(buf, sizeof(buf) - 1) == NULL)
+ return NULL;
+ strcat(buf, "/");
+ strcat(buf, devname);
+ } else {
+ strcpy(buf, devname);
+ }
+ /* truncate filename before calling realpath */
+ ptr = strrchr(buf, '/');
+ if (ptr == NULL) {
+ free(path);
+ return NULL;
+ }
+ *ptr = '\0';
+ if (path != realpath(buf, path)) {
+ free(path);
+ return NULL;
+ }
+ /* add the filename back */
+ strcat(path, "/");
+ strcat(path, ptr + 1);
+ return path;
+}
+