Blog
Linux Question
Date: 12/10/2004
I'm wondering how an application can tell if 2 paths point to the same thing. For instance my home directory on linux can be addressed by:
/u/matthew
-or-
/home/lemon/matthew
Whats the best way to compare these to see if they point to the same thing?

My first thought would be to lstat both of them and compare the inode of each. But there might be a better way, and more importantly a more cross platform way. As this should ideally work on windows as well. But I don't mind #ifdef'ing around the platform specific parts if I have to.

Ideas?
Comments:
fret
12/10/2004 1:37am
e.g.
bool IsSubDir(char *Child, char *Parent)
{
    #if 1
    
    int PathLen = strlen(Child);
    
    return FsNCompare(Child, Parent, PathLen) == 0;

    #else

    struct stat c, p;
    if (lstat(Child, &c) == 0)
    {
        char Path[256];
        strsafecpy(Path, Parent, sizeof(Path));
        while (strlen(Path) > 1)
        {
            if (lstat(Path, &p) == 0)
            {
                /*
                printf("cmp(%s,%s)=%i,%i - %i\n", Path, Child,
                    c.st_ino, p.st_ino,
                    c.st_ino == p.st_ino);                    
                */
                
                if (c.st_ino == p.st_ino)
                {
                    return true;
                }
            }
            else break;
            
            // Trim off one directory off the path
            LgiTrimDir(Path);
        }
    }
    
    return false;

    #endif
}
 
Reply
From:
Email (optional): (Will be HTML encoded to evade harvesting)
Message:
 
Remember username and/or email in a cookie.
Notify me of new posts in this thread via email.
BBcode:
[q]text[/q]
[url=link]description[/url]
[img]url_to_image[/img]
[pre]some_code[/pre]
[b]bold_text[/b]