468,294 Members | 1,906 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,294 developers. It's quick & easy.

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

22
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/../..
Nov 19 '08 #1
3 1441
vmpstr
63
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/
Nov 19 '08 #2
krreks
22
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?
Nov 19 '08 #3
vmpstr
63
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.
Nov 20 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by newbie_mw | last post: by
reply views Thread by Miki Tebeka | last post: by
2 posts views Thread by Alexander Bartzas | last post: by
6 posts views Thread by Fao | last post: by
1 post views Thread by Shapper | last post: by
1 post views Thread by eggie5 | last post: by
3 posts views Thread by katieh | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.