"Problem" with navigating in dirs with chdir...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krreks
    New Member
    • Oct 2008
    • 22

    "Problem" with navigating in dirs with chdir...

    I'm developing a very simple file server and client at school and have the following case.

    The server maintains a current directory string and also holds a initial dir (which is a record of where the script was started from). The user is able to navigate in files and folders on the server, but should not be able to navigate outside the initDir.

    My first idea was to keep the current dir in a temporary variable, change directory and check with strstr that the initDir is contained in the new current dir.

    An example:
    1. init=/home/user/test curr=/home/user/test => OK
    2. inti=/home/user/test curr=/home/user/test/1 => OK
    3. /home/user/test curr=/home/user/ => NOT OK
    3. /home/user/test curr=/home/user/test/.. => NOT OK
    3. /home/user/test curr=/home/user/test/../test => OK

    The problem is the '..' which I thought would be gode when changing the dir, so that when you change currentdir with chdir(/home/user/test/../..) the value returned from getcwd would be /home/user/test/../..
  • vmpstr
    New Member
    • Nov 2008
    • 63

    #2
    You can use realpath if it is available on your OS.

    It takes 2 parameters, one the path you calculated (with, possible, some /.././) and the second is the output where it will store the best path that it can resolve.

    Example:
    char buf[] = "/local/home/directory/../../bin/";
    char output[500];

    realpath(buf, output);

    printf("%s\n", output);

    this will print out:
    /local/bin/

    Comment

    • krreks
      New Member
      • Oct 2008
      • 22

      #3
      Originally posted by vmpstr
      You can use realpath if it is available on your OS.
      Found the realpath function a little before i got the notification about your reply, but thanks a bunch anyway :)

      BTW: Is there a reference on which operating system the different functions/libs is available?

      Comment

      • vmpstr
        New Member
        • Nov 2008
        • 63

        #4
        Hmm... I was under the impression that the man pages will provide a section specifying which standard the function comes from, but I can't find one...

        In my experience, if a function is in the 3rd section of man pages, then it is standard (C89/C99). If it is in the 2nd section, then it is POSIX... I'm more than certain that there are exceptions though.

        Anyhoo, if it's in C89/C99, then it is available on all compilers (some don't support all C99 functionality well though). If it's POSIX, then you can expect to find it on most *nix systems (linux, unix), but you might have a harder time on windows.

        I know this is a hand-wavey explanation, so hopefully someone has a better one.

        Comment

        Working...