Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Oct 2008
Posts: 22
#1: Nov 19 '08
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/../..

Member
 
Join Date: Nov 2008
Posts: 64
#2: Nov 19 '08

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


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/
Newbie
 
Join Date: Oct 2008
Posts: 22
#3: Nov 19 '08

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


Quote:

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?
Member
 
Join Date: Nov 2008
Posts: 64
#4: Nov 20 '08

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


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.
Reply