-/* 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;
- int len;
-
- path = malloc(sizeof(buf));
- if (path == NULL)
- return NULL;
-
- if (devname[0] != '/') {
- if (getcwd(buf, sizeof(buf) - 1) == NULL) {
- free(path);
- return NULL;
- }
- len = snprintf(path, sizeof(buf), "%s/%s", buf, devname);
- if (len >= sizeof(buf)) {
- free(path);
- return NULL;
- }
- } else {
- len = snprintf(path, sizeof(buf), "%s", devname);
- if (len >= sizeof(buf)) {
- free(path);
- return NULL;
- }
- }
-
- /* truncate filename before calling realpath */
- ptr = strrchr(path, '/');
- if (ptr == NULL) {
- free(path);
- return NULL;
- }
- *ptr = '\0';
- if (buf != realpath(path, buf)) {
- free(path);
- return NULL;
- }
- /* add the filename back */
- len = snprintf(path, PATH_MAX, "%s/%s", buf, ptr+1);
- if (len >= PATH_MAX) {
- free(path);
- return NULL;
- }
- return path;
-}
-